<template>
    <a-radio-group v-model:value="svalue" :disabled="disabled">
        <a-radio v-for="item in options" :key="item.itemNo" :label="item.itemNo">
            {{ item.itemName }}
        </a-radio>
    </a-radio-group>
</template>
<script>
import { getOptions } from '@/api/common'
export default {
    name: 'ProRadio',
    props: {
        value: String,
        optionData: {
            type: Array,
        },
        disabled: {
            type: Boolean,
            default: () => { false }
        }
    },
    watch: {
        value (val, oldVal) {
            if (val != oldVal) {
                this.svalue = val;
            }
        },
        //判断下拉框的值是否有改变
        svalue (val, oldVal) {
            if (val != oldVal) {
                this.$emit('input', this.svalue);
            }
        },
        optionData (val, oldVal) {
            if (val != oldVal) {
                this.options = val;
            }
        },
    },
    mounted () {
        //初始话下拉框的值
        const { colName, tableName, url } = this.$attrs
        //静态变量直接赋值
        if (this.optionData && this.optionData.length > 0) {
            this.options = this.optionData
        } else {
            let queyUrl = ''
            let params
            //通过字段和表名查tbl
            if (colName && tableName) {
                queyUrl = '/sys/dic/getOptions'
                params = { colName, tableName }
            } else if (url) {//自定义查询
                queyUrl = url
                params = {}
            }
            if (queyUrl) {
                getOptions(queyUrl, params)
                    .then((response) => {
                        if (response.success) {
                            this.options = response.data;
                        }
                    })
            }
        }
    },
    data () {
        return {
            options: [],
            svalue: this.value
        }
    }
}
</script>
<style></style>
