const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'settingValue',
    tableData: [],
    formData: {},
    cpId: 1,
  },

  /**
 * @desc :   收起展开实践
 * @date : 2024/2/1 15:49
 * @author : 姜永辉
 */
  collapseAndExpand(e) {
    let index = e.currentTarget.dataset.index
    let tableData = this.data.tableData
    let item = tableData[index]
    item.checked = !item.checked
    tableData[index] = item
    this.setData({
      tableData
    })
  },

  /**
  * @desc   : 修改输入框的值
  * @author : 周兴
  * @date   : 2024/2/2 11:46
  */
  changeField(e) {
    let ds = e.currentTarget.dataset
    let findex = ds.findex
    let index = ds.index
    let tableData = this.data.tableData
    let item = tableData[findex]
    item.children[index].settingValue = e.detail
    tableData[findex] = item
    console.log("changeField", e, tableData, index);
    this.setData({
      tableData: tableData
    })
  },

  /**
  * @desc   : 修改Switch的值
  * @author : 周兴
  * @date   : 2024/2/2 11:46
  */
  onChangeSwitch(e) {
    let ds = e.currentTarget.dataset
    let findex = ds.findex
    let index = ds.index
    let tableData = this.data.tableData
    let item = tableData[findex]
    item.children[index].settingValue = e.detail
    tableData[findex] = item
    console.log("onChangeSwitch", e, tableData, index);
    this.setData({
      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 : 姜永辉
  */
  setSearchParams(params) {
    console.log("setSearchParams", params);
    // 在职离职状态
    params.cpId = this.data.cpId
    return params
  },

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

  /**
 * @desc   : 处理数据-保存和查询后的逻辑
 * @author : 姜永辉
 * @date   : 2024/1/26 11:46
 */
  handleData() {
    wx.navigateBack({
      data: 1
    })
  },

  /**
    * @desc : 保存的參數設置
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  setParams(params) {
    console.log("setParams", params, this.data.tableData);
    // 修改之后的 值 
    let tableData = this.data.tableData
    let item = {}
    let settingValueList = []
    //格式化数据 
    tableData.forEach(res => {
      if (res.children && res.children.length > 0) {
        res.children.forEach(it => {
          item = {
            settingCode: it.itemCode,
            cpId: this.data.cpId,
            settingValue: it.settingValue ? it.settingValue : it.defValue,
          }
          settingValueList.push(item)
        })
      }
    })
    params.settingValueList = settingValueList
    return params
  },

  /**
  * @desc : 批量修改敏感信息接口
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  saveData(params) {
    return this.excute(this.data.service, this.data.service.updateBatch, params);
  },



})