const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'roleFun',
    active: 0,
    activeTabs: 0,
    tableData: [],
    tableDataWXP: [],
    tableDataWEB: [],
    formData: {},
    buttonSaveList: [{ code: 'add', title: '保存', width: '120rpx' }],
    pageFlag: false
  },
  /**
 * @desc :   全选事件
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  onChangeSwitch(e) {
    console.log(e)
    let formData = this.data.formData
    let tableData = this.data.tableData
    formData.checked = 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
          })
        }
      })
    }

    this.setData({
      formData: formData,
      tableData: tableData
    })
  },



  /**
 * @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 : 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 : 于继渤
   */
  setSearchParams(params) {
    if (this.data.id && this.data.id) {
      params.roleId = this.data.id
    }
    if (this.data.item && this.data.item.roleId) {
      params.roleId = this.data.item.roleId
    }
    if (this.data.activeTabs == 1) {
      params.appCode = 'CP-WEB'
    } else {
      params.appCode = 'CP-WXP'
    }

    let menuUuids = []  //没保存进数据库之前选择的数据需要回显
    let chooseData = this.data.chooseData
    if (chooseData && chooseData.roleFunList && chooseData.roleFunList.length > 0) {
      chooseData.roleFunList.forEach(it => {
        if (it && it.menuUuid) {
          menuUuids.push(it.menuUuid)
        }
      })
    }
    params.menuUuids = menuUuids
    return params;
  },
  /**
 * @desc : 加载数据
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  getData(params) {
    return this.excute(this.data.service, this.data.service.getRoleFun, 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 : 于继渤
   */
  toAdd() {
    let tableDataWEB = this.data.tableDataWEB
    let tableDataWXP = this.data.tableDataWXP
    //格式化数据 web
    let tableDataWEBTemp = []
    tableDataWEB.forEach(res => {
      if (res.checked) {
        tableDataWEBTemp.push({
          funUuid: res.funUuid,
          menuUuid: res.menuUuid,
          appCode: 'CP-WEB'
        })
      }
      if (res.children && res.children.length > 0) {
        res.children.forEach(it => {
          if (it.checked) {
            tableDataWEBTemp.push({
              funUuid: it.funUuid,
              menuUuid: it.menuUuid,
              appCode: 'CP-WEB'
            })
          }

          if (it.children && it.children.length > 0) {
            it.children.forEach(itt => {
              if (itt.checked) {
                tableDataWEBTemp.push({
                  funUuid: itt.funUuid,
                  menuUuid: itt.menuUuid,
                  appCode: 'CP-WEB'
                })
              }
            })
          }
        })


      }
    })
    //格式化数据 小程序
    let tableDataWXPTemp = []
    tableDataWXP.forEach(res => {
      if (res.checked) {
        tableDataWXPTemp.push({
          funUuid: res.funUuid,
          menuUuid: res.menuUuid,
          appCode: 'CP-WXP'
        })
      }
      if (res.children && res.children.length > 0) {
        res.children.forEach(it => {
          if (it.checked) {
            tableDataWXPTemp.push({
              funUuid: it.funUuid,
              menuUuid: it.menuUuid,
              appCode: 'CP-WXP'
            })
          }
        })


      }
    })
    //功能权限跳转编辑
    let  list = tableDataWXPTemp.concat(tableDataWEBTemp)
    console.log('list',list)
    if (this.data.formType == Constants.formMode.edit || this.data.formType == Constants.formMode.add) {
      //调用接口修改权限
      let formData = this.data.formData
      formData.roleFunList = list
      formData.roleId = this.data.id
      this.setData({
        formData: JSON.stringify(this.data.formData)
      })
      let param = {}
      this.save(param)
    } else {
      // 给父页面传递数据
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.emit('bindData', {
        showFlag: 'selectFunctionalPermissions',
        list: list
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
    * @desc   : 处理数据
    * @author : 于继渤
    * @date   : 2024/1/26 11:46
    */
  handleData(data) {

  },
  /**
    * @desc : 批量修改权限接口
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  saveData(params) {
    return this.excute(this.data.service, this.data.service.updateBatch, params);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //接收父页面传递的参数
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('params', function (data) {
      let formData = that.data.formData
      formData.roleName = data.item ? data.item.roleName : data.roleName
      that.setData({
        searchForm: JSON.stringify({
          roleId: data.item ? data.item.roleId : (data.id ? data.id : data.roleId)
        }),
        formData: formData,
        id: data.item ? data.item.roleId : (data.id ? data.id : data.roleId),
        formType: data.item ? data.item.button.formType : data.formType
      })
    })
  },


})