<template>
    <el-select v-model="valueSync" :placeholder="placeholder" clearable :multiple="multiple">
        <el-option v-for="dict in getList(type)"
                    :key="dict.value" :label="dict.label" :value="dict.value"/>
    </el-select>
</template>
<script>
const selfDefinedDict = {
    _yesno: [
        {label: '是', value: '1'},
        {label: '否', value: '0'}
    ]
}
export default {
    props:{
        placeholder: {
            type: String,
            default: '请选择'
        },
        type: String,
        value: [String, Number, Array],
        multiple: Boolean,
        forceString: {
            type: Boolean,
            default: true
        }
    },
    data(){
        return {
            valueSync: null
        }
    },
    watch:{
        valueSync(){
            this.$emit('input', this.forceString ? String(this.valueSync) : this.valueSync)
        },
        value(val){
            if(!val || val === null){
                return
            }
            if(this.forceString && this.multiple){
                this.valueSync = val.split(',')
            }else this.valueSync = this.forceString ? String(val) : val
        }
    },
    methods:{
        getList(){
            return selfDefinedDict[this.type] || this.getDictDatas(this.type)
        }
    }
}
</script>