$(function () {
    $("#jqGrid").jqGrid({
        url: '../mkstoreticketdiscountgoods/list',
        datatype: "json",
        colModel: [
			{label: 'discGoodId', name: 'discGoodId', index: 'disc_good_id', key: true, hidden: true},
			{label: '优惠券id', name: 'tickDiscId', index: 'tick_disc_id', width: 80},
			{label: '适用类型，【apply_type，00:全部商品参与，01:指定商品参与，02:指定商品不参与】', name: 'applyType', index: 'apply_type', width: 80},
			{label: '门店商品id', name: 'storeRelaId', index: 'store_rela_id', width: 80},
			{label: '是否有效,0:有效，1:无效', name: 'isValid', index: 'is_valid', width: 80},
			{label: '备注', name: 'note', index: 'note', width: 80},
			{label: '创建人编号', name: 'createrSn', index: 'creater_sn', 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,
		mkStoreTicketDiscountGoods: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.mkStoreTicketDiscountGoods = {};
		},
		update: function (event) {
            let discGoodId = getSelectedRow();
			if (discGoodId == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

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

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
				    url: "../mkstoreticketdiscountgoods/delete",
				    contentType: "application/json",
				    data: JSON.stringify(discGoodIds),
				    success: function (r) {
						if (r.code == 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(discGoodId){
			$.get("../mkstoreticketdiscountgoods/info/"+discGoodId, function (r) {
                vm.mkStoreTicketDiscountGoods = r.mkStoreTicketDiscountGoods;
            });
		},
        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);
        }
	}
});