<template>
  <a-tree
    :key="updateKey"
    :block-node="true"
    :tree-data="treeData"
    :load-data="onLoadTreeData"
    :default-expanded-keys="defaultExpandedKeys"
    @select="onTreeSelect"
  >
  </a-tree>
</template>

<script>
import OrgService from './org-service'
import components from './_import-components/sd-org-role-tree-import'

export default {
  name: 'SdOrgRoleTree',
  metaInfo: {
    title: 'SdOrgRoleTree',
  },
  components,
  props: {
    groupId: {
      type: String,
      default: function() {
        return null
      },
    },
  },
  data() {
    return {
      treeData: [],
      updateKey: 0,
      defaultExpandedKeys: [],
    }
  },
  created() {
    this.init()
  },
  methods: {
    onLoadTreeData() {
      return new Promise((resolve) => {
        OrgService.roles.getTree({ groupId: this.groupId, maxResults: -1 }).then((res) => {
          this.treeData = this.parseTreeData([res.data])

          this.defaultExpandedKeys = this.treeData[0].children.map((item) => {
            return item.key
          })

          resolve()
        })
      })
    },
    onTreeSelect(key, e) {
      this.selectedKeys = [e.node.dataRef.key]
      // 部分场景下需要，暂时注释掉，如需要再去掉注释即可
      // const pathIds = e.node._props.pos.split('-').map((item) => {
      //   return parseInt(item, 10)
      // })
      // pathIds.splice(0, 2)

      this.$emit('treeSelect', {
        id: e.node.dataRef.key,
        text: e.node.dataRef.title,
        code: e.node.dataRef.code,
        // parentNodes: [this.treeData[0]].concat(
        //   this.getPathNodes(this.treeData[0].children, pathIds)
        // ),
      })
    },
    getPathNodes(nodes, pathIds) {
      // 点击节点的所有父节点
      let pathNodes = []
      if (pathIds.length > 0) {
        const node = nodes[pathIds.splice(0, 1)[0]]
        pathNodes.push(node)
        if (pathIds.length > 1) {
          pathNodes = pathNodes.concat(this.getPathNodes(node.children, pathIds))
        }
      }
      return pathNodes
    },
    getPrantNodes(name, value) {
      // 根据属性名及对应属性值查节点的父节点
      return this.getInnerPrant(this.treeData, name, value)
    },
    getInnerPrant(nodes, name, value) {
      let nodePath = []

      nodes.filter((node) => {
        if (node[name] === value) {
          nodePath = nodePath.concat([node])
          return true
        } else {
          if (node.children) {
            const childNodePath = this.getInnerPrant(node.children, name, value)
            if (childNodePath.length > 0) {
              nodePath = nodePath.concat([node]).concat(childNodePath)
              return true
            }
          }
        }
      })

      return nodePath
    },
    parseTreeData(deps) {
      return deps.map((dep, index) => {
        return {
          key: dep.id,
          title: dep.text,
          isLeaf: !!(dep.children && dep.children.length === 0),
          code: dep.props.code,
          children: this.parseTreeData(dep.children),
        }
      })
    },
    init() {
      this.onLoadTreeData().then((_) => {
        ++this.updateKey
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
