<template>
    <div>
        <!--数据字典单选框-->
        <el-radio-group v-model="radioVal"
                        :size="size" v-if="!showText"
                        :disabled="disabled" @change="change"
                        :style="{'width':width,'height':height}"
                        :attrs="{'style':controlCss}">
            <el-radio v-for="item in radioArr" :key="item.id" :label="item.id">
                {{item.dataName}}
            </el-radio>
        </el-radio-group>
        <div v-if="showText" style="min-height:36px;">
            {{radioObj[radioVal] || ""}}
        </div>
    </div>
</template>

<script>
    export default {
        name: "pl-data-radio-box",
        props:{
            "value":{type:String},
            "field":{type:String},
            "size":{type:String,default:"small"},
            "width":{type:String,default:"100%"},
            "height":{type:String,default:""},
            "controlCss":{type:String,default:""},
            "disabled":{type:Boolean,default:false},
            "typeId":{type:String,required:true},
            "parentId":{type:String,default:""},
            showText:{type:Boolean,default:false},
            rememberSysDataObj:{type:Function}
        },
        data(){
            return {
                radioVal: '',
                radioArr:[],
                radioObj:{},
            }
        },
        watch:{
            value(){
                this.radioVal = this.value;
            },
            typeId(){
                this.init();
            },
            parentId(){
                this.init();
            }
        },
        created(){
            this.init();
        },
        methods:{
            change(val){
                this.$emit("input", val);
            },
            init(){
                if(!this.typeId){
                    return;
                }
                this.radioVal = this.value;
                let proimise = this.DATA_GLOBAL.getDatas(this.typeId,this.parentId);
                if(this.showText){
                    proimise = this.DATA_GLOBAL.getDatasContainOutDate(this.typeId,this.parentId);
                }
                proimise.then(datas => {
                    this.radioArr = datas || [];
                    this.radioArr.forEach(item => {
                        this.$set(this.radioObj,item.id,item.dataName);
                    });
                    this.rememberSysDataObj && this.rememberSysDataObj(this.field,this.radioObj);
                })
            }
        }
    }
</script>

<style scoped>

</style>