<!--
下拉树组件
@Author: linqian
@Date: 2021-05-20 15：34
-->
<template>
    <div>
        <dg-tree-drop
            ref="tree"
            v-model="value"
            :props="props"
            :tree-props="treeProps"
            clearable
            filterable
            visible-type="branch"
            radioType= "all"
            @change="handleChange"
        >
        </dg-tree-drop>
    </div>
</template>

<script>
import * as commonApi from "@/api/common";
export default {
    props: {
      nodeKey: {
          type: String,
          default:"code",
      },
        apiName: {
            type: String,
            default: "getTree"
        }
    },
    components: {},
    data() {
        return {
            value: "",
            props: {
                value: this.nodeKey,
                label: "name",
                children: "children",
                isLeaf(data) {
                    return data.isParent !== true;
                }
            },
            treeProps: {
                nodeKey: this.nodeKey,
                lazy: true,
                load: this.loadNode
            }
        };
    },
    computed: {},
    methods: {
        loadNode(node, resolve) {
            this.getTree(node.level === 0 ? void 0 : node.data.id, resolve, node.level);
        },
        // 组织机构
        getTree(id, resolve) {
            const { params, apiName } = this;
            commonApi[apiName]({ id, ...params })
                .then((res) => {
                    // 就为了存根节点, 用于刷新
                    return resolve(res || []);
                })
                .catch(() => resolve([]));
        },
         handleChange(val) {
            this.$emit("submitTreeValue", val)
         }
    },
    created() {},
    mounted() {}
};
</script>

<style lang='scss'>
</style>