Page({

    /**
     * 页面的初始数据
     */
    data: {
        address         : '',    //默认收货地址
        allAddress      : '',    //收货地址列表
        addressShow     : false, //收货地址显示
        noticeShow      : false, //须知显示状态
        pointMoreShow   : false, //重要提示显示状态
        isdeliver       : -1,
        groupId         : '',    //权益id
        contData        : '',    //全局内容
        rightData       : '',    //权益详情
        content         : '',    //内容介绍
        notification    : '',    //重要提示
        remark          : '',    //使用须知
        platformCp      : [],    //选择提交数组
        platIndex       : 0,     //选择提交方式下标
        payWayIndex     : 0,     //选择支付方式下标
        payWay          :[
            {value: 0, name: "微信支付"},
            {value: 1, name: "沃钱包支付"}
        ]

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        this.setData({
            groupId   : options.id
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // 获取详情
        this.rightsInfo();
    },

    /**
     * 详情
     */
    rightsInfo() {
        wx.$api.index.welfares(this.data.groupId).then(res=>{
            let obj    = res.data.detail.right.express
            let defGet = res.data.detail.right.def_get
            let arr    = new Array
        
            arr = Object.keys(obj).map(val=>{
                return { ...obj[val], ...{key: val} }
            })

            if(this.data.isdeliver == -1){
                if(res.data.detail.right.type == 'physical') {
                    this.setData({
                        isdeliver : res.data.detail.right.def_get
                    })
                }
                if(res.data.detail.right.type == 'virtual') this.setData({isdeliver : 1})
            }

            this.setData({
                address     : res.data.address,
                allAddress  : res.data.all_address,
                contData    : res.data.detail,
                platformCp  : arr,
                rightData   : res.data.detail.right,
                remark      : res.data.detail.remark.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"'),
                content     : res.data.detail.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
            })
        }).catch(err=>{
            if(!err.login){
                wx.showModal({
                    title       : '用户登录已过期',
                    content     : '请重新登录',
                    showCancel  : false,
                    success : res => {
                      if (res.confirm) {
                        wx.redirectTo({
                          url: '/pages/login/login'
                        })
                      }
                    }
                })
            }
        })
    },

    /**
     * 须知展开收起状态
     */
    noticeTap() {
        this.setData({
            noticeShow : !this.data.noticeShow
        })
    },

    /**
     * 重要提示展开收起状态
     */
    pointMoreTap() {
        this.setData({
            pointMoreShow : !this.data.pointMoreShow
        })
    },

    /**
     * 普通商品支付提交
     */
    ordinary() {
        this.submitOrder();
        this.setData({
            disabled : true
        })
    },

    /**
     * 选择提交方式
     */
    platBind(e) {
        this.setData({
            platIndex : e.detail.value,
            isdeliver : this.data.platformCp[e.detail.value].value
        })
        // 获取详情
        this.rightsInfo()
    },

    /**
     * 收货地址收起
     */
    addressTap() {
        this.setData({
            addressShow : !this.data.addressShow
        })
    },

    /**
     * 选择收货地址
     */
    selectAddress(e){
        let new_addressId = e.currentTarget.dataset.id,
            addressId     = this.data.address.id
        if (new_addressId != addressId) {
            this.setData({
                address     : e.currentTarget.dataset.index,
                addressShow : false
            })
        }else{
            this.setData({
                addressShow : false
            })
        }

        // 获取详情
        this.rightsInfo()
    },

    /**
     * 新增收货地址
     */
    addSelect() {
        wx.navigateTo({
          url: '/pages/address/address?type=selectAddress'
        })
        this.setData({
            addressShow : false
        })
    },

    /**
     * 支付选择
     */
    payBind(e) {
        this.setData({
            payWayIndex: e.detail.value
        })
    },

    /**
     * 支付提交
     */
    submitOrder() {
        let welfareId    = this.data.contData.welfare_id,
            address_id   = this.data.address.id,
            is_deliver   = this.data.isdeliver

        wx.$api.index.fridayInfo(welfareId, address_id, is_deliver).then(res=>{
            if(res.data.canPay == false) {
                wx.showToast({
                    title   : '支付成功',
                    icon    : 'success',
                    duration: 2000
                })
                setTimeout(()=>{
                    if(this.data.isdeliver == 1) {
                        wx.reLaunch({
                            url: '/pages/order/order?orderType=rightsCoupons'
                        })
                    } else {
                        wx.reLaunch({
                            url: '/pages/order/order?orderType=rights'
                        })
                    }
                },3000)
                this.setData({
                    rightsTap: true
                })
            }else {
                // payWayIndex为0的时候为微信支付
                if(this.data.payWayIndex == 0) {
                    wx.$api.index.fridayPay(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(()=>{
                                        if(this.data.isdeliver == 1) {
                                            wx.reLaunch({
                                                url: '/pages/order/order?orderType=welfareGoods'
                                            })
                                        } else {
                                            wx.reLaunch({
                                                url: '/pages/order/order?orderType=welfare'
                                            })
                                        }
                                    },3000)
                                }
                            },
                            fail  : res=>{
                                if(this.data.isdeliver == 1) {
                                    wx.reLaunch({
                                        url: '/pages/order/order?orderType=welfareGoods&stateType=unpay'
                                    })
                                } else {
                                    wx.reLaunch({
                                        url: '/pages/order/order?orderType=welfare&stateType=unpay'
                                    })
                                }
                            }
                        })
                    })
                }
                // payWayIndex为1的时候为沃钱包支付
                if(this.data.payWayIndex == 1) {
                    const newUrl     = "https://lifetest.ysd-bs.com/unicom/payment?trade_no=" + res.data.trade_no
                    let url= encodeURIComponent(newUrl)
                    wx.redirectTo({
                        // 跳转到webview页面
                        url: `/pages/webView/webView?url=${url}`
                    });
                }
            }
        })
    }
})