// pages/activityOrder/activityOrder.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderType   : '',    //订单来源
        stateType   : 'all', //状态
        counts      : '',    //数量
        orderArr    : [],     //列表
        page        : {},   //下一页
        lodingStats : false, //加载状态
        pay         : {
            payTips     : 1,    //支付方式
            payState    : false, //支付状态
            orderId     : '',    //支付订单
        },
        unicomPay   : '' //是否有沃支付
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        this.setData({
            orderType: options.orderType,
            stateType: options.stateType,
            unicomPay: getApp().globalData.unicomPay
        })
    },

    onShow() {
        // 获取商品活动订单
        this.orderInfo();
    },

    /**
     * 商品活动订单
     */
    orderInfo(page) {
        let url = ''
        if(this.data.orderType == 'welfare') url = wx.$api.exchange.welfare
        if(this.data.orderType == 'welfareGoods') url = wx.$api.exchange.welfareCoupons
        if(this.data.orderType == 'rights') url = wx.$api.exchange.orders
        if(this.data.orderType == 'rightsCoupons') url = wx.$api.exchange.ordersCoupons
        url(this.data.stateType, page).then(res=>{
            let listArr = this.data.orderArr,
                newData = []
            if(page == 1 || page == undefined) listArr = []

            newData = listArr.concat(res.data.data)
            this.setData({
                counts      : res.data.count,
                orderArr    : newData,
                page        : res.data.page,
                lodingStats : false
            })
            wx.stopPullDownRefresh()
        }).catch(err => {});
    },

    /**
     * tabs
     */
    orderTab(e){
        if(this.data.stateType != e.currentTarget.dataset.state){
            this.setData({
                stateType: e.currentTarget.dataset.state
            })

            // 获取商品活动订单
            this.orderInfo()

            wx.pageScrollTo({
                scrollTop: 0
            })
        }
    },

    /**
     * 取消订单
     */
    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=>{
                        // 获取商品活动订单
                        this.orderInfo()

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

    /**
     * 支付提交
     */
    orderPay() {
        let url = ''
        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(this.data.pay.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
            })
        }).catch(err => {});
        
    },

    /**
     * 支付选择弹出
     */
    submitPay(e) {
        this.setData({
            ['pay.payState']: !this.data.pay.payState,
            ['pay.orderId'] : e.currentTarget.dataset.id
        })
    },

    /**
     * 上拉加载
     */
    onReachBottom(){
        this.setData({
            lodingStats: true
        })
        let pageNumber = this.data.page.current
        if(this.data.page.has_more){
            pageNumber++
            this.orderInfo(pageNumber)
        }
    },

    /**
     * 选择支付方式
     */
    radioChange(e) {
        this.setData({
            ['pay.payTips']: e.detail.value
        })
    }
})