<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"
      :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 { treeselect } 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: {
    addAttr (data) {
        data.map((item) => {
          item.scopedSlots = { title: 'label' }
          if (item.children) this.addAttr(item.children)
        })
        return data
    },
    generateLists () {
       treeselect().then(response => {
        this.deptOptions = this.addAttr(response.data)
        generateList(this.deptOptions)
      })
    },
      onChange (e) {
      const value = e.target.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
      })
    },
    // 节点单击事件
    handleNodeClick (keys, event) {
      this.$emit('select', keys[0])
      event.node.onExpand()
    },
    onExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    }
  }
}
</script>
<style>

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

</style>
