<template>
    <!-- 图片单选 -->
    <div class="weui-cell">
        <div class="label">
            <slot></slot>
        </div>
        <option-img
            radio
            v-model="checked"
            :option="listItem.option"
            @change="change"
        ></option-img>
    </div>
</template>

<script>
import common from '@/common/formRender/mixin/common.js'
export default {
    name: 'FormRadioImg',
    mixins: [common],
    components: {
        OptionImg: () => import('./components/OptionImg')
    },
    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) {
            const obj = this.listItem.option.list.find(item => item.value === checked)
            this.formItem.value = { value: obj.name, img: obj.img }
            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>
