
/*
 * 本时生活
 */

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
            })
        }).catch(err => {});
    },

    /**
     * 取消订单
     */
    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'
                        })
                    }).catch(err => {});
                } else if (res.cancel) {
                    wx.showToast({
                        title   : '取消',
                        icon    : 'loading',
                        duration: 1000
                    })
                }
            }
        }) 
    },

    /**
     * 支付提交
     */
    orderPay(e) {
        let url = ''
        let orderid = e.currentTarget.dataset.id
        if(this.data.orderType == 'welfare' || this.data.orderType == 'welfareGoods') url = wx.$api.exchange.welfarePay
        if(this.data.orderType == 'rights' || this.data.orderType == 'rightsCoupons') url = wx.$api.exchange.rightsPay
        url(orderid).then(res=>{
            // payTips为1的时候为微信支付
            if(this.data.pay.payTips == 1) {
                let wechaUrl = ''
                if(this.data.orderType == 'welfare' || this.data.orderType == 'welfareGoods') wechaUrl = wx.$api.index.fridayPay
                if(this.data.orderType == 'rights' || this.data.orderType == 'rightsCoupons') wechaUrl = wx.$api.index.wechat
                wechaUrl(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(()=>{
                                    // 获取商品活动订单
                                    this.orderInfo();
                                },2000)
                            }
                        },
                        fail  : res=>{
                            // 获取商品活动订单
                            this.orderInfo();
                        }
                    })
                }).catch(err => {});
            }
            // payTips为2的时候为沃钱包支付
            if(this.data.pay.payTips == 2) {
                const newUrl     = "https://lifetest.ysd-bs.com/unicom/payment?trade_no=" + res.data.trade_no
                let url= encodeURIComponent(newUrl)
                wx.navigateTo({
                    // 跳转到webview页面
                    url: `/pages/webView/webView?url=${url}`
                });
            }

            this.setData({
                ['pay.payState']: false
            })
        })
        
    },

    /**
     * 返回上一页
     */
    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: '复制成功'
                        })
                    }
                })
            }
        })
    }
})