<template>
    <div class="oRate controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'rate'">
        <van-field
            :name="widget.model"
            :label="widget.name || '文本框'"
            :required="fileCheck ? false : options.required || false"
            clearable
            :placeholder="options.placeholder || '请输入'"
            :rules="rule"
            v-if="!preview"
        >
            <template #input>
                <van-rate
                    v-model="dataModel"
                    :size="25"
                    :disabled="options.disabled || false"
                    :count="options.max"
                    color="#ffd21e"
                    void-icon="star"
                    void-color="#eee"
                />
            </template>
        </van-field>
        <van-field
            v-else
            :name="widget.model"
            :label="widget.name || '文本框'"
            class="preview"
        >
            <template #input>
                <van-rate
                    v-model="dataModel"
                    :size="25"
                    :readonly="true"
                    :count="options.max"
                    color="#ffd21e"
                    void-icon="star"
                    void-color="#eee"
                />
            </template>
        </van-field>
    </div>
</template>
<script>

export default {
    name: 'oRate',
    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');
.oRate {
    /deep/.van-rate {
        flex-wrap: wrap;
    }
}
</style>