brand-relation.js 2.17 KB
var $ = require('jquery'),
	common = require('../../common/common');


var e = new common.edit('#brandWrap'),
	brandSelectArr = [];
common.util.__ajax({
	url: '/goods/brands/queryBrandsByStatus'
}, function(res) {
	var data = res.data,
		map = {},
		firstName = '';

	$.each(data, function(i, item) {
		firstName = item.brandAlif.toUpperCase();
		if (/^[a-zA-Z]$/.test(firstName)) {
			if (!map.hasOwnProperty(firstName)) {
				map[firstName] = [];
			}
			map[firstName].push(item);
		} else {
			if (!map.hasOwnProperty('0-9')) {
				map['0-9'] = [];
			}
			map['0-9'].push(item);
		}
	});
	$('.brand-list').html(common.util.__template2($('#brandTemp').html(), {
		map: map
	}));

	if (NETSALEDATA.brandRelationList) {
		$('.selected-brand').html(common.util.__template2($('#brandShowTemp').html(), {
			brandRelationList: NETSALEDATA.brandRelationList,
			brandRelation: brandSelectArr.join('|')
		}));
	};
	e.init();
	if ($('.contentpanel').data('type') == 'info') {
		$('.brand-list').find('input').attr('disabled', true);
	}

}, true);

$.each(NETSALEDATA.brandRelationList, function(index, value) {
	brandSelectArr.push(value.brandId);
});

$('#brandWrap').on('change', 'input[name="brandRelationList"]', function() {
	var $val = parseInt($(this).val()),
		position = brandSelectArr.indexOf($val);

	if (position == -1) {
		brandSelectArr.push($val);
		$('<div class="col-sm-2" data-id="' + $val + '">' + $(this).data('name') + '</div>').appendTo($('.selected-brand'));
	} else {
		brandSelectArr.splice(position, 1);
		$('div[data-id="' + $val + '"]').remove();
	};

});

$('#saveBrandRelation').on('click', function() {
	e.submit('/goods/product/saveBrandRelation', function(option) {
		option.data = GOLABDATA.fire("LYbrandRelation");
		option.success = function(res) {
			res = res.data;
			if (res.code == '200') {
				e.$tip(res.message, function() {}, 'growl-success');
			} else {
				e.$tip(res.message);
			}
			return false;
		}
	});
});

GOLABDATA.on("LYbrandRelation", function() {
	return e.submit('', function(option) {
		option.data.productSkn = $('#productSkn').val();
		option.data.brandRelationList = JSON.stringify(option.data.brandRelationList.split('|'));
		option.debug = true;
	});
});