$(function () {
    $("#jqGrid").jqGrid({
        url: '../mkdistsellstatis/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '分销渠道名称', name: 'distFlag', index: 'dist_flag', width: 80, align: 'center'},
            {label: '所属三方商户', name: 'thirdPartyMerchName', index: 'thirdPartyMerchName', width: 70, align: 'center'},
            {label: '所属供应商', name: 'childSupplierName', index: 'childSupplierName', width: 70, align: 'center'},
            {label: '商品', name: 'goodsName', index: 'goodsName', width: 80, align: 'center'},
            {label: '商品所属分销商', name: 'goodsDistName', index: 'distName', width: 70, align: 'center'},
			{label: '商品价格', name: 'goodsPrice', index: 'goods_price', width: 50, align: 'center'},
			{label: '分配方式', name: 'allocType', index: 'alloc_type', width: 50, align: 'center',
                formatter: function (value) {
                    if (value == '00') {
                        return '比例';
                    } else if (value == '10') {
                        return '金额';
                    }
                    return '';
                }},
            {label: '子级分销商id规则', name: 'expendDistIds', index: 'expendDistIds', width: 80, align: 'center'},
            {label: '子级分销额度分配规则', name: 'expendDistLines', index: 'expendDistLines', width: 80, align: 'center'},
			{label: '子级分销商', name: 'expendDistName', index: 'expend_dist_id', width: 80, align: 'center'},
			{label: '子级分销商额度【点数/金额】', name: 'expendDistPrice', index: 'expend_dist_price', width: 100, align: 'center'},
			{label: '子级分销商等级', name: 'expendDistLevel', index: 'expend_dist_level', width: 70, align: 'center',
                formatter: function (value) {
                    if (value == '1') {
                        return '一级';
                    } else if (value == '2') {
                        return '二级';
                    } else if (value == '3') {
                        return '三级';
                    }
                    return '';
                }},
            {label: '订单编号', name: 'orderSn', index: 'orderSn', width: 100, align: 'center'},
            // {label: '订单用户', name: 'userName', index: 'userName', width: 70},
			// {label: '来源', name: 'expendSource', index: 'expend_source', width: 80},
			{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: 'note', index: 'note', width: 60},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 110, align: 'center',
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }},
            {label: '修改时间', name: 'modTime', index: 'mod_time', width: 110, 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,
		mkDistSellStatis: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
        q: {
            distChnlId: '',
            storeId:'',
            distId:'',
            goodsId:'',
            startTime:'',
            endTime: ''
        },
        queryGoodsList:[],
        queryStoreList:[],
        queryMkDistList:[],
        queryMkDistChnlList:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.mkDistSellStatis = {};
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.mkDistSellStatis.id == null ? "../mkdistsellstatis/save" : "../mkdistsellstatis/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.mkDistSellStatis),
                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: "../mkdistsellstatis/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("../mkdistsellstatis/info/"+id, function (r) {
                vm.mkDistSellStatis = r.mkDistSellStatis;
            });
		},
        queryChangeGoodsByStore: function (storeId) {
            if(storeId){
                $.get("../productstorerela/queryAll?storeId=" + storeId, function (r) {
                    vm.queryGoodsList = r.list;
                });
            }
        },
        queryChangeByDistChnlId: function (distChnlId) {
            $.get("../mkdistchnl/info/"+distChnlId, function (r) {
                var thirdMerchSn = r.mkDistChnl.thirdMerchSn;
                $.get("../store/queryAll?thirdMerchSn="+thirdMerchSn, function (r) {
                    vm.queryStoreList = r.list;
                });
                $.get("../mkdist/queryAll?thirdMerchSn="+thirdMerchSn, function (r) {
                    vm.queryMkDistList = r.list;
                });
            });
        },
        exportDistSellStatis: function () {
            var params = {};
            params.distChnlId = vm.q.distChnlId, params.storeId = vm.q.storeId, params.distId = vm.q.distId
                , params.startTime = vm.q.startTime, params.endTime = vm.q.endTime, params.goodsId = vm.q.goodsId;

            exportFile('#rrapp', '../mkdistsellstatis/export', params);
        },
        reloadSearch: function() {
            vm.q = {
                distChnlId: '',
                storeId:'',
                distId:'',
                goodsId:'',
                startTime: '',
                endTime: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'distChnlId': vm.q.distChnlId,'storeId': vm.q.storeId,'distId': vm.q.distId,'goodsId': vm.q.goodsId,'startTime':vm.q.startTime,'endTime':vm.q.endTime},
                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("../mkdistchnl/queryAll", function (r) {
            vm.queryMkDistChnlList = r.list;
        });
    }
});