<template>
  <a-tree
    :load-data="loadAddvData"
    :tree-data="treeData"
    :fieldNames="treeProps"
    :check-strictly="checkStrictly"
    @check="handleCheckChange"
    class="el-treeWhite"
    :default-expanded-keys="tableDefaultExpandedKeys"
    style="max-height:100%;overflow:auto;" />
</template>

<script>
import { getOrgByPid } from '../api/duChaAPI.js';
export default {
  props: {
    rootCode: {},
    orgIdLists: {},
    checkStrictly: {
      // 在显示复选框的情况下，是否严格的遵循父子不互相关联的做法，默认为 false
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      treeData: [],
      tableDefaultExpandedKeys: [],
      treeProps: {
        title: 'name',
        children: 'children',
        key: 'id',
        disabled: false,
        expandOnClickNode: false,
        first: true,
      },
    };
  },
  created() {},
  mounted: function () {
    this.loadTreeListData();
  },
  methods: {
    loadTreeListData: function () {
      if (this.rootCode && this.rootCode != '') {
        getOrgByPid(this.rootCode).then((res) => {
          console.log(res);
          const children = [];
          res.data.forEach((element, index) => {
            children.push({
              code: element.orgId,
              name: element.orgNm,
            });
          });
          this.treeData = children;
        });
      } else {
        getOrgByPid('1').then((res) => {
          console.log(res);
          const children = [];
          res.data.forEach((element, index) => {
            children.push({
              code: element.orgId,
              name: element.orgNm,
            });
          });
          this.treeData = children;
        });
      }
    },
    handleCheckChange(data, e) {
      this.$emit('provTreeSelectChange', data);
    },
    loadAddvData: function (treeNode) {
      return new Promise((resolve) => {
        if (treeNode.dataRef.children) {
          resolve();
          return;
        }
        setTimeout(() => {
          console.log(this.treeProps, 'treeProps');
          getOrgByPid(treeNode.dataRef.code).then((response) => {
            console.log(response, 'response');
            var data = response.data;
            const children = [];
            response.data.forEach((element, index) => {
              children.push({
                code: element.orgId,
                name: element.orgNm,
              });
            });
            treeNode.dataRef.children = children;
            this.treeData = [...this.treeData];
          });
          resolve();
        }, 500);
      });
    },
  },
};
</script>

<style lang='less' scoped>

</style>
