<template>
    <div>
        <ap-search-tree ref="orgTree" :options.sync="opts"></ap-search-tree>
    </div>
</template>

<script>

import OrgBase from "./OrgBase";

export default {
    name: "OrgTree",
    components: {},
    extends: OrgBase,
    props: {
        options: {
            type: Object,
            required: true
        },
        orgValue: {
            type: "",
            required: false
        },
    },
    data() {
        return {
            opts: {},
            dataKey: "fdName"
        }
    },
    watch: {
        orgValue(value) {
            this.opts.searchTreeValue = this.orgValue;
            this.initText();
        }
    },
    created() {
        // 参数转换
        this.opts = {
            searchTreeValue: this.orgValue,
            searchTreeInitText: false, // 手动处理，不用默认
            searchTreeTitle: "部门选择",
            searchTreeText: "请选择部门",
            searchTreeExpandAll: false,
            searchTreeUrl: (this.options.url != undefined && this.options.url != null && this.options.url != "") ? this.options.url: "/apm/open/org/getOrgList",
            searchTreeSelectMultiple: this.options.multiSelect,
            searchTreeConfirm: this.confirm,
            searchTreeClear: this.clear,
            searchTreeParams: this.options.params
        }
        this.initText();

    },
    mounted: function () {

    },
    methods: {
        // 初始化显示的文本
        initText() {
            if (this.orgValue == null || this.orgValue == "") {
                this.opts.searchTreeText = "请选择部门";
                return;
            }
            this.getOrgName(this.orgValue, this.options.multiSelect, this.options.fullName,
                (res) => {
                    if (res.data != null && res.data != '') {
                        this.opts.searchTreeText = res.data;
                    }
                });
        },
        confirm(selectedRows) {
            let flag = this.options.confirm(selectedRows);
            if (flag) {
                this.opts.searchTreeText = this.getSelectedRowsText(selectedRows);
            }
            return flag;
        },
        clear(selectedRows) {
            let flag = this.options.clear();
            if (flag) {
                this.opts.searchTreeText = "";
                this.setOrgValue("");
            }
            return flag;
        },
        getSelectedRowsText(selectedRows) {
            if (selectedRows == null || selectedRows.length < 1) {
                return "";
            }
            let textArr = [];
            let idArr = [];
            for (let i = 0; i < selectedRows.length; i++) {
                textArr.push(selectedRows[i][this.dataKey]);
                idArr.push(selectedRows[i]["fdId"]);
            }
            let values = idArr.join();
            this.setOrgValue(values);

            let text = "";
            // 全称处理
            if (this.options.fullName) {
                text = this.$refs.orgTree.getFullName(this.dataKey);
            } else {
                text = textArr.join();
            }
            return text;
        }
    }
}
</script>

<style scoped>
</style>
