$(function () {
    $("#jqGrid").jqGrid({
        url: '../mngchange/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '变动类型', name: 'changeType', index: 'change_type', width: 70, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '还原';
                    } else if (value == '1') {
                        return '扣减';
                    } else if (value == '2') {
                        return '新增调整';
                    } else if (value == '3') {
                        return '更新库存增加';
                    } else if (value == '4') {
                        return '更新库存减少';
                    }
                    return '-';
                }
            },
            {label: '变动原因', name: 'changeReason', index: 'change_reason', width: 90, align: 'center'},
			{label: '商品库存变化数', name: 'changeNum', index: 'change_num', width: 70, align: 'center'},
			{label: '商品原库存数', name: 'originalNum', index: 'original_num', width: 65, align: 'center'},
			{label: '商品库存可用数', name: 'validNum', index: 'valid_num', width: 70, align: 'center'},
			{label: '所属商户', name: 'merchName', index: 'merchName', width: 70, align: 'center'},
			{label: '第三方商户代码', name: 'thirdPartyMerchCode', index: 'third_party_merch_code', width: 70, align: 'center'},
            {label: '第三方商户名称', name: 'thirdPartyMerchName', index: 'thirdPartyMerchName', width: 70, align: 'center'},
            {label: '所属商品', name: 'goodsName', index: 'goodsName', width: 140, align: 'center'},
            // {label: '是否有效', name: 'isValid', index: 'is_valid', width: 50, align: 'center',
            //     formatter: function (value) {
            //         if (value == '0') {
            //             return '有效';
            //         } else if (value == '1') {
            //             return '无效';
            //         }
            //         return '-';
            //     }
            // },
			{label: '创建人', name: 'createrSn', index: 'creater_sn', width: 75, align: 'center'},
			{label: '创建时间', name: 'createTime', index: 'create_time', width: 90, align: 'center', formatter: function (value) {
                return transDate(value,'yyyy-MM-dd hh:mm:ss');
            }},
			{label: '修改人', name: 'moderSn', index: 'moder_sn', width: 75, align: 'center'},
			{label: '修改时间', name: 'modTime', index: 'mod_time', width: 90, 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,
		mngChange: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
            goodsId: '',
            changeType: ''
		},
        goodss: [],
        macros: []
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.mngChange = {};
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.mngChange.id == null ? "../mngchange/save" : "../mngchange/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.mngChange),
                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: "../mngchange/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("../mngchange/info/"+id, function (r) {
                vm.mngChange = r.mngChange;
            });
		},
        reloadSearch: function() {
            vm.q = {
                goodsId: '',
                changeType: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'goodsId': vm.q.goodsId,'changeType': vm.q.changeType},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	},
    mounted() {
        // this.uploadList = this.$refs.upload.fileList;
        /*$.get("../category/getCategorySelect", function (r) {
            vm.queryCategories = r.list;
        });*/
        $.get("../goods/queryAll", function (r) {
            vm.goodss = r.list;
        });

        $.get("../sys/macro/queryMacrosByValue?value=changeType", function (r) {
            vm.macros = r.list;
        });

    }
});