// pages/empAdd/empAdd.js
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //0是新增，1是编辑
    userInfo: '',
    entInfo:'',
    userInfoBei: '', //被编辑人信息
    showRoles: [],
    rolesList: [],
    rolesIndex: '',
    ents: [],
    entsIndex: '',
    userName: '',
    userMobile: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: Number(options.type),
      userInfo: utils.getInfo(),
      entInfo:utils.getCurrEntInfo()
    })
    this.getRoles()
    if (this.data.type === 1) {
      wx.setNavigationBarTitle({
            title: '编辑人员'
          })
      let detail = wx.getStorageSync('configUser')
      this.setData({
        userInfoBei: detail,
        userMobile:detail.userMobile,
        userName:detail.userName
      })
     
      // this.setData({
      //   mobile: options.mobile
      // })
      // this.getDetail()
    }
  },

  //如果是编辑操作，就获取回显信息
  // getDetail() {
  //   wx.setNavigationBarTitle({
  //     title: '编辑人员'
  //   })
  //   let params = {
  //     mobile: this.data.mobile
  //   }
  //   utils.axios({
  //     method: 'get',
  //     url: '/auth/ent/user/findManageInfoByMobile',
  //     data: params,
  //     sendBefore() {
  //       wx.showLoading({
  //         title: '信息加载中...',
  //         mask: true
  //       })
  //     },
  //     complete() {
  //       wx.hideLoading()
  //     },
  //     success: res => {
  //       this.setData({
  //         name: res.data.name,
  //         userInfoBei: res.data
  //       })
  //       this.getRoles()
  //       this.getManager()
  //     }
  //   })
  // },

  //提交
  submitData() {
    if (this.data.type === 0) {
      this.addNew()
    }
    if (this.data.type === 1) {
      this.edit()
    }
  },

  //新增人员
  addNew() {
    let params = {
      entUserMobile: utils.trimAll(this.data.userMobile) ,
      entUserName:utils.trimAll(this.data.userName),
      reqChannel: 5,
      mobile:utils.getInfo().mobile,
      entTaxId:this.data.entInfo.entTaxId
    }
    if (params.name === '') {
      utils.toast('员工姓名不能为空')
      return
    }
    if (params.mobile === '') {
      utils.toast('员工手机号码不能为空')
      return
    }
    if (this.data.rolesIndex !== '') {
      params.roleId = this.data.rolesList[this.data.rolesIndex].id
    }
    // if (this.data.entsIndex !== '') {
    //   params.entTaxIds = [this.data.ents[this.data.entsIndex].entTaxId]
    // }
    utils.axios({
      method: 'post',
      url: '/auth/ent/user/saveOnEnt',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '人员新增中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.funPrev(prev => {
          prev.toFresh()
          utils.toast('人员新增成功')
          wx.navigateBack()
        })
      }
    })
  },

  //编辑人员
  edit() {
    let params = {
      entUserMobile: utils.trimAll(this.data.userMobile) ,
      entUserName:utils.trimAll(this.data.userName),
      reqChannel: 5,
    
      mobile:utils.getInfo().mobile,
      entTaxId:this.data.entInfo.entTaxId
    }
    if (params.name === '') {
      utils.toast('员工姓名不能为空')
      return
    }
    if (params.mobile === '') {
      utils.toast('员工手机号码不能为空')
      return
    }
    if (this.data.rolesIndex !== '') {
      params.roleId = this.data.rolesList[this.data.rolesIndex].id
    }
    // if (this.data.entsIndex !== '') {
    //   params.entTaxIds = [this.data.ents[this.data.entsIndex].entTaxId]
    // }
    utils.axios({
      method: 'post',
      url: '/auth/ent/user/saveOnEnt',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '保存中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.funPrev(prev => {
          prev.toFresh()
          utils.toast('编辑成功')
          wx.navigateBack()
        })
      }
    })
  },

  //角色列表
  getRoles() {
    utils.axios({
      method: 'get',
      url: '/auth/ent/role/findCategoryRoles',
      sendBefore() {
        wx.showLoading({
          title: '角色获取中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        let rolesList = res.data
        let showRoles = []
        for (let i = 0; i < rolesList.length; i++) {
          showRoles.push(rolesList[i].remark)
        }
        this.setData({
          showRoles,
          rolesList
        })
        if (this.data.type === 1) {
          let roleId = this.data.userInfoBei.roleId
          for (let i = 0; i < rolesList.length; i++) {
            if (rolesList[i].id === roleId) {
              this.setData({
                rolesIndex: i
              })
            }
          }
        }
      }
    })
  },
  setRoles(e) {
    let rolesIndex = Number(e.detail.value)
    this.setData({
      rolesIndex
    })
  },

  //绑定企业
  getManager() {
    let params = {
      mobile: this.data.userInfo.mobile
    }
    utils.axios({
      method: 'get',
      url: '/auth/ent/user/findManageInfoByMobile',
      data: params,
      sendBefore() {
        wx.showNavigationBarLoading()
      },
      complete() {
        wx.hideNavigationBarLoading()
      },
      success: res => {
        let ents = res.data.ents
        for (let i = 0; i < ents.length; i++) {
          ents[i].selected = false
        }
        if (this.data.type === 1) {
          let userEnts = this.data.userInfoBei.ents
          for (let i = 0; i < ents.length; i++) {
            for (let j = 0; j < userEnts.length; j++) {
              if (ents[i].entTaxId === userEnts[j].entTaxId) {
                ents[i].selected = true
                this.setData({
                  entsIndex: i
                })
              }
            }
          }
        }
        this.setData({
          ents
        })
      }
    })
  },
  choose(e) {
    let index = Number(e.currentTarget.dataset.index)
    let ents = this.data.ents
    for (let i = 0; i < ents.length; i++) {
      ents[i].selected = false
    }
    ents[index].selected = true
    this.setData({
      ents,
      entsIndex: index
    })
  },

  //员工姓名
  getName(e) {
    let value = e.detail.value
    this.setData({
      userName: value
    })
  },
  getMobile(e) {
    let value = e.detail.value
    this.setData({
      userMobile: value
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})