jqMultipleShow("click", ".navList", ".tab", ".con");
var orderPage = new Vue({
    el: "#orderPage",
    data: {
        isRequset:true,
        userId: 0,
        orderTabBarIndex:0,
        orderTabBar: [
            {state: 0,text: '全部订单',bages:0},
            {state: 1,text: '待确认',bages:0},
            {state: 2,text: '待付款',bages:0},
            {state: 3,text: '待发货',bages:0},
            {state: 4,text: '已发货',bages:0},
            {state: 5,text: '退货/款',bages:0}
        ],
        listQuery:{
            searchNo:'',
            orderState:0,
            userId:0,
            beginTime:'',
            endTime:'',
            pageNum:1,
            pageSize:10
        },
        listRecord: 0,
        pageInput: '1',
        orderList:[],
        confirmedCount:'',//待確認數量
        paymentCount:'',//待付款
        waitShipmentsCount:'',//待发货
        shipmentsCount:'',//已发货
        salesReturnCount:'',//退货款
        modelType:0
    },
    filters: {
        NumFormat:function(text) {//处理金额
            return Number(text).toFixed(2);
        },
        NumBadge:function(n){//处理
            var num ='';
            if(n>100){num = 99}else{num = n;}
            return num;
        },
        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: {
        pageTotal: function () {
            var total = Math.ceil(this.listRecord / this.listQuery.pageSize);
            return total > 0 ? total : 1;
        },
        showPageBtn: function () {
            var total = Math.ceil(this.listRecord / this.listQuery.pageSize);
            total = total > 0 ? total : 1;
            var index = this.listQuery.pageNum, arr = [];
            if (total <= 6) {
                for (var i = 1; i <= total; i++) {
                    arr.push(i);
                }
                return arr;
            }
            if (index <= 3) return [1, 2, 3, 4, 5, 0, total];
            if (index >= total - 2) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
            return [1, 0, index - 2, index - 1, index, index + 1, index + 2, 0, total];
        }
    },
    methods: {
        toPagination: function (pageNum) {//点击切换分页
            if (pageNum <= this.pageTotal) {
                this.listQuery.pageNum = pageNum;
                this.GetQueryOrderListData();
            }
        },
        checkNum: function () {//输入跳转分页
            if (this.pageInput > this.pageTotal) {
                this.pageInput = this.pageTotal;
            } else if (this.pageInput < 1) {
                this.pageInput = 1;
            }
        },
        changeOrderFn:function(index,status){//点击Tab切换查询订单
            console.log(status);
            var  _self = this;
            _self.orderTabBarIndex = index;
            _self.listQuery.orderState = status;
            _self.listQuery.searchNo = '';
            _self.listQuery.beginTime = '';
            _self.listQuery.endTime = '';
            _self.listQuery.pageNum = 1;
            _self.isRequset = true;
            _self.GetQueryOrderListData()
        },
        GetQueryOrderNumInfo:function(){//查询tabs数量
            var _self = this;
            OrderApi.GetQueryOrderNum({userId:_self.userId},function (response) {
                var data = response.data;
                _self.orderTabBar[1].bages = data.confirmedCount;
                _self.orderTabBar[2].bages = data.paymentCount;
                _self.orderTabBar[3].bages = data.waitShipmentsCount;
                _self.orderTabBar[4].bages = data.shipmentsCount;
                _self.orderTabBar[5].bages = data.salesReturnCount;
            })
        },
        GetQueryOrderListData:function(){//查询订单列表
            var _self = this;
            OrderApi.GetQueryOrderListData(_self.listQuery,function (response) {
                if(response.code == 0){
                    var data = response.data;
                    if(data.results && data.results.length>0) {
                        _self.orderList = [];
                        _self.orderList = data.results;
                        _self.listRecord = data.totalRecord;
                    }else{
                        _self.orderList = [];
                        _self.orderList = data.results;
                    }
                    _self.isRequset = false;
                }else{
                    CAIMEI.Alert(response.msg, '确定', false);
                }
            })
        },
        hanldConfirmOrderFn:function(orderId){//确认订单
            var _self = this;
            CAIMEI.Modal('是否确认订单？','取消','确定',function () {
                OrderApi.AffirmOrderOrder({orderID:orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('确认成功',true,function () {
                            _self.GetQueryOrderListData();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldCancelOrderFn:function(orderId){//取消订单
            var _self = this;
            CAIMEI.Modal('确认取消该订单吗？','取消','确定',function () {
                OrderApi.GetCancelOrder({orderID:orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('取消成功',true,function () {
                            _self.GetQueryOrderListData();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldDeleteOrderFn:function(orderId){//删除订单
            var _self = this;
            CAIMEI.Modal('确认删除该订单吗？','取消','确定',function () {
                OrderApi.GetDeleteOrder({orderID:orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('删除成功',true,function () {
                            _self.GetQueryOrderListData();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldReceivingOrderFn:function(orderId){//确认收货
            var _self = this;
            CAIMEI.Modal('是否确认收货？','取消','确定',function () {
                OrderApi.GetAffirmOrder({orderID:orderId},function(response){
                    if(response.code == 0){
                        CAIMEI.dialog('操作成功',true,function () {
                            _self.GetQueryOrderListData();
                        });
                    }else{
                        CAIMEI.Alert(response.msg, '确定', false);
                    }
                })
            })
        },
        hanldDetilsOrderFn:function(orderId){//订单详情
            window.location.href='/user/order/detail.html?order='+orderId;
        },
        hanldPayOrderFn:function(orderId){//支付订单

        }
    },
    mounted: function () {
        if(globalUserData){
            this.userId = globalUserData.userId;
            this.listQuery.orderState = CAIMEI.getUrlParam('state');
            this.listQuery.userId = this.userId;
            this.GetQueryOrderListData();
            this.GetQueryOrderNumInfo();
        }
        console.log(this.listQuery.orderState)
        $('.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");

    }
});
