<template>
    <ve-select
            ref="veSelect"
            :data="getData"
            :model.sync="value"
            :noplease="propertys.noplease"
            style="width:100%"
            :clearable="propertys.clearable"
            :placeholder="propertys.placeholder"
            :change="onChange"
            @focus="$emit('focus')"
    ></ve-select>
</template>

<script>
    /**
     * getData(){
     *     return [id:,name:]
     *     return {id:name}
     * }
     * <item @getData="getData" @change="change" :model.sync="model"></item>
     *  parent.$refs.item.init(model);
     */
    export default {
    name: "edit_select",
    data() {
        return {
            value:"",
            propertys:{},
        };
    },
    props: {
        model:{
            type:String,
            default:""
        },
        isShow:{
            type:Boolean,
            default:false
        },
        getData:{
            type:Function,
        },
        property:{
            type:Object,
            default:()=>{}
        }
    },
    created() {
        MapUtil.setDftValue(this.propertys,{
            noplease:false,
            clearable:false,
            placeholder:'请选择',
            ...this.property
        });
    },
    methods: {
        init(value){
            let vm=this;
            this.value=value;
            vm.show=true;
            this.getData().then(function(d){
                if (d instanceof Array) {
                    vm.data = {list: d};
                } else {
                    vm.data = {map: d};
                }
                vm.$nextTick(function () {
                    vm.$refs['veSelect'].$refs['select'].focus();
                })
            });
        },
        /**
         * 获取应该显示的文本
         */
        getLabel(){
            let vm=this;
            if (vm.data.list) {
                for (let i = 0; i <vm.data.list.length ; i++) {
                    let item=vm.data.list[i];
                    if(item.id==this.value)return item.name;
                }
            } else {
                return vm.data.map[this.value];
            }
        },
        onChange(){
            this.$emit("update:model",this.value);
            this.$emit("change",this.value);
        },
        /**
         * 组件数据提交
         */
        submit(){
            let vm=this;
                vm.$emit("submit",{
                    sucess:()=>{
                    },
                    error:(obj)=>{//obj 错误信息
                        this.$message(obj.msg)
                    }
                });
        },
    }
};
</script>
