$(function () {
    $("#jqGrid").jqGrid({
        url: '../merch/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '商户编号', name: 'merchSn', index: 'merch_sn', width: 80},
			{label: '商户名称', name: 'merchName', index: 'merch_name', width: 80},
			{label: '商户简称', name: 'merchShortName', index: 'merch_short_name', width: 80},
			{label: '排序', name: 'sortOrder', index: 'sort_order', width: 80},
            {
                label: '显示',
                name: 'isShow',
                align: 'center',
                index: 'is_show',
                width: '50px',
                formatter: function (value) {
                    return transIsNot(value);
                }
            },
			{label: '备注', name: 'remark', index: 'remark', width: 80}],
		viewrecords: true,
        height: 385,
        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,
		merch: {merchSn:'',merchName:'',merchShortName:'',sortOrder:'',isShow:1},
		ruleValidate: {
            merchSn: [
				{required: true, message: '商户编号不能为空', trigger: 'blur'}
			],
            merchName: [
                {required: true, message: '商户名称不能为空', trigger: 'blur'}
            ],
            merchShortName: [
                {required: true, message: '商户简称不能为空', trigger: 'blur'}
            ]
		},
		q: {
            merchName: '',
            merchSn: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.merch = {isShow:1};
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.merch.id == null ? "../merch/save" : "../merch/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.merch),
                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: "../merch/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("../merch/info/"+id, function (r) {
                vm.merch = r.merch;
            });
		},
        reloadSearch: function() {
            vm.q = {
                merchName: '',
                merchSn:''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'merchName': vm.q.merchName,'merchSn': vm.q.merchSn},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});