$(function () {
    $("#jqGrid").jqGrid({
        url: '../syscusunitcode/list',
        datatype: "json",
        colModel: [
			{label: 'sn', name: 'sn', index: 'sn', key: true, hidden: true},
			{label: '代码', name: 'code', index: 'code', width: 80},
			{label: '名称', name: 'name', index: 'name', width: 80},
            {
                label: '是否有效',
                name: 'isValid',
                align: 'center',
                index: 'is_valid',
                width: '50px',
                formatter: function (value) {
                    if(value == 0){
                        value = 1;
                    }else if(value == 1){
                        value = 0;
                    }
                    return transIsNot(value);
                }
            },
            {label: '备注', name: 'remark', index: 'remark', 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,
        sysCusUnitCode: {code:'',name:'',isValid:'',remark:''},
		ruleValidate: {
            code: [
                {required: true, message: '代码不能为空', trigger: 'blur'}
            ],
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.sysCusUnitCode = {};
		},
		update: function (event) {
            let sn = getSelectedRow();
			if (sn == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

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

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