<template>
    <div class="oTextarea controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'textarea'">
        <van-field
            :name="widget.model"
            v-model="dataModel"
            :label="widget.name || '文本框'"
            :required="fileCheck ? false : options.required || false"
            :disabled="options.disabled || false"
            :show-error="false"
            :scroll-to-error="true"
            :placeholder="options.placeholder || '请输入'"
            type="textarea"
            clearable
            :autosize=" { maxHeight: 120, minHeight: 60 }"
            :rules="rule"
            v-if="!preview"
        />
        <van-cell class="preview" :title="widget.name || '文本框'" :value="dataModel" v-else></van-cell>
    </div>
</template>
<script>

export default {
    name: 'oTextarea',
    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');
</style>