$(function () {
	$('.has-many-spec .add.btn')
		.after('
 同步供应商最新规格
 批量设置库存
 批量设置市场价
 批量设置销售价
')
		.remove();
	//同步最新规格
	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 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));
		});
	});
});