$(function () {
    $("#jqGrid").jqGrid({
        url: '../thirdmerchexpress/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '商户快递账号', name: 'expressAccount', index: 'express_account', width: 80},
			{label: '商户快递密码', name: 'expressPwd', index: 'express_pwd', width: 80},
			{label: '商户快递月结账号', name: 'expressCusId', index: 'express_cus_id', width: 80},
			{label: '第三方商户代码', name: 'thirdPartyMerchName', index: 'third_party_merch_code', width: 80},
			{label: '备注', name: 'remark', index: 'remark', width: 80},
			{label: '创建人编号', name: 'createrSn', index: 'creater_sn', width: 80},
			{label: '创建时间', name: 'createTime', index: 'create_time', width: 80, formatter: function (value) {
                return transDate(value,'yyyy-MM-dd hh:mm:ss');
            }},
			{label: '修改人编号', name: 'moderSn', index: 'moder_sn', width: 80},
			{label: '修改时间', name: 'modTime', index: 'mod_time', width: 80, formatter: function (value) {
                return transDate(value,'yyyy-MM-dd hh:mm:ss');
            }}],
		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,
		thirdMerchExpress: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
            thirdPartyMerchCode: ''
		},
        thirdMerchantBizList: []
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.getThirdMerchantBizList();
			vm.thirdMerchExpress = {};
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

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

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