<div class="headbar">
	<div class="position"><span>营销</span><span>></span><span>营销活动管理</span><span>></span><span>特价活动</span></div>
</div>
<div class="content_box">
	<div class="content form_content">
		<form action="{url:/market/sale_edit_act}"  method="post" name='sale_edit'>
			<input type='hidden' name='id' />
			<table class="form_table">
				<colgroup>
					<col width="150px" />
					<col />
				</colgroup>

				<tr>
					<th>特价活动名称：</th>
					<td><input type='text' class='normal' name='name' pattern='required' alt='特价活动名称' /><label>* 特价活动名称</label></td>
				</tr>
				<tr>
					<th>是否开启：</th>
					<td>
						<label class='attr'><input type='radio' name='is_close' value='0' checked=checked />是</label>
						<label class='attr'><input type='radio' name='is_close' value='1' />否</label>
					</td>
				</tr>
				<tr>
					<th>设置特价条件：</th>
					<td>
						<select name="type" pattern="required" onchange="createSale();">
							<option value="">请选择</option>
							<option value="2">商品分类特价</option>
							<option value="3">商品单品特价</option>
							<option value="4">商品品牌特价</option>
						</select>
						<label>* 选择特价商品的筛选条件，每件商品不能设置多个特价</label>
					</td>
				</tr>
				<tr>
					<th>筛选条件：</th>
					<td id="conditionBox">请设置特价条件...</td>
				</tr>
				<tr id="percentBox">
					<th>商品折扣百分比：</th>
					<td>
						<input type='text' class='small' name='award_value' pattern='percent' alt='商品特价折扣百分比，必须是 1-99 数字' /> %
						<label> * 商品原价的百分数，比如填写 <90%> 则商品会自动更新这个折扣价格</label>
					</td>
				</tr>
				<tr>
					<th>排序：</th>
					<td><input type='text' class='small' name='sort' pattern='int' empty alt='排序' /></td>
				</tr>
				<tr><td></td><td><button class="submit" type='submit'><span>确 定</span></button></td></tr>
			</table>
		</form>
	</div>
</div>

<!--特价单品模板-->
<script type="text/html" id="goodsItemTemplate">
<tr class="td_c">
	<td><input type='hidden' name='goods_id[]' value='<%=templateData['goods_id']%>' /><%=templateData['name']%></td>
	<td><input type='text' class='small' name='goods_price[<%=templateData['goods_id']%>]' value='<%=templateData['sell_price']%>' /></td>
	<td><img src="{skin:images/admin/icon_del.gif}" alt="删除" title="删除" onclick="$(this).parent().parent().remove();" /></td>
</tr>
</script>

<!--特价单品模板-->
<script type="text/html" id="goodsTemplate">
<button type='button' onclick="searchGoods('{url:/block/search_goods/type/checkbox}',searchGoodsCallback);" class='btn'><span>选择商品</span></button>
<table class='border_table' style='width:60%'>
<thead><tr><th>特价商品名称</th><th>特价设置</th><th>操作</th></tr></thead>
<tbody id="goodsListBox"></tbody>
</table>
</script>

<!--特价品牌模板-->
<script type="text/html" id="brandTemplate">
<select name="brand_id">
	<option value="0">请选择</option>
	{query:name=brand}
	<option value="{$item['id']}">{$item['name']}</option>
	{/query}
</select>
</script>

<!--特价分类模板-->
<script type="text/html" id="categoryTemplate">
<div id="__categoryBox" style="margin-bottom:8px"></div>
<button class="btn" type="button" name="_goodsCategoryButton"><span class="add">设置分类</span></button>
</script>
{set:plugin::trigger('goodsCategoryWidget',array("type" => "checkbox","name" => "category[]"))}

<script type='text/javascript'>
//商品单品特价回调
function searchGoodsCallback(goodsList)
{
	goodsList.each(function()
	{
		var temp         = $.parseJSON($(this).attr('data'));
		var templateHTML = template.render('goodsItemTemplate',{"templateData":temp});
		$('#goodsListBox').append(templateHTML);
	});
}

//创建特价条件
function createSale()
{
	var typeVal = $('select[name="type"]').val();
	switch(typeVal)
	{
		case "2":
		{
			var templateHTML = template.render('categoryTemplate');
			$('#conditionBox').html(templateHTML);
			$('#percentBox').show();
		}
		break;

		case "3":
		{
			var templateHTML = template.render('goodsTemplate');
			$('#conditionBox').html(templateHTML);
			$('#percentBox').hide();
		}
		break;

		case "4":
		{
			var templateHTML = template.render('brandTemplate');
			$('#conditionBox').html(templateHTML);
			$('#percentBox').show();
		}
		break;

		default:
		{
			alert("请选择正确筛选条件");
		}
	}
}

{if:$this->promotionRow}
jQuery(function()
{
	//表单回填
	var formObj = new Form('sale_edit');
	formObj.init({echo:JSON::encode($this->promotionRow)});

	//产生特价条件
	createSale();

	//复写特价条件数据
	{if:$this->promotionRow['type'] == 2}
		{set:$categoryId = $this->promotionRow['condition']}
		{query:name=category items=$categoryData where=id in ($categoryId)}{/query}
		createGoodsCategory({echo:JSON::encode($categoryData)});

	{elseif:$this->promotionRow['type'] == 3}
		{set:$goodsId = $this->promotionRow['condition']}
		{set:$goodsDiscount = JSON::decode($this->promotionRow['intro'])}
		{query:name=goods items=$goodsList where=id in ($goodsId) fields=id as goods_id,name,sell_price}
		{if:$this->promotionRow['is_close'] == 1}
			{set:$goodsList[$key]['sell_price'] -= $goodsDiscount[$item['goods_id']]}
		{/if}
		{/query}
		var goodsList = {echo:JSON::encode($goodsList)};
		for(var i in goodsList)
		{
			var temp = goodsList[i];
			var templateHTML = template.render('goodsItemTemplate',{"templateData":temp});
			$('#goodsListBox').append(templateHTML);
		}

	{elseif:$this->promotionRow['type'] == 4}
		{set:$brandId = $this->promotionRow['condition']}
		$("[name='brand_id']").val({$brandId});
	{/if}
});
{/if}
</script>