const app = getApp()

import Notify from '@vant/weapp/notify/notify';

import {
    appointmentRecordRq,
    selectFreeShowRoomRq
} from "../../../../api/meeting/exhibition.js"


import {
    selfFormatTimeYMDHMS,
    selfFormatTimeYMDH
} from "../../../../utils/util.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        minTime: null,
        maxTime: null,
        endMaxTime: null,
        startTime: null,
        endTime: null,
        showTime: false,
        dataList: [],
        filterTime(type, options) {
            if (type === 'minute') {
                return options.filter((option) => option == '00');
            }
            return options;
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('onLoad', options);
        // 获取传递参数
        this.setData({
            ...options
        })
        // 初始化时间
        this.initParamTime()
    },

    // 初始化时间
    initParamTime() {
        let maxTime = new Date();
        maxTime.setFullYear(maxTime.getFullYear() + 3)
        this.setData({
            maxTime: maxTime.getTime()
        })
        this.setEndMaxTime(this.data.startTime)
        // 设置最小时间
        this.setMinTime()
    },

    // 设置最小时间
    setMinTime() {
        let minTime = new Date(selfFormatTimeYMDH(new Date()) + ':00:00').getTime()
        this.setData({
            minTime,
            startTime: minTime
        })
    },

    // 指定天的最后一秒
    setEndMaxTime(time) {
        let endMaxTime = new Date(new Date(time).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;
        this.setData({
            endMaxTime: endMaxTime
        })
    },

    // 选择-开始时间
    onInputStartTime(event) {
        this.setData({
            startTime: event.detail,
        });
        this.setEndMaxTime(event.detail)
    },

    // 选择-结束时间
    onInputEndTime(event) {
        let _this = this;
        this.setData({
            endTime: event.detail,
        });
    },

    // 预约时间
    reservationTime() {
        this.setData({
            showTime: true
        })
    },

    // 确认时间
    confirmTime() {
        let _this = this;
        let id = _this.data.id;
        let startTime = _this.data.startTime;
        let endTime = _this.data.endTime;
        let paramUrl = "?id=" + id + "&startTime=" + selfFormatTimeYMDHMS(startTime) + "&endTime=" + selfFormatTimeYMDHMS(endTime);

        // 预约时间不能小于1小时
        if ((1000 * 60 * 60) > (endTime - startTime)) {
            Notify({
                type: 'danger',
                message: '预约时间不能小于1小时',
                duration: 1000,
                selector: '#notify',
            });
            return
        }

        // 查询展厅能否预约
        selectFreeShowRoomRq({
            "id": id,
            "startTime": selfFormatTimeYMDHMS(startTime),
            "endDate": selfFormatTimeYMDHMS(endTime)
        }).then(res => {
            console.log('selectFreeShowRoomRq', res);
            // 可以预约
            if (res.code == 0) {
                wx.navigateTo({
                    url: "/pages/meeting/exhibition/order/order" + paramUrl,
                })
                _this.setData({
                    showTime: false
                })
            } else { // 不能预约
                Notify({
                    type: 'danger',
                    message: res.msg,
                    duration: 1000,
                    selector: '#notify',
                });
            }
        })
    },

    // 取消时间
    cancelTime() {
        this.setData({
            showTime: false
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log('onShow');
        let _this = this;
        appointmentRecordRq({
            "showroomId": _this.data.id
        }).then(res => {
            console.log('appointmentRecordRq', res);
            let dataList = res.data;
            dataList.map(item => {
                item.nowDate = item.nowDate.substring(0, 10);
                item.showroomRecords = item.showroomRecords.map(record => {
                    record.startTime = record.startTime.substring(11)
                    record.endDate = record.endDate.substring(11)
                    return record;
                })
                return item
            })
            _this.setData({
                dataList
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})