;
var orderPage = new Vue({
    el: "#orderPage",
    data: {
        isRequset:true,
        orderId:0,              //订单ID
        userId: 0,              //用户ID
        orderInfo:{},           //订单信息
        shopOrderList:[],       //订单商品信息
        userInfo:{},            //订单用户信息
        orderInvoice:{},        //发票信息
        discernReceiptList:[],  //订单付款信息
        returnedPurchaseList:[],//订单退款信息
        orderIdentificationId:'',//订单标识
        modelType:0,
        isFiexd:true
    },
    filters: {
        NumFormat:function(text) {//处理金额
            return Number(text).toFixed(2);
        },
        NumBadge:function(n){//处理
            var num ='';
            if(n>100){num = 99}else{num = n;}
            return num;
        },
        formatIncludedTax:function(value) {
            if (value === '1') {
                return '不含税 ';
            } else if (value === '2') {
                return '含税';
            } else {
                return '';
            }
        },
        stateExp:function (state){ //订单状态文字和颜色
            var stateText = '',
                stateTextObject={
                    0:'待确认',
                    4:'交易完成',
                    5:'订单完成',
                    6:'已关闭',
                    7:'交易全退',
                    77:'交易全退',
                    11:'待付款待发货',
                    12:'待付款部分发货',
                    13:'待付款已发货',
                    21:'部分付款待发货',
                    22:'部分付款部分发货',
                    23:'部分付款已发货',
                    31:'已付款待发货',
                    32:'已付款部分发货',
                    33:'已付款已发货',
                    111:'待付款待发货'
                };
            Object.keys(stateTextObject).forEach(function(key){
                if(key == state){
                    stateText = stateTextObject[key]
                }
            });
            return stateText;
        }
    },
    computed: {

    },
    methods: {
        GetQueryOrderDetailsInfo:function(){//查询订单详情
            var _self = this;
            OrderApi.QueryOrderDetailsInfo({orderID:_self.orderId},function (response) {
                if(response.code == 0){
                    var data = response.data;
                    _self.orderInfo = data.order;
                    _self.userInfo = data.userInfo;
                    _self.orderInvoice = data.orderInvoice;
                    _self.shopOrderList = data.shopOrderList;
                    _self.discernReceiptList = data.discernReceiptList;
                    _self.returnedPurchaseList = data.returnedPurchaseList;
                    _self.orderIdentificationId = '#'+ _self.orderInfo.orderID+'#';
                    _self.isRequset = false;
                }else{
                    CAIMEI.Alert(response.msg, '确定', false);
                }
            })
        },
        hanldConfirmOrderFn:function(){//确认订单
            var _self = this;
            CAIMEI.Modal('是否确认订单？','取消','确定',function () {
                OrderApi.AffirmOrderOrder({orderID:_self.orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('确认成功',true,function () {
                            _self.GetQueryOrderDetailsInfo();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldCancelOrderFn:function(){//取消订单
            var _self = this;
            CAIMEI.Modal('确认取消该订单吗？','取消','确定',function () {
                OrderApi.GetCancelOrder({orderID:_self.orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('取消成功',true,function () {
                            _self.GetQueryOrderDetailsInfo();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldDeleteOrderFn:function(){//删除订单
            var _self = this;
            CAIMEI.Modal('确认删除该订单吗？','取消','确定',function () {
                OrderApi.GetDeleteOrder({orderID:_self.orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('删除成功',true,function () {
                            window.location.href='/user/order/list.html?state=0';
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldReceivingOrderFn:function(){//确认收货
            var _self = this;
            CAIMEI.Modal('是否确认收货？','取消','确定',function () {
                OrderApi.GetAffirmOrder({orderID:_self.orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('操作成功',true,function () {
                            _self.GetQueryOrderDetailsInfo();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        showModelTypePop: function(){
            CAIMEI.Popup({
                content: '<div class="payAlert">' +
                           '<p>您已通过线下转账的方式支付了订单部分款项，剩余款项依然需要使用线下转账方式，给您带来的不便敬请谅解</p>' +
                           '<p>待付金额：<em>￥'+toFloat(this.orderInfo.pendingPayments)+'</em></p>' +
                           '<ul><li>开户行：中信银行（深圳泰然支行）</li>' +
                                '<li>银行卡号：6217 6803 0362 0897</li>' +
                                '<li>户名：周仁声</li>' +
                                '<li>订单标识：<em class="no">'+this.orderIdentificationId+'</em><em class="copy">复制</em></li></ul>' +
                           '<p><i>请在转账备注中填写上述订单标识，方便财务快速审核，提高发货速度</i></p>' +
                         '</div>',
                confitmBtnText: '确定',
                cancelBtnText: '关闭'
            },function(){
                // 确定
                this.hidePaymentConfirm();
            },function(){
                // 关闭
            });
        },
        hanldPayOrderFn:function(){//支付订单
            var _self = this;
            OrderApi.OrderPaymentValidation({orderId:_self.orderId},function(r){
                if(r.code*1 === 0){
                    var dataCode = r.data.code;
                    _self.payModelData = r.data;
                    switch(dataCode){
                        case 1:
                            _self.modelType = 1;
                            CAIMEI.Popup({
                                content: '<div class="payAlert">您有采美余额<em>￥'+toFloat(r.data.ableUserMoney)+'</em>暂未使用，是否需要抵扣订单？ 抵扣后您只需再支付<em>￥'+toFloat(r.data.pendingPayments)+'</em></div>',
                                confitmBtnText: '抵扣,继续付款',
                                cancelBtnText: '不抵扣,继续付款'
                            },function(){
                                // 抵扣,继续付款
                                _self.hanldPaymentConfirm(1)
                            },function(){
                                // 不抵扣,继续付款
                                _self.hanldCancelConfirm()
                            });
                            break;
                        case 2:
                            _self.modelType = 2;
                            CAIMEI.Popup({
                                content: '<div class="payAlert">您有采美余额<em>￥'+toFloat(r.data.ableUserMoney)+'</em>暂未使用，是否需要抵扣订单？ 抵扣后订单支付完成</div>',
                                confitmBtnText: '抵扣',
                                cancelBtnText: '不抵扣,继续付款'
                            },function(){
                                // 抵扣全款
                                _self.hanldPaymentConfirm(2);
                            },function(){
                                // 不抵扣,继续付款
                                _self.hanldCancelConfirm();
                            });
                            break;
                        case -1:
                            CAIMEI.Alert('订单已申请全部退款，无需再付款！', '确定', false);
                            break;
                        default:
                            if(r.data.onlinePayFlag == '1'){
                                _self.modelType = 3;
                                _self.showModelTypePop();
                            }else{
                                window.location.href = '/pay/caimei-paycash.html?type=payfirm&orderID='+_self.orderId;
                            }
                    }
                }else{
                    CAIMEI.Alert(r.msg, '确定', false);
                }
            })
        },
        hanldPaymentConfirm:function() {//余额抵扣跳转
            var _self = this;
            OrderApi.OrderBalanceDeduction({orderId: _self.orderId}, function (response) {
                if (response.code == 0) {
                    if (_self.modelType === 2) {
                        window.location.href = '/pay/success.html?pageType=www&type=success&payAmount='+_self.payModelData.pendingPayments;
                    } else {
                        if (_self.orderInfo.onlinePayFlag === '1') {
                            _self.modelType = 3;
                            _self.showModelTypePop();
                        } else {
                            window.location.href = '/pay/caimei-paycash.html?type=payfirm&orderID='+_self.orderId;
                        }
                    }
                } else {
                    CAIMEI.Alert(response.msg, '确定', false);
                }
            });
        },
        hanldCancelConfirm:function(){//不使用余额抵扣直接跳转收银台
            var _self = this;
            if(_self.orderInfo.onlinePayFlag == '1'){
                _self.modelType = 3;
                _self.showModelTypePop();
            }else{
                window.location.href = '/pay/caimei-paycash.html?type=payfirm&orderID='+_self.orderId;
            }
        },
        hidePaymentConfirm:function(){//隐藏弹窗
            var _self = this;
            _self.modelType = 0;
        },
        hanldQueryExpressFn:function(){//跳转查看物流
            var _self = this;
            window.location.href='/user/order/logistics.html?orderId='+_self.orderId;
        },
        copyOrderBtnSubmitFn:function(){//复制订单标识
            var _self = this;
            var oInput = document.createElement("textarea");
            oInput.value = _self.orderIdentificationId;
            document.body.appendChild(oInput);
            oInput.select(); // 选择对象
            document.execCommand("Copy"); // 执行浏览器复制命令
            document.body.removeChild(oInput);
            CAIMEI.dialog('复制成功');
        },
        showBadge:function(n){//处理标签
            var num ='';
            if(n>100){num = 99}else{num = n;}
            return num;
        },
        toFixedFn:function(text){//处理小数点后两位数
            return Number(text).toFixed(2);
        },
        toggleThisLadder: function(event){
            var el = event.currentTarget;
            if($(el).hasClass("on")){
                $(el).removeClass("on").siblings('.mFixed').hide();
                if(!isPC){looseBody();}
            }else{
                $(el).addClass("on").siblings('.mFixed').show();
                if(!isPC){fixedBody();}
            }
        },
        hideThisLadder: function(event){
            var el = event.currentTarget;
            $(el).parents('.priceTag').find('.tag').removeClass("on").siblings('.mFixed').hide();
            if(!isPC){looseBody();}
        },
    },
    mounted: function () {
        var _self = this;
        if(globalUserData){
            this.userId = globalUserData.userId;
            this.orderId = CAIMEI.getUrlParam('orderId');
            this.GetQueryOrderDetailsInfo();
        }
        $(window).scroll(function (event) {
            var supportPageOffset = window.pageXOffset !== undefined; // 判断是否支持pageXOffset
            var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); // 判断渲染模式是不是标准模式
            var scrollHeight = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
            if(scrollHeight<100){
                _self.isFiexd = true
            }else{
                _self.isFiexd = false
            }
        });
        $('.navLayout').find('.navList').removeClass("on").find('.con').hide().find('a').removeClass("on");
        $('.navLayout').find('.navList').eq(0).addClass("on").find('.con').show().find('a').eq(0).addClass("on");

    }
});
