const Parse = getApp().Parse;
const company = getApp().globalData.company
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
const dateF = require('../../../../utils/date');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        list: [],
        totalPrice: 0,
        stickytop: navigationBarHeight,
    },
    async acceptResult(e) {
        let { activeOrder } = this.data
        let that = this
        let {
            params,
            no
        } = e.detail;
        that.setData({
            showPayment: false
        })
        try {
            if (params == "ok") {
                // activeOrder.set("status", 200)
                // activeOrder.set("isPay", true)
                await that.getorder()
                    // await activeOrder.save()
                wx.showToast({
                    title: '支付成功',
                    icon: 'none',
                    duration: 1500,
                    mask: false,
                });
                wx.navigateBack({
                    delta: 1,
                })
            } else {
                wx.showToast({
                    title: '支付失败，取消订单',
                    icon: 'none',
                    duration: 1500,
                    mask: false,
                });
            }
        } catch (error) {
            console.log(error)
            wx.showToast({
                title: "支付失败",
                icon: "error",
                duration: 1500,
            });
            wx.hideLoading()
        }
    },
    async getorder() {
        let id = this.data.id
        console.log(id);
        let shopOrder
        let ShopOrder = new Parse.Query("FoodOrder")
        shopOrder = await ShopOrder.get(id)
        shopOrder.set("state", "400")
        shopOrder.save().then(res => {
            console.log(res)
        })
    },
    onChange(event) {
        let active = event.detail.name
        this.setData({
            active: active
        })
        console.log(active);
        this.getFoodOrder()
    },
    storepackage(e) {
        let userLogin = wx.getStorageSync("userLogin");
        if (userLogin == "") {
            login.loginNow();
            return;
        }

        let totalPrice = e.currentTarget.dataset.item.totalPrice
        let id = e.currentTarget.dataset.item.objectId
        console.log(totalPrice);
        this.setData({
            show: true,
            totalPrice: totalPrice,
            id: id
        })
    },
    //删除
    delOrder(e) {
        console.log(e);
        let {
            index
        } = e.currentTarget.dataset
        let {
            list
        } = this.data
        let _this = this
        wx.showModal({
            title: '',
            content: '你确定删除该订单吗？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: async(result) => {
                if (result.confirm) {
                    await this.upDel(list[index].objectId)
                    list.splice(index, 1)
                    _this.setData({
                        list
                    })
                    wx.showToast({
                        title: '已取消',
                        icon: 'none',
                        image: '',
                        duration: 1500,
                    });
                }
            },
            fail: () => {},
            complete: () => {}
        });
    },
    async upDel(id) {
        let FoodOrder = new Parse.Query('FoodOrder')
        let order = await FoodOrder.get(id)
        if (order && order.id) {
            console.log(order);
            await order.destroy()
        }
    },

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

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

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

    },

    async getFoodOrder() {
        let FoodOrder = new Parse.Query('FoodOrder')
        FoodOrder.equalTo('company', company)
        FoodOrder.equalTo('user', Parse.User.current().id)
        if (this.data.active == 1) {
            FoodOrder.equalTo('state', "100")
        }
        // if (this.data.active == 2) {
        //     FoodOrder.equalTo('status', "200")
        // }
        if (this.data.active == 2) {
            console.log(this.data.active)
            FoodOrder.equalTo('state', "400")
        }
        FoodOrder.include('foods')
        FoodOrder.include('table')
        let foodOrder = await FoodOrder.find()
        let listJSON = []
        if (foodOrder && foodOrder.length > 0) {
            foodOrder.forEach(c => {
                let order = c.toJSON()
                order.Time = dateF.formatTime('YYYY-mm-dd HH:MM:SS', order.createdAt);
                listJSON.push(order)
            })
        }
        this.setData({
            list: listJSON
        })
        console.log(this.data.list);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getFoodOrder()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})