/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：折叠面板
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		admin		  	2022-11-17  	 1.00		   	  新建
 *******************************************************************************/
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 数据源
    list: {
      type: Array,
      value: []
    },
    activeNames: {
      type: Array,
      value: []
    },
  },

  /**
   * 组件的选项
   */
  options: {
    addGlobalClass: true,
  },

  /**
   * 组件的初始数据
   */
  data: {
    tableData: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      this.setData({
        activeNames: event.detail,
      });
    },
    onChange1(event) {
      this.setData({
        activeNames1: event.detail,
      });
    },
    onCheckedItem(e) {
      console.log(e)
      let _that = this
      let index = e.currentTarget.dataset.index
      let list = _that.data.list
      list[index].checked = !list[index].checked
      if (list[index].children && list[index].children.length > 0) {
        if (list[index].checked) {
          list[index].childrenChecked = false
        }
        list[index].children.forEach(res => {
          res.checked = list[index].checked
          if (res.children && res.children.length > 0) {
            if (res.checked) {
              res.childrenChecked = false
            }
            res.children.forEach(it => {
              it.checked = res.checked
            })
          }
        })

      }
      list[index].checked = list[index].checked
      console.log('list', list)
      _that.setData({
        list: list
      })
      this.triggerEvent("obtainList", { list: list })
    },
    onCheckedItem1(e) {
      let _that = this
      let index = e.currentTarget.dataset.index
      let index_ = e.currentTarget.dataset.index_
      let list = _that.data.list
      list[index].children[index_].checked = !list[index].children[index_].checked
      //设置子级
      if (list[index].children[index_].children && list[index].children[index_].children.length > 0) {
        list[index].children[index_].children.forEach(res => {
          res.checked = list[index].children[index_].checked
        })
      }
      //设置按钮样式
      let checkedList = []
      list[index].children.forEach(it => {
        if (it.checked) {
          checkedList.push(it)
        }
      })
      if (checkedList.length == list[index].children.length) {
        list[index].checked = true
        list[index].childrenChecked = false
        list[index].children[index_].checked = true
        list[index].children[index_].childrenChecked = false
      } else {
        list[index].checked = false
        // list[index].children[index_].checked = false
        //控制父级选中图标
        list[index].childrenChecked = checkedList.length == 0 ? false : true
        // list[index].children[index_].childrenChecked = checkedList.length == 0 ? false : true
      }
      let checkedListF = []
      list[index].children[index_].children.forEach(it => {
        if (it.checked) {
          checkedListF.push(it)
        }
      })
      if (checkedListF.length == list[index].children[index_].children.length) {
        list[index].children[index_].checked = true
        list[index].children[index_].childrenChecked = false
      } else {

        list[index].children[index_].checked = false
        //控制父级选中图标

        list[index].children[index_].childrenChecked = checkedListF.length == 0 ? false : true
      }
      _that.setData({
        list: list
      })
    },



    //todo
    onCheckedItem_(e) {
      let index = e.currentTarget.dataset.index
      let index_ = e.currentTarget.dataset.index_
      let index_3 = e.currentTarget.dataset.index_3
      let list = this.data.list
      list[index].children[index_].children[index_3].checked = !list[index].children[index_].children[index_3].checked

      let checkedList = []
      list[index].children[index_].children.forEach(it => {
        if (it.checked) {
          checkedList.push(it)
        }
      })
      if (checkedList.length == list[index].children[index_].children.length) {
        list[index].children[index_].checked = true
        list[index].children[index_].childrenChecked = false
      } else {
        list[index].children[index_].checked = false
        //控制父级选中图标
        list[index].children[index_].childrenChecked = checkedList.length == 0 ? false : true
      }
      //设置按钮样式
      let checkedListF = []
      list[index].children.forEach(it => {
        if (it.checked || it.childrenChecked) {
          checkedListF.push(it)
        }
      })
      if (checkedListF.length == list[index].children.length) {
        list[index].checked = true
        list[index].childrenChecked = false
      } else {
        list[index].checked = false
        //控制父级选中图标
        list[index].childrenChecked = checkedListF.length == 0 ? false : true
      }
      this.setData({
        list: list
      })
      this.triggerEvent("obtainList", { list: list })
    },
  }
})
