// nova-tourism/pages/my/my-order/order-detail/index.js
let Parse = getApp().Parse;
const company = getApp().globalData.company
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        screenHeight: 0,
        customHeight: 0,
        bottomNavHeight: 0,
        contentHeight: 0,

        objectId:'',
        value:'',
        roomList:{},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const {
            objectId,
            value
        } = options;
        const Value = decodeURIComponent(value);
        this.setData({
            objectId,
            value:Value
        })
        console.log(this.data.value,this.data.objectId);

        const systemInfo = wx.getSystemInfoSync();
        const statusBarHeight = systemInfo.statusBarHeight || 0;
        const screenHeight = systemInfo.screenHeight || 0;
        const custom = wx.getMenuButtonBoundingClientRect();
        const customHeight = custom.height + 10 + 2 || 0;
        const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;

        const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
        this.setData({
            statusBarHeight,
            screenHeight,
            customHeight,
            bottomNavHeight,
            contentHeight
        });
        if(this.data.value=='民宿'){
            this.getRoomOrder()
        }

    },
    async getRoomOrder() {
        let Order = new Parse.Query('RoomOrder');
        Order.equalTo('company', company);
        Order.equalTo('objectId', this.data.objectId);
        Order.include('room');
        Order.equalTo('user', Parse.User.current().id);
        Order.include('shopStore');
        let room = await Order.find();
        let roomList = room.map(async item => {
            let roomItme = item.toJSON();
            roomItme.tiem = await this.formatDate2(roomItme.startTime.iso, roomItme.endTime.iso)
            roomItme.startTime = await this.formatDate(roomItme.startTime.iso)
            roomItme.endTime = await this.formatDate(roomItme.endTime.iso)
            return roomItme
        });
        let roomList2 = await Promise.all(roomList);
        this.setData({
            roomList: roomList2[0]
        })
        console.log(this.data.roomList);
    },
    formatDate2(date1, date2) {
        date1 = new Date(date1);
        date2 = new Date(date2);

        // 获取年份、月份和日期
        const year1 = date1.getFullYear();
        const month1 = date1.getMonth() + 1; // 月份从0开始，所以要加1
        const day1 = date1.getDate();

        const year2 = date2.getFullYear();
        const month2 = date2.getMonth() + 1; // 月份从0开始，所以要加1
        const day2 = date2.getDate();

        // 返回格式化的字符串
        return `${year1}年${month1}月${day1}日入住 - ${year2}年${month2}月${day2}日离店`;
    },
     formatDate(isoString) {
        const date = new Date(isoString);
        const year = date.getUTCFullYear();
        const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // 月份从0开始
        const day = String(date.getUTCDate()).padStart(2, '0');
        const hours = String(date.getUTCHours()).padStart(2, '0');
        const minutes = String(date.getUTCMinutes()).padStart(2, '0');
        const seconds = String(date.getUTCSeconds()).padStart(2, '0');
    
        return `${year}-${month}-${day} 14:00:00`;
    },
    //复制
    copyOrderNum() {
        const orderNum = this.data.roomList.orderNum; // 获取订单编号
        if (orderNum) {
            wx.setClipboardData({
                data: orderNum,
                success: () => {
                    wx.showToast({
                        title: '复制成功',
                        icon: 'success',
                        duration: 2000
                    });
                },
                fail: (err) => {
                    console.error('复制失败:', err);
                    wx.showToast({
                        title: '复制失败',
                        icon: 'none',
                        duration: 2000
                    });
                }
            });
        } else {
            wx.showToast({
                title: '没有订单编号',
                icon: 'none',
                duration: 2000
            });
        }
    },
    //续住
    gourl() {
        const start = new Date(this.data.roomList.endTime);
        const end = new Date(start); // 复制 start 日期
        end.setDate(start.getDate() + 1); // 将 end 设置为 start 的后一天
    
        // 构造要传递的信息
        const info = {
            objectId: this.data.roomList.room.objectId,
            date_start: start.toISOString(), // 转换为 ISO 格式字符串
            date_end: end.toISOString(), // 转换为 ISO 格式字符串
        };
        console.log('info', info);
    
        // 将信息转为查询字符串
        var queryString = Object.keys(info)
            .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
            .join('&');
        console.log(queryString);
        
        wx.navigateTo({
            url: `../../../homestay/homestay-order2/index?${queryString}`
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})