
/*
 * 本时生活
 */

const app = getApp()


Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusHeight : app.globalData.statusBarHeight,
        orderId      : '',  //订单id
        order        : '',   //订单详情
        orderType    : ''   //订单来源
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        // 获取商品活动订单详情
        this.setData({
            orderType : options.orderType,
            orderId   : options.id
        })
        this.orderInfo();
    },

    /**
     * 商品活动订单详情
     */
    orderInfo() {
        let url = ''
        if(this.data.orderType == 'welfare' || this.data.orderType == 'welfareGoods') url =  wx.$api.exchange.welfaresShow
        if(this.data.orderType == 'rights' || this.data.orderType == 'rightsCoupons') url = wx.$api.exchange.show
        url(this.data.orderId).then(res=>{
            this.setData({
                order : res.data
            })
        })
    },

    /**
     * 取消订单
     */
    orderDelete(e) {
        let orderId = e.currentTarget.dataset.id
        wx.showModal({
            title   : '订单取消',
            content : '确认取消吗？',
            success : res=> {
                if (res.confirm) {
                    let url = ''
                    if(this.data.orderType == 'welfare' || this.data.orderType == 'welfareGoods') url = wx.$api.exchange.welfaresCancel
                    if(this.data.orderType == 'rights' || this.data.orderType == 'rightsCoupons') url = wx.$api.exchange.cancel

                    url(orderId).then(res=>{
                        if(this.data.orderType == 'rightsCoupons') {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=rightsCoupons'
                            })
                        } else if(this.data.orderType == 'rights') {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=rights'
                            })
                        } else if(this.data.orderType == 'welfare') {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=welfare'
                            })
                        } else {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=welfareGoods'
                            })
                        }

                        wx.showToast({
                            title: res.data,
                            icon : 'none'
                        })
                    }) 
                } else if (res.cancel) {
                    wx.showToast({
                        title   : '取消',
                        icon    : 'loading',
                        duration: 1000
                    })
                }
            }
        }) 
    },

    /**
     * 支付提交
     */
    orderPay(e) {
        let orderid = e.currentTarget.dataset.id
        wx.$api.exchange.payments(orderid).then(res=>{
            wx.$api.index.wechat(res.data.trade_no).then(res=>{
                let payInfo = JSON.parse(res.data)
                wx.requestPayment({
                    timeStamp: payInfo.timeStamp,
                    nonceStr : payInfo.nonceStr,
                    package  : payInfo.package,
                    paySign  : payInfo.paySign,
                    signType : payInfo.signType,
                    success  : res=>{
                        if(res.errMsg == "requestPayment:ok"){
                            wx.showToast({
                                title: '支付成功',
                                icon : 'success'
                            })
                            setTimeout(()=>{
                                wx.reLaunch({
                                    url: '/pages/coupon/coupon?type=couponPublic'
                                })
                            },2000)
                        }
                    },
                    fail  : res=>{
                        wx.reLaunch({
                            url: '/pages/order/order?stateType=unpay'
                        })
                    }
                })
            })
        })
        
    },

    /**
     * 返回上一页
     */
    orderRun() {
        wx.navigateBack({
            delta: 1
        })
    },

    /**
     * 复制快递单号
     */
    copyText (e) {
        let text = e.currentTarget.dataset.text
        wx.setClipboardData({
            data    : text,
            success : res=> {
                wx.getClipboardData({
                    success: res => {
                        wx.showToast({
                            title: '复制成功'
                        })
                    }
                })
            }
        })
    }
})