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

    /**
     * 页面的初始数据
     */
    data: {
        orderId: '',
        address: null,
        goods: null,
        shipper: null,
        trackingNumber: null,
        order: null,
        companyInfo: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let orderId = options.id
        console.log(orderId);
        this.setData({
            orderId: orderId
        })
        this.queryOrder(orderId)

    },
    async queryOrder(id) {
        let ShopOrder = new Parse.Query('Order')
        ShopOrder.include('goods')
        ShopOrder.include('address')
        let shopOrder = await ShopOrder.get(id)
        if (shopOrder && shopOrder.id) {
            let order = shopOrder.toJSON()
            order.orderTime = dateF.formatTime("YYYY-mm-dd HH:MM:SS", order.createdAt)
            this.setData({
                order: order,
                goods: order.goods,
                address: order.address,
                shipper: order.shipper,
                trackingNumber: order.trackingNumber
            })
            console.log(this.data.order);
        }

    },
    async getCompany() {
        let Company = new Parse.Query('Company')
        let companyInfo = await Company.get(company)
        if (companyInfo) {
            this.setData({
                companyInfo: companyInfo.toJSON()
            })
        }
        console.log(this.data.companyInfo);
    },


    //更换地址
    toAddress() {
        let { order } = this.data
        if (order.status != 200) {
            wx.showToast({
                title: '已发货物件不支持更换地址',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: false,
            });
            return
        }
        wx.navigateTo({
            url: "/common-page/pages/address/index?type=1"
        })
    },

    async refund() {
        let that = this
        wx.showModal({
            title: '',
            content: '你确定要申请退款吗？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (result) => {
                if (result.confirm) {
                    that.upRefund()
                }
            },
            fail: () => {},
            complete: () => {}
        });
    },

    async upRefund() {
        let { order } = this.data
        let ShopOrder = new Parse.Query("Order")
        let res = await ShopOrder.get(order.objectId)
        if (res && res.id) {
            res.set("status", '500')
            await res.save()
            wx.showToast({
                title: '已申请退款',
                icon: 'none',
                image: '',
                duration: 1500,
                mask: false,
            });
            order.status = '500'
            this.setData({
                order
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.getCompany()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})