const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'roleSensitive',
    tableData: [],
    buttonSaveList:[{code:'add',title:'保存',width:'120rpx'}],
    formData: {},
  },
  /**
     * @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 : 于继渤
     */
  onCheckedItem(e) {
    console.log(e)
    let _that = this
    let index = e.currentTarget.dataset.index
    let tableData = _that.data.tableData
    tableData[index].checked = !tableData[index].checked
    if (tableData[index].children && tableData[index].children.length > 0) {
      console.log('list[index].children', tableData[index].children)
      tableData[index].children.forEach(res => {
        res.checked = tableData[index].checked
      })
    }
    _that.setData({
      tableData: tableData
    })

  },
  /**
     * @desc :   列表交换器选择事件子级
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
  onCheckedItem_(e) {
    let index = e.currentTarget.dataset.index
    let index_ = e.currentTarget.dataset.index_
    let tableData = this.data.tableData
    tableData[index].children[index_].checked = !tableData[index].children[index_].checked
    if (tableData[index].children.filter(res => {
      res.checked
    }).length == tableData[index].children.length) {
      tableData[index].checked = true
    } else {
      tableData[index].checked = false
    }
    this.setData({
      tableData: tableData
    })
  },



  /**
   * @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
    }
    return params;
  },
  /**
 * @desc :   加载数据
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  getData(params) {
    return this.excute(this.data.service, this.data.service.getRoleSensitive, params);
  },

  /**
* @desc :   处理接口返回数据
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  handleSearchData(tableData) {
    this.setData({
      tableData: util.convertToChildren(tableData, 'parentCode', 'itemCode')
    })
  },
  /**
 * @desc :   折叠面板多选框事件
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  obtainList(e) {
    this.setData({
      tableData: e.detail.list
    })
  },

  toAdd() {
    let tableData = this.data.tableData
    //格式化数据
    let tableDataTemp = []
    tableData.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
            })
          }
        })
      }
    })
    //铭感信息跳转编辑
    if (this.data.formType == Constants.formMode.edit || this.data.formType  == Constants.formMode.add) {
      //调用接口修改权限
      let formData = this.data.formData
      formData.roleSensitiveList = tableDataTemp
      formData.roleId = this.data.id
      this.setData({
        formData: JSON.stringify(this.data.formData)
      })
      let params = {}
      this.save(params)
    } else {
      // 给父页面传递数据
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.emit('bindData', {
        showFlag: 'selectSensitiveData',
        list: tableDataTemp
      })
      wx.navigateBack({
        delta: 1
      })
    }

  },

  /**
  * @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
      console.log(data)
      formData.roleName = data.item ?  data.item.roleName : data.roleName
      formData.roleId= data.item ?  data.item.roleId : data.roleId
      that.setData({
        formData: formData,
        id: data.item ? data.item.roleId : data.id,
        formType: data.item ?  data.item.button.formType : data.formType
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})