// pages/create-project-role/index.js
import http from '../../utils/http'
import util from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    projects: [],
    projectIndex: -1,
    users: [],
    userIndex: -1,
    roleIndex: -1,
    id: -1,
    // create/edit
    type: 'create',
    project_user: null,
    roles: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getProjects()
    if (options.id) {
      this.setData({
        id: options.id,
        type: 'edit'
      })
      this.getProjectUser()
      wx.setNavigationBarTitle({
        title: '修改权限'
      })
    }
    this.getRoles()
  },

  updateIndex: function () {
    var project_user = this.data.project_user
    if (!project_user) return false;
    var projects = this.data.projects
    if (projects.length > 0) {
      for (var i = 0; i < projects.length; ++i) {
        if (projects[i].id == project_user.project_id) {
          this.setData({
            projectIndex: i
          })
          break;
        }
      }
    }
    var roles = this.data.roles
    if (roles.length > 0) {
      for (var i = 0; i < roles.length; ++i) {
        if (roles[i].id == project_user.project_role_id) {
          this.setData({
            roleIndex: i
          })
          break;
        }
      }
    }
  },

  getProjectUser() {
    var that = this
    http({
      url: 'project-users/detail',
      data: {
        id: this.data.id
      },
      success: function (res) {
        if (res.code == 0) {
          that.setData({
            project_user: res.data
          })
          that.updateIndex()
          that.search()
        }
      }
    })
  },

  submit() {
    if (this.data.users.length <= 0) {
      util.error('请选择成员')
      return false
    }
    if (this.data.projectIndex < 0) {
      util.error('请选择项目')
      return false
    }
    if (this.data.roleIndex < 0) {
      util.error('请选择角色')
      return false
    }
    var user_id = this.data.users[0].id;
    var project_id = this.data.projects[this.data.projectIndex].id;
    var role_id = this.data.roles[this.data.roleIndex].id;
    var url = this.data.type == 'create' ? 'projects/addUser' : 'projects/updateUser'
    var data = {
      user_id: user_id,
      project_id: project_id,
      project_role_id: role_id
    }
    if (this.data.type == 'edit') {
      data.id = this.data.project_user.id
    }
    http({
      url: url,
      data: data,
      success: function (res) {
        if (res.code == 0) {
          util.success('操作成功')
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000);
        }
      }
    })
  },

  clear: function () {
    this.setData({
      keyword: ''
    })
  },

  blur: function () {
    var that = this
    setTimeout(function () {
      that.search()
    }, 300)
  },

  search: function () {
    var that = this
    if (!this.data.keyword && this.data.type == 'create') return false;
    var data = {
      keyword: this.data.keyword
    }
    if (this.data.type == 'edit') {
      data = {
        keyword: this.data.keyword,
        id: this.data.project_user.user_id
      }
      console.log(data)
    }
    http({
      url: 'users/search',
      data: data,
      success: function (res) {
        if (res.code == 0) {
          if (res.data.length <= 0) {
            util.error('未找到相应用户')
          }
          that.setData({
            users: res.data
          })
          that.getProjects()
        }
      }
    })
  },

  bindPickerChange: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail.value
    })
  },

  getRoles: function () {
    var that = this
    http({
      url: 'project-roles/getByExclude',
      data: {
        id: this.data.id
      },
      success: function (res) {
        if (res.code == 0) {
          that.setData({
            roles: res.data
          })
          if (that.data.type == 'edit') that.updateIndex()
        }
      }
    })
  },

  getProjects: function () {
    var that = this
    var users = this.data.users
    var user_id = ''
    if (users.length > 0) {
      user_id = users[0].id
    }
    http({
      url: 'projects/getAll',
      data: {
        user_id: user_id,
        self: true,
        type: that.data.type
      },
      success: function (res) {
        if (res.code == 0) {
          that.setData({
            projects: res.data,
            projectIndex: -1
          })
          if (that.data.type == 'edit') that.updateIndex()
        }
      }
    })
  },

  updateInput: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})