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


var e = new common.edit('#brandWrap');
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) {
		$('#brandWrap').html(common.util.__template2($('#brandWrap').html(), {
			brandRelation: NETSALEDATA.brandRelationList.join('|')
		}));
	};
	e.init();
}, true);


var brandSelectArr = NETSALEDATA.brandRelationList;
$.each(NETSALEDATA.relationBrandNameList, function(i, value) {
	$('<div class="col-sm-2" data-id="' + NETSALEDATA.brandRelationList[i] + '">' + NETSALEDATA.relationBrandNameList[i] + '</div>').appendTo($('.selected-brand'));
});

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

	console.log($val, position);
	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.productSkn = $('#productSkn').val();
		option.data.brandRelationList = JSON.stringify(option.data.brandRelationList.split('|'));
		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;
		}
	});
});