// pages/cashier/cashier.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        allAmount: '',  //支付金额
        tradeNo  : '',  //支付订单
        current  : '',  //支付方式类型
        paytType : '',  //支付来源类型  welfarePay为周五福利支付   rightsPay为权益支付
        isDeliver: '',  //提货方式  1为自提 0为快递
        barHeight: getApp().globalData.statusBarHeight,  //顶部菜单栏
        unicomPay: ''  //是否有沃支付
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        this.setData({
            allAmount : options.amount,
            tradeNo   : options.trade_no,
            paytType  : options.pay_type,
            isDeliver : options.is_deliver,
            unicomPay: getApp().globalData.unicomPay
        })

        if(getApp().globalData.unicomPay == true) {
            this.setData({
                current: 1
            })
        } else {
            this.setData({
                current: 2
            })
        }
    },

    /**
     * 选择支付方式
     */
    radioChange(e) {
        this.setData({
            current   : e.detail.value
        })
    },

    /**
     * 支付提交
     */
    submitPay() {
        // current为1的时候为沃支付-直接跳转h5
        if (this.data.current == 1) {
            const newUrl     = "https://card.ysd-bs.com/unicom/payment?trade_no=" + this.data.tradeNo
            let url= encodeURIComponent(newUrl)
            wx.redirectTo({
                // 跳转到webview页面
                url: `/pages/webView/webView?url=${url}`
            });
        }

        // current为2的时候为微信支付
        if(this.data.current == 2) {
            let newUrl = '' //定义接口来源名称

            if(this.data.paytType == "welfarePay") newUrl = wx.$api.index.fridayPay
            if(this.data.paytType == "rightsPay") newUrl = wx.$api.index.wechat

            newUrl(this.data.tradeNo).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(()=>{
                                if(this.data.isDeliver == 1) {
                                    if( this.data.paytType == "rightsPay" ) {
                                        wx.reLaunch({
                                            url: '/pages/order/order?orderType=rightsCoupons'
                                        })
                                    }
                                    if( this.data.paytType == "welfarePay" ) {
                                        wx.reLaunch({
                                            url: '/pages/order/order?orderType=welfareGoods'
                                        })
                                    }
                                } else {
                                    if( this.data.paytType == "rightsPay" ) {
                                        wx.reLaunch({
                                            url: '/pages/order/order?orderType=rights'
                                        })
                                    }
                                    if( this.data.paytType == "welfarePay" ) {
                                        wx.reLaunch({
                                            url: '/pages/order/order?orderType=welfare'
                                        })
                                    }
                                    
                                }
                            },3000)
                        }
                    },
                    fail  : res=>{
                        if(this.data.isDeliver == 1) {
                            if( this.data.paytType == "rightsPay" ) {
                                wx.reLaunch({
                                    url: '/pages/order/order?orderType=rightsCoupons&stateType=unpay'
                                })
                            }
                            if( this.data.paytType == "welfarePay" ) {
                                wx.reLaunch({
                                    url: '/pages/order/order?orderType=welfareGoods&stateType=unpay'
                                })
                            }
                        } else {
                            if( this.data.paytType == "rightsPay" ) {
                                wx.reLaunch({
                                    url: '/pages/order/order?orderType=rights&stateType=unpay'
                                })
                            }
                            if( this.data.paytType == "welfarePay" ) {
                                wx.reLaunch({
                                    url: '/pages/order/order?orderType=welfare&stateType=unpay'
                                })
                            }
                        }
                    }
                })
            }).catch(err => {});
        }
    },

    /**
     * 是否取消支付
     */
    payReturn() {
        wx.showModal({
            title   : '提示',
            content : '是否取消支付',
            success : res=> {
                if (res.confirm) {
                    if(this.data.isDeliver == 1) {
                        if( this.data.paytType == "rightsPay" ) {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=rightsCoupons&stateType=unpay'
                            })
                        }
                        if( this.data.paytType == "welfarePay" ) {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=welfareGoods&stateType=unpay'
                            })
                        }
                    } else {
                        if( this.data.paytType == "rightsPay" ) {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=rights&stateType=unpay'
                            })
                        }
                        if( this.data.paytType == "welfarePay" ) {
                            wx.reLaunch({
                                url: '/pages/order/order?orderType=welfare&stateType=unpay'
                            })
                        }
                    }
                }
            }
        })
    }
})