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

export default {
    name: 'oCheckbox',
    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');
.oCheckbox {
    /deep/.van-checkbox  {
        padding: .03rem 0;
    }
    /deep/.van-checkbox--horizontal {
        margin-right: .15rem;
    }
}
</style>