{js:artTemplate}
{js:areaSelect}
<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 name="ModelForm" action="{url:/order/order_update}" method="post">
	<input type='hidden' name='id' value='' />
	<input type='hidden' name='takeself' value='' />

	<div class="nav-tabs-custom">
		<ul class="nav nav-tabs">
			<li class="active">
				<a href="#tab1" data-toggle="tab">商品信息</a>
			</li>
			<li>
				<a href="#tab2" data-toggle="tab">订单配置</a>
			</li>
			<li>
				<a href="#tab3" data-toggle="tab">收货人信息</a>
			</li>
		</ul>
	</div>

	<div class="tab-content">

		<!--商品信息-->
		<div class="tab-pane active" id="tab1">
			<table class="table list-table">
				<colgroup>
					<col />
					<col width="120px" />
					<col width="100px" />
					<col width="120px" />
				</colgroup>

				<thead>
					<tr>
						<th>商品名称</th>
						<th>商品价格</th>
						<th>商品数量</th>
						<th>操作</th>
					</tr>
				</thead>

				<tbody id="goodsBox"></tbody>
				<tfoot>
					<tr>
						<td colspan="4">
							<a class="btn btn-default" href='javascript:searchGoods({"callback":searchGoodsCallback,"type":"checkbox","is_products":1});'>
								<i class='fa fa-plus'></i>添加商品
							</a>
						</td>
					</tr>
				</tfoot>

				<!--商品模板-->
				<script type='text/html' id='goodsTrTemplate'>
				<tr>
					<input type='hidden' name='goods_id[]' value='<%=item.id%>' />
					<input type='hidden' name='product_id[]' value='<%=item.product_id%>' />
					<td>
						<%=item.name?item.name:"商品不存在"%>
						<%if(item.spec_array){%>
							<span class="text-orange">
							<%var spec_array = typeof item.spec_array == 'string' ? JSON().parse(item.spec_array) : item.spec_array;%>
							<%for(var index in spec_array){%>
								<%var value = spec_array[index]%>
								<%=value['name']%>:
								<%if(value['type'] == 1){%>
									<%=value['value']%>
								<%}else{%>
									<img src="{echo:IUrl::creatUrl()}<%=value['value']%>" width="35px" height="35px" />
								<%}%>
							<%}%>
							</span>
						<%}%>
						<%if(item.products_no){%>
							【<%=item.products_no%>】
						<%}%>

						<%if(item.goods_no){%>
							【<%=item.goods_no%>】
						<%}%>
					</td>
					<td><%=item.real_price%></td>
					<td><input class="form-control" name="goods_nums[]" value="<%=item.goods_nums ? item.goods_nums : 1%>" /></td>
					<td>
						<a onclick="$(this).parent().parent().remove();">
							<i class='operator fa fa-close'></i>
						</a>
					</td>
				</tr>
				</script>
			</table>
		</div>

		<!--订单配置-->
		<div class="tab-pane" id="tab2">
			<table class="table form-table">
				<colgroup>
					<col width="130px" />
					<col />
				</colgroup>

				<tbody>
					<tr>
						<th>配送方式：</th>
						<td>
							<select name="distribution" alt="请选择配送方式" class="form-control" onchange="selectTakeself(this);">
								{foreach:items=Api::run('getDelivery')}
								<option value="{$item['id']}" datatype="{$item['type']}">{$item['name']}</option>
								{/foreach}
							</select>
							<p id="takeself"></p>

							<!--自提点模板-->
							<script type='text/html' id='takeselfTemplate'>
								<%=item['province_str']%> <%=item['city_str']%> <%=item['area_str']%> <%=item['address']%> <%=item['name']%> <%=item['phone']%> <%=item['mobile']%>
							</script>
						</td>
					</tr>
					<tr>
						<th>配送运费：</th>
						<td>
							<input class="form-control" type="text" name="real_freight" value="" pattern="float" empty />
							<p class="help-block">不填写运费则系统自动计算</p>
						</td>
					</tr>
					<tr>
						<th>支付方式：</th>
						<td>
							<select name="pay_type" pattern="required" alt="请选择配送方式" class="form-control">
								<option value="">请选择支付</option>
                                {foreach:items=Api::run('getPayment')}
								<option value="{$item['id']}">{$item['name']}</option>
								{/foreach}
							</select>
						</td>
					</tr>
					<tr>
						<th>是否要发票：</th>
						<td>
							<label class='checkbox-inline'>
								<input type="checkbox" name="invoice" value="1" onchange="$('#taxArea').toggle();" />发票
							</label>
						</td>
					</tr>
					<tr id="taxArea" style="display:none;">
						<th>发票信息：</th>
						<td>
							<table class="table">
								<tr>
									<td>公司名称：<input class="form-control" type="text" name="invoice_company_name" value="" /></td>
									<td>纳税人识别码：<input class="form-control" type="text" name="invoice_taxcode" value="" /></td>
								</tr>

								<tr>
									<td>注册地址：<input class="form-control" type="text" name="invoice_address" value="" /></td>
									<td>注册电话：<input class="form-control" type="text" name="invoice_telphone" value="" /></td>
								</tr>

								<tr>
									<td>开户银行：<input class="form-control" type="text" name="invoice_bankname" value="" /></td>
									<td>银行账号：<input class="form-control" type="text" name="invoice_bankno" value="" /></td>
								</tr>

								<tr>
									<td>
										发票类型：
										<select name="invoice_type" class="form-control">
											<option value="1">普通发票</option>
											<option value="2">增值税专用票</option>
										</select>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<th>订单备注：</th>
						<td>
							<input class="form-control" type="text" name="note" value="" />
						</td>
					</tr>
					<tr>
						<th>指定送货时间：</th>
						<td>
							<label class='radio-inline'>
								<input type='radio' name='accept_time' checked="checked" value='任意' />任意
							</label>
							<label class='radio-inline'>
								<input type='radio' name='accept_time' value='周一到周五' />周一到周五
							</label>
							<label class='radio-inline'>
								<input type='radio' name='accept_time' value='周末' />周末
							</label>
						</td>
					</tr>
					{if:isset($this->orderRow)}
					<tr>
						<th>减价或涨价：</th>
						<td>
							<input class="form-control" type="text" name="discount" value="" empty pattern="float" />
							<p class="help-block">折扣用" - ",涨价用" + "，单位:元</p>
						</td>
					</tr>
					{/if}
				</tbody>
			</table>
		</div>

		<!--收货人信息-->
		<div class="tab-pane" id="tab3">
			<table class="table form-table">
				<colgroup>
					<col width="130px" />
					<col />
				</colgroup>

				<tbody>
					<tr>
						<th>所属用户名:</th>
						<td>
							<input type='text' name='username' value='{$this->username}' class='form-control' placeholder="用户注册名" />
							<p class="help-block">订单所属于的用户，直接填写用户名，订单创建后与该用户绑定在一起，如果为空则为游客订单或者线下订单</p>
						</td>
					</tr>
					<tr>
						<th>收货人姓名:</th>
						<td>
							<input class="form-control" type="text" name="accept_name" value="" pattern="required" placeholder="请填写收货人姓名" />
						</td>
					</tr>
					<tr>
						<th>收货地区:</th>
						<td>
                            <div class="row">
                                <div class="col-xs-3">
                                    <select name="province" child="city,area" class="form-control"></select>
                                </div>
                                <div class="col-xs-3">
                                    <select name="city" child="area" class="form-control"></select>
                                </div>
                                <div class="col-xs-3">
                                    <select name="area" pattern="required" class="form-control"></select>
                                </div>
                            </div>
						</td>
					</tr>
					<tr>
						<th>收货地址:</th>
						<td>
							<input class="form-control" type="text" name="address" pattern="required" value="" placeholder="请填写收货地址" />
						</td>
					</tr>
					<tr>
						<th>联系手机:</th>
						<td>
							<input class="form-control" type="text" name="mobile" value="" pattern="mobi" placeholder="填写手机号码" />
						</td>
					</tr>
					<tr>
						<th>联系电话:</th>
						<td>
							<input class="form-control" type="text" name="telphone" value="" empty pattern="phone" placeholder="请输入正确的固定电话" />
						</td>
					</tr>
					<tr>
						<th>邮编:</th>
						<td>
							<input class="form-control" type="text" name="postcode" value="" empty pattern="zip" placeholder="请输入正确的邮编" />
						</td>
					</tr>
					<tr>
						<th>用户附言:</th>
						<td>
							<textarea class="form-control" rows="5" cols="15" name="postscript" placeholder="填写用户的附言"></textarea>
						</td>
					</tr>
				</tbody>
			</table>
		</div>

		<div class="text-center">
			<button class='btn btn-primary' type="submit">保 存</button>
		</div>
	</div>
	</form>
</div>

<script type="text/javascript">
//DOM加载完毕
$(function(){
	//完整订单数据JSON
	var orderRow = {echo:JSON::encode($this->orderRow)};

	//表单回填
	var formInstance = new Form();
	formInstance.init(orderRow);

	//动态数据回填
	if(orderRow)
	{
		var goodsList = {echo:JSON::encode($this->orderGoods)};
		for(var index in goodsList)
		{
			insertGoods(goodsList[index]);
		}

		//自提数据回填
		{if:$this->takeself}
		var takeselfJSON = {echo:JSON::encode($this->takeself)};
		$('#takeself').html(template.render('takeselfTemplate',{"item":takeselfJSON}));
		{/if}

		//发票数据回填
		if($('input[type="checkbox"][name="invoice"]:checked').val() == 1)
		{
			$('#taxArea').show();
			if(orderRow.invoice_info)
			{
				var invoiceObj = JSON.parse(orderRow.invoice_info);
				for(var i in invoiceObj)
				{
					formInstance.setValue("invoice_"+i,invoiceObj[i]);
				}
			}
		}
	}

	//地区联动插件
	var areaInstance = new areaSelect('province');
	areaInstance.init({echo:JSON::encode($this->orderRow)});

	//提交按钮解锁
	$('button[type="submit"]').removeAttr('disabled');
});

/**
 * 筛选商品回调
 * @param goodsList JQ选中的商品列表节点
 */
function searchGoodsCallback(goodsList)
{
	//循环插入DOM节点
	goodsList.each(function()
	{
		var temp = $.parseJSON($(this).attr('data'));
		var insertObject = {"id":temp.goods_id,"name":temp.name,"real_price":temp.sell_price,"product_id":temp.product_id,"spec_array":temp.spec_array ? $.parseJSON(temp.spec_array) : "","goods_no":temp.goods_no};
		insertGoods(insertObject);
	});
}

/**
 * 生成商品信息
 */
function insertGoods(goodsRow)
{
	var goodsRow = goodsRow ? goodsRow : {};
	var goodsTrHtml = template.render('goodsTrTemplate',{item:goodsRow});
	$('#goodsBox').append(goodsTrHtml,goodsRow);
}

//选择自提点
function selectTakeself(obj)
{
	var type = $(obj).find("option:selected").attr("datatype");
	if(type != 2)
	{
		$('[name="takeself"]').val(0);
		$('#takeself').empty();
		return;
	}

	art.dialog.open("{url:/block/takeself}",{
		title:'选择自提点',
		okVal:'选择',
		ok:function(iframeWin, topWin)
		{
			var takeselfJson = $(iframeWin.document).find('[name="takeselfItem"]:checked').val();

			if(!takeselfJson)
			{
				alert('请选择自提点');
				return false;
			}
			var json = $.parseJSON(takeselfJson);
			$('#takeself').empty();
			$('[name="takeself"]').val(json.id);
			$('#takeself').html(template.render('takeselfTemplate',{"item":json}));
			return true;
		}
	});
}
</script>