const Parse = getApp().Parse
const dateF = require('../../../../../utils/date')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        foodorder: null,
        time: null,
        show: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            activeColor:getApp().globalData.activeColor || '#229293'
        })
        let id = options.id
        console.log(id);
        this.setData({ id: id })
        console.log(this.data.id);
        this.foodorder()
    },
    async foodorder() {
        let FoodOrder = new Parse.Query("FoodOrder")
        FoodOrder.include("store")
        FoodOrder.include("table")
        FoodOrder.include("foods")
        let foodorder = await FoodOrder.get(this.data.id)
        console.log(foodorder.toJSON());
        let time = dateF.formatTime("YYYY-mm-dd HH:MM:SS", foodorder.toJSON().createdAt)
        this.setData({
            foodorder: foodorder.toJSON(),
            time: time
        })
    },
    submit() {
        let userLogin = wx.getStorageSync("userLogin");
        if (userLogin == "") {
            login.loginNow();
            return;
        }
        this.setData({
            show: true
        })
    },
    async acceptResult(e) {
        let { activeOrder } = this.data
        let that = this
        let {
            params,
            no
        } = e.detail;
        that.setData({
            show: false
        })
        try {
            if (params == "ok") {
                // activeOrder.set("status", 200)
                // activeOrder.set("isPay", true)
                await that.getorder()
                // await activeOrder.save()
                wx.showToast({
                    title: '购买成功',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: false,
                });
                wx.navigateBack({
                    delta: 1,
                })
            } 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()
        }
    },
    async getorder() {
        let FoodOrder = new Parse.Query("FoodOrder")
        let foodorder = await FoodOrder.get(this.data.id)
        foodorder.set("state", "200")
        foodorder.save().then(res => {
            console.log(res)
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})