<template>
    <div class="app-org-picker">
        <dropdown @on-click="orgSelect" @on-visible-change="visibleChange">
            <Input :value="getSelectedOrgName" placeholder="请选择部门" readonly :icon="iconClass" />
            <dropdown-menu slot="list">
                <dropdown-item
                    :class="{ 'is-active': Object.is(item.srforgsectorid, getSelectedOrgName) }"
                    :name="item.srforgsectorid"
                    v-for="(item, index) in selectedOrgArray"
                    :key="index"
                >
                    {{ item.srforgsectorname }}
                </dropdown-item>
                <div class="app-org-picker__empty" v-show="!selectedOrgArray.length">暂无数据</div>
            </dropdown-menu>
        </dropdown>
    </div>
</template>
<script lang='ts'>
import { Vue, Component, Inject, Prop } from 'vue-property-decorator';

@Component({})
export default class AppAuthOrgPicker extends Vue {

    /**
     * 输入值
     *
     * @type {*}
     * @memberof AppAuthOrgPicker
     */
    @Prop() public value!: string;

    /**
     * 默认数据来源模式
     *
     * @type {string}
     * @memberof AppAuthOrgPicker
     *
     */
    public selectMode: 'REMOTE' | 'LOCAL' = 'LOCAL';

    /**
     * 选中组织部门id
     *
     * @type {string}
     * @memberof AppAuthOrgPicker
     */
    public selectedOrgId: string = '';

    /**
     * 图标名称
     *
     * @type {boolean}
     * @memberof AppAuthOrgPicker
     */
    public iconClass: string = 'ios-arrow-down';

    /**
     * 选中组织部门名称
     *
     * @type {string}
     * @memberof AppAuthOrgPicker
     */
    get getSelectedOrgName() {
        if (this.value) {
            const selectedValue = this.selectedOrgArray.find((item: any) => {
                return item.srforgsectorid == this.value;
            });
            if (selectedValue) {
                return selectedValue.srforgsectorname;
            }
        }
    }

    /**
     * 组织部门名称数组
     *
     * @type {Array<any>}
     * @memberof AppAuthOrgPicker
     */
    public selectedOrgArray: Array<any> = [];

    /**
     * 组件初始化数据，vue生命周期
     *
     * @memberof AppAuthOrgPicker
     */
    public created() {
        this.getOrgData();
    }

    /**
     * 选择组织部门回调
     *
     * @memberof AppAuthOrgPicker
     */
    public orgSelect(data: string) {
        if (Object.is(data, this.selectedOrgId)) {
            return;
        }
        let item: any = this.selectedOrgArray.find((_item: any) => Object.is(_item.srforgsectorid, data));
        this.$emit('valueChange', { value: item.srforgsectorid });
    }

    /**
     * 下拉框打开或收起回调
     *
     * @memberof AppAuthOrgPicker
     */
    visibleChange(data: boolean) {
        this.iconClass = data ? 'ios-arrow-up' : 'ios-arrow-down';
    }

    /**
     * 获取数据
     *
     * @memberof AppAuthOrgPicker
     */
    public async getOrgData() {
        // todo
        console.log('获取数据暂未实现');
    }
}
</script>
<style lang="less">
@import './app-login-org.less';
</style>