<template>
  <div>
    <a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onChange" />
    <a-tree
      v-if="deptOptions.length"
      :tree-data="deptOptions"
      :replaceFields="replaceFields"
      :expandedKeys="expandedKeys"
      checkable
      @check="onCheck"
      :auto-expand-parent="autoExpandParent"
      @expand="onExpand"
      @select="handleNodeClick"
    >
      <template slot="label" slot-scope="{ label }">
        <span :title="label" class="showname" v-if="label.indexOf(searchValue) > -1">
          {{ label.substr(0, label.indexOf(searchValue)) }}
          <span :title="label" style="color: #f50">{{ searchValue }}</span>
          {{ label.substr(label.indexOf(searchValue) + searchValue.length) }}
        </span>
        <div v-else class="showname" >
          <span :title="label"> {{ label }}</span>
        </div>
      </template>
    </a-tree>
    <div v-else style="text-align: center;">
      <a-spin />
    </div>
  </div>
</template>
<script>
import { treeSelectUser } from '@/api/system/dept'
const getParentKey = (id, tree) => {
  let parentKey
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i]
    if (node.children) {
      if (node.children.some(item => item.id === id)) {
        parentKey = node.id
      } else if (getParentKey(id, node.children)) {
        parentKey = getParentKey(id, node.children)
      }
    }
  }
  return parentKey
}
const dataList = []
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i]
    const id = node.id
    const label = node.label
    dataList.push({ id, label: label })
    if (node.children) {
      generateList(node.children)
    }
  }
}
export default {
  name: 'DeptTree',
  props: {
  },
  components: {
  },
  data () {
    return {
      replaceFields: { children: 'children', title: 'label', key: 'id', value: 'id' },
      deptNodes: [],
      expandedKeys: [],
      deptOptions: [],
      searchValue: '',
      autoExpandParent: true
    }
  },
  filters: {
  },
  created () {
    this.generateLists()
  },
  computed: {
  },
  watch: {
  },
  methods: {
     dataChange (data) {
       const arr = []
       data.map(item => {
            if (!isNaN(item / 1)) {
              arr.push(item)
              console.log('%c [ arr ]-90', 'font-size:13px; background:pink; color:#bf2c9f;', arr)
            }
       })
       return arr
    },
    onCheck (checkedKeys, info) {
      this.$emit('setDeptIds', this.dataChange(checkedKeys))
    },
    // getDisabled (data) {
    //   data.map(item => {
    //     item.scopedSlots = { title: 'label' }
    //     if (item.children) {
    //         item.disabled = true
    //         this.getDisabled(item.children)
    //     } else if (item.type === 'dept') {
    //         item.disabled = true
    //     }
    //   })
    //   return data
    // },
    addAttr (data) {
        data.map((item) => {
          item.scopedSlots = { title: 'label' }
          if (item.children) this.addAttr(item.children)
        })
        return data
    },
    generateLists () {
       treeSelectUser().then(response => {
        this.deptOptions = this.addAttr(response.data)
        generateList(this.deptOptions)
      })
    },
     onChange (e) {
      const value = e.target.value
      console.log('%c [ value ]-74', 'font-size:13px; background:pink; color:#bf2c9f;', value)
      const expandedKeys = dataList
        .map(item => {
          if (item.label.indexOf(value) > -1) {
            return getParentKey(item.id, this.deptOptions)
          }
          return null
        })
        .filter((item, i, self) => item && self.indexOf(item) === i)
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true
      })
    },
    //  onLoadData (treeNode) {
    //   return new Promise(resolve => {
    //     if (treeNode.dataRef.children) {
    //       resolve()
    //       return
    //     }
    //     setTimeout(() => {
    //       treeNode.dataRef.children = [
    //         { title: 'Child Node', key: `${treeNode.eventKey}-0` },
    //         { title: 'Child Node', key: `${treeNode.eventKey}-1` }
    //       ]
    //       this.treeData = [...this.treeData]
    //       resolve()
    //     }, 1000)
    //   })
    // },
    getAllDeptNode (nodes) {
      if (!nodes || nodes.length === 0) {
        return []
      }
      nodes.forEach(node => {
        this.deptNodes.push({ id: node.id, label: node.label })
        return this.getAllDeptNode(node.children)
      })
    },
    // 筛选节点
    filterNode (e) {
      this.getAllDeptNode(this.deptOptions)
      const value = e.target.value
      const gData = this.deptOptions
      const expandedKeys = this.deptNodes
        .map(item => {
          if (item.label.indexOf(value) > -1) {
            return getParentKey(item.id, gData)
          }
          return null
        })
        .filter((item, i, self) => item && self.indexOf(item) === i)
      Object.assign(this, {
        expandedKeys: expandedKeys,
        searchValue: value,
        autoExpandParent: true
      })
      this.deptNodes = []
    },
    // 节点单击事件
      handleNodeClick (keys, event) {
      this.$emit('select', keys[0])
      event.node.onExpand()
    },
    onExpand (selectedKeys) {
      this.autoExpandParent = false
      this.expandedKeys = selectedKeys
    }
    // onExpand (expandedKeys) {
    //   this.expandedKeys = expandedKeys
    //   this.autoExpandParent = false
    // }
  }
}
</script>
<style>

.showname{
    width: 120px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    display: block;
}

</style>
