﻿﻿﻿﻿﻿﻿<template>
  <div id="">
    <a-tree
      :load-data="loadAddvData"
      :tree-data="treeData"
      @select="nodeClick"
      :fieldNames="treeProps"
      class="el-treeWhite"
      :default-expanded-keys="tableDefaultExpandedKeys"
      style="overflow:auto;"
      :style="{ 'max-height': dxdcAsideHeight+'px' }"/>
  </div>
</template>

<script>
import { getOrgByPid } from '../api/duChaAPI.js';
export default {
  props: ['rootCode'],
  data() {
    return {
      dxdcAsideHeight: window.innerHeight - 90,
      treeData: [],
      tableDefaultExpandedKeys: [],
      treeProps: {
        title: 'name',
        children: 'children',
        key: 'code',
      },
    };
  },
  mounted: function () {
    this.loadTreeListData();
  },
  methods: {
    loadTreeListData: function () {
      if (this.rootCode && this.rootCode != '') {
        getOrgByPid(this.rootCode).then((res) => {
          const children = [];
          res.data.forEach((element, index) => {
            children.push({
              code: element.orgId,
              name: element.orgNm,
            });
          });
          this.treeData = children;
          // if(children.length && children[0].code != '11' && children[0].code != '12'){
          //   this.$emit("provTreeSelectChange", { code:children[0].code, name: children[0].name});
          // }
        });
      } else {
        getOrgByPid('1').then((res) => {
          const children = [];
          res.data.forEach((element, index) => {
            children.push({
              code: element.orgId,
              name: element.orgNm,
            });
          });
          this.treeData = children;
        });
      }
    },
    nodeClick(selectedKeys, e) {
      const { dataRef: data } = e.node;
      this.$emit('provTreeSelectChange', { code: data.code, name: data.name });
    },
    loadAddvData: (treeNode) =>
      new Promise((resolve) => {
        if (treeNode.dataRef.children) {
          resolve();
          return;
        }
        setTimeout(() => {
          getOrgByPid(treeNode.dataRef.code).then((response) => {
            var data = response.data;
            const children = [];
            response.data.forEach((element, index) => {
              children.push({
                code: element.orgId,
                name: element.orgNm,
              });
            });
            treeNode.dataRef.children = children;
            resolve();
          });
        }, 500);
      }),
  },
};
</script>

<style lang='less' scoped>

</style>
