/*
 * 手太欠
 * 企获客商城
 */

const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        coupons     : [],        //优惠券列表
        couponShow  : false,     //优惠券弹出状态
        goodBuyQty  : '',       //商品数量
        goodBuyId   : '',       //商品id
        addressDel  : '',       //默认地址
        detail      : '',       //商品详情
        allShow     : false,    //优惠券状态
        choiceTitle : '请选择优惠券',       //优惠券标题
        couponId    : 0,        //优惠券id
        amount      : 0,        //商品总价格
        freight     : '0.00',   //运费
        allAmount   : 0,        //总共价格
        remark      : '',       //备注信息
        lists       : [],       //购物车列表
        typeCart    : '',       //订单类型  arrCart为购物车订单
        cartIds     : '',       //购物车id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        console.log(options)
        this.setData({
            goodBuyQty  : options.qty,
            goodBuyId   : options.skuId,
            cartIds     : options.cart_ids,
            typeCart    : options.type
        })
        
        // 获取订单信息
        this.getBuyNow()
    },

    /**
     * 订单信息
     */
    getBuyNow () {
        if(this.data.typeCart == 'arrCart') {
            wx.$api.mall.cartSystem({
                cart_ids  : this.data.cartIds
            }).then(res=>{
                console.log(res)
                this.setData({
                    coupons     : res.coupons,
                    addressDel  : res.address,
                    lists       : res.lists,
                    amount      : res.total,
                    freight     : res.freight
                })
            })
        } else {
            wx.$api.mall.orderSystem({
                goods_sku_id  : this.data.goodBuyId,
                qty           : this.data.goodBuyQty
            }).then(res=>{
                this.setData({
                    coupons     : res.coupons,
                    addressDel  : res.address,
                    detail      : res.detail,
                    amount      : res.amount,
                    freight     : res.freight
                })
            })
        }
    },

    /**
     * 优惠券弹出
     */
    couponTap() {
        this.setData({
            couponShow : !this.data.couponShow
        })
    },

    /**
     * 优惠券关闭
     */
    couponHide() {
        this.setData({
            couponShow : !this.data.couponShow
        })
    },

    /**
     * 支付
     */
    submitOrder() {
        if(this.data.typeCart == 'arrCart') {
            wx.$api.mall.cartBuy({
                cart_ids      : this.data.cartIds, 
                user_coupon_id: this.data.couponId,
                remark        : this.data.remark,
                address_id    : this.data.addressDel.address_id
            }).then(res=>{
                let trade_no = res.trade_no
                // 获取code
                wx.login({
                    success: res=>{
                        wx.$api.mall.wechat({
                            code      : res.code,
                            trade_no  : trade_no
                        }).then(res=>{
                            let payInfo = JSON.parse(res)
                            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(()=>{
                                            wx.redirectTo({
                                                url: '/pages/mall/mall_order/mall_order'
                                            })
                                        },2000)
                                    }
                                },
                                fail  : res=>{
                                    wx.redirectTo({
                                        url: '/pages/mall/mall_order/mall_order?stateType=unpaid'
                                    })
                                }
                            })
                        })
                    }
                })
            })
        } else {
            wx.$api.mall.orderBuy({
                goods_sku_id  : this.data.goodBuyId, 
                qty           : this.data.goodBuyQty,
                user_coupon_id: this.data.couponId,
                remark        : this.data.remark,
                address_id    : this.data.addressDel.address_id
            }).then(res=>{
                let trade_no = res.trade_no

                // 获取code
                wx.login({
                    success: res=>{
                        wx.$api.mall.wechat({
                            code      : res.code,
                            trade_no  : trade_no
                        }).then(res=>{
                            let payInfo = JSON.parse(res)
                            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(()=>{
                                            wx.reLaunch({
                                                url: '/pages/mall/mall_order/mall_order'
                                            })
                                        },2000)
                                    }
                                },
                                fail  : res=>{
                                    wx.reLaunch({
                                        url: '/pages/mall/mall_order/mall_order?stateType=unpaid'
                                    })
                                }
                            })
                        })
                    }
                })
            })
        }
    },

    /**
     * 选择使用优惠券
     */
    detailLink(e) {
        let index      = e.currentTarget.dataset.index,
            coupons    = this.data.coupons,
            coupon_id  = ''

        coupons[index].is_check = !coupons[index].is_check

        if(coupons[index].is_check){
            coupon_id = coupons[index].user_coupon_id
        }else{
            coupon_id = 0
        }
        for(let i in coupons){
            if(i!=index){
                coupons[i].is_check = false
            }
        }

        console.log(coupon_id)

        this.setData({
            couponShow  : false,
            coupons     : coupons,
            choiceTitle : coupons[index].title,
            allShow     : !this.data.allShow,
            couponId    : coupon_id,
            allAmount   : Number(coupons[index].price)
        })
    },

    /**
     * 备注信息
     */
    bindTextAreaBlur(e) {
        this.setData({
            remark : e.detail.value
        })
    },

})