<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="groupName" slot-scope="{ groupName }">
        <span v-if="groupName.indexOf(searchValue) > -1">
          {{ groupName.substr(0, groupName.indexOf(searchValue)) }}
          <span style="color: #f50">{{ searchValue }}</span>
          {{ groupName.substr(groupName.indexOf(searchValue) + searchValue.length) }}
        </span>
        <span v-else>{{ groupName }}</span>
      </template>
    </a-tree>
    <div v-else style="text-align: center;">
      <a-spin />
    </div>
  </div>
</template>
<script>

import { listGroup } from '@/api/system/group'
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.groupId === id)) {
        parentKey = node.groupId
      } 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 groupId = node.groupId
    const groupName = node.groupName
    dataList.push({ groupId, groupName: groupName })
    if (node.children) {
      generateList(node.children)
    }
  }
}
export default {
  name: 'DeptTree',
  props: {

  },
  components: {
  },
  data () {
    return {
      replaceFields: { children: 'children', title: 'groupName', key: 'groupId', value: 'groupId' },
      deptNodes: [],
      expandedKeys: [],
      deptOptions: [],
      searchValue: '',
      autoExpandParent: true
    }
  },
  filters: {
  },
  created () {
    this.generateLists()
  },
  computed: {
  },
  watch: {
  },
  methods: {
     onCheck (checkedKeys, info) {
      this.$emit('setDeptIds', checkedKeys)
      console.log('%c [ checkedKeys ]-181', 'font-size:13px; background:pink; color:#bf2c9f;', checkedKeys)
    },
    addAttr (data) {
        data.map((item) => {
          item.scopedSlots = { title: 'groupName' }
          if (item.children) this.addAttr(item.children)
        })
        return data
    },
    generateLists () {
        listGroup().then(response => {
          const treeData = this.handleTree(response.data, 'groupId')
          this.deptOptions = this.addAttr(treeData)
          generateList(this.deptOptions)
       })
    },
    onChange (e) {
      const value = e.target.value
      const expandedKeys = dataList
        .map(item => {
          if (item.groupName.indexOf(value) > -1) {
            return getParentKey(item.groupId, this.deptOptions)
          }
          return null
        })
        .filter((item, i, self) => item && self.indexOf(item) === i)
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true
      })
    },
    handleNodeClick (keys, event) {
      console.log('%c [ event ]-121', 'font-size:13px; background:pink; color:#bf2c9f;', event)
      console.log('%c [ keys ]-121', 'font-size:13px; background:pink; color:#bf2c9f;', keys)
      this.$emit('select', keys[0])
      event.node.onExpand()
    },
    onExpand (selectedKeys) {
       this.autoExpandParent = false
      this.expandedKeys = selectedKeys
    }
  }
}
</script>
