<template>
    <div class="oDate controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'date'">
        <van-field
            :name="widget.model"
            v-model="dataModel"
            :label="widget.name || '文本框'"
            :required="fileCheck ? false : options.required || false"
            :disabled="options.disabled || false"
            readonly
            clearable
            :placeholder="options.placeholder || '请输入'"
            :clickable="true"
            :rules="rule"
            @click="onClick"
            v-if="!preview"
        >
            <template #right-icon>
                <i class="van-icon van-icon-arrow van-cell__right-icon"></i>
            </template>
        </van-field>

        <van-cell class="preview" :title="widget.name || '文本框'" :value="dataModel" v-else></van-cell>

        <van-popup v-model="showDate" position="bottom">
            <van-datetime-picker
                v-model="currentDate"
                :type="dateType"
                :formatter="formatter"
                @confirm="onConfirm"
                @cancel="onCancel"
            />
        </van-popup>
    </div>
</template>
<script>
import dayjs from 'dayjs'
export default {
    name: 'oDate',
    props: ['widget', 'preview', 'value', 'fileCheck'],
    data() {
        return {
            dataModel: this.widget.options?.defaultValue || null,
            showDate: false,
            currentDate: new Date(),
            dateType: 'date'
        }
    },
    mounted() {
        // 初始化参数
        if(this.options?.type == 'dates' || this.options?.type == 'daterange') {
            this.dateType = 'date'
        } else if(this.options?.type == 'datetimerange') {
            this.dateType = 'datetime'
        } else {
            this.dateType = this.options.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: {
        onClick() {
            this.showDate = true
        },
        onConfirm(val) {
            this.dataModel = dayjs(val).format('YYYY-MM-DD')
            this.showDate = false
        },
        onCancel() {
            this.showDate = false
        },
        formatter(type, val) {
            if (type === 'year') {
                return val + '年';
            }
            if (type === 'month') {
                return val + '月';
            }
            if (type === 'day') {
                return val + '日';
            }
            if(type == 'hour') {
                return val + '时'
            }
            if(type == 'minute') {
                return val + '分'
            }
            return val;
        },
    },
    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>