<template>
    <!-- 文字单选 -->
    <div class="weui-cell">
        <div class="label">
            <slot></slot>
        </div>
        <div class="radio">
            <div
                v-for="(item, index) in listItem.option.list"
                :key="index"
                class="radio-item"
                @click="change(checked = item.value)"
            >
                <el-radio :value="checked" :label="item.value">{{ item.name }}</el-radio>
                <div class="describe" v-if="item.describe">{{ item.describe }}</div>
            </div>
        </div>
    </div>
</template>

<script>
import common from '@/common/formRender/mixin/common.js'
export default {
    name: 'FormRadioText',
    mixins: [common],
    components: {
        ElRadio: () => import('./components/ElRadio')
    },
    props: {
        subform: { type: Object, default: () => ({}) },
        listItem: { type: Object, default: () => ({}) },
        formItem: { type: Object, default: () => ({}) }
    },
    data () {
        return {
            checked: this.listItem.option.checked
        }
    },
    created () {
        this.verifyInit()
    },
    methods: {
        // 切换选项同步 value，并改变关联项的隐藏
        change (checked) {
            this.formItem.value = this.listItem.option.list.find(item => item.value === checked).name
            this.radioHide(this.subform, this.listItem.option.list, checked) // mixin
            this.clearValidate()
        },
        // 校验初始化
        verifyInit () {
            const rules = []
            const { required } = this.listItem.verify
            if (required) rules.push(this.required(this.listItem.title))
            if (rules.length) this.validator = new this.Schema({ value: rules })
        },
        // 校验
        verify () {
            return this.validate({ value: this.formItem.value })
        }
    }
}
</script>

<style lang="less" scoped>
</style>
