<template>
    <div class="oInput controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'input'">
        <van-field
            :name="widget.model"
            v-model="dataModel"
            :label="widget.name || '文本框'"
            :type="inputType"
            :required="fileCheck ? false : options.required || false"
            :disabled="options.disabled || false"
            clearable
            :placeholder="options.placeholder || '请输入'"
            autocomplete="off"
            :rules="rule"
            v-if="!preview"
        />
        <van-cell class="preview" :title="widget.name || '文本框'" :value="dataModel" v-else></van-cell>
    </div>
</template>
<script>

export default {
    name: 'oInput',
    props: ['widget', 'preview', 'value', 'fileCheck'],
    data() {
        return {
            dataModel: this.widget.options?.defaultValue || null,
            inputType: null
        }
    },
    mounted() {
        // 初始化参数
        let type = null
        if(this.options.dataType === 'integer') {
            type = 'digit'
        } else if(this.options.dataType === 'float' || this.options.dataType.number) {
            type = 'number'
        }
        this.inputType = type

        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');
</style>