/**
 *Created by ZHJY on 2020/7/14.
 */
var payContainer = new Vue({
    el:"#payContainer",
    data: {
        loginLoading:true,
        payInfo:{
            PAY_TYPE:'',//支付类型 WEIXIN 微信支付  ALIPAY 支付宝支付
            PAY_ORDERID:'', //订单ID
            PAY_PAYAMOUNT:0,
            PAY_UNPAIDAMOUNT:0,//本次支付金额
            PAY_MOBILE_URL:'',//URL
        },
        orderNo:'',//订单编号
        payableAmount:'',//应付金额
        receiptAmount:'',//已支付金额
        balanceAmount:'',//应付剩余金额
        payAmount:'',//本次支付金额
        discernReceiptList:[],//支付记录列表
        tabIndex:0,//支付方式 0 移动支付 1网银支付 2线下转账
        paySuccessCounter:'',
        mobileTabIndex:0,
        bankTabIndex:0,
        isRequest:false,
        isRefresh:false,
        maxtime:300,
        pageType:'',//页面跳转类型
        productID:''//商品ID
    },
    filters: {
        NumFormat:function(value) {
            if(!value) return '0.00';
            var intPart = Number(value) - Number(value)%1; //获取整数部分（这里是windy93的方法）
            var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断
            var floatPart = ".00"; //预定义小数部分
            var value2Array = value.toString().split(".");
            if(value2Array.length == 2) {  //=2表示数据有小数位
                floatPart = value2Array[1].toString(); //拿到小数部分
                if(floatPart.length == 1) { //补0,实际上用不着
                    return intPartFormat + "." + floatPart + '0';
                } else {
                    return intPartFormat + "." + floatPart;
                }
            } else {
                return intPartFormat + floatPart;
            }
        }
    },
    methods: {
        infoPayOrderCheckoutCounter:function(){//初始化订单数据
            var _self = this;
            if( _self.pageType === '1'){
                setTimeout(function(){
                    switch (_self.payInfo.PAY_TYPE) {
                        case 'WEIXIN':
                            _self.getWrchatQrcodeShow();
                            break;
                        case 'ALIPAY':
                            _self.getAliPayQrcodeInfo();
                            break;
                    }
                },2000);
            }else{
                PayApi.PayOrderCheckoutCounter({orderId:_self.payInfo.PAY_ORDERID},function(response){
                    if(response.code == 0){
                        // console.log(response);
                        var data = response.data;
                        _self.orderNo = data.order.orderNo;
                        _self.paySuccessCounter = data.order.paySuccessCounter;
                        _self.payInfo.PAY_PAYAMOUNT = data.order.payableAmount-data.order.receiptAmount;
                        switch (_self.payInfo.PAY_TYPE) {
                            case 'WEIXIN':
                                _self.getWrchatQrcodeShow();
                                break;
                            case 'ALIPAY':
                                _self.getAliPayQrcodeInfo();
                                break;
                        }

                    }else{
                        layer.msg(response.msg, {icon: 7});
                    }
                })
            }
        },
        getAliPayQrcodeInfo:function(){//生成支付宝二维码
            var _self = this;
            var qrcode = new QRCode(document.getElementById("ali_pay_container"), {
                    render: "canvas", //也可以替换为table
                    text: _self.payInfo.PAY_MOBILE_URL,
                    width: 220,
                    height: 220,
                    colorDark : "#000000",
                    colorLight : "#ffffff",
                    correctLevel : QRCode.CorrectLevel.H
            });
            _self.loginLoading = false;
            _self.isRequest = true;
            var timeClock = setInterval(function(){
                    _self.maxtime--;
                    if (_self.maxtime == 0) {
                        clearInterval(timeClock);
                        _self.isRefresh = true;
                    }
            },1000);
            _self.setTimerAliPay();
        },
        getWrchatQrcodeShow:function(){//生成微信二维码
            var _self = this;
            if( _self.pageType === '1'){
                var orderInfo = {//二手微信二维码参数
                        pageType:1,
                        productId:_self.productID,
                        payAmount:_self.payAmount
                };
            }else{
                var orderInfo = {//普通订单微信二维码参数
                        pageType:0,
                        orderID:_self.payInfo.PAY_ORDERID,
                        payAmount:_self.payAmount
                };
            }
            _self.payInfo.PAY_MOBILE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxea43a0f9ebce9e66&redirect_uri=https://www.caimei365.com/pay/wechatpay.html&response_type=code&scope=snsapi_base&state="+JSON.stringify(orderInfo)+"#wechat_redirect";
            var qrcode = new QRCode(document.getElementById("wx_pay_container"), {
                render: "canvas", //也可以替换为table
                text: _self.payInfo.PAY_MOBILE_URL,
                width: 220,
                height: 220,
                colorDark : "#000000",
                colorLight : "#ffffff",
                correctLevel : QRCode.CorrectLevel.H
            });
            _self.loginLoading = false;
            _self.isRequest = true;
            var timeClock = setInterval(function(){
                _self.maxtime--;
                if (_self.maxtime == 0) {
                    clearInterval(timeClock);
                    _self.isRefresh = true;
                }
            },1000);
            _self.setTimerWechatPay();
        },
        setTimerWechatPay:function () {//定时任务查询微信支付结果
            var _self = this;
            var timer;
            PayApi.PayOrderPayWhetherSuccess({orderId: _self.payInfo.PAY_ORDERID, paySuccessCounter:_self.paySuccessCounter},function(response){
                // console.log(response);
                if(response.code === 0){
                    clearTimeout(timer); //清理定时任务
                    layer.msg('支付成功', {icon: 1});
                    setTimeout(function(){
                        window.location.href = '/index.html';
                    },3000);
                }else{
                    timer = setTimeout(function(){
                        _self.setTimerWechatPay()
                    }, 1000*10);
                }
            });
        },
        setTimerAliPay:function () {//定时任务查询支付宝支付结果
            var _self = this;
            var timer;
            PayApi.PayOrderFindOrderStatus({mbOrderId:_self.mbOrderId},function(response){
                var data = response.data.data;
                if(data.status === '1'){
                    clearTimeout(timer); //清理定时任务
                    layer.msg('支付成功', {icon: 1});
                    setTimeout(function(){
                        window.location.href = '/index.html';
                    },3000);
                }else{
                    timer = setTimeout(function(){
                        _self.setTimerAliPay()
                    }, 1000*10);
                }
            });
        },
        hidePayAlert:function(){//隐藏已支付弹窗
            var _self = this;
            _self.isPayAlert = false;
        },
        toFixedFn:function(text){//处理小数点后两位数
            return Number(text).toFixed(2);
        },
    },
    mounted: function () {
        var _self = this;
        var payData = JSON.parse(CAIMEI.Storage.getItem('PAY_DATA'));
        _self.pageType = CAIMEI.getUrlParam('pageType');
        console.log(payData);
        if( _self.pageType === '1'){
            _self.payInfo.PAY_TYPE = payData.type;
            _self.productID = payData.productId;
            _self.payInfo.PAY_MOBILE_URL = payData.url;
            _self.mbOrderId = payData.mbOrderId;
            _self.payAmount = _self.payableAmount = payData.amount;
        }else{
            _self.payInfo.PAY_TYPE = payData.type;
            _self.payInfo.PAY_ORDERID = payData.orderid;
            _self.payInfo.PAY_UNPAIDAMOUNT = payData.amount;
            _self.payInfo.PAY_MOBILE_URL = payData.url;
            _self.mbOrderId = payData.mbOrderId;
            _self.payAmount =_self.payableAmount = _self.payInfo.PAY_UNPAIDAMOUNT;
        }
        console.log(_self.payInfo.PAY_TYPE);
        _self.infoPayOrderCheckoutCounter();
    }
});
