<div class="breadcrumbs" id="breadcrumbs">
	<ul class="breadcrumb">
		<li>
			<i class="home-icon fa fa-home"></i>
			<a href="#">营销</a>
		</li>
		<li>
			<a href="#">营销活动管理</a>
		</li>
		<li class="active">特价活动</li>
	</ul>
</div>

<div class="content">
	<form action="{url:/market/sale_edit_act}"  method="post" name='sale_edit'>
		<input type='hidden' name='id' />
		<table class="table form-table">
			<colgroup>
				<col width="130px" />
				<col />
			</colgroup>

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

<!--特价单品模板-->
<script type="text/html" id="goodsItemTemplate">
<tr>
	<td><input type='hidden' name='goods_id[]' value='<%=templateData['goods_id']%>' /><%=templateData['name']%></td>
	<td><i class='operator fa fa-close' onclick="$(this).parent().parent().remove();"></i></td>
</tr>
</script>

<!--特价单品模板-->
<script type="text/html" id="goodsTemplate">
<button type='button' onclick='searchGoods({"type":"checkbox","callback":searchGoodsCallback});' class='btn btn-default'>选择商品</button>
<table class="table table-bordered list-table" style="width:80%;margin-top:10px;">
<thead><tr><td>特价商品名称</td><td>操作</td></tr></thead>
<tbody id="goodsListBox"></tbody>
</table>
</script>

<!--特价品牌模板-->
<script type="text/html" id="brandTemplate">
<select name="brand_id" class="form-control">
	<option value="0">请选择</option>
	{foreach:items=Api::run('getBrandListAllOnce')}
	<option value="{$item['id']}">{$item['name']}</option>
	{/foreach}
</select>
</script>

<!--特价分类模板-->
<script type="text/html" id="categoryTemplate">
<div id="__categoryBox" style="margin-bottom:8px"></div>
<button class="btn btn-default" 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);
		}
		break;

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

		case "4":
		{
			var templateHTML = template.render('brandTemplate');
			$('#conditionBox').html(templateHTML);
		}
		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']}
		{set:$categoryData = Api::run('getCategorySale',array('id'=>$categoryId))}
		_goodsCategoryWidget.createGoodsCategory({echo:JSON::encode($categoryData)});

	{elseif:$this->promotionRow['type'] == 3}
		{set:$goods_id = $this->promotionRow['condition']}
		{set:$goodsList = Api::run('getGoodSale',array('goods_id'=>$goods_id))}
		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>