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

Page({
    data: {
        goods_id     : '',  // 店铺id
        goods_sku    : '',  // 产品id
        goods_qty    : '',  // 产品数量
        address      : '',  // 地址
        addressId    : '',  // 地址id
        goodskData   : '',  // 数据
        amount       : '',  // 总金额
        freight      : '',  // 运费
        weight       : '',  // 重量
        disabled     : true,//按钮
        paySuccess   : false, // 兑换成功显示
        maxcoupon    : '',   //拥有兑换券数量
        coupon_qty   : 1,   //兑换券数量
        checked      : false//兑换券
    },

    onLoad(options) {
        this.setData({
            goods_id  : options.goodsid,
            goods_sku : options.skuid,
            goods_qty : options.qty
        })
    },

    onShow() {
        // 获取下单信息
        this.confirmInfo()
    },

    /**
     * 产品下单信息
     */
    confirmInfo (){
        wx.$api.mall.mallPlace({
            goods_sku_id: this.data.goods_sku,
            address_id: this.data.addressId,
            qty: this.data.goods_qty
        }).then(res => {
            this.setData({
                address   : res.data.address,
                addressId : res.data.address.address_id,
                goodskData: res.data.detail,
                amount    : res.data.amount,
                freight   : res.data.freight,
                weight    : res.data.weight,
                maxcoupon : res.data.couponCount
            })

            // 计算总价格
            this.totalPrice()
        }).catch(err => { })
    },

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

        // 计算总价格
        this.totalPrice()
    },

    /**
     * 是否使用兑换券
     */
    checkedTap() {
        this.setData({
            checked: !this.data.checked
        })

        // 计算总价格
        this.totalPrice()
    },

    /**
     * 兑换券数量加减
     */
    couponNumber(e){
        let minVal = Math.min(this.data.goods_qty, this.data.maxcoupon)
        let num = this.data.coupon_qty,
            val = e.currentTarget.dataset.type
        if (val == 'plus'){
            if(num >= minVal){
                this.setData({
                    coupon_qty: minVal
                })
                wx.showToast({
                    title: '兑换券数量超出产品数量或兑换券不足',
                    icon : 'none'
                })
                return
            }
            num ++;

        }else{
            if (num > 1){
                num --;
            }else{
                wx.showToast({
                    title   : '兑换券数量不能小于1',
                    icon    : 'none'
                })
            }
            this.setData({
                coupon_qty: num
            })
        }
        this.setData({
            coupon_qty: num
        })

        // 计算总价格
        this.totalPrice()
    },

    /**
     * 计算总价格
     */
    totalPrice() {
        let couponQty  = this.data.coupon_qty
        let goodNum    = this.data.goods_qty
        let goodPrice  = this.data.goodskData[0].items[0].price
        let freight    = this.data.freight
        let totalPrice = 0
        
        totalPrice = this.data.checked ? 
                    (goodNum - couponQty) * goodPrice :
                    goodNum * goodPrice
        if(freight > 0) totalPrice += freight

        this.setData({
            amount: totalPrice
        })
    },

    /**
     * 产品确认购买
     */
    buyTap() {
        wx.showLoading({
            title: '加载中...',
        })
        let that = this
        let data = {
            goods_sku_id: this.data.goods_sku,
            qty         : this.data.goods_qty,
            address_id  : this.data.addressId,
            use_coupon  : this.data.checked,
            coupon_qty  : this.data.coupon_qty
        }
        wx.$api.mall.mallAffirm(data).then(res => {
            this.setData({
                disabled: false
            })
            wx.hideLoading()

            if(res.data.can_pay) {
                wx.redirectTo({
                    url: '/pages/pay/index?order_no=' + res.data.order_no + '&total=' + res.data.total
                })
                return
            }
            
            wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 2000,
                //显示透明蒙层，防止触摸穿透
                mask:true,
                success: function () {
                    that.setData({
                        paySuccess: true
                    })
                    setTimeout(()=>{
                        wx.redirectTo({
                            url: '/pages/order/index?state=paid'
                        })
                    },3000)
                }
            })

        }).catch(err => {
            this.setData({
                disabled: true
            })
        })
    }
})
