<template>
  <div>
    <div class="treebox-header">
      <el-input :placeholder="$t('UserMixed.roletree.5205u7dq5po0')" v-model="filterTree" size="mini" clearable> </el-input>
    </div>
    <div class="treebox-content">
      <el-tree
        v-if="treeData"
        ref="tree"
        node-key="value"
        :data="treeData"
        :check-strictly="checkStrictly"
        :check-on-click-node="true"
        :expand-on-click-node="false"
        :default-expanded-keys="defaultExpandedKeys"
        :default-checked-keys="defaultCheckedKeys"
        :current-node-key="currentNodeKey"
        :filter-node-method="filterNode"
        @check-change="nodeClick"
        :highlight-current="true"
        :show-checkbox="showCheckbox"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span v-if="data.icon" style="margin-right:10px;"><i :class="data.icon"/></span>
          <span>{{ node.label }}</span>
        </span>
      </el-tree>
    </div>
  </div>
</template>

<script>
export default {
  name: 'roletree',
  props: {
    value: { required: true },
    treeData: { dafault: null },
    closeTag: { default: null },
    defaultExpandedKeys:{default:null},
    defaultCheckedKeys:{default:null},
    currentNodeKey:{default:null},
    multiple: Boolean //是否多选

  },
  data() {
    return {
      title: this.$t('UserMixed.roletree.5205u7dq7eo0'),
      filterTree: ''
    }
  },
  computed: {
    checkStrictly() {
      if (this.multiple) {
        return false
      } else {
        return true
      }
    },
    showCheckbox() {
      if (this.multiple) {
        return true
      } else {
        return false
      }
    }
  },
  methods: {
    deepClone(obj) {
      var _obj = JSON.stringify(obj),
        objClone = JSON.parse(_obj)
      return objClone
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.includes(value)
    },
    addChildrenTag(childrens) {
      console.log(childrens)
      let chickChildrens = childrens.filter(item => {
        return this.$refs.tree.getCheckedKeys().includes(item.value)
      })
      chickChildrens.forEach(data => {
        let index = this.value.findIndex(value => value.Id == data.id && value.Type == data.type)
        if (index == -1) {
          this.value.push({ Id: data.id, Label: data.label, Type: data.type })
        }
      })
    },
    deleteRoleTag(childrens) {
      childrens.forEach(data => {
        let index = this.value.findIndex(value => value.Id == data.id && value.Type == data.type)
        if (index > -1) {
          this.value.splice(index, 1)
        }
      })
    },
    parentIsChecked() {
      return this.$refs.tree.getCheckedKeys().findIndex(key => key == 'role_0') > -1
    },
    nodeClick(data, checked, indeterminate) {
      if (this.multiple) {
        let index = this.value.findIndex(value => value.Id == data.id && value.Type == data.type)
        if (!checked) {
          if (index > -1) {
            this.value.splice(index, 1)
          }
          if (indeterminate) {
            this.addChildrenTag(data.children)
          }
        } else {
          if (data.id == 0) {
            this.deleteRoleTag(data.children)
          } else {
            if (this.parentIsChecked()) {
              return
            }
          }

          if (index == -1) {
            this.value.push({ Id: data.id, Label: data.label, Type: data.type })
          }
        }
      } else {
        if (this.value.length > 0) {
          this.value.splice(0, 1, { Id: data.id, Label: data.label, Type: data.type })
        } else {
          this.value.push({ Id: data.id, Label: data.label, Type: data.type })
        }
      }
    }
  },
  mounted() {},
  watch: {
    closeTag: {
      handler(val) {
        if (val) {
          if (this.multiple) {
            this.$refs.tree.setChecked(`${val.Type}_${val.Id}`, false, true)
          }
        }
      },
      deep: true
    },
    filterTree(val) {
      console.log(val)
      this.$refs.tree.filter(val)
    }
  }
}
</script>
