{js:kindeditor}
{js:my97date}
{js:artTemplate}
{js:fileUpload}

<div class="headbar clearfix">
	<div class="position"><span>商品</span><span>></span><span>商品管理</span><span>></span><span>商品编辑</span></div>
	<ul class="tab" name="menu1">
		<li id="li_1" class="selected"><a href="javascript:void(0)" hidefocus="true" onclick="select_tab('1')">商品信息</a></li>
		<li id="li_2"><a href="javascript:void(0)" hidefocus="true" onclick="select_tab('2')">描述</a></li>
		<li id="li_3"><a href="javascript:void(0)" hidefocus="true" onclick="select_tab('3')">营销选项</a></li>
	</ul>
</div>

<div class="content_box">
	<div class="content form_content">
		<form action="{url:/goods/goods_update}" name="goodsForm" method="post" novalidate="true">
			<input type="hidden" name="id" value="" />
			<input type='hidden' name="img" value="" />
			<input type='hidden' name="_imgList" value="" />
			<input type='hidden' name="callback" value="{echo:IUrl::getRefRoute()}" />

			<div id="table_box_1">
				<table class="form_table">
					<colgroup>
						<col width="150px" />
						<col />
					</colgroup>
					<tr>
						<th>商品名称：</th>
						<td>
							<input class="normal" name="name" type="text" value="" pattern="required" alt="商品名称不能为空" /><label>*</label>
						</td>
					</tr>
					<tr>
						<th>关键词：</th>
						<td>
							<input type='text' class='middle' name='search_words' value='' />
							<label>每个关键词最长为15个字符，必须以","(逗号)分隔符</label>
						</td>
					</tr>
					<tr>
						<th>所属商户：</th>
						<td>
							<select class="auto" name="seller_id">
								<option value="0">商城平台自营 </option>
								{query:name=seller where=is_del eq 0}
								<option value="{$item['id']}">{$item['true_name']}-{$item['seller_name']}</option>
								{/query}
							</select>
							<a href='{url:/member/seller_edit}' class='orange'>请点击添加商户</a>
						</td>
					</tr>
					<tr>
						<th>所属分类：</th>
						<td>
							<div id="__categoryBox" style="margin-bottom:8px"></div>
							<button class="btn" type="button" name="_goodsCategoryButton"><span class="add">设置分类</span></button>
							{set:plugin::trigger('goodsCategoryWidget',array("type" => "checkbox","name" => "_goods_category[]","value" => isset($goods_category) ? $goods_category : ""))}
							<a href='{url:/goods/category_edit}' class='orange'>请点击添加分类</a>
						</td>
					</tr>
					<tr>
						<th>是否上架：</th>
						<td>
							<label class='attr'><input type="radio" name="is_del" value="0" checked> 是</label>
							<label class='attr'><input type="radio" name="is_del" value="2"> 否</label>
							<label>只有上架的商品才会在前台显示出来，客户是无法看到下架商品</label>
						</td>
					</tr>
					<tr>
						<th>是否共享：</th>
						<td>
							<label class='attr'><input type="radio" name="is_share" value="1"> 是</label>
							<label class='attr'><input type="radio" name="is_share" value="0" checked> 否</label>
							<label>共享商品，只有商城平台的商品可以被商家复制，分销</label>
						</td>
					</tr>
					<tr>
						<th>是否免运费：</th>
						<td>
							<label class='attr'><input type="radio" name="is_delivery_fee" value="1"> 是</label>
							<label class='attr'><input type="radio" name="is_delivery_fee" value="0" checked> 否</label>
							<label>商品是否免运费</label>
						</td>
					</tr>
					<tr>
						<th>附属数据：</th>
						<td>
							<div class="con">
								<table class="border_table">
									<thead>
										<tr>
											<th>购买成功增加积分</th><th>排序</th><th>计件单位显示</th><th>购买成功增加经验值</th>
										</tr>
									</thead>
									<tbody>
										<tr>
											<td><input class="small" name="point" type="text" pattern="int" value="0"/></td>
											<td><input class="small" name="sort" type="text" pattern="int" value="99"/></td>
											<td><input class="small" name="unit" type="text" value="件"/></td>
											<td><input class="small" name="exp" type="text" pattern="int" value="0"/></td>
										</tr>
									</tbody>
								</table>
							</div>
						</td>
					</tr>
					<tr>
						<th>基本数据：</th>
						<td>
							<div class="con">
								<table class="border_table">
									<thead id="goodsBaseHead"></thead>

									<!--商品标题模板-->
									<script id="goodsHeadTemplate" type='text/html'>
									<tr>
										<th>商品货号</th>
										<%var isProduct = false;%>
										<%for(var item in templateData){%>
										<%isProduct = true;%>
										<th><a href="javascript:confirm('确定要删除此列规格？','delSpec(<%=templateData[item]['id']%>)');"><%=templateData[item]['name']%>【删】</a></th>
										<%}%>
										<th>库存</th>
										<th>市场价格</th>
										<th>销售价格</th>
										<th>成本价格</th>
										<th>重量(克)</th>
										<%if(isProduct == true){%>
										<th>操作</th>
										<%}%>
									</tr>
									</script>

									<tbody id="goodsBaseBody"></tbody>

									<!--商品内容模板-->
									<script id="goodsRowTemplate" type="text/html">
									<%var i=0;%>
									<%for(var item in templateData){%>
									<%item = templateData[item]%>
									<tr class='td_c'>
										<td><input class="small" name="_goods_no[<%=i%>]" pattern="required" type="text" value="<%=item['goods_no'] ? item['goods_no'] : item['products_no']%>" /></td>
										<%var isProduct = false;%>
										<%var specArrayList = typeof item['spec_array'] == 'string' && item['spec_array'] ? JSON().parse(item['spec_array']) : item['spec_array'];%>
										<%for(var result in specArrayList){%>
										<%result = specArrayList[result]%>
										<input type='hidden' name="_spec_array[<%=i%>][]" value='<%=JSON().stringify(result)%>' />
										<%isProduct = true;%>
										<td>
											<%if(result['type'] == 1){%>
												<%=result['value']%>
											<%}else{%>
												<img class="img_border" width="30px" height="30px" src="<%=webroot(result['value'])%>">
											<%}%>
										</td>
										<%}%>
										<td><input class="tiny" name="_store_nums[<%=i%>]" type="text" pattern="int" value="<%=item['store_nums']?item['store_nums']:100%>" /></td>
										<td><input class="tiny" name="_market_price[<%=i%>]" type="text" pattern="float" value="<%=item['market_price']%>" /></td>
										<td>
											<input type='hidden' name="_groupPrice[<%=i%>]" value="<%=item['groupPrice']%>" />
											<input class="tiny" name="_sell_price[<%=i%>]" type="text" pattern="float" value="<%=item['sell_price']%>" />
											<button class="btn" type="button" onclick="memberPrice(this);"><span class="add <%if(item['groupPrice']){%>orange<%}%>">会员组价格</span></button>
										</td>
										<td><input class="tiny" name="_cost_price[<%=i%>]" type="text" pattern="float" empty value="<%=item['cost_price']%>" /></td>
										<td><input class="tiny" name="_weight[<%=i%>]" type="text" pattern="float" empty value="<%=item['weight']%>" /></td>
										<%if(isProduct == true){%>
										<td><a href="javascript:void(0)" onclick="delProduct(this);"><img class="operator" src="{skin:images/admin/icon_del.gif}" alt="删除" /></a></td>
										<%}%>
									</tr>
									<%i++;%>
									<%}%>
									</script>
								</table>
							</div>
						</td>
					</tr>
					<tr>
						<th>商品模型：</th>
						<td>
							<select class="auto" name="model_id" onchange="create_attr(this.value)">
								<option value="0">通用类型 </option>
								{query:name=model}
								<option value="{$item['id']}">{$item['name']}</option>
								{/query}
							</select>
							<label>可以加入商品扩展属性，比如：型号，年代，款式...</label>
						</td>
					</tr>
					<tr>
						<th>规格：</th>
						<td>
							<select class='auto' onchange='selSpecVal(this);' id='specNameSel'>
								<option value=''>选规格名称</option>
								{query:name=spec where=seller_id eq 0 and is_del eq 0}
								<option value='{$item['id']}'>{$item['name']}</option>
								{/query}
							</select>
							<select class='auto' onchange='selSpec(this);' id='specValSel'><option value='0'>选规格数据</option></select>
							<button class="btn" onclick="addNewSpec(0);" type="button"><span class="add">新建规格</span></button>
							<label>可从现有规格中选择或新建规格生成货品。比如：尺码，颜色，类型...</label>
						</td>
					</tr>
					<tr id="properties" style="display:none">
						<th>扩展属性：</th>
						<td>
							<table class="border_table1" id="propert_table">
							<script type='text/html' id='propertiesTemplate'>
							<%for(var item in templateData){%>
							<%item = templateData[item]%>
							<%var valueItems = item['value'].split(',');%>
							<tr>
								<th><%=item["name"]%></th>
								<td>
									<%if(item['type'] == 1){%>
										<%for(var tempVal in valueItems){%>
										<%tempVal = valueItems[tempVal]%>
											<label class="attr"><input type="radio" name="attr_id_<%=item['id']%>" value="<%=tempVal%>" /><%=tempVal%></label>
										<%}%>
									<%}else if(item['type'] == 2){%>
										<%for(var tempVal in valueItems){%>
										<%tempVal = valueItems[tempVal]%>
											<label class="attr"><input type="checkbox" name="attr_id_<%=item['id']%>[]" value="<%=tempVal%>"/><%=tempVal%></label>
										<%}%>
									<%}else if(item['type'] == 3){%>
										<select class="auto" name="attr_id_<%=item['id']%>">
										<%for(var tempVal in valueItems){%>
										<%tempVal = valueItems[tempVal]%>
										<option value="<%=tempVal%>"><%=tempVal%></option>
										<%}%>
										</select>
									<%}else if(item['type'] == 4){%>
										<input type="text" name="attr_id_<%=item['id']%>" value="<%=item['value']%>" class="normal" />
									<%}%>
								</td>
							</tr>
							<%}%>
							</script>
							</table>
						</td>
					</tr>
					<tr>
						<th>商品推荐类型：</th>
						<td>
							<label class="attr"><input name="_goods_commend[]" type="checkbox" value="1" />最新商品</label>
							<label class="attr"><input name="_goods_commend[]" type="checkbox" value="2" />特价商品</label>
							<label class="attr"><input name="_goods_commend[]" type="checkbox" value="3" />热卖商品</label>
							<label class="attr"><input name="_goods_commend[]" type="checkbox" value="4" />推荐商品</label>
						</td>
					</tr>
					<tr>
						<th>商品品牌：</th>
						<td>
							<select class="auto" name="brand_id">
								<option value="0">请选择</option>
								{query:name=brand}
								<option value="{$item['id']}">{$item['name']}</option>
								{/query}
							</select>
						</td>
					</tr>
					<tr>
						<th>产品相册：</th>
						<td>
							<input id="fileUpload" type="file" style="width:65px;" accept="image/png,image/gif,image/jpeg" name="_goodsFile" multiple="multiple" data-url="{url:/goods/goods_img_upload}" />
							<label id="uploadPercent">可以上传多张图片，分辨率3000px以下，大小不得超过{echo:IUpload::getMaxSize()}</label>
						</td>
					</tr>
					<tr>
						<td></td>
						<td id="thumbnails"></td>

						<!--图片模板-->
						<script type='text/html' id='picTemplate'>
						<span class='pic'>
							<img name="picThumb" onclick="defaultImage(this);" style="margin:5px; opacity:1;width:100px;height:100px" src="<%=webroot(picRoot)%>" alt="<%=picRoot%>" />
							<p>
								<a class='orange' href='javascript:void(0)' onclick="$(this).parents('.pic').insertBefore($(this).parents('.pic').prev());"><img src="{skin:images/admin/arrow_left.png}" title="左移动" alt="左移动" /></a>
								<a class='orange' href='javascript:void(0)' onclick="$(this).parents('.pic').remove();"><img src="{skin:images/admin/sign_cacel.png}" title="删除" alt="删除" /></a>
								<a class='orange' href='javascript:void(0)' onclick="$(this).parents('.pic').insertAfter($(this).parents('.pic').next());"><img src="{skin:images/admin/arrow_right.png}" title="右移动" alt="右移动" /></a>
							</p>
						</span>
						</script>
					</tr>
				</table>
			</div>

			<div id="table_box_2" cellpadding="0" cellspacing="0" style="display:none">
				<table class="form_table">
					<colgroup>
						<col width="150px" />
						<col />
					</colgroup>
					<tr>
						<th>产品描述：</th>
						<td><textarea id="content" name="content" style="width:700px;height:400px;"></textarea></td>
					</tr>
				</table>
			</div>

			<div id="table_box_3" cellpadding="0" cellspacing="0" style="display:none">
				<table class="form_table">
					<col width="150px" />
					<col />
					<tr>
						<th>SEO关键词：</th><td><input class="normal" name="keywords" type="text" value="" /></td>
					</tr>
					<tr>
						<th>SEO描述：</th><td><textarea name="description"></textarea></td>
					</tr>
				</table>
			</div>

			<table class="form_table">
				<col width="150px" />
				<col />
				<tr>
					<td></td>
					<td><button class="submit" type="submit" onclick="return checkForm()"><span>发布商品</span></button></td>
				</tr>
			</table>
		</form>
	</div>
</div>

<script language="javascript">
//创建表单实例
var formObj = new Form('goodsForm');

//默认货号
var defaultProductNo = '{echo:goods_class::createGoodsNo()}';

$(function()
{
	//商品图片的回填
	{if:isset($goods_photo)}
	var goodsPhoto = {echo:JSON::encode($goods_photo)};
	for(var item in goodsPhoto)
	{
		var picHtml = template.render('picTemplate',{'picRoot':goodsPhoto[item].img});
		$('#thumbnails').append(picHtml);
	}
	{/if}

	//商品默认图片
	{if:isset($form['img']) && $form['img']}
	$('#thumbnails img[name="picThumb"][alt="{echo:$form['img']}"]').addClass('current');
	{/if}

	initProductTable();

	//存在商品信息
	{if:isset($form)}
	var goods = {echo:JSON::encode($form)};

	var goodsRowHtml = template.render('goodsRowTemplate',{'templateData':[goods]});
	$('#goodsBaseBody').html(goodsRowHtml);

	formObj.init(goods);

	//模型选择
	$('[name="model_id"]').change();
	{else:}
	$('[name="_goods_no[0]"]').val(defaultProductNo);
	{/if}

	//存在货品信息,进行数据填充
	{if:isset($product)}
	var spec_array = {echo:$product[0]['spec_array']};
	var product    = {echo:JSON::encode($product)};

	var goodsHeadHtml = template.render('goodsHeadTemplate',{'templateData':spec_array});
	$('#goodsBaseHead').html(goodsHeadHtml);

	var goodsRowHtml = template.render('goodsRowTemplate',{'templateData':product});
	$('#goodsBaseBody').html(goodsRowHtml);
	{/if}

	//商品促销回填
	{if:isset($goods_commend)}
	formObj.setValue('_goods_commend[]',"{echo:join(';',$goods_commend)}");
	{/if}

	//编辑器载入
	KindEditorObj = KindEditor.create('#content',{"filterMode":false});
});

//删除货品
function delProduct(_self)
{
	$(_self).parent().parent().remove();
	if($('#goodsBaseBody tr').length == 0)
	{
		initProductTable();
	}
}

//提交表单前的检查
function checkForm()
{
	//整理商品图片
	var goodsPhoto = [];
	$('#thumbnails img[name="picThumb"]').each(function(){
		goodsPhoto.push(this.alt);
	});
	if(goodsPhoto.length > 0)
	{
		$('input[name="_imgList"]').val(goodsPhoto.join(','));
		$('input[name="img"]').val($('#thumbnails img[name="picThumb"][class="current"]').attr('alt'));
	}
	return true;
}

//tab标签切换
function select_tab(curr_tab)
{
	$("form[name='goodsForm'] > div").hide();
	$("#table_box_"+curr_tab).show();
	$("ul[name=menu1] > li").removeClass('selected');
	$('#li_'+curr_tab).addClass('selected');
}

//根据模型动态生成扩展属性
function create_attr(model_id)
{
	$.getJSON("{url:/block/attribute_init}",{'model_id':model_id,'random':Math.random()}, function(json)
	{
		if(json && json.length > 0)
		{
			var templateHtml = template.render('propertiesTemplate',{'templateData':json});
			$('#propert_table').html(templateHtml);
			$('#properties').show();

			//表单回填设置项
			{if:isset($goods_attr)}
			{set:$attrArray = array();}
			{foreach:items = $goods_attr}
			{set:$valArray = explode(',',$item);}
			{set:$attrArray[] = '"attr_id_'.$key.'[]":"'.join(";",IFilter::act($valArray)).'"'}
			{set:$attrArray[] = '"attr_id_'.$key.'":"'.join(";",IFilter::act($valArray)).'"'}
			{/foreach}
			formObj.init({{echo:join(',',$attrArray)}});
			{/if}
		}
		else
		{
			$('#properties').hide();
		}
	});
}

//添加新规格
function addNewSpec(seller_id)
{
	var url = creatUrl("goods/spec_edit/seller_id/@seller_id@");
	url     = url.replace("@seller_id@",seller_id);
	art.dialog.open(url,{
		id:'addSpecWin',
	    title:'规格设置',
	    okVal:'确定',
	    ok:function(iframeWin, topWin){
	    	var formObject = iframeWin.document.forms['specForm'];
	    	if(formObject.onsubmit() == false)
	    	{
	    		return false;
	    	}
			$.getJSON(formObject.action,$(formObject).serialize(),function(json){
				if(json.flag == 'success' && json.data)
				{
					var insertHtml = '<option value="'+json.data.id+'">'+json.data.name+'</option>';
					$('#specNameSel').append(insertHtml);
					$('#specNameSel').find('option:last').attr("selected",true);
					$('#specNameSel').trigger('change');
					return true;
				}
				else
				{
					alert(json.message);
					return false;
				}
			});
	    }
	});
}
</script>
<script type="text/javascript" src="{webroot:public/javascript/goods_edit.js}"></script>