/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.员工资料-新组建
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		admin		   2024-2-12	   1.00		   	    
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const Constants = require('@/utils/Constants')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: ['main', 'oauth', 'remarks'],
    // 底部保存按钮
    buttonFootList: [{
      name: 'save',
      title: mixins.$t('save')
    }],
    contentObj: {
      main:
        [
          { code: 'staffCode', type: 'str', required: true, title: mixins.$t('staffCode') },
          { code: 'staffName', type: 'str', required: true, title: mixins.$t('staffName') },
          { code: 'staffPhone', type: 'phone', title: mixins.$t('staffPhone'), required: true },
          { code: 'orgId', name: 'orgName', type: 'choose', required: true, urlKey: 'chooseOrg' },
          { code: 'roleId', name: 'roleName', type: 'choose', required: false, urlKey: 'chooseRole' },

        ],
      oauth:
        [],
      remarks:
        [
          { code: 'remarks', type: 'textarea', title: mixins.$t('remarks') }
        ]
    },
    // 路由
    routeObjName: 'staff',
    gradeCode: app.globalData.company.gradeCode,
  },

  /**
* @desc : 选择数据源
* @date : 2024/2/1 15:49
* @author : 姜永辉
*/
  chooseData(e) {
    console.log("chooseData", e);
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data

    if (code == "staffId") { //员工
      formData.staffId = data.id
      formData.staffName = data.name
    }
    if (code == "orgId") { //部门
      formData.orgId = data.id
      formData.orgName = data.name
    }
    if (code == "roleId") { //角色
      let ids = []
      let names = []
      if (data && data.list) {
        data.list.forEach(element => {
          ids.push(element.roleId)
          names.push(element.roleName)
        });
      }
      formData.roleIds = ids.join(',')
      formData.roleName = names.join(',')
    }

    this.setData({
      formData: JSON.stringify(formData)
    })
  },

  /**
  * @desc : 保存的參數設置
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  setParams(params) {
    let formData = JSON.parse(this.data.formData)
    params.roleIds = (params.roleIds && params.roleIds.length > 0) ? params.roleIds.split(',') : []
    // 专业版
    if (this.data.gradeCode == Constants.gradeCode.PRO) {
      params.flgCanLogin = (formData.flgCanPcLogin || formData.flgCanWxLogin)
      // 1：PC端，2：移动端，3：PC+移动端
      if (formData.flgCanPcLogin && formData.flgCanWxLogin) {
        params.loginType = Constants.loginType.loginTypeWxPc
      } else if (formData.flgCanPcLogin) {
        params.loginType = Constants.loginType.loginTypePc
      } else if (formData.flgCanWxLogin) {
        params.loginType = Constants.loginType.loginTypeWx
      } else {
        params.loginType = Constants.loginType.loginTypeNo
      }
    } else {
      //  标准版的 loginType 只能是  2：移动端  没有pc端
      params.loginType = Constants.loginType.loginTypeWx
    }
    return params
  },

  /**
  * @desc : 保存数据服务
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  saveData() {
    if (this.data.formMode == Constants.formMode.edit) {
      return this.excute(this.data.service, this.data.service.update, this.data.params)
    } else {
      return this.excute(this.data.service, this.data.service.insert, this.data.params)
    }
  },
  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  setValuesByEdit(data) {
    console.log(data, "setValuesByEdit");
    data.roleIds = (data.roleIds && data.roleIds.length > 0) ? data.roleIds.join(',') : undefined
    data.roleName = data.roleNames
    // 专业版
    if (this.data.gradeCode == Constants.gradeCode.PRO) {
      // 1：PC端，2：移动端，3：PC+移动端 4无
      if (data.loginType == Constants.loginType.loginTypeWxPc) {
        if (data.flgCanLogin) {
          data.flgCanPcLogin = true
          data.flgCanWxLogin = true
        }
      } else if (data.loginType == Constants.loginType.loginTypePc) {
        if (data.flgCanLogin) {
          data.flgCanPcLogin = data.flgCanLogin
        }
      } else if (data.loginType == Constants.loginType.loginTypeWx) {
        if (data.flgCanLogin) {
          data.flgCanWxLogin = data.flgCanLogin
        }
      }

    }

    this.setData({
      formData: JSON.stringify(data)
    })
  },

  /**
   * @desc : 页面加载数据方法
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  loadInit() {
    let contentObj_ = this.data.contentObj
    if (this.data.formMode == Constants.formMode.edit) {
      wx.setNavigationBarTitle({
        title: mixins.$t('staffEdit'),
      })

    } else {
      wx.setNavigationBarTitle({
        title: mixins.$t('staffAdd'),
      })
      let formData = {}
      formData.orgId = app.globalData.user.orgId
      formData.orgName = app.globalData.user.orgName
    }
    // 专业版
    if (this.data.gradeCode == Constants.gradeCode.PRO) {
      contentObj_['oauth'] =
        [
          { code: 'flgCanPcLogin', type: 'switch', title: mixins.$t('flgCanPcLogin') },
          { code: 'flgCanWxLogin', type: 'switch', title: mixins.$t('flgCanWxLogin') },
        ]
    } else {
      contentObj_['oauth'] =
        [
          { code: 'flgCanLogin', type: 'switch', title: mixins.$t('flgCanLogin') },
        ]
        //标准版-新建选择部门页面隐藏新建
      if (this.data.formMode == Constants.formMode.edit) { } else {
        let formData = {}
        formData.orgId = app.globalData.user.orgId
        formData.orgName = app.globalData.user.orgName
        this.setData({
          formData: JSON.stringify(formData)
        })
      }

    }
    this.setData({
      contentObj: contentObj_,
    })
  },
})