let cacheData = {
}
$(function () {
    let shippingStatus = getQueryString("shippingStatus");
    let payStatus = getQueryString("payStatus");
    let orderStatus = getQueryString("orderStatus");
    let orderType = getQueryString("orderType");
    let url = '../order/list';
    if (shippingStatus) {
        url += '?shippingStatus=' + shippingStatus;
    }
    if (payStatus) {
        url += '?payStatus=' + payStatus;
    }
    if (orderStatus) {
        url += '?orderStatus=' + orderStatus;
    }
    if (orderType) {
        url += '?orderType=' + orderType;
    }
    $("#jqGrid").Grid({
        url: url,
        rownumWidth:60,
        datatype: "json",
        multiboxonly:true,
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: 'brandId',name:'brandId',index:'brandId',hidden:true},
            {label: '订单号', name: 'orderSn', index: 'order_sn', width: 150},
            // {label: '会员', name: 'userName', index: 'user_name', width: 80,sortable:false},
            {label: '会员昵称', name: 'nickName', index: 'nick_name', width: 80,sortable:false},
            {label: 'VR场景', name: 'brandName', index: 'brand_name', width: 80,sortable:false},
            {
                label: '订单类型', name: 'orderType', index: 'order_type', width: 80, formatter: function (value) {
                    if (value == '1') {
                        return '普通订单';
                    } else if (value == '2') {
                        return '团购订单';
                    } else if (value == '3') {
                        return '砍价订单';
                    } else if (value == '4') {
                        return '立即购买';
                    }
                    return '-';
                }
            },
            {
                label: '订单状态', name: 'orderStatus', index: 'order_status', width: 80, formatter: function (value) {
                    if (value == '0') {
                        return '待付款';
                    } else if (value == '101') {
                        return '订单已取消';
                    } else if (value == '102') {
                        return '订单已删除';
                    } else if (value == '201') {
                        return '订单已付款';
                    } else if (value == '300') {
                        return '订单已发货';
                    } else if (value == '301') {
                        return '用户确认收货';
                    } else if (value == '401') {
                        return '退款（未发货）';
                    } else if (value=='402') {
                        return '退款（已发货）';
                    } else if (value == '501') {
                        return '完成';
                    }
                    return value;
                }
            },
            {
                label: '发货状态',
                name: 'shippingStatus',
                index: 'shipping_status',
                width: 60,
                formatter: function (value) {
                    if (value == '0') {
                        return '未发货';
                    } else if (value == '1') {
                        return '已发货';
                    } else if (value == '2') {
                        return '已收货';
                    } else if (value == '4') {
                        return '退货';
                    }
                    return value;
                }
            },
            {
                label: '付款状态', name: 'payStatus', index: 'pay_status', width: 80,
                formatter: function (value) {
                    if (value == '0') {
                        return '未付款';
                    } else if (value == '1') {
                        return '付款中';
                    } else if (value == '2') {
                        return '已付款';
                    } else if (value == '4') {
                        return '退款';
                    }
                    return value;
                }
            },
            {label: '快递公司', name: 'shippingName', index: 'shipping_name', width: 80},
            {label: '快递单号', name: 'shippingNo', index: 'shipping_No', width: 80},
            // {label: '商品信息', name: 'goodsList', index: 'goods_list', width: 80,sortable:false},
            {label: '实际支付金额', name: 'actualPrice', index: 'actual_price', width: 80},
            // {label: '订单总价', name: 'orderPrice', index: 'order_price', width: 60},
            // {label: '商品总价', name: 'goodsPrice', index: 'goods_price', width: 60},
            {
                label: '下单时间', name: 'addTime', index: 'add_time', width: 120,
                formatter: function (value) {
                    return transDate(value);
                }
            },
            {
                label: '操作', width: 200, align: 'center', sortable: false, formatter: function (value, col, row) {
                    cacheData[col.rowId] = row
                    return '<button class="btn btn-outline btn-info" onclick="vm.lookDetail(' + col.rowId + ')"><i class="fa fa-info-circle"></i>&nbsp;详情</button>' +
                        '<button class="btn btn-outline btn-primary" style="margin-left: 15px;" onclick="vm.printDetail(' + col.rowId + ')"><i class="fa fa-print"></i>&nbsp;打印</button>';
                }
            }
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        detail: false,
        title: null,
        order: {},
        shippings: [],
        q: {
            orderSn: '',
            orderStatus: '',
            brandId: ''
        },
        brands:[],
        orderStatusStr:{
            0:'待付款',
            101:'订单已取消',
            102:'订单已删除',
            201:'订单已付款',
            300:'订单已发货',
            301:'用户确认收货',
            401:'退款(未发货)',
            402:'退款(已发货)',
            501:'完成'
        },
        shippingStatusStr:{
            0:'待发货',
            1:'已发货',
            2:'已收货',
            4:'退货'
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        exports: function () {
            let rowIds = getSelectedRows("#jqGrid");
            let orderSns = rowIds.map(item=>{
                return cacheData[item].orderSn
            })

            if (rowIds == null) {
                return;
            }
            confirm('确定要导出订单记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../order/export",
                    params: {
                        '_search': false,
                        'nd': Math.random(),
                        'sidx': '',
                        'order': 'asc',
                        '_': Math.random(),
                        'orderSnList': orderSns.join(','),
                        'orderStatus': vm.q.orderStatus,
                        'brandId': vm.q.brandId
                    },
                    successCallback: function (r) {
                        if (r.code == 0) {
                            alert('导出成功，点击确定跳转下载', function (index) {
                                vm.reload();
                                window.open(r.url,"_blank")
                            });
                        } else {
                        }
                    }
                });
            });
        },
        /**
         * 获取品牌
         */
        getBrands: function () {
            Ajax.request({
                url: "../brand/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.brands = r.list;
                }
            });
        },
        sendGoods: function (event) {
            let rowId = getSelectedRow("#jqGrid");
            let id = cacheData[rowId].id
            let brandId = cacheData[rowId].brandId

            if (rowId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "发货";
            Ajax.request({
                url: "../order/info/" + id+'/'+ brandId,
                async: true,
                successCallback: function (r) {
                    vm.order = r.order;
                }
            });
        },
        confirm: function (event) {
            let rowId = getSelectedRow("#jqGrid");
            let id = cacheData[rowId].id,brandId = cacheData[rowId].brandId

            if (parseInt(cacheData[rowId].shippingStatus) === 2) {
                return alert('该订单已收货', function (index) {
                });
            }

            if (rowId == null) {
                return;
            }

            confirm('确定收货？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../order/confirm/",
                    contentType: "application/json",
                    params: JSON.stringify({
                        id:id,
                        brandId:brandId
                    }),
                    successCallback: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            Ajax.request({
                type: "POST",
                url: "../order/sendGoods",
                contentType: "application/json",
                params: JSON.stringify(vm.order),
                successCallback: function (r) {
                    if (r.code == 0) {
                        alert('发货成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.detail = false;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'orderSn': vm.q.orderSn,
                    'orderStatus': vm.q.orderStatus,
                    'brandId': vm.q.brandId
                },
                page: page
            }).trigger("reloadGrid");
        },
        lookDetail: function (rowId) { //第三步：定义编辑操作
            let id = cacheData[rowId].id, brandId = cacheData[rowId].brandId
            console.log(brandId)
            vm.detail = true;
            vm.title = "订单详情";
            Ajax.request({
                url: "../order/info/" + id+'/'+ brandId,
                async: true,
                successCallback: function (r) {
                    vm.order = r.order;
                }
            });
        },
        printDetail: function (rowId) {
            let id = cacheData[rowId].id,brandId = cacheData[rowId].brandId

            openWindow({
                type: 2,
                title: '<i class="fa fa-print"></i>打印票据',
                content: '../shop/orderPrint.html?orderId=' + id + '&brandId=' + brandId
            })
        }
    },
    created: function () {
        let vue = this;
        this.getBrands()
        Ajax.request({
            url: "../shipping/list",
            async: true,
            params: {
                'page': 1,
                'limit': 1000,
                'sidx': '',
                'order': 'asc'
            },
            successCallback: function (r) {
                vue.shippings = r.page.list;
            }
        });
    }
});
window.vm = vm