$(function () {
    $("#jqGrid").jqGrid({
        url: '../mkdisthier/list',
        datatype: "json",
        colModel: [
			{label: 'distHierId', name: 'distHierId', index: 'dist_hier_id', key: true, hidden: true},
			{label: '分销渠道id', name: 'distChnlId', index: 'dist_chnl_id', width: 80, hidden: true},
			{label: '上级分销商id', name: 'pdistId', index: 'pdist_id', width: 80, hidden: true},
			{label: '分销商id', name: 'distId', index: 'dist_id', width: 80, hidden: true},
            {label: '所属供应商', name: 'supplierId', index: 'supplier_id', width: 80, hidden: true},
            {label: '层级级别', name: 'hierLvl', index: 'hier_lvl', width: 50, align: 'center'},
            {label: '上级分销商', name: 'pdDistName', index: 'pdDistName', width: 80},
            {label: '下级分销商', name: 'distName', index: 'distName', width: 80},
            {label: '分销渠道名称', name: 'distFlag', index: 'distFlag', width: 80},
			{label: '分销层级名称', name: 'distHierName', index: 'dist_hier_name', width: 140},
			{label: '分销层级昵称', name: 'distHierNname', index: 'dist_hier_nname', width: 80},
            {label: '所属供应商', name: 'childSupplierName', index: 'childSupplierName', width: 80},
			{label: '是否有效', name: 'isValid', index: 'is_valid', width: 60,
                formatter: function (value) {
                    if (value == '0') {
                        return '有效';
                    } else if (value == '1') {
                        return '无效';
                    }
                    return '';
                }},
            {label: '排序', name: 'sortOrder', index: 'sort_order', width: 80},
            {label: '创建人编号', name: 'createrSn', index: 'creater_sn', width: 60},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 100,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
            {label: '修改人编号', name: 'moderSn', index: 'moder_sn', width: 60},
            {label: '修改时间', name: 'modTime', index: 'mod_time', width: 100,
                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,
		mkDistHier: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
            distHierName: '',
            distId:''
		},
        suppliers:[],
		mkDistList:[],
		mkDistChnlList:[],
        mkChildDistList:[],
        mkUpDistList:[],
        lvlEntitieyList: [],
        isOperatorShow: false,
        hierLvl: '',
        queryMkDistList:[]
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.suppliers = [];
            vm.mkDistList = [];
            vm.mkDistChnlList = [];
			vm.mkDistHier = {isValid:0};
            vm.isOperatorShow = false;
			// vm.showSupplier();
			// vm.getMkDistList();
			vm.getMkDistChnlList();
			vm.getLvlList();
		},
		update: function (event) {
            let distHierId = getSelectedRow();
			if (distHierId == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.isOperatorShow = true;

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

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
				    url: "../mkdisthier/delete",
				    contentType: "application/json",
				    data: JSON.stringify(distHierIds),
				    success: function (r) {
						if (r.code == 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(distHierId){
			$.get("../mkdisthier/info/"+distHierId, function (r) {
                vm.mkDistHier = r.mkDistHier;
                vm.upDist(vm.mkDistHier.pdistId);
            });
		},
        showSupplier: function (supplierId) {
            $.get("../supplier/queryAll?supplierId="+supplierId, function (r) {
                vm.suppliers = r.list;
            });
        },
        // changeLvl: function (opt) {
        //     var distId = opt.value;
        //     if(distId){
        //         $.get("../mkdisthier/queryNextLvlByDistId?distId="+distId+"&isOperatorShow="+vm.isOperatorShow, function (r) {
        //             console.log(r.lvlEntitieyList)
        //             if(r.lvlEntitieyList){
        //                 vm.lvlEntitieyList = r.lvlEntitieyList;
        //             }else{
        //                 vm.getLvlList();
        //             }
        //             console.log(vm.lvlEntitieyList)
        //         });
        //     }
        // },
        getLvlList: function () {
            $.get("../mkdisthier/queryAllConfig", function (r) {
                vm.lvlEntitieyList = r.lvlEntitieyList;
            });
        },
        changeDist: function (option) {
		    var distChnlId = option.value;
		    if(distChnlId){
                $.get("../mkdistchnl/info/"+distChnlId, function (r) {
                    var thirdMerchSn = r.mkDistChnl.thirdMerchSn;
                    var supplierId = r.mkDistChnl.supplierId;
                    vm.getMkDistList(thirdMerchSn,supplierId);
                    vm.showSupplier(supplierId);
                });
            }
        },
        getMkDistList: function (thirdMerchSn,supplierId) {
            $.get("../mkdist/queryAll?thirdMerchSn="+thirdMerchSn+"&supplierId="+supplierId, function (r) {
                vm.mkDistList = r.list;
            });
        },
        upDist: function (distId) {
		    if(distId){
                $.get("../mkdist/queryAllById?distId="+distId, function (r) {
                    vm.mkUpDistList = r.list;
                });
            }
        },
        changeChildDist: function (option) {
		    var distId = option.value;
            $.get("../mkdist/queryChildAll?childDistId="+distId, function (r) {
                vm.mkChildDistList = r.list;
            });
        },
        getMkDistChnlList: function () {
            $.get("../mkdistchnl/queryAll", function (r) {
                vm.mkDistChnlList = r.list;
            });
        },
        reloadSearch: function() {
            vm.q = {
                distHierName: '',
                distId:''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'distHierName': vm.q.distHierName,'distId':vm.q.distId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	},
    mounted() {
        $.get("../mkdist/queryAllByHier", function (r) {
            vm.queryMkDistList = r.list;
        });
    }
});