index.js 7.33 KB
'use strict';
var $ = require('jquery'),
	common = require('../common/common');

var renderHd = false;
var g = new common.grid({
	el: "#basicTable",
	parms: function() {
		return {
			state: $('.state-select').find('.current').find('input').val()
		}
	},
	columns: [{
			display: '',
			name: 'foler',
			render: function(item) {
				var html = '';
				if (!renderHd) {
					html += $('#tableHdTemp').html();
					html += common.util.__template2($('#tableTemp').html(), item);
					renderHd = true;
				} else {
					html += common.util.__template2($('#tableTemp').html(), item);
				}

				return html;
			}
		}
	]
});

g.init($("#gridurl").val());

//编辑排序
$(document).on('change', '.saleCategoryNumInput', function() {
	if(checkNumInput($(this))){
		$(this).removeClass("errorNumInput");
		var categoryId = $(this).attr("categoryId");
		var orderBy = $(this).val();
		savePosition(categoryId,orderBy);
	}
});
//排序框校验
function checkNumInput(inputObj){
	if(inputObj.val()){
		if(!isNaN(inputObj.val())){
			if(inputObj.val() < 0){
				inputObj.focus().select().addClass("errorNumInput");
				common.util.__tip("排序数字请大于等于0!");
				return false;
			}
		}else{
			inputObj.focus().select().addClass("errorNumInput");
			common.util.__tip("排序必须为数字!");
			return false;
		}
	}else{
		inputObj.focus().addClass("errorNumInput");
		common.util.__tip("排序不能为空!");
		return false;
	}
	return true;
}
//排序实时保存
function savePosition(categoryId, orderBy){
	common.util.__ajax({
		url:'/sale/salesCategory/updateSC',
		data: {
			"categoryId": categoryId,
			"orderBy": orderBy
		}
	},function(rs){
		common.util.__tip("排序保存成功!","success");
	},true);
}

//打开层级类目
$(document).on('click', '.folder', function() {
	var $wrap = $(this).parent();

	if (!$(this).hasClass('open')) {
		if ($wrap.find('.level-2-wrap').size() > 0) {
			$wrap.find('.level-2-wrap').show();
			$(this).addClass('open');
		} else if ($wrap.find('.level-3-wrap').size() > 0) {
			$wrap.find('.level-3-wrap').show();
			$(this).addClass('open');
		}
	} else {
		$(this).removeClass('open');
		if ($wrap.find('.level-2-wrap').size() > 0) {
			$wrap.find('.level-2-wrap').hide();
		} else if ($wrap.find('.level-3-wrap').size() > 0) {
			$wrap.find('.level-3-wrap').hide();
		}
	}
});

var e = null,
	tabTree = null,
	tabTreeArr = [],
	editHtml = $('#editTemp').html();

function initForm(data) {
	common.dialog.open({
		title: '销售类目编辑',
		content: common.util.__template2(editHtml, data),
		width: 900
	});
	e = new common.edit('#basicForm', {
		bucket: 'taobaocms'
	});
	e.init();
}

//添加根分类
$('#add-root-category').on('click', function() {
	var data = {};
	data.action = '/sale/salesCategory/addSC';
	initForm(data);

	tabTreeArr=[];
	tabTree = new common.tabTree(".sortTree", {
		status: 1,
		sortLevel: 1
	});
	tabTree.init();
	tabTreeArr.push(tabTree);
	checkSortNum();
});

//添加子分类
$(document).on('click', '.add-sub-category', function() {
	var categoryId = $(this).parent().data('id');
	common.util.__ajax({
		url: '/sale/salesCategory/querySCById',
		data: {
			categoryId: categoryId
		}
	}, function(res) {
		var data = {};
		data.action = '/sale/salesCategory/addSC';
		data.parentSC = res.data.parentSC;
		data.subCategoryName = res.data.categoryName;
		data.parentId = res.data.categoryId;
		initForm(data);

		tabTreeArr = [];
		tabTree = new common.tabTree(".sortTree", {
			status: 1,
			sortLevel: 1
		});
		tabTree.init();
		tabTreeArr.push(tabTree);
		checkSortNum();
	}, true);
});

//编辑销售类目
$(document).on('click', '.edit-btn', function() {
	var categoryId = $(this).parent().data('id');
	common.util.__ajax({
		url: '/sale/salesCategory/querySCById',
		data: {
			categoryId: categoryId
		}
	}, function(res) {
		res.data.action = '/sale/salesCategory/updateSC';
		initForm(res.data);

		
		//渲染物理类目
		var selectedArr = [];
		$.each(res.data.relationProductSort, function(i, item) {
			var sort = [{id: item.id, sortName: item.sortName}];
			addSelected(sort, item);
			selectedArr.push(sort);

		});
		if (selectedArr.length > 0) {
			$('.sort-wrap').empty();
			tabTreeArr = [];
			$.each(selectedArr, function(i, item) {
				var $sortGroup = $($('#sortWrap').html());
				$sortGroup.appendTo($('.sort-wrap'));
				checkSortNum();
				tabTree = new common.tabTree(".sortTree:last", {
					status: 1,
					sortLevel: 1
				});
				tabTreeArr.push(tabTree);
				tabTree.init(item);
			});
		} else {
			tabTree = new common.tabTree(".sortTree", {
				status: 1,
				sortLevel: 1
			});
			tabTree.init();
			tabTreeArr.push(tabTree);
		}
	}, true);
});

function addSelected(arr, item) {
	if (item && item.parentPS) {
		arr.unshift({id: item.parentPS.id, sortName: item.parentPS.sortName});
		addSelected(arr, item.parentPS)
	}
}

function checkSortNum() {
	if ($('.sort-wrap').find('.row').size() > 1) {
		$('.sort-wrap').find('.row').find('.add-sort-btn').hide().end().find('.del-sort-btn').show();
		$('.sort-wrap').find('.row:last').find('.add-sort-btn').show().end().find('.del-sort-btn').show();
	} else {
		$('.sort-wrap').find('.row:first').find('.add-sort-btn').show().end().find('.del-sort-btn').hide();
	}
}

//添加物理类目
$(document).on('click', '.add-sort-btn',function() {
	var $sortGroup = $($('#sortWrap').html());
	$sortGroup.appendTo($('.sort-wrap'));
	tabTree = new common.tabTree(".sortTree:last", {
		status: 1,
		sortLevel: 1
	});
	tabTreeArr.push(tabTree);
	tabTree.init();
	checkSortNum();
});

//删除物理类目
$(document).on('click', '.del-sort-btn', function() {
	var sortIndex = $(this).parents('.row').index();
	$(this).parents('.row').remove();
	tabTreeArr.splice(sortIndex, 1);
	checkSortNum();
});

//保存销售类目
$(document).on('click', '#save_brand', function() {
	e.submit($('#basicForm').attr('action'), function(option) {
		var selected = [];
		$.each(tabTreeArr, function(i, item){
			if (item.selected.length > 0) {
				selected.push(item.selected[item.selected.length - 1].id);
			}
		});
		option.data.relationParameter = selected.join(',');
		option.success = function(res) {
			res = res.data;
			if (res.code == '200') {
				e.$tip(res.message, function() {
					//location.reload();
				}, 'growl-success');
			} else {
				e.$tip(res.message);
			}
			return false;
		}
	});
});

//开启销售类目
$(document).on('click', '.open-category', function() {
	var categoryId = $(this).parent().data('id'),
		categoryName = $(this).parent().data('name');

	common.dialog.confirm("温馨提示", "确定开启(" + categoryName + ")分类吗", function() {
		common.util.__ajax({
			url: '/sale/salesCategory/updateSC',
			data: {
				categoryId: categoryId,
				state: 1
			}
		}, function(res){
			if (res.code == 200) {
				g.reload();
			}
		})
	});
})

//关闭销售类目
$(document).on('click', '.close-category', function() {
	var categoryId = $(this).parent().data('id'),
		categoryName = $(this).parent().data('name');

	common.dialog.confirm("温馨提示", "确定关闭(" + categoryName + ")分类吗", function() {
		common.util.__ajax({
			url: '/sale/salesCategory/updateSC',
			data: {
				categoryId: categoryId,
				state: 0
			}
		}, function(res){
			if (res.code == 200) {
				g.reload();
			}
		})
	});
})

//切换状态重载列表
$('input[name="state"]').on('change', function() {
	$(this).parent().addClass('current').siblings().removeClass('current');
	g.reload();
});