/*******************************************************************************
 * 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')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: ['main'],
    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' },
        // { code: 'flgCanLogin', type: 'checkbox', title: mixins.$t('flgCanLogin') },
        { code: 'remarks', type: 'textarea', title: mixins.$t('remarks') }
      ]
    },
    // 路由
    routeObjName: 'staff',
  },

  /**
* @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) {
    params.roleIds = (params.roleIds && params.roleIds.length > 0) ? params.roleIds.split(',') : []
    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) {
    data.roleIds = (data.roleIds && data.roleIds.length > 0) ? data.roleIds.join(',') : undefined
    this.setData({
      formData: JSON.stringify(data)
    })
  },

  /**
   * @desc : 页面加载数据方法
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  loadInit() {
    if (this.data.formMode == Constants.formMode.edit) {
      wx.setNavigationBarTitle({
        title: mixins.$t('staffEdit'),
      })
      let contentObj_ = this.data.contentObj
      contentObj_['main'].forEach(it => {
        if (it.code === 'staffCode') {
          it.readonly = true
        }
      })
      this.setData({
        contentObj: contentObj_
      })
    } else {
      wx.setNavigationBarTitle({
        title: mixins.$t('staffAdd'),
      })
    }
  },
})