const app = getApp()

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

import {
    meetingRoomBookedRecordRq,
    selectFreeMeetingRoomRq
} from "../../../../api/meeting/meetingRoom.js"

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

Page({

    /**
     * 页面的初始数据
     */
    data: {
        meetingRoomId: 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;
        },
    },

    // 选择-开始时间
    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 meetingRoomId = _this.data.meetingRoomId;
        let startTime = _this.data.startTime;
        let endTime = _this.data.endTime;
        let paramUrl = "?meetingRoomId=" + meetingRoomId + "&startTime=" + selfFormatTimeYMDHMS(startTime) + "&endTime=" + selfFormatTimeYMDHMS(endTime);

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

        // 当前会议室是否可以预约
        selectFreeMeetingRoomRq({
            "roomContentId": meetingRoomId,
            "startTime": selfFormatTimeYMDHMS(startTime),
            "endDate": selfFormatTimeYMDHMS(endTime)
        }).then(res => {
            console.log('selectFreeMeetingRoomRq', res);
            // 可以预约
            if (!res.count) {
                wx.navigateTo({
                    url: "/pages/meeting/meetingRoom/meetingOrder/meetingOrder" + paramUrl,
                })
                _this.setData({
                    showTime: false
                })
            } else { // 不能预约
                Notify({
                    type: 'danger',
                    message: res.msg,
                    duration: 1000,
                    selector: '#notify',
                });
            }
        })
    },

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

    // 初始化时间
    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
        })
    },


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

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log('onShow', 111);
        let _this = this;
        meetingRoomBookedRecordRq(this.data.meetingRoomId).then(res => {
            console.log('meetingRoomBookedRecordRq', res);
            let dataList = res.data;
            dataList.map(item => {
                item.nowDate = item.nowDate.substring(0, 10);
                item.reservations = item.reservations.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() {

    }
})