$(function () {
    $("#jqGrid").jqGrid({
        url: '../mkdistchnl/list',
        datatype: "json",
        colModel: [
			{label: 'distChnlId', name: 'distChnlId', index: 'dist_chnl_id', key: true, hidden: true},
			{label: '分销渠道名称', name: 'distFlag', index: 'dist_flag', width: 80},
			// {label: '所属分类', name: 'partType', index: 'part_type', width: 80,
             //    formatter: function (value) {
             //        if (value == '00') {
             //            return '三方商户';
             //        } else if (value == '10') {
             //            return '供应商';
             //        } else if (value == '20') {
             //            return '三方商户及供货商';
             //        }
             //        return '';
             //    }},
            {label: '所属三方商户', name: 'thirdPartyMerchName', index: 'thirdPartyMerchName', width: 80},
            {label: '所属供应商', name: 'childSupplierName', index: 'childSupplierName', width: 80},
			{label: '排序', name: 'sortOrder', index: 'sort_order', width: 80},
			{label: '是否有效', name: 'isValid', index: 'is_valid', width: 80,
                formatter: function (value) {
                    if (value == '0') {
                        return '有效';
                    } else if (value == '1') {
                        return '无效';
                    }
                    return '';
                }},
			{label: '备注', name: 'note', index: 'note', width: 80},
            {label: '创建人编号', name: 'createrSn', index: 'creater_sn', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
            {label: '修改人编号', name: 'moderSn', index: 'moder_sn', width: 80},
            {label: '修改时间', name: 'modTime', index: 'mod_time', width: 80,
                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,
		mkDistChnl: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
            distFlag: ''
		},
        suppliers: [],
        thirdMerchantBizList: []
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
            vm.thirdMerchantBizList = [];
            vm.suppliers = [];
			vm.mkDistChnl = {isValid:0};
            vm.getThirdMerchantBizList();
		},
		update: function (event) {
            let distChnlId = getSelectedRow();
			if (distChnlId == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

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

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
				    url: "../mkdistchnl/delete",
				    contentType: "application/json",
				    data: JSON.stringify(distChnlIds),
				    success: function (r) {
						if (r.code == 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(distChnlId){
			$.get("../mkdistchnl/info/"+distChnlId, function (r) {
                vm.mkDistChnl = r.mkDistChnl;
            });
		},
        getThirdMerchantBizList: function() {
            $.get("../thirdmerchantbiz/queryAll", function (r) {
                vm.thirdMerchantBizList = r.list;
            });
        },
        showSupplier: function (thirdMerchSn) {
            $.get("../supplier/queryAll?thirdMerchSn=" + thirdMerchSn, function (r) {
                vm.suppliers = r.list;
            });
        },
        showStockShare:function(opt){
            var thirdMerchantCode = opt.value;
            vm.getSuppliers(thirdMerchantCode);
        },
        reloadSearch: function() {
            vm.q = {
                distFlag: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'distFlag': vm.q.distFlag},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});