// commpents/timePicker/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        label: { type: String },
        model: { type: String },
        value: {
            type: String,
            observer: function (val) {
                this.initData()
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        minDate: new Date().getTime(),
        currentDate: new Date().getTime(),
        text: '',
        show: false,
    },
    /**
     * 组件的方法列表
     */
    methods: {
        initData() {
            const val = this.properties.value
            if (!val) return;
            this.setData({ text: val })
        },
        toConfirm(event) {
            const data = event.detail;
            const datetimestr = this.getDateString(data)
            this.setData({
                currentDate: event.detail,
                text: datetimestr
            });
            this.triggerEvent('selected', { value: datetimestr, model: this.properties.model })
            this.toClose();
        },
        getDateString(datetime) {
            datetime = new Date(datetime);
            const year = datetime.getFullYear();
            let month = datetime.getMonth() + 1;
            if (month < 10) month = `0${month}`
            let date = datetime.getDate();
            if (date < 10) date = `0${date}`
            let hours = datetime.getHours();
            if (hours < 10) hours = `0${hours}`
            let min = datetime.getMinutes();
            if (min < 10) min = `0${min}`
            return `${year}-${month}-${date} ${hours}:${min}`
        },
        toOpen() {
            this.setData({
                show: true
            })
        },
        toClose() {
            this.setData({
                show: false,
            })
        },

    }
})
