$(function () {
    let url = '../order/wxOrderResendQueryAllList';
    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
			{label: 'wxOrderId', name: 'wxOrderId', index: 'id', key: true, hidden: true},
			// {label: '订单编号', name: 'wxOrderId', index: 'wxOrderId', width: 40},
			{label: '商户订单编号', name: 'merchOrderId', index: 'merchOrderId', width: 90},
			{label: '商户子订单编号', name: 'orderSn', index: 'orderSn', width: 100},
            {label: '商户编号', name: 'merchSn', index: 'merchSn', width: 100},
            {label: '商户名称', name: 'merchName', index: 'merchName', width: 70},
            {label: '第三方商户代码', name: 'thirdPartyMerchCode', index: 'thirdPartyMerchCode', width: 70},
            {label: '商户下单时间', name: 'placeOrderlTimeStr', index: 'placeOrderlTimeStr', width: 80},
            {label: '实际支付金额', name: 'acturalPaid', index: 'acturalPaid', width: 80, hidden: true},
            {label: '订购人支付人核验', name: 'buyerPayCheck', index: 'buyerPayCheck', width: 80,
                formatter: function (value) {
                    if (value == '0') {
                        return '未知';
                    } else if (value == '1') {
                        return '一致';
                    } else if (value == '2') {
                        return '不一致';
                    }
                    return '-';
                }
            },
			{label: '电子订单发送状态', name: 'clecOrderStatus', index: 'clecOrderStatus', width: 80,
                formatter: function (value) {
                    if (value == '0') {
                        return '待发送';
                    } else if (value == '1') {
                        return '发送中';
                    } else if (value == '2') {
                        return '已发送';
                    }else {
                        return '失败';
                    }
                    return '-';
                }
            },
            {label: '运单发送状态', name: 'wayOrderStatus', index: 'wayOrderStatus', width: 70,
                formatter: function (value) {
                    if (value == '0') {
                        return '待发送';
                    } else if (value == '1') {
                        return '发送中';
                    } else if (value == '2') {
                        return '已发送';
                    }else {
                        return '失败';
                    }
                    return '-';
                }
            },
            {label: '核验发送状态', name: 'projectCheckStatus', index: 'projectCheckStatus', width: 70,
                formatter: function (value) {
                    if (value == '0') {
                        return '待发送';
                    } else if (value == '1') {
                        return '发送中';
                    } else if (value == '2') {
                        return '已发送';
                    }else {
                        return '失败';
                    }
                    return '-';
                }
            },
            {label: '支付单发送状态', name: 'playOrderStatus', index: 'playOrderStatus', width: 80,
                formatter: function (value) {
                    if (value == '0') {
                        return '待发送';
                    } else if (value == '1') {
                        return '发送中';
                    } else if (value == '2') {
                        return '已发送';
                    }else {
                        return '失败';
                    }
                    return '-';
                }
            },
            {label: '失败原因', name: 'failMsg', index: 'failMsg', width: 100},
            {label: '操作', width: 180, sortable: false,align: 'center',
                formatter: function (value, col, row) {
                    let htmlStr = '<button class="btn btn-outline btn-info" onclick="vm.lookDetail('+row.wxOrderId+')"><i class="fa fa-info-circle"></i>失败原因</button>&nbsp;';

                    if (hasPermission('order:resendWxOrder')) {
                        if (row.clecOrderStatus == 3) {
                            htmlStr += '<button class="btn btn-outline btn-primary" ' +
                                'onclick="vm.resendWxOrder(' + row.wxOrderId + ')"><i class="fa fa-check-circle"></i>&nbsp;重发电子订单</button>&nbsp;';
                        }
                        if (row.wayOrderStatus == 3) {
                            htmlStr += '<button class="btn btn-outline btn-primary" ' +
                                'onclick="vm.resendWxOrder(' + row.wxOrderId + ')"><i class="fa fa-check-circle"></i>&nbsp;重发运单</button>&nbsp;';
                        }
                        if (row.playOrderStatus == 3) {
                            htmlStr += '<button class="btn btn-outline btn-primary" ' +
                                'onclick="vm.resendWxOrder(' + row.wxOrderId + ')"><i class="fa fa-check-circle"></i>&nbsp;重发支付单</button>&nbsp;';
                        }
                    }
                    return htmlStr;
                }
            }],
		viewrecords: true,
        height: 645,
        rowNum: 999,
        // rowList: [200, 300, 500],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: 0,
        title: null,
        orderResend: {},
		ruleValidate: {
            approvalRemark: [
				{required: true, message: '审核备注不能为空', trigger: 'blur'}
			]
		},
		q: {
            orderSn: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
        reloadSearch: function() {
            vm.q = {
                orderSn: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = 0;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'orderSn': vm.q.orderSn},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        resendWxOrder: function (rowId) {
            let params = {};
            params.wxOrderId = rowId;
            confirm('是否确认重发？', function () {
                $.ajax({
                    type: "POST",
                    url: '../order/resendWxOrder',
                    data: params,
                    success: function (r) {
                        if (r.code === 0) {
                            alert('重发成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            })
        },
        lookDetail: function (sn) {
            // vm.showList = 1;
            // vm.title = "维权订单详情";
            let jqGrid = $("#jqGrid").jqGrid('getRowData',sn);
            alert(jqGrid.failMsg);
        }
	}
});
