const mixins = require('@/mixins/index.js')
const app = getApp()
const util = require('@/utils/util.js')
const Constants = require('@/utils/Constants.js');
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: ['main'],
    contentObj: {
      main: [
        { code: 'roleName', type: 'str', required: true, title: mixins.$t('roleName') },
        { code: 'remarks', type: 'textarea', title: mixins.$t('remarks') },
        { formMode: 'index', code: 'roleFunList', idKey: 'roleId', placeholder: mixins.$t('notSet'), urlKey: 'selectFunctionalPermissions', chooseType: true, required: false, type: 'choose', title: mixins.$t('functionalPermissions') },
        { formMode: 'index', idKey: 'roleId', chooseType: true, placeholder: mixins.$t('notSet'), urlKey: 'selectSensitiveData', required: false, code: 'roleSensitiveList', type: 'choose', title: mixins.$t('sensitiveData') }
      ],
    },
    // 路由
    routeObjName: 'role',
    roleFunService: app.globalData['roleFunService'],
    roleSensitiveService: app.globalData['roleSensitiveService'],
    buttonSaveList: [{ code: 'add', title: '保存', width: '120rpx', color: '#1B365D' }],
    contentSaveList: [],
  },

  /**
   * @desc :   表单选择跳转回调事件
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  chooseData(e) {
    let data = e.detail.data
    console.log('chooseData', e)
    let formData = JSON.parse(this.data.formData)
    let contentObj = this.data.contentObj
    if (data.showFlag == 'selectFunctionalPermissions') {
      console.log(data.showFlag,contentObj.main)
      formData.roleFunList = data.list
      if(this.data.formMode == Constants.formMode.edit){
        contentObj.main[3].placeholder = mixins.$t('setUp')
      }else{
        contentObj.main[2].placeholder = mixins.$t('setUp')
      }
      this.setData({
        selectFunctionalPermissionsFlag: data.list.length > 0,
        contentObj: contentObj,
        formData: JSON.stringify(formData)
      })
    } else if (data.showFlag == 'selectSensitiveData') {
      formData.roleSensitiveList = data.list
      if(this.data.formMode == Constants.formMode.edit){
        contentObj.main[4].placeholder = mixins.$t('setUp')
      }else{
        contentObj.main[3].placeholder = mixins.$t('setUp')
      }
     
      this.setData({
        selectSensitiveDataFlag: data.list.length > 0,
        contentObj: contentObj,
        formData: JSON.stringify(formData)
      })
    }
  },







  /**
 * @desc : 校验-需要额外校验就在此函数写
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  validData() {
    let params = this.data.formData
    // 修改之后的 值
    return true;
  },

  /**
  * @desc : 保存数据服务
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  saveData() {
    if (this.data.formMode == Constants.formMode.edit && this.data.formType !== Constants.formMode.add) {
      return this.excute(this.data.service, this.data.service.update, this.data.params);
    } else {
      let params = this.data.params
      params.roleId = null
      params.roleCode = null
      params.flgValid = true
      return this.excute(this.data.service, this.data.service.insert, params);
    }

  },
  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setValuesByEdit(data) {
    if (this.data.formType == Constants.formMode.add) {
      //调用接口查询功能权限和敏感数据
      this.getRoleFun(data.roleId)
      this.getRoleSensitive(data.roleId)

      this.setData({
        id: null
      })

    }
    let contentObj = this.data.contentObj
    contentObj.main.forEach(res => {
      if (data.selectFunctionalPermissionsFlag && res.code == 'roleFunList') {
        data.roleFunList = [{}]
        res.placeholder = mixins.$t('setUp')
      }
      if (data.selectSensitiveDataFlag && res.code == 'roleSensitiveList') {
        data.roleSensitiveList = [{}]
        res.placeholder = mixins.$t('setUp')
      }
    })

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

  },
  /**
   * @desc : 查询功能权限(复制新增)
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  getRoleFun(roleId) {
    return this.excute(this.data.roleFunService, this.data.roleFunService.getRoleFun, { roleId: roleId }).then(res => {
      if (res.data.code == 200) {
        let list = util.convertToChildren(res.data.data.list)
        let tableDataTemp = this.formatDataList(list)
        let formData = JSON.parse(this.data.formData)
        formData.roleFunList = tableDataTemp
        this.setData({
          formData: JSON.stringify(formData)
        })
      }
    });
  },
  /**
   * @desc : 查询敏感信息(复制新增)
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  getRoleSensitive(roleId) {
    return this.excute(this.data.roleSensitiveService, this.data.roleSensitiveService.getRoleSensitive, { roleId: roleId }).then(res => {
      if (res.data.code == 200) {
        let list = util.convertToChildren(res.data.data.list, 'parentCode', 'itemCode')
        let tableDataTemp = this.formatDataList(list, 'getRoleSensitive')
        let formData = JSON.parse(this.data.formData)
        formData.roleSensitiveList = tableDataTemp
        this.setData({
          formData: JSON.stringify(formData)
        })
      }
    });
  },

  /**
   * @desc : 格式化数据(复制新增)
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  formatDataList(list, key) {
    let tableDataTemp = []
    if (key == 'getRoleSensitive') {
      list.forEach(res => {
        if (res.checked) {
          tableDataTemp.push({
            senItemCode: res.itemCode,
            flgShow: res.checked
          })
        }
        if (res.children && res.children.length > 0) {
          res.children.forEach(it => {
            if (it.checked) {
              tableDataTemp.push({
                senItemCode: it.itemCode,
                flgShow: it.checked
              })
            }
          })
        }
      })
    } else {
      list.forEach(res => {
        if (res.checked) {
          tableDataTemp.push({
            funUuid: res.funUuid,
            menuUuid: res.menuUuid,
            appCode: res.appCode
          })
        }
        if (res.children && res.children.length > 0) {
          res.children.forEach(it => {
            if (it.checked) {
              tableDataTemp.push({
                funUuid: it.funUuid,
                menuUuid: it.menuUuid,
                appCode: it.appCode
              })
            }
          })
        }
      })
    }
    return tableDataTemp
  },


  /**
   * @desc : 页面加载数据方法
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  loadInit() {
    if (this.data.formMode == Constants.formMode.edit && this.data.formType !== Constants.formMode.add) {
      wx.setNavigationBarTitle({
        title: mixins.$t('roleEdit')
      })
      let contentObj = this.data.contentObj
      contentObj.main = [
        { code: 'roleCode', type: 'str', required: false, readonly: true, title: mixins.$t('roleCode') },
        { code: 'roleName', type: 'str', required: true, title: mixins.$t('roleName') },
        { code: 'remarks', type: 'textarea', title: mixins.$t('remarks') },
        { formMode: 'index', code: 'roleFunList', idKey: 'roleId', placeholder: mixins.$t('notSet'), urlKey: 'selectFunctionalPermissions', required: false, type: 'choose', title: mixins.$t('functionalPermissions') },
        { formMode: 'index', idKey: 'roleId', placeholder: mixins.$t('notSet'), urlKey: 'selectSensitiveData', required: false, code: 'roleSensitiveList', type: 'choose', title: mixins.$t('sensitiveData') }
      ]
      this.setData({
        contentObj: contentObj
      })
    } else {
      wx.setNavigationBarTitle({
        title: mixins.$t('roleAdd'),
      })
      // let that = this
      // const eventChannel = this.getOpenerEventChannel()
      // eventChannel.on('params', function (data) {
      //   console.log(data)
      //   if(data.item){
      //     that.setData({
      //       formData:JSON.stringify(data.item)
      //     })
      //   }

      // })
    }
  },

  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {



  },






})