<template>
    <div>
        <template v-if="opts.type === 'org-tree'">
            <org-tree :options.sync="opts" :orgValue.sync="orgValue"></org-tree>
        </template>
        <template v-if="opts.type === 'org-select'">
            <org-select :options.sync="opts" :orgValue.sync="orgValue"></org-select>
        </template>
        <template v-if="opts.type === 'org-pick'">
            <org-pick :options.sync="opts" :orgValue.sync="orgValue" :param="param" :afterConfirm="afterConfirm"></org-pick>
        </template>
    </div>
</template>

<script>
import {handleDefaultParams} from 'ap-util/util/ObjectUtil'
import OrgPick from './OrgPick'
import OrgTree from './OrgTree'
import OrgSelect from './OrgSelect'

export default {
    name: "ApOrg",
    components: {
        OrgPick,
        OrgTree,
        OrgSelect
    },
    model: {
        prop: 'modelValue',
        event: 'changeModelValue'
    },
    props: {
        options: {
            type: Object,
            required: true
        },
        modelValue: {
            required: false
        },
        afterConfirm: {
            type: Function,
            required: false
        },
        param: {
            type: Object,
            required: false
        }
    },
    data() {
        return {
            opts: {},
            orgValue: null,
        }
    },
    watch: {
        opts: {
            handler(value) {
                this.$emit('update:options', this.opts);
            },
            deep: true
        },
        orgValue(value) {
            this.$emit('changeModelValue', this.orgValue);
        },
        modelValue(value) {
            this.orgValue = value
        }
    },
    created() {
        // 初始化组件前修改默认参数
        this.opts = handleDefaultParams(this.options, {
            type: 'org-pick',
            multiSelect: false,
            fullName: false,
            url: "",
            params: {},
            confirm() {
                return true
            },
            clear() {
                return true
            },
        });
        this.orgValue = this.modelValue;
    },
    mounted: function () {
    },
    methods: {}
}
</script>

<style scoped>
</style>
