const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const config = require('@/config/config.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'staffRight',
    imageUrl: config.image_url + '/static/img/',
    activeTabs: 0,
    tableData: [],
    tableDataWXP: [],
    tableDataWEB: [],
    allchecked: false,
    btnFormData: null,
    // 底部保存按钮
    buttonFootList: [{
      name: 'save',
      title: mixins.$t('save')
    }],
    formMode: Constants.formMode.index,
    pageFlag: false
  },

  /**
   * @desc : tab选择事件
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  onChange(e) {
    this.setData({
      activeTabs: e.detail.index
    })
    if (this.data.tableDataWEB.length == 0) {
      this.searchData()
    }
    if (this.data.tableDataWXP.length == 0) {
      this.searchData()
    }
  },

  /**
    * @desc : 切換是否全部
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  onChangeSwitch(e) {
    let tableData = []
    if (this.data.activeTabs == 0) {
      tableData = this.data.tableDataWXP
    } else {
      tableData = this.data.tableDataWEB
    }
    this.setData({
      allchecked: e.detail
    })
    if (tableData && tableData.length > 0) {
      tableData.forEach(res => {
        res.checked = e.detail
        if (res.children && res.children.length > 0) {
          res.children.forEach(it => {
            it.checked = e.detail
          })
        }
      })
    }
    if (this.data.activeTabs == 0) {
      this.setData({
        tableDataWXP: tableData
      })
    } else {
      this.setData({
        tableDataWEB: tableData
      })
    }
  },

  /**
  * @desc : 保存的參數設置
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  setParams(params) {
    // 修改之后的 值
    params = Object.assign({}, params, this.data.item);
    // params.roleIds = (params.roleIds && params.roleIds.length > 0) ? params.roleIds.join(',') : undefined
    let tableData = this.data.tableData
    //格式化数据
    let tableDataTemp = []
    tableData.forEach(res => {
      if (res.checked) {
        tableDataTemp.push({
          funUuid: res.funUuid,
          appCode: res.appCode
        })
      }
      if (res.subMenus && res.subMenus.length > 0) {
        res.subMenus.forEach(it => {
          if (it.checked) {
            tableDataTemp.push({
              funUuid: it.funUuid,
              appCode: it.appCode
            })
          }
        })
      }
    })

    params.staffRightList = tableDataTemp
    return params
  },



  /**
  * @desc : 保存数据服务
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  saveData(params) {
    return this.excute(this.data.service, this.data.service.saveStaffRight, params)
  },

  /**
   * @desc : 处理接口返回数据
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  handleSearchData(tableData) {
    let temp = util.convertToChildren(tableData)
    temp.forEach(res => {
      if (!res.checked && res.children && res.children.length > 0) {
        let childrenChecked = false
        let trueList = res.children.filter(item => {
          return item.checked == true
        })
        if (trueList.length > 0) {
          childrenChecked = true
        }
        res.childrenChecked = childrenChecked
      }
    })
    if (this.data.activeTabs == 0) {
      this.setData({
        tableDataWXP: temp
      })
    } else {
      this.setData({
        tableDataWEB: temp
      })
    }
  },

  /**
   * @desc : 设置额外参数
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  setSearchParams(params) {
    params.staffId = this.data.item.staffId
    if (this.data.activeTabs == 1) {
      params.appCode = 'CP-WEB'
    } else {
      params.appCode = 'CP-WXP'
    }
    return params
  },
  /**
   * @desc : 获取数据
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  getData(params) {
    return this.excute(this.data.service, this.data.service.getStaffRight, params)
  },
  /**
    * @desc : 点击功能权限的返回值
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  obtainList(e) {
    if (this.data.activeTabs == 0) {
      this.setData({
        tableDataWXP: e.detail.list
      })
    } else {
      this.setData({
        tableDataWEB: e.detail.list
      })
    }
  },

  /**
  * @desc : 页面设置初始信息
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  loadInit() {
    //接收父页面传递的参数 
  },
})