 /*
 * 手太欠
 * 愿这世界都如故事里一样 美好而动人~
 */

Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodId       : '', //店铺id
        skuId        : '', //产品id
        num          : 1,  //数量默认1
        address      : '', //地址
        addressId    : '', //地址id
        stockData    : '', //数据
        amount       : '', //总金额
        freight      : '', //运费
        weight       : '', //重量
        remark       : '', //备注
        exchangeSee  : false, //是否兑换
        disabled     : false,
        orderNo      : '',  // 订单编号
        surplus      : '',  // 水滴数量
        paySuccess  : false, // 兑换成功显示
        payType      : 'wechat'  //支付类型  wechat微信  water水滴兑换
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            goodId: options.goodsid,
            skuId : options.skuid,
            num   : options.qty
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // 获取订单前置
        this.orderShow()

        // 获取用户信息
        this.userInfo();
    },

    /**
     * 用户信息
     */
    userInfo() {
        wx.$api.user.home().then(res => {
            this.setData({
                surplus : res.data.account.score.surplus
            })
        }).catch(err => {})
    },

    /**
     * 订单前置
     */
    orderShow() {
        wx.$api.mall.foundOrder({
            goods_sku_id: this.data.skuId,
            address_id   : this.data.addressId,
            qty         : this.data.num
        }).then(res => {
            this.setData({
                address   : res.data.address,
                addressId : res.data.address.address_id,
                stockData : res.data.detail,
                amount    : res.data.amount,
                freight   : res.data.freight,
                weight    : res.data.weight
            })
        }).catch(err => {})
    },

    /**
     * 商品数量加减
     */
    goodsNumber(e){
        let num = this.data.num,
            val = e.currentTarget.dataset.type
        if (val == 'plus'){
            num ++;
        }else{
            if (num > 1){
                num --;
            }else{
                wx.showToast({
                    title   : '商品数量不能小于1',
                    icon    : 'none'
                })
            }
            this.setData({
                num: num
            })
        }
        this.setData({
            num: num
        })

        // 获取订单前置
        this.orderShow()
    },

    /**
     * 输入商品数量
     */
    goodsNumberInput(e) {
        let goodsNum = e.detail.value;
        if (goodsNum > 0) {
            this.setData({
                num: goodsNum
            })
        } else {
            wx.showToast({
                title: '商品数量不能小于1',
                icon: 'none'
            });
            this.setData({
                num: 1
            })
        }

    },

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

    /**
     * 是否确认兑换
     */
    exchangeShow() {
        let newQty = this.data.num
        let data = {
            goods_sku_id: this.data.skuId,
            remark      : this.data.remark,
            qty         : newQty,
            address_id  : this.data.address.address_id
        }
        wx.$api.mall.affirmOrder(data).then(res => {
            this.setData({
                orderNo: res.data.order_no
            })
            // this.data.current == wechat 为微信支付
            if(this.data.payType == 'wechat') {
                this.wxPay()
                return
            }

            // this.data.payType == water 为水滴兑换
            this.setData({
                exchangeSee: !this.data.exchangeSee
            })
        }).catch(err => {})
    },

    /**
     * 水滴兑换确认订单
     */
    goodsForm() {
        this.waterPay();
    },

    /**
     * 关闭兑换弹出
     */
    goodsCancel() {
        this.setData({
            exchangeSee: false
        })
    },

    // 微信支付
    wxPay() {
        wx.showLoading({
            title: '支付中...',
            mask : true
        })
        let that = this
        wx.login({
            success: res => {
                wx.$api.member.openid(res).then(openidRes => {
                    wx.$api.member.wechatPay(this.data.orderNo, {
                        type   : 'miniapp',
                        openid : openidRes.data
                    }).then(PayRes => {
                        wx.hideLoading()
                        this.setData({
                            disabled   : true
                        })
                        
                        let payInfo = JSON.parse(PayRes.data.wechat)
                        wx.requestPayment({
                            timeStamp: payInfo.timeStamp,
                            nonceStr : payInfo.nonceStr,
                            package  : payInfo.package,
                            paySign  : payInfo.paySign,
                            signType : payInfo.signType,
                            success  : payInfoRes=>{
                                if(payInfoRes.errMsg == "requestPayment:ok"){
                                    wx.showToast({
                                        title: '支付成功',
                                        icon: 'none',
                                        duration: 2000,
                                        //显示透明蒙层，防止触摸穿透
                                        mask:true,
                                        success: function () {
                                            that.setData({
                                                paySuccess: true
                                            })
                                            setTimeout(()=>{
                                                wx.redirectTo({
                                                    url: '/pages/order/list/index?listType=paid'
                                                })
                                            },3000)
                                        }
                                    })
                                }
                            },
                            fail  : res=>{
                                wx.showToast({
                                    title: '支付失败',
                                    icon: 'none',
                                    duration: 500,
                                    //显示透明蒙层，防止触摸穿透
                                    mask:true,
                                    success: function () {
                                        that.setData({
                                            paySuccess: true
                                        })
                                        setTimeout(()=>{
                                            wx.redirectTo({
                                                url: '/pages/order/list/index?listType=unpay'
                                            })
                                        },3000)
                                    }
                                })
                            }
                        })
                    }).catch(err => {})
                }).catch(err => {})
            }
        })
    },

    // 水滴兑换
    waterPay() {
        wx.showLoading({
            title: '兑换中...',
            mask : true
        })
        let that = this
        wx.$api.mall.affirmPay(this.data.orderNo, {gateway: 'miniapp'}).then(res => {
            wx.hideLoading()
            this.setData({
                exchangeSee: false,
                disabled   : true
            })
            wx.showToast({
                title: '兑换成功',
                icon: 'none',
                duration: 2000,
                //显示透明蒙层，防止触摸穿透
                mask:true,
                success: function () {
                    that.setData({
                        paySuccess: true
                    })
                    setTimeout(()=>{
                        wx.redirectTo({
                            url: '/pages/order/list/index?listType=paid'
                        })
                    },3000)
                }
            })
        }).catch(err => {})
    }
})