<template>
    <div class="oRadio controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'radio'">
        <van-field
            :name="widget.model"
            :label="widget.name || '单选框'"
            :required="fileCheck ? false : options.required || false"
            :disabled="options.disabled || false"
            clearable
            :placeholder="options.placeholder || '请选择'"
            :rules="rule"
             v-if="!preview"
        >
            <template #input>
                <van-radio-group v-model="dataModel" :direction="options.inline ? 'horizontal' : 'vertical'">
                    <van-radio :name="item.value" v-for="item in options.options" :key="item.value">{{ item.value }}</van-radio>
                </van-radio-group>
            </template>
        </van-field>
        <van-cell class="preview" :title="widget.name || '单选框'" :value="dataModel" v-else></van-cell>
    </div>
</template>
<script>

export default {
    name: 'oRadio',
    props: ['widget', 'preview', 'value', 'fileCheck'],
    data() {
        return {
            dataModel: this.widget.options?.defaultValue || null,
        }
    },
    mounted() {
        // 初始化参数
        if(this.value) {
            const widget = this.widget
            const model = widget.originModel || widget.model
            for(let v in this.value) {
                if(v == model) {
                    this.dataModel = this.value[v]
                }
            }
        }
    },
    methods: {
    },
    computed: {
        options() {
            return this.widget.options || {}
        },
        rule() {
            let rules = this.widget.rules || []
            if(rules && rules.length > 0) {
                rules.forEach(item => {
                    if(item.pattern) {
                        item.pattern = eval(item.pattern)
                    }
                    // 判断是否上传文件
                    if(this.fileCheck) {
                        item.required = false
                    }
                });
            }
            return rules
        }
    },
    watch: {
        // dataModel: {
        //     deep: true,
        //     handler(newValue) {
        //         if (newValue !== undefined && newValue !== null) {
        //         }
        //     }
        // },
    }
}
</script>

<style lang='less' scoped>
@import url('./controlCommon.less');
.oRadio {
    /deep/.van-radio  {
        padding: .03rem 0;
    }
    /deep/.van-radio--horizontal {
        margin-right: .15rem;
    }
}
</style>