| 
						 | 
						$(function () {	$('.has-many-spec .add.btn').parent().attr('class', 'col-md-12')		.after(`
			<div class="btn btn-spec spec-sync btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i> 同步供应商最新规格</div>			<div class="btn btn-spec batch-stock btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i> 批量设置库存</div>			<div class="btn btn-spec batch-original-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i> 批量设置市场价</div>			<div class="btn btn-spec batch-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i> 批量设置销售价</div>		`);
	$('.has-many-spec_self .add.btn').parent().attr('class', 'col-md-12')		.after(`
			<div class="btn btn-spec batch-add btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i> 批量新增</div>			<div class="btn btn-spec batch-delete btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-minus"></i> 删除所有</div>		`);
	//切换类型
	$('input[name="type"]').change(function () {		if ($(this).val() == 3) {			$('#btn-group1').hide();			$('#btn-group2').show();		} else {			$('#btn-group1').show();			$('#btn-group2').hide();		}		$('.has-many-spec-forms').html('');		$('.has-many-spec_self-form').html('');	}).change();
	//批量新增
	$('.has-many-spec_self .batch-add').click(function () {		var name = prompt('请输入规格名称');		if (!name) {			return;		}
		var date = new Date();		date = prompt('请输入起始日期', date.format('yyyy-MM-dd'));		if (!date || isNaN(new Date(date).getTime())) {			return;		}
		var num = parseInt(prompt('请输入新增数量,最多不超过180', '30'));		if (!num || isNaN(num)) {			return;		}		if (num > 180) {			num = 180;		}
		var stock = parseInt(prompt('请输入默认库存', '9999'));		if (!stock || isNaN(stock)) {			return;		}
		var original_price = parseFloat(prompt('请输入默认市场价'));		if (!original_price || isNaN(original_price)) {			return;		}
		var price = parseFloat(prompt('请输入默认销售价'));		if (!price || isNaN(price)) {			return;		}
		$('template.spec-tpl .field_name').prop('value', name);		var html = $('template.spec_self-tpl').html();
		var fields = {			'name': name,			'date': date,			'stock': stock,			'original_price': original_price,			'price': price,		};		for (var key in fields) {			html = html.replace('name="spec_self[new___LA_KEY__][' + key + ']" value=""', 'name="spec_self[new___LA_KEY__][' + key + ']" value="' + fields[key] + '"');		}
		var nestedIndex = $('.has-many-spec-forms tr').length - 1;
		function replaceNestedFormIndex(value) {			return String(value).replace(/__LA_KEY__/g, nestedIndex);		}
		for (var i = 0; i < num; i++) {			nestedIndex++;
			var d = new Date(date);			d.setDate(d.getDate() + i);
			var date_template = replaceNestedFormIndex(html.replace(date, d.format('yyyy-MM-dd')));			$('.has-many-spec_self-forms').append(date_template);		}	});
	//删除所有
	$('.has-many-spec_self .batch-delete').click(function () {		$('.has-many-spec_self-forms .remove.btn').click();	});
	//同步最新规格
	var template = $('template.spec-tpl').html();	var is_load = false;	$('.spec-sync').click(function () {		if (is_load) {			Dcat.confirm('本次载入将清除上次载入的所有数据,是否继续?', null, function () {				Dcat.loading();				post();			});		} else {			Dcat.loading();			post();			is_load = true;		}		function post() {			$.ajax({				url: '`{{url}}`',				method: 'POST',				data: {					product_id: $('input[name="product_id"]').val(),					agent_product_id: `{{agent_product_id}}`,					_form_: '`{{class}}`',				},				success: function (res) {					var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', 'supplier_cost_price',						'price', 'stock', 'original_price', 'product_spec_id'];					var data = res.data;					var forms = $('.has-many-spec-forms');
					//只remove掉上次载入的,原来数据库保存的记录(.field_id不为空)不处理
					forms.children().each(function () {						if (!$(this).find('.field_id').val()) {							$(this).remove();						}						//清理供应商已删除的规格
						else if ($(this).find('.field_supplier_stock').val() == 0 && $(this).find('.field_supplier_price').val() == 0) {							$(this).find('.remove.btn').click();						}					});
					for (var key in data) {						var row = $(template.replace(/new___LA_KEY__/g, key));						for (var i=0; i<fields.length; i++) {							var field = fields[i];							if (['original_price', 'price', 'stock'].indexOf(field) !== -1) {								var value = data[key]['supplier_' + field], rule = '';								//不处理市场价
								if (field === 'original_price') {									rule = 'if(!this.value || isNaN(this.value) || parseFloat(this.value)<' + value + ') this.value=' + value;								}								row.find('input.field_' + field).val(value).attr({onblur: rule});							} else {								row.find('input.field_' + field).val(data[key][field]);							}						}						forms.append(row);					}					Dcat.loading(false);				},				error: function () {					Dcat.loading(false);					Dcat.error('服务器出现未知错误,获取供应商产品规格失败');				}			});		}	});
	//批量库存
	$('.batch-stock').click(function () {		var num = prompt('请输入你的库存(注:库存不能大于供应商库存)');		if (!/^\d+$/.test(num)) {			return;		}
		$('.has-many-spec input.field_stock').each(function () {			var value = $(this).parents('tr').find('.field_supplier_stock').val();			var result = parseFloat(num) > parseFloat(value) ? value : num;			$(this).val(result);		});	});
	//批量设置市场价
	$('.batch-original-price').click(function () {		var num = prompt('请输入加价金额(在供应商销售价基础上加)');		if (!/^\d+$/.test(num)) {			return;		}
		$('.has-many-spec input.field_original_price').each(function () {			var value = $(this).parents('tr').find('.field_supplier_price').val();			var result = parseFloat(value) + parseFloat(num);			$(this).val(result.toFixed(2));		});	});
	//批量销售价
	$('.batch-price').click(function () {		var num = prompt('请输入加价金额(在供应商销售价基础上加)');		if (!/^\d+$/.test(num)) {			return;		}
		$('.has-many-spec input.field_price').each(function () {			var value = $(this).parents('tr').find('.field_supplier_price').val();			var result = parseFloat(value) + parseFloat(num);			$(this).val(result.toFixed(2));		});	});});
Date.prototype.format = function (fmt) {	var o = {		'M+': this.getMonth() + 1,                 //月份
		'd+': this.getDate(),                    //日
		'h+': this.getHours(),                   //小时
		'm+': this.getMinutes(),                 //分
		's+': this.getSeconds(),                 //秒
		'q+': Math.floor((this.getMonth() + 3) / 3), //季度
		'S': this.getMilliseconds()             //毫秒
	};	if (/(y+)/.test(fmt)) {		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));	}	for (var k in o) {		if (new RegExp('(' + k + ')').test(fmt)) {			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));		}	}	return fmt;};
  |