/**
 *Created by ZHJY on 2020/7/14.
 */
var payContainer = new Vue({
    el:"#payContainer",
    data: {
        isSubMitStatus:false,
        payInfo:{
            PAY_BANK_NUM:'ICBC_B2B',//银行网管信息
            PAY_CODE:'',//链接状态
            PAY_ORDERID:'',//主订单ID
            PAY_UNPAIDAMOUNT:0,//本次支付金额
            PAY_TIME:''//链接失效时间
        },
        bankData:[
            {imgUrl:'/img/pay/iconbank-01@2x.png',bankName:'中国工商银行',bankNum:'ICBC_B2B'},
            {imgUrl:'/img/pay/iconbank-02@2x.png',bankName:'招商银行',bankNum:'CMBCHINA_B2B'},
            {imgUrl:'/img/pay/iconbank-03@2x.png',bankName:'中国建设银行',bankNum:'CCB_B2B'},
            {imgUrl:'/img/pay/iconbank-04@2x.png',bankName:'交通银行',bankNum:'BOCO_B2B'},
            {imgUrl:'/img/pay/iconbank-05@2x.png',bankName:'兴业银行',bankNum:'CIB_B2B'},
            {imgUrl:'/img/pay/iconbank-06@2x.png',bankName:'中国民生银行',bankNum:'CMBC_B2B'},
            {imgUrl:'/img/pay/iconbank-07@2x.png',bankName:'中国广发银行',bankNum:'CEB_B2B'},
            {imgUrl:'/img/pay/iconbank-08@2x.png',bankName:'中国银行',bankNum:'BOC_B2B'},
            {imgUrl:'/img/pay/iconbank-09@2x.png',bankName:'平安银行',bankNum:'SZPA_B2B'},
            {imgUrl:'/img/pay/iconbank-10@2x.png',bankName:'中信银行',bankNum:'ECITIC_B2B'},
            {imgUrl:'/img/pay/iconbank-11@2x.png',bankName:'浦发银行',bankNum:'SPDB_B2B'},
            {imgUrl:'/img/pay/iconbank-12@2x.png',bankName:'华夏银行',bankNum:'HXB_B2B'},
            {imgUrl:'/img/pay/iconbank-13@2x.png',bankName:'北京银行',bankNum:'BCCB_B2B'},
            {imgUrl:'/img/pay/iconbank-14@2x.png',bankName:'中国农业银行',bankNum:'ABC_B2B'},
            {imgUrl:'/img/pay/iconbank-15@2x.png',bankName:'中国邮政储蓄银行',bankNum:'PSBC_B2B'},
            {imgUrl:'/img/pay/iconbank-16@2x.png',bankName:'徽商银行',bankNum:'HSB_B2B'},
            {imgUrl:'/img/pay/iconbank-17@2x.png',bankName:'青岛银行',bankNum:'QDYH_B2B'},
            {imgUrl:'/img/pay/iconbank-18@2x.png',bankName:'浙商银行',bankNum:'CZ_B2B'},
            {imgUrl:'/img/pay/iconbank-19@2x.png',bankName:'齐鲁银行',bankNum:'QLYH_B2B'},
        ],
        userName :'',
        goodList:'',
        freight :'',       //运费
        discountFee:'',//经理折扣
        balancePayFee:'',//余额抵扣
        payTotalFee: '',   //合计
        payableAmount:'',//本次支付金额
        payment:'', //剩余支付金额
        receiptAmount:'',//已支付金额
        discernReceipt:'',//支付记录
        discernReceiptList:[],//支付记录列表
        orderProductList:[],//商品列表
        paySuccessCounter:0,
        tabIndex:0,
        downHour:'00',//倒计时
        downMinute:'00',//倒计分
        downSecond:'00',//倒计秒
        isSiled:false,
        mbOrderId:'',
        isRequest:false,
        isPayAlert:false,
        isErrorShow:false,
        iconErrorClass:'',
        iconErrorText:'',
        iconErrorMsgnone:''

    },
    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;
            PayApi.PayOrderCheckoutCounter({orderId:_self.payInfo.PAY_ORDERID},function(response){
                if(response.code == 0){
                    var data = response.data;
                    _self.discernReceiptList = data.discernReceipt;
                    _self.orderProductList = data.orderProductList;
                    _self.userName =  data.userName;
                    _self.freight =  _self.freightText(data.order.freight);
                    _self.discountFee =  data.order.discountFee;
                    _self.balancePayFee =  data.order.balancePayFee;
                    _self.payTotalFee =  data.order.payTotalFee;
                    _self.receiptAmount=  data.order.receiptAmount;
                    _self.payableAmount =  _self.payInfo.PAY_UNPAIDAMOUNT;
                    if(data.order.status == '7'){
                        _self.payment = _self.toFixedFn(_self.payTotalFee - _self.payableAmount);
                    }else{
                        _self.payment = _self.toFixedFn(_self.payTotalFee - _self.receiptAmount - _self.payableAmount);
                    }
                    _self.paySuccessCounter = data.order.paySuccessCounter;
                    _self.isRequest = true;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        PaySubmitFn:function(){//立即支付
            var _self = this;
            if(_self.payInfo.PAY_BANK_NUM == ''){
                layer.msg('请选择银行！');
                return;
            }
            var params = {
                    payWay:'UNIONPAY',
                    payAmount: _self.payInfo.PAY_UNPAIDAMOUNT*100,
                    bankCode:_self.payInfo.PAY_BANK_NUM,
                    returnUrl:'https://www.caimei365.com/',
                    orderId:_self.payInfo.PAY_ORDERID
                };
            _self.PayOrderPcMallPay(params);
        },
        PayOrderPcMallPay:function(params){//网银支付请求
            var _self = this;
            _self.isSubMitStatus=true;
            PayApi.PayOrderPcMallPay(params,function(response){
                if(response.code == 0){
                    _self.isPayAlert = true;
                    _self.mbOrderId = response.data.data.mbOrderId;
                    window.open(response.data.data.payUrl);
                    _self.isSubMitStatus=false;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                    _self.isSubMitStatus=false;
                }
            })
        },
        RefreshBody:function(){
            var _self = this;
            _self.isPayAlert = false;
            PayApi.PayOrderFindOrderStatus({mbOrderId:_self.mbOrderId},function(response){
                _self.isPayAlert = false;
                var data = response.data.data;
                if(data.status === '1'){
                    window.location.href = '/pay/success.html?pageType=wechat&type=success&payAmount='+ _self.payInfo.PAY_UNPAIDAMOUNT;
                }else{
                    window.location.href = '/pay/success.html?pageType=wechat&type=error&payAmount='+ _self.payInfo.PAY_UNPAIDAMOUNT;
                }
            });
        },
        changeTab:function(index,item){
            this.tabIndex = index;
            this.payInfo.PAY_BANK_NUM = item.bankNum;
        },
        slideToggleContent:function(){
            this.isSiled = !this.isSiled;
            console.log(this.isSiled);
        },
        countTime:function(value){//支付链接倒计时
            var _self = this;
            //获取当前时间
            var date = new Date();
            var now = date.getTime();
            //设置截止时间
            var endDate = new Date(value);
            var end = endDate.getTime();
            //时间差
            var differTime = end - now;
            //定义变量,h,m,s保存倒计时的时间
            var h, m, s;
            if (differTime >= 0) {
                h = Math.floor(differTime / 1000 / 60 / 60);
                m = Math.floor(differTime / 1000 / 60 % 60);
                s = Math.floor(differTime / 1000 % 60);
                _self.downHour = h < 10 ? ("0" + h) : h;
                _self.downMinute = m < 10 ? ("0" + m) : m;
                _self.downSecond = s < 10 ? ("0" + s) : s;
                //递归调用函数所以是延时器不是定时器
                setTimeout(function () {
                    _self.countTime(value)
                }, 1000);
            } else {
                _self.downHour = '00';
                _self.downMinute = '00';
                _self.downSecond = '00';
            }
        },
        freightText:function (type) {//处理邮费显示
            var text ="";
            if(type > 0){
                text = '￥'+type.toFixed(2);
            }else if(type == -1){
                text = '到付';
            }else{
                text = '包邮';
            }
            return text
        },
        payTypeText:function(state){//处理支付记录文字
            var stateText = '',
                stateTextObject={
                    12:'企业网银',
                    13:'微信支付',
                    14:'支付宝',
                    15:'微信支付',
                    16:'余额抵扣',
                };
            Object.keys(stateTextObject).forEach(function(key) {
                if(key == state){
                    stateText = stateTextObject[key]
                }
            });
            return stateText;
        },
        toFixedFn:function(text){
            return Number(text).toFixed(2);
        },
    },
    mounted: function () {
        var _self = this;
        _self.payInfo.PAY_CODE = CAIMEI.getUrlParam('code');
        _self.payInfo.PAY_ORDERID = CAIMEI.getUrlParam('orderId');
        _self.payInfo.PAY_UNPAIDAMOUNT = Number(CAIMEI.getUrlParam('unpaidAmount'));
        _self.payInfo.PAY_TIME = decodeURI(CAIMEI.getUrlParam('time'));
        if(_self.payInfo.PAY_CODE==0){
            setTimeout(function () {  //支付倒计时
                _self.countTime(_self.payInfo.PAY_TIME)
            }, 1000);
            _self.infoPayOrderCheckoutCounter();
        }else{
            _self.isErrorShow = true;
            _self.isRequest = true;
            switch (_self.payInfo.PAY_CODE) {
                case '-1':
                    _self.iconErrorClass = 'again';
                    _self.iconErrorText = '链接已更新，请联系业务人员获取最新链接再进行支付';
                    _self.iconErrorMsgnone = '';
                    break;
                case '-2':
                    _self.iconErrorClass = 'offline';
                    _self.iconErrorText = '订单已通过线下转账方式付款';
                    _self.iconErrorMsgnone = '不能再使用企业网银支付';
                    break;
                case '-3':
                    _self.iconErrorClass = 'fail';
                    _self.iconErrorText = '链接超过24小时未完成支付，已失效';
                    _self.iconErrorMsgnone = '请重新生成支付链接，继续支付';
                    break;
                case '5':
                    _self.iconErrorClass = 'paid';
                    _self.iconErrorText = '款项已支付完成，无需重复支付';
                    _self.iconErrorMsgnone = '';
                    break;
            }
        }
    }
});
