{include "header.inc.html"}
{$orderLang=$lang.admin.order.form}
<div class="content_box">
	<div class="toolbar_box">
		<div class="toolbar"> <span> <strong>{$lang.admin.order.name}</strong><br />
		<span class="english">{$lang.admin.order.name_en}</span> </span> </div>
	</div>
{if $smarty.get.act=="edit"}
<div class="adminForm">
<form class="form" action="" method="post" id="orderForm">
<input type="hidden" name="act" value="{$smarty.get.act}" />
{if $smarty.get.act=="edit"}
<input type="hidden" name="id" value="{$smarty.get.id}" />
{/if}
<div class="form-line">
<label class="label">用户：</label><div class="element">{$orderInfo.userId}</div>
</div>
<div class="form-line">
<label class="label">{$orderLang.amount}：</label><div class="element"><input type="text" class="text w200" name="amount" value="{$orderInfo.amount}" /></div>
</div>
<div class="form-line">
<label class="label">{$orderLang.status}：</label><div class="element"><select name="iStatus"><option value="0">{$orderLang.status}</option>
{orderstatus typeId=1}<option value="{$item.id}"{if $orderInfo.iStatus==$item.id} selected{/if}>{$item.name}</option>{/orderstatus}
</select>&nbsp;&nbsp;&nbsp;&nbsp;{$orderLang.deliveryType}：<select name="deliveryType"><option value="0">物流方式</option>
{deliverytype iStatus=1}<option value="{$item.id}"{if $orderInfo.deliveryTypeId==$item.id} selected{/if}>{$item.name}</option>{/deliverytype}
</select></div>
</div>
<div class="form-line">
<label class="label">支付状态：</label><div class="element"><select name="payStatusId">{orderstatus typeId=2}<option value="{$item.id}"{if $orderInfo.payStatusId==$item.id} selected{/if}>{$item.name}</option>{/orderstatus}</select></div>
</div>
<div class="form-line">
<label class="label">支付方式：</label><div class="element">{if $orderInfo.divideTypeId==1}全额支付{else if $orderInfo.divideTypeId==2}分期支付：{$orderInfo.divideNum}期&nbsp;&nbsp;&nbsp;&nbsp;{$orderInfo.divideMoney}元/期{/if}</div>
</div>

<div class="form-line none">
<label class="label">支付方式：</label><div class="element"><select name="paymentId"><option value="0">支付方式</option>
{payment iStatus=1}
<option value="{$item.id}"{if $orderInfo.paybankId==$item.id} selected{/if}>{$item.name}</option>
{/payment}
</select></div>
</div>

<div class="form-line">
<label class="label">物流单号：</label><div class="element"><input type="text" class="text w200" name="deliveryNumber" value="{$orderInfo.deliveryNumber}" /></div></div>
<div class="form-line">
<label class="label">{$orderLang.receiver}:</label><div class="element">{if $cfg_lang=="zh-cn"}<input type="text" name="receiver" class="text" value="{$orderInfo.receiver}" />{else}<input type="text" name="firstName" class="text" value="{$orderInfo.firstName}" /> <input type="text" name="lastName" class="text" value="{$orderInfo.lastName}" />{/if}</div>
</div>
<div class="form-line">
<label class="label">{$orderLang.street}:</label><div class="element"><input type="text" name="address" class="text w300" value="{$orderInfo.address}" /></div>
</div>
<div class="form-line">
<label class="label">{$orderLang.zipCode}:</label><div class="element"><input type="text" name="zipCode" class="text" value="{$orderInfo.zipCode}" /></div>
</div>
<div class="form-line">
<label class="label">{$orderLang.mobile}:</label><div class="element"><input type="text" name="mobile" class="text" value="{$orderInfo.mobile}" /></div>
</div>
<div class="form-line">
<label class="label">{$orderLang.phone}:</label><div class="element"><input type="text" name="phone" class="text" value="{$orderInfo.phone}" /></div>
</div>
<div class="form-line">
	<label class="label">{$orderLang.goods}:</label><div class="element">
	<table class="orderGoods">
	<tr><th>{$lang.admin.goods.form.name}</th><th>备注</th><th>数量</th><th>{$lang.admin.goods.form.price}</th></tr>
	{foreach from=$orderInfo.goods item=item}
	<tr id="orderGoods{$item.id}"><td><a href="{$baseUrl}goods/view/id/{$item.goodsId}" target="_blank">{$item.name}</a></td><td>{$item.remark}</td><td><span class="pnum">{$item.pnum}</span>{if $item.prizeId} <font color='red'>[奖品]</font>{/if}</td><td>{$item.price}</td></tr>
	{/foreach}
	<tr class="none"><td></td><td></td><td></td><td><a href="javascript:void(0)" onclick="addGoods({$orderInfo.id})">添加</a><a href="javascript:void(0)" onclick="editGoods({$item.id})">{$lang.edit}</a></td></tr>
	</table>
	</div>
</div>

<div class="form-line">
<label class="label">{$orderLang.remark}:</label><div class="element"><textarea name="remark" class="text" style="width:300px;height:60px;">{$orderInfo.remark}</textarea></div>
</div>
<div class="form-line">
<label class="label"></label><div class="element"><input type="submit" class="btn_sub" value="{$lang.form.submit}" /></div>
</div>
</form>
</div>
 {else}

<div class="tool clearfix">
<div class="left">
<form action="{$adminUrl}order/" method="post" class="form">
<select name="divideTypeId"><option value="0">支付方式</option><option value="1">全额支付</option><option value="2">分期支付</option></select> <select name="iStatus"><option value="0">{$orderLang.iStatus}</option>{orderstatus typeId=1}<option value="{$item.id}"{if ($smarty.request.iStatus==$item.id)} selected{/if}>&nbsp;{$item.name}</option>{/orderstatus}</select> <select name="payStatusId"><option value="0">支付状态</option>{orderstatus typeId=2}<option value="{$item.id}"{if ($smarty.request.iStatus==$item.id)} selected{/if}>&nbsp;{$item.name}</option>{/orderstatus}</select>
	{if $cfg_lang!="zh-cn"}Email:<input type="text" name="email" value="{$smarty.request.email}" />{/if}
	<input class="btn_sub" type="submit" value="{$lang.form.search}">
</form>
</div>
<div class="right">
<a id="J_addOrder" class="none">添加订单</a>&nbsp;&nbsp;&nbsp;
{if $smarty.get.act!="add" || $smarty.get.act!="edit"}
<a id="deleteChecked">删除所选</a>
{/if}
</div>
<div class="clr"></div>
</div><!--.tool-->

 <table class="table-ca table_1 orderList" id="tInfo">
 <tr>
<th class="w30"><input type="checkbox" value="1" name="allid" id="checkall" /></th><th class="w60">{$lang.form.id}</th><th class="">{$orderLang.goods}</th><th class="w60">{$lang.form.user}</th><th class="w60">{$orderLang.receiver}</th><th class="w70">{$orderLang.amount}</th><th class="w60">支付状态</th><th class="w60">{$orderLang.status}</th><th class="w130">{$lang.form.posttime}</th><th class="w100">{$lang.form.operate}</th>
 </tr>
{order limit=15 pager="true" iStatus=$smarty.request.iStatus payStatusId=$smarty.request.payStatusId divideTypeId=$smarty.request.divideTypeId email=$smarty.request.email userId=$smarty.request.userId}
<tr sid="{$item.id}">
<td><input type="checkbox" name="id[]" value="{$item.id}" /></td><td>{$item.id}</td><td>{foreach from=$item.goods item=goods}{if $goods.name}{$goods.name} <span class="tip">(数量:<span class="i">{$goods.pnum}</span>{if $goods.prizeId} <font color="red">[奖品]</font>{/if}  价格:<span class="i">{$goods.price}</span>)</span><br>{/if}{/foreach}</td><td><div class="orderUser"><div class="w50 nowrap J-user-anchor" sid="{$item.userId}">{$item.userId}</div></td><td>{if $cfg_lang=="zh-cn"}{$item.receiver}{else}{$item.firstName} {$item.lastName}{/if}</td><td><span class="smodedit" name="amount" sid="{$item.id}" width="60">{$item.amount}</span></td><td>{$item.payStatus}</td><td>{$item.status}</td><td>{$item.posttime|date_format:"Y-m-d H:i:s"}</td><td><a href="javascript:void(0)" class="shipBtn" title="{$lang.ship}">{$lang.ship}</a> <a href="{$adminUrl}order?act=edit&id={$item.id}" class="modBtn" title="{$lang.edit}">{$lang.edit}</a> <a href="javascript:void(0)" class="delBtn" sid="{$item.id}" title="{$lang.del}">{$lang.del}</a> <a href="{$baseUrl}order/prints?id={$item.id}" target="_blank">打印</a></td>
</tr>
{/order}
</table>
<div class="page">
{if $smarty.request.iStatus}
{$iStatus = $smarty.request.iStatus}
{$iStatus = "/iStatus/$iStatus"}
{/if}
	<div class="pagin fr">{pager allCount=$allCount url="/qkadmin/order" number="false" dispatcher='false'}</div>
</div>
{/if}
</div><!--.content_box-->
<script type="text/javascript" src="/static/js/global.js"></script>
<script type="text/javascript" src="/static/js/form_ctrl.js"></script>
<script type="text/javascript" src="/static/js/area.js"></script>
<script type="text/javascript" src="{$adminUrl}js/order.js"></script>

<script type="text/javascript">
function addOrder(){
	var config = { name:"", action:"/qkadmin/order/", method:"POST", className:"form", id:"addOrderForm",
		input:[{if $config.user.loginName}{ label:"会员登录名",name:"loginName",className:"text",tip:"",value:"" }{else}{ label:"邮箱：",name:"email",className:"text",tip:"",value:"" }{/if}, { label:"物流方式：",name:"deliveryTypeId",className:"text",type:"select",option:[ {foreach from=$deliveryType item=item}{ value:{$item.id},name:"{$item.name}" }{if not $item@last},{/if}{/foreach} ] }],
		hidden:[{ name:"act",value:"addOrder" },{ name:"inajax",value:"1" }]};
	var html = "";
		$('<div id="addOrderDialog"></div>').dialog({ title:"添加订单",dialogClass:'layer_global_main',width:470,height:200,modal:true,resizable:false,buttons:[{
				text:'确定',click:function(){ 
					$("#addOrderForm").validate({
						rules:{
							{if $config.user.loginName}loginName:{ required:true }{else}email:{ required:true,email:true }{/if},deliveryTypeId:{ required:true }
						},
						messages:{
							{if $config.user.loginName}loginName:{ required:'请填写会员登录名' }{else}email:{ required:'请填写邮箱',email:'邮箱格式不正确' }{/if},deliveryTypeId:{ required:'请选择物流方式' }
						},
						submitHandler:function(){
							$("#addOrderForm").ajaxSubmit({ cache:false,dataType:"json",success:function(rt){
									var tr = $("table.orderGoods").find("tbody").find("tr");
									if(rt.success){ location.href="/qkadmin/order/act/edit/id/"+rt.data.orderId+"/"; }else{ $("#ajaxError").html(rt.message); }
								}
							});
						}
					});
					$("#addOrderForm").submit();
				}
			},{ text:'取消',click:function(){ $('#addOrderDialog').dialog('destory');$('#addOrderDialog').remove(); } }]
		});
	
		var form = createForm(config);
		$("#addOrderDialog").append(form);
		$("#addOrderDialog").append($("<div id='ajaxError'></div>"));
}
function addGoods(orderId){
	if(!orderId || isNaN(parseInt(orderId,10))){
		{if $smarty.get.act=="add"}
		var loginName = $("input[name='loginName']");
		var email = $("input[name='email']");
		if(loginName.length==1){
			if(loginName.val()==""){ alert("请先输入用户登录名"); return false; }else{ loginName = loginName.val();}
		}
		if(email.length==1){
			if(email.val()==""){ alert("请先输入用户电子邮件"); return false; }else{ email = email.val(); }		
		}
		if(!loginName && !email){ return false; }
		{/if}
		return false;
	}
	var config = { name:"",action:"{$adminUrl}order/",method:"POST",className:"form",id:"addGoodsForm",input:[{ label:"{if $cfg_lang=='zh-cn'}商品{else}Goods {/if}ID",name:"goodsId",className:"text",tip:"" },{ label:"购买数量",name:"pnum",className:"text",tip:"" }],
		hidden:[{ name:"act",value:"addGoods" },{ name:"inajax",value:"1" },{ name:"orderId",value:orderId}]}
	var html = "";
	$('<div id="addGoodsDialog"></div>').dialog({ title:'添加购买商品',width:570,height:200,modal:true,resizable:false,dialogClass:'layer_global_main',buttons:[{
			text:'确定',click:function(){
					$("#addGoodsForm").validate({
						rules:{ goodsId:{ required:true,number:true },pnum:{ required:true,number:true } },
						messages:{ goodsId:{ required:'商品ID不能为空',number:'商品ID为数字' },pnum:{ required:'购买数量不能为空',number:'购买数量需为数字' } },
						submitHandler:function(){
								$("#addGoodsForm").ajaxSubmit({ dataType:"json",success:function(rt){
									var tr = $("table.orderGoods").find("tbody").find("tr");
									if(rt.success){
										var newline = $("<tr></tr>");
										newline.append("<td>"+rt.data.name+"</td>");
										newline.append("<td>"+$("input[name='pnum']").val()+"</td>");
										newline.append("<td>"+rt.data.price+"</td>");
										newline.append('<td><a href="javascript:void(0)" onclick="changeGoodsNum('+rt.data.id+')">修改订购数量</a></td>');
										tr.eq(tr.length-2).after(newline);
										$("input[name='amount']").val(rt.data.amount);
										$("#addGoodsDialog").dialog("destroy");$("#addGoodsDialog").remove();
									}else{
										$("#ajaxError").html(rt.message);
									}
								}
							});
						}
					});
					$("#addGoodsForm").submit();
			}
		}]
	});
		var form = createForm(config);
		$("#addGoodsDialog").append(form);
		$("#addGoodsDialog").append($("<div id='ajaxError'></div>"));
}

$(function(){	
	$("a#deleteChecked").button().click(function(){
		if(!confirm(lang[4])){ return false; }		
		var id = [];
		$("input[name='id[]']:checkbox:checked").each(function(index){
			id[index] = $(this).val();
			var sid = $(this).val();
			if(sid){
				var tr = $(this).parent("td").parent("tr");
				var sdata = "inajax=1&act=del&id="+sid;
				$.ajax({
					type:"POST",
					url:"{$adminUrl}order",
					data:sdata,
					dataType:"json",
					success:function(data){
						if(data.success){
							tr.fadeOut("slow").remove();
						}else{
							alert(data.message);
						}
					}
				});			
			}
		});
		
	});
	$(".delBtn").click(function(){
		if(!confirm(lang[4])){ return false; }
		var id = $(this).attr("sid");
		var tr = $(this).parent("td").parent("tr");
		if(!id || !parseInt(id)){ return false; }
		$.ajax({
			type:"POST",
			url:"{$adminUrl}order",
			data:"inajax=1&act=del&id="+id,
			dataType:"json",
			success:function(json){
				if(json.success){
					tr.slideUp().remove();
				}else{
					qlib.tips(json.message);
				}
			}
		})
	});

	$("#tInfo").find("tr").each(function(index){
		if(index>0){
			$(this).find("td").eq(1).css({ "text-align":"left" });
		}
	});


	$(".shipBtn").click(function(){

		//pop({ "title":"{$orderLang.deliveryInfo}",width:770,height:350,"text":"","type":false,"top":10 },function(){
		//	$("#shipForm").submit();
			//alert($("#shipForm").html());
		//	return false;
		//});
		var id = $(this).parent("td").parent("tr").attr('sid');
		$.ajax({
			type:"POST",
			url:"{$baseUrl}data/order/",
			data:"id="+id,
			dataType:"json",
			cache:false,
			success:function(data){
				var text = data;
				var html = "<form action='{$adminUrl}order/' method='post' class='form' id='shipForm'>";
				html += "<input type='hidden' name='inajax' value='1' /><input type='hidden' name='act' value='ship' /><input type='hidden' name='id' value='"+id+"' />";
				html += "<table>";
				html += "<tr><td>{$orderLang.receiver}：</td><td>"+(data.receiver?data.receiver:(data.firstName +" "+ data.lastName))+"</td></tr>";
				html += "<tr><td>{$orderLang.street}：</td><td>"+G.app.form_ctrl.location.getLocName(data.receiveAddrId)+" "+data.address+" "+data.suburb+"</td></tr>";
				html += "<tr><td>{$orderLang.zipCode}：</td><td>"+data.zipCode+"</td></tr>";
				html += "<tr><td>{$orderLang.deliveryType}：</td><td>"+data.deliveryTypeName+"</td></tr>";
				html += "<tr><td>{$orderLang.mobile}：</td><td>"+data.mobile+"</td></tr>";
				html += "<tr><td>{$orderLang.phone}：</td><td>"+data.phone+"</td></tr>";
				html += "<tr><td></td><td></td></tr>";
				html += "<tr><td>{$orderLang.deliveryNumber}：</td><td><input type='text' name='deliveryNumber' value='"+data.deliveryNumber+"'></td></tr>";
				html += "<tr><td>{$orderLang.deliveryRemark}：</td><td><textarea name='deliveryRemark' class='w400' style='height:50px'>"+data.deliveryRemark+"</textarea></td></tr>";
				html += "</table></form>";
				//$("#popup").find(".container").find(".content").find("div").html(html);
				var fhdialog = $(html).dialog({ title:'发货【物流信息】',dialogClass:'layer_global_main',width:770,height:400,modal:true,resizable:false,buttons:{
						'确定':function(){
							$('#shipForm').submit();
						}
					}
				});
				$("#shipForm").validate({
					rules:{
						deliveryNumber:{ required:true }
					},
					messages:{
						deliveryNumber:{ required:"{$orderLang.error.deliveryNumber}" }					
					},
					submitHandler:function(){
						$("#shipForm").ajaxSubmit({ dataType:"json",success:showResponse })
						return false;
					}
				});
				function showResponse(rt,st){
					if(rt.success){
						fhdialog.dialog("destroy"); fhdialog.remove();
						qlib.tips({ icon:'success',msg:"{$lang.admin.success}" });
					}else{
						qlib.tips(rt.message);
						return false;
					}
				}
			}
		});
	});

	$("#J_addOrder").button().click(function(){ addOrder(); });
});
</script>
{include "footer.inc.html"}
