$(function () {
    $("#jqGrid").jqGrid({
        url: '../thirdmerchantbiz/list',
        datatype: "json",
        colModel: [
			{label: 'thirdMerchSn', name: 'thirdMerchSn', index: 'third_merch_sn', key: true, hidden: true},
            {label: '商户编号', name: 'merchSn', index: 'merch_sn', width: 80, align: 'center'},
			{label: '第三方商户代码', name: 'thirdPartyMerchCode', index: 'third_party_merch_code', width: 80, align: 'center'},
			{label: '第三方商户名称', name: 'thirdPartyMerchName', index: 'third_party_merch_name', width: 80, align: 'center'},
            {label: '库存是否共享', name: 'isStockShare', index: 'isStockShare', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '禁用';
                    } else if (value == '1') {
                        return '启用';
                    }
                    return '';
                }},
            // {label: '门店用户是否共享', name: 'isStoreUserShare', index: 'isStoreUserShare', width: 80, align: 'center',
            //     formatter: function (value) {
            //         if (value == '0') {
            //             return '否';
            //         } else if (value == '1') {
            //             return '是';
            //         }
            //         return '';
            //     }},
            {label: '商户地图是否显示', name: 'isMapShow', index: 'isMapShow', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '禁用';
                    } else if (value == '1') {
                        return '启用';
                    }
                    return '';
                }},
            {label: '渠道分销是否启用', name: 'isDistSellStart', index: 'isDistSellStart', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '禁用';
                    } else if (value == '1') {
                        return '启用';
                    }
                    return '';
                }},
            {label: '供应商模式商品是否启用', name: 'isSupplierGoods', index: 'isSupplierGoods', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '禁用';
                    } else if (value == '1') {
                        return '启用';
                    }
                    return '';
                }},
            {label: '是否使用人脸核验', name: 'isFaceCheck', index: 'isFaceCheck', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '禁用';
                    } else if (value == '1') {
                        return '启用';
                    }
                    return '';
                }},
			{label: '是否有效', name: 'isValid', index: 'is_valid', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == '0') {
                        return '有效';
                    } else if (value == '1') {
                        return '无效';
                    }
                    return '';
                }}],
		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,
		thirdMerchantBiz: {isValid: '', isStockShare:'',isStoreUserShare:'',isMapShow:'',isDistSellStart:'',isSupplierGoods:'',isFaceCheck:''},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		},
        merchList: [],
        isOperate: false
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
            vm.getMerchList();
			vm.thirdMerchantBiz = {isValid: 0,isStockShare:0,isStoreUserShare:0,isMapShow:0,isDistSellStart:0,isFaceCheck:0};
			vm.isOperate = true;
		},
		update: function (event) {
            let thirdMerchSn = getSelectedRow();
			if (thirdMerchSn == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.isOperate = false;
            vm.getMerchList();

            vm.getInfo(thirdMerchSn)
		},
		saveOrUpdate: function (event) {
            let url = vm.thirdMerchantBiz.thirdMerchSn == null ? "../thirdmerchantbiz/save" : "../thirdmerchantbiz/update";

            // confirm('库存是否共享，商户地图是否显示字段添加成功后则不可修改！请确认后添加？', function () {
            confirm('该信息需与商户确认后再行修改，是否确认修改？', function () {
                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.thirdMerchantBiz),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            })
		},
		del: function (event) {
            let thirdMerchSns = getSelectedRows();
			if (thirdMerchSns == null){
				return;
			}

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