const Parse = getApp().Parse;
const app = getApp();
const company = getApp().globalData.company
const dateF = require('../../../../../../utils/date')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        list: [],
        price: null,
        storeId: null,
        //入住
        show: false,
        showid:null,
        //订单完成
        show2: false,
        showid2:null,
        //接受退房
        show3: false,
        showid3:null,
        //拒绝退房
        show4: false,
        showid4:null,

    },
   async  getUserInfo(e) {
        console.log(e.currentTarget.dataset.item.objectId);
        let objectId = e.currentTarget.dataset.item.objectId
        let newOrder = new Parse.Query('RoomOrder');
        newOrder.equalTo('company', company);
        newOrder.equalTo('objectId', objectId);
        newOrder.notEqualTo('isDeleted', true);
        let order = await newOrder.first();
        console.log(order);
        order.set('status',200)
        try{
            let saveDate = await order.save();
            console.log('保存成功');
            this.setData({
                showid:null
            })
            this.getShopOrder()
        }catch (error) {
            console.error("保存数据时出现错误:", error);
        }
    },
    //确认入住
    onClose() {
        this.setData({
            show: false,
        }); // 关闭弹窗并重置标志位
    },
    ONShow(e) {
        const id =e.currentTarget.dataset.item.objectId
        this.setData({
            showid:id,
            show: true,
        }); // 设置弹窗显示
        console.log(this.data.showid);
    },
    //订单完成
    async ordercom(e){
        console.log(e.currentTarget.dataset.item.objectId);
        let objectId = e.currentTarget.dataset.item.objectId
        let newOrder = new Parse.Query('RoomOrder');
        newOrder.equalTo('company', company);
        newOrder.equalTo('objectId', objectId);
        newOrder.notEqualTo('isDeleted', true);
        let order = await newOrder.first();
        console.log(order);
        order.set('status',800)
        try{
            let saveDate = await order.save();
            console.log('保存成功');
            this.setData({
                showid2:null
            })
            this.getShopOrder()
        }catch (error) {
            console.error("保存数据时出现错误:", error);
        }
    },
    onClose2() {
        this.setData({
            show2: false,
        }); // 关闭弹窗并重置标志位
    },
    ONShow2(e) {
        const id =e.currentTarget.dataset.item.objectId
        this.setData({
            showid2:id,
            show2: true,
        }); // 设置弹窗显示
        console.log(this.data.showid);
    },
    // 接受退房
    async jieshou(e){
        console.log(e.currentTarget.dataset.item.objectId);
        let objectId = e.currentTarget.dataset.item.objectId
        let newOrder = new Parse.Query('RoomOrder');
        newOrder.equalTo('company', company);
        newOrder.equalTo('objectId', objectId);
        newOrder.notEqualTo('isDeleted', true);
        let order = await newOrder.first();
        console.log(order);
        order.set('status',103)
        try{
            let saveDate = await order.save();
            console.log('保存成功');
            this.setData({
                showid3:null
            })
            this.getShopOrder()
        }catch (error) {
            console.error("保存数据时出现错误:", error);
        }
    },
    onClose3() {
        this.setData({
            show3: false,
        }); // 关闭弹窗并重置标志位
    },
    ONShow3(e) {
        const id =e.currentTarget.dataset.item.objectId
        this.setData({
            showid3:id,
            show3: true,
        }); // 设置弹窗显示
    },
    //拒绝退房
    async jujue(e){
        console.log(e.currentTarget.dataset.item.objectId);
        let objectId = e.currentTarget.dataset.item.objectId
        let newOrder = new Parse.Query('RoomOrder');
        newOrder.equalTo('company', company);
        newOrder.equalTo('objectId', objectId);
        newOrder.notEqualTo('isDeleted', true);
        let order = await newOrder.first();
        console.log(order);
        order.set('status',104)
        try{
            let saveDate = await order.save();
            console.log('保存成功');
            this.setData({
                showid4:null
            })
            this.getShopOrder()
        }catch (error) {
            console.error("保存数据时出现错误:", error);
        }
    },
    onClose4() {
        this.setData({
            show4: false,
        }); // 关闭弹窗并重置标志位
    },
    ONShow4(e) {
        const id =e.currentTarget.dataset.item.objectId
        this.setData({
            showid4:id,
            show4: true,
        }); // 设置弹窗显示
    },


    onChange(event) {
        let active = event.detail.name
        this.setData({
            active: active
        })
        console.log(active);
        this.getShopOrder()
    },
    storepackage(e) {
        let price = e.currentTarget.dataset.item.price
        console.log(price);
        this.setData({
            show: true,
            price: price
        })
    },
    async acceptResult(e) {
        let {
            order,
        } = this.data
        let that = this
        let {
            params,
            no
        } = e.detail;
        that.setData({
            show: false
        })
        try {
            if (params == "ok") {
                order.set("isPay", true)
                await order.save()
                this.setData({
                    isPay: true
                })
                wx.showToast({
                    title: '报名成功',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
            } else {
                wx.showToast({
                    title: '支付失败，取消订单',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
            }
        } catch (error) {
            console.log(error)
            wx.showToast({
                title: "支付失败",
                icon: "error",
                duration: 1500,
            });
            wx.hideLoading()
        }
    },

    //删除

    // evaluation(e) {
    //     let id = e.currentTarget.dataset.item.objectId
    //     console.log(id);
    //     wx.navigateTo({
    //         url: '/nova-tourism/pages/my/homestay-order/evaluation/index?id=' + id
    //     });

    // },
    // orderpay(e) {
    //     let id = e.currentTarget.dataset.item.objectId
    //     console.log(id);
    //     wx.navigateTo({
    //         url: '/nova-tourism/pages/my/homestay-order/homestay-details/index?id=' + id
    //     });

    // },
    phone(e) {
        let phone = e.currentTarget.dataset.item.shopStore.mobile

        console.log(phone);
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    async getShopOrder() {
        let ShopOrder = new Parse.Query('RoomOrder');
        ShopOrder.equalTo('company', company);
        ShopOrder.include('room');
        ShopOrder.equalTo('shopStore', this.data.storeId);

        if (this.data.active == 1) {
            ShopOrder.equalTo('status', 100);
        }
        if (this.data.active == 2) {
            ShopOrder.equalTo('status', 200);
        }
        if (this.data.active == 3) {
            console.log(this.data.active);
            ShopOrder.equalTo('status', 400);
        }

        ShopOrder.include('room');
        ShopOrder.include('user');
        ShopOrder.exists('status');

        ShopOrder.find().then(res => {
            let list = [];
            res.forEach(item => {
                let activitys = item.toJSON();
                activitys.orderTime = dateF.formatTime("mm-dd", activitys.startTime.iso);
                activitys.Time = dateF.formatTime("mm-dd", activitys.endTime.iso);
                activitys.a = dateF.formatTime("YYYY-mm-dd", activitys.startTime.iso); // 只保留日期部分
                activitys.e = dateF.formatTime("YYYY-mm-dd", activitys.endTime.iso); // 只保留日期部分

                // 计算天数，只考虑日期部分
                activitys.day = parseInt((new Date(activitys.e).getTime() - new Date(activitys.a).getTime()) / (1000 * 60 * 60 * 24));
                list.push(activitys);
            });

            this.setData({
                list
            });
            console.log(this.data.list);
        });
    },

    orderdeils(e) {
        let id = e.currentTarget.dataset.item.objectId
        console.log(id);
        wx.navigateTo({
            url: '../order2-list/order-deils/index?id=' + id,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let storeId = options.storeId;
        console.log(storeId);
        this.setData({
            storeId: storeId
        })
        this.getShopOrder()

        // this.getDayAll()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})