<template>
  <a-modal v-model="isShowOrg" title="选择机构" :footer="null" >
    <div>
      <a-input-search style="margin-bottom: 8px" placeholder="搜索名称" @change="onChange" />
      <a-tree
        :disable-branch-nodes="true"
        :expanded-keys="expandedKeys"
        :auto-expand-parent="autoExpandParent"
        :tree-data="orgList"
        @expand="onExpand"
        @select="onSelect"
        :replaceFields="{
          children:'children',
          title:'name',
          key:'id'
        }"
      >
        <template slot="name" slot-scope="{ name }">
          <span v-if="name.indexOf(searchValue) > -1">
            {{ name.substr(0, name.indexOf(searchValue)) }}
            <span style="color: #f50">{{ searchValue }}</span>
            {{ name.substr(name.indexOf(searchValue) + searchValue.length) }}
          </span>
          <span v-else>{{ name }}</span>
        </template>
      </a-tree>
    </div>
  </a-modal>
</template>

<script>
import { orgList } from '@/api/org/org'
import { listToTree } from '@/utils/util'
const rootParentId = 0

// 所有节点放进dataList,便于搜索
const dataList = []
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i]
    // 设置插槽,搜索结果样式变化
    node.scopedSlots = { title: 'name' }
    const id = node.id
    const name = node.name
    dataList.push({ id, name })
    if (node.children) {
      generateList(node.children)
    }
  }
}

// 获取传入id所在节点的父节点
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
}
export default {
  data () {
    return {
      expandedKeys: [],
      searchValue: '',
      autoExpandParent: true,
      orgList: [],
      isShowOrg: false
    }
  },
  methods: {
    onExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSelect (selectedKeys, info) {
      var orgInfo = info.node.dataRef
      const org = { id: orgInfo.id, name: orgInfo.name }
      this.$emit('selectOrg', org)
      this.isShowOrg = false
    },
    onChange (e) {
      const value = e.target.value
      // 获取搜索结果所在节点的父节点集合
      const expandedKeys = dataList
        .map(item => {
          if (item.name.indexOf(value) > -1) {
            return getParentKey(item.id, this.orgList)
          }
          return null
        })
        .filter((item, i, self) => item && self.indexOf(item) === i)
      // 拷贝,根据搜索结果设置展开节点
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true
      })
    },
    loadOrg () {
      orgList({ orgType: 1 }).then(res => {
        if (!res.code === 200 || !res.data.length) {
          return
        }
        const orgTree = listToTree(res.data, [], rootParentId)
        for (var item of orgTree) {
          if (item.pid === 0) {
            this.expandedKeys.push(item.id)
          }
        }
        generateList(orgTree)
        this.orgList = orgTree
      })
      this.isShowOrg = true
    }
  }
}
</script>
