$(function () {
    $("#jqGrid").jqGrid({
        url: '../mkcentermenu/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '菜单名称', name: 'menuName', index: 'menu_name', width: 80, align: 'center'},
			{label: '菜单描述', name: 'menuDetail', index: 'menu_detail', width: 80, align: 'center'},
			{label: '菜单图片链接', name: 'picUrl', index: 'pic_url', width: 80, align: 'center'},
			{label: '菜单跳转url', name: 'menuUrl', index: 'menu_url', width: 80, align: 'center'},
            {label: '所属三方商户', name: 'thirdPartyMerchName', index: 'thirdPartyMerchName', width: 80, align: 'center'},
            {label: '所属门店', name: 'storeName', index: 'storeName', width: 80, align: 'center'},
			{label: '是否有效', name: 'isValid', index: 'is_valid', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '有效';
                    } else if (value == '1') {
                        return '无效';
                    }
                    return '';
                }},
            {label: '备注', name: 'note', index: 'note', width: 60, align: 'center'},
            {label: '创建人编号', name: 'createrSn', index: 'creater_sn', width: 70, align: 'center'},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 110, align: 'center',
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
            {label: '修改人编号', name: 'moderSn', index: 'moder_sn', width: 70, align: 'center'},
            {label: '修改时间', name: 'modTime', index: 'mod_time', width: 110, align: 'center',
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }}],
		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,
		mkCenterMenu: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    menuName: ''
		},
        thirdMerchantBizList: [],
        storeList:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.thirdMerchantBizList = [];
			vm.storeList = [];
			vm.mkCenterMenu = {isValid: 0};
			vm.getThirdMerchantBizList();
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.mkCenterMenu.id == null ? "../mkcentermenu/save" : "../mkcentermenu/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.mkCenterMenu),
                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: "../mkcentermenu/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);
						}
					}
				});
			});
		},
        getThirdMerchantBizList: function() {
            $.get("../thirdmerchantbiz/queryAll", function (r) {
                vm.thirdMerchantBizList = r.list;
            });
        },
        getStoreList: function(thirdMerchSn){
            $.get("../store/queryAll?thirdMerchSn=" + thirdMerchSn, function (r) {
                vm.storeList = r.list;
            });
        },
        changeStore: function (thirdMerchSn) {
            vm.getStoreList(thirdMerchSn);
        },
		getInfo: function(id){
			$.get("../mkcentermenu/info/"+id, function (r) {
                vm.mkCenterMenu = r.mkCenterMenu;
            });
		},
        reloadSearch: function() {
            vm.q = {
                menuName: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'menuName': vm.q.menuName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});