<template>
  <mc-tree-node
    :data="data"
    :multiple="multiple"
    :selected-key="selectedKey"
    :data-key-map="dataKeyMap"
    :expand="expand"
    :edited="edited"
    :delimiter="delimiter"
    :iconCaretRight="iconCaretRight"
    :iconCaretDown="iconCaretDown"
    :on-item-checked="onItemChecked"
    :on-item-dblclick="onItemDblclick"
    :on-item-click="onItemClick">
  </mc-tree-node>
</template>
<script>
  export default {
    props: {
      defaultSelectedKey: { type: String, default: '' },
      defaultCheckedKeys: { type: Array, default: () => [] },
      multiple: { type: Boolean, default: true },
      expand: { type: Boolean, default: false },
      data: { type: Array, required: true }, // [{title: '', key: '', children: '', disabled: false}]
      edited: { type: Boolean, default: false },
      delimiter: { type: String, default: '-' },
      iconCaretRight: { type: String, default: 'mc-icon-triangle-right' },
      iconCaretDown: { type: String, default: 'mc-icon-triangle-down' }
    },
    data () {
      return {
        selectedKey: this.defaultSelectedKey,
        dataKeyMap: this.getResetDataKeyMap()
      }
    },
    watch: {
      defaultSelectedKey (nowVal, oldVal) {
        this.selectedKey = nowVal
      }
    },
    methods: {
      getResetDataKeyMap (checkedKeys) {
        const dataKeyMap = {}
        initDataKeysMap(dataKeyMap, new Set(this.defaultCheckedKeys), this.data, '-' ,null )
        // console.log(JSON.stringify(dataKeyMap))
        // console.log(dataKeyMap)
        return dataKeyMap
      },
      onItemChecked (item) {
        // debugger;
        
        
        var status = this.dataKeyMap[item.key][0]
        var children = this.dataKeyMap[item.key][1]
        // // console.log('status:'+status)
        // // console.log('children:'+children)
        // // this.dataKeyMap[key].splice(0, 1,state)

        

        if (this.dataKeyMap[item.key][1]) {
          for (var k in children) {
            console.log(this.dataKeyMap[children[k]][3])
            console.log(this.dataKeyMap[children[k]])
            console.log(this.dataKeyMap[children[k]][0])
            if (this.dataKeyMap[children[k]][3] && this.dataKeyMap[children[k]][1]==="checked") {
              return true
            } else{
              // return this.dataKeyMap[children[k]]==="open"
            }
          }
          // console.log(this.dataKeyMap[item.key][1].length);
          
        }
        


        if (this.dataKeyMap[item.key][0] === 'checked') {
          this.setAllChildrenState(item.key, '')
        } else {
          this.setAllChildrenState(item.key, 'checked')
        }
        this.setAllParentState(item.key)
        this.$emit('item-checked', this.getCheckedKeys())
      },
      onItemDblclick (item) {
        this.$emit('item-dblclick', item)
      },
      onItemClick (item) {
        this.selectedKey = item.key
        this.$emit('item-click', item)
      },
      onItemRemove (data, item, itemIndex) {
        this.$emit('item-remove', data, item, itemIndex)
      },
      getCheckedKeys () {
        // 获取所有选择的keys
        let checkedKeys = Object.keys(this.dataKeyMap).filter((key) => {
          return this.dataKeyMap[key][0] === 'checked'
        })
        return checkedKeys.filter((key, index) => {
          for (let k of checkedKeys) {
            let a = checkedKeys;
            let b = this.dataKeyMap[key][2]
            if (k !== key && this.dataKeyMap[key][2] === k ) {
              return false
            }
          }
          return true
        })
      },
      setAllChildrenState (key,state) {
        // 设置所有子节点的状态
        
        this.dataKeyMap[key].splice(0, 1,state)
        // console.log(this.dataKeyMap[key][1])
        this.dataKeyMap[key][1] && this.dataKeyMap[key][1].forEach((child) => {
          if (!this.dataKeyMap[child][3]) {
            this.setAllChildrenState(child,state)
          } else {
            
            return false
          }
        })
      },
      setAllParentState (key) {
        // 设置所有父亲节点的状态
        if (this.dataKeyMap[key]) {
          let parentKey = this.dataKeyMap[key][2]
          if (parentKey === '') return
          if (this.dataKeyMap[parentKey] && this.dataKeyMap[parentKey][1]) {
            let parentAllChildrenChecked = this.dataKeyMap[parentKey][1].every((child) => {
              return this.dataKeyMap[child][0] === 'checked'
            })
            let parentAllChildrenOpen = this.dataKeyMap[parentKey][1].some((child) => {
              return this.dataKeyMap[child][0] === 'open' || this.dataKeyMap[child][0] === 'checked'
            })
            this.dataKeyMap[parentKey].splice(0, 1, parentAllChildrenChecked ? 'checked' : (parentAllChildrenOpen ? 'open' : ''))
          }
          this.setAllParentState(parentKey)
        }
      }
    }
  }
  const getKeyState = (setKeys, key) => {
    if (setKeys.has(key)) return 'checked'
    for (let setKey of setKeys.keys()) {
      if (setKey.startsWith(key)) return 'open'
      if (key.startsWith(setKey)) return 'checked'
    }
    return ''
  }
  const initDataKeysMap = (dataKeysMap, setKeys, items, parentKey , disabled) => {
    items && items.forEach((item) => {
      dataKeysMap[item.key] = [getKeyState(setKeys, item.key) , item.children && item.children.map((child) => child.key) ,parentKey, item.disabled]
      initDataKeysMap(dataKeysMap, setKeys, item.children, item.key , item.disabled)
    })
  }
</script>