$(function () {
    $("#jqGrid").jqGrid({
        url: '../storemngchange/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '变动类型', name: 'changeType', index: 'change_type', width: 60,
                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 '-';
                }, align: 'center'
            },
            {label: '变动原因', name: 'changeReason', index: 'change_reason', width: 90, align: 'center'},
			{label: '门店库存变化数', name: 'storeChangeNum', index: 'store_change_num', width: 70, align: 'center'},
			{label: '门店原库存数', name: 'storeOriginalNum', index: 'store_original_num', width: 65, align: 'center'},
			{label: '门店库存可用数', name: 'storeValidNum', index: 'store_valid_num', width: 70, align: 'center'},
			{label: '所属商户', name: 'merchName', index: 'merchName', width: 70, align: 'center'},
			{label: '所属门店', name: 'storeName', index: 'storeName', width: 80, align: 'center'},
			{label: '所属商品', name: 'goodsName', index: 'goodsName', width: 140, align: 'center'},
            // {label: '是否有效', name: 'isValid', index: 'is_valid', width: 60, 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: 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,
		storeMngChange: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
            goodsId: '',
            changeType: '',
            storeId: ''
		},
        goodss: [],
        stores: [],
        macros: []
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.storeMngChange = {};
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.storeMngChange.id == null ? "../storemngchange/save" : "../storemngchange/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.storeMngChange),
                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: "../storemngchange/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("../storemngchange/info/"+id, function (r) {
                vm.storeMngChange = r.storeMngChange;
            });
		},
        changeGoods:function (opt) {
		    var storeId = opt.value;
		    if(storeId){
                $.get("../productstorerela/queryAll?storeId=" + storeId, function (r) {
                    vm.goodss = r.list;
                });
            }
        },
        reloadSearch: function() {
            vm.q = {
                goodsId: '',
                changeType: '',
                storeId: ''
            }
            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,'storeId':vm.q.storeId},
                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("../store/queryAll", function (r) {
            vm.stores = r.list;
        });

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

    }
});