$(function () {
    $("#jqGrid").jqGrid({
        url: '../pickupcode/list',
        datatype: "json",
        colModel: [
			{label: '订单编号', name: 'orderSn', index: 'order_sn', key: true, hidden: false},
			{label: '取货码', name: 'pickUpCodeSn', index: 'pick_up_code_sn', width: 80},
			{label: '商户订单编号', name: 'merchOrderSn', index: 'merch_order_sn', width: 80},
			{label: '取货码状态,0:未取,1:已取', name: 'pickUpCodeStatus', index: 'pick_up_code_status', width: 80},
			{label: '取货码生成时间', name: 'pickUpCodeCreatetime', index: 'pick_up_code_createtime', width: 80},
			{label: '取货码核销时间', name: 'pickUpCodeVerificationtime', index: 'pick_up_code_verificationtime', width: 80},
			{label: '创建时间', name: 'createTime', index: 'create_time', width: 80},
			{label: '修改人编号', name: 'moderSn', index: 'moder_sn', width: 80},
			{label: '修改时间', name: 'modTime', index: 'mod_time', width: 80},
			{label: '时间戳', name: 'tstm', index: 'tstm', width: 80}],
		viewrecords: true,
        height: 550,
        rowNum: 10,
        rowList: [10, 30, 50],
        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"});
        }
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		pickUpCode: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.pickUpCode = {};
		},
		update: function (event) {
            let orderSn = getSelectedRow();
			if (orderSn == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(orderSn)
		},
		saveOrUpdate: function (event) {
            let url = vm.pickUpCode.orderSn == null ? "../pickupcode/save" : "../pickupcode/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.pickUpCode),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
			});
		},
		del: function (event) {
            let orderSns = getSelectedRows();
			if (orderSns == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
				    url: "../pickupcode/delete",
				    contentType: "application/json",
				    data: JSON.stringify(orderSns),
				    success: function (r) {
						if (r.code == 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(orderSn){
			$.get("../pickupcode/info/"+orderSn, function (r) {
                vm.pickUpCode = r.pickUpCode;
            });
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});