<%@ Page CodeFileBaseClass="IlgharCMS.Lib.AdminPageBase" %>
<%@ Register TagPrefix="IlgharUC" TagName="Ime" Src="../../ime.dwt.ascx"%>
<%@ Register TagPrefix="IlgharUC" TagName="LanguageMenu" Src="../../language_menu.dwt.ascx"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- TemplateInfo codeOutsideHTMLIsLocked="true" -->
<ilghar:Get ID="Order" Object="Order" InnerID="Request.QueryString['ID']" />
<base target="_self" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Expires" content="Mon,1 Fab 1979 23:36:05 GMT" />
<title>生成退货单</title>
<ilghar:Comment><link rel="stylesheet" type="text/css" href="../../S01/CHS/css/common.css" /></ilghar:Comment>
<link rel="stylesheet" href="<%=CssPath%>common.css" />
<script type="text/javascript" src="<%=CommonPath%>ajax.js"></script>
<script type="text/javascript" src="<%=JsPath%>common.js"></script>
<ilgharUC:Ime Ime="true" Eot="true" runat="server" />
<style type="text/css">
TD TABLE{background:#FFF}
TD TABLE TH{width:100px}
#DetailTable THEAD TH,#DetailTable TBODY TD{width:auto;text-align:center}
</style>
<script type="text/javascript">
var _action = null;
var _productIndex = 0;
var _product = new Array();
function XmlHttpCallBack(xmlHttp)
{
	if (xmlHttp.status == 200)
	{
		if (_action == "Return")
		{
			window.status = "正在生成发货单...";
			_action = "MakeReturn";
			_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Return&Operation=Add", "Number=" + _product[_productIndex].ReturnNumber + "&OrderID=" + _product[_productIndex].OrderID + "&OrderDetailID=" + _product[_productIndex].OrderDetailID + "&Amount=" + _product[_productIndex].Amount + "&Time=" + _product[_productIndex].Time + "&Memo=" + _product[_productIndex].Memo, XmlHttpCallBack);
		}
		else if (_action == "MakeReturn")
		{
			_productIndex++;
			if (_productIndex < _product.length)
			{
				_action = "Return";
				_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order.Detail&Operation=Return&ID=" + _product[_productIndex].InnerID, "Amount=" + _product[_productIndex].Amount, XmlHttpCallBack);
			}
			else
			{
				alert("退货成功！");
				document.getElementById("Submit").disabled = false;
				window.status = "";
				
				if (g_browser == "IE")
					window.dialogArguments.OnSearch();
				else
					window.opener.OnSearch();
				window.close();			
			}
		}
	}
	else
	{
		if (_action == "Return")
			alert("退货失败！请查看日志！");
		else if (_action == "MakeReturn")
			alert("生成退货单失败！请查看日志！");
		else
			alert("失败！请查看日志！");
		document.getElementById("Submit").disabled = false;
		window.status = "";
	}
}

function OnSubmit(form)
{
	var dateTime = /^(\d{1,4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/;
	if (!dateTime.test(form.Time.value))
	{
		alert("退货时间格式不正确！");
		form.Time.select();
		return false;
	}
	
	var number = FormatDate(new Date(), "YYYYMMDDhhmmssSSS");

	_productIndex = 0;
	_product.length = 0;
	var rows = document.getElementById("DetailTable").rows;
	for (var i = 1; i < rows.length; i++)
	{
		var amount = document.getElementById("Amount" + rows[i].id);
		if (amount == null)
			continue;
			
		if (!amount.value.match(/^\d+$/))
		{
			alert("退货量格式不正确！");
			amount.select();
			return false;
		}
		
		if (parseInt(amount.value) == 0)
			continue;
		
		if (parseInt(rows[i].cells[3].innerHTML) < parseInt(amount.value))
		{
			alert("退货量不能超过已发货量！");
			amount.select();
			return false;
		}
		
		_product[_product.length] = new Object();
		_product[_product.length - 1].InnerID = rows[i].id;
		_product[_product.length - 1].ReturnNumber = (document.getElementById("Number" + rows[i].id).value == "" ? number + "-" + i : document.getElementById("Number" + rows[i].id).value);
		_product[_product.length - 1].OrderID = "<%=Order[ID]%>";
		_product[_product.length - 1].OrderDetailID = rows[i].guid;
		_product[_product.length - 1].Time = form.Time.value;
		_product[_product.length - 1].Amount = amount.value;
		_product[_product.length - 1].Memo = encodeURIComponent(document.getElementById("Memo" + rows[i].id).value);
	}
	
	if (_product.length == 0)
	{
		alert("没有任何可退货的产品！");
		return false;
	}
	
	document.getElementById("Submit").disabled = true;
	window.status = "正在退货...";
	_action = "Return";
	if (_productIndex < _product.length)
		_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order.Detail&Operation=Return&ID=" + _product[_productIndex].InnerID, "Amount=" + _product[_productIndex].Amount, XmlHttpCallBack);
	return false;
}
</script>
</head>
<body>
<div class="Block">
	<ul class="Title">
		<li class="Left"></li>
		<li class="Middle">生成发货单</li>
		<li class="Right"></li>
	</ul>
	<form method="post" onsubmit="return OnSubmit(this)">
		<table border="0" cellpadding="4" cellspacing="1">
			<tbody>
				<tr>
					<th>订单号：</th>
					<td><%=Order[Number]%></td>
				</tr>
				<tr>
					<th>发货时间：</th>
					<td><input type="text" name="Time" value="<%=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")%>" maxlength="20" class="Text" /></td>
				</tr>
				<tr>
					<td colspan="2">
						<table id="DetailTable" border="0" cellpadding="4" cellspacing="1">
							<thead>
								<tr>
									<th style="width:100px">退货单</th>
									<th>名称</th>
									<th style="width:60px">需发货</th>
									<th style="width:60px">已发货</th>
									<th style="width:60px">退货量</th>
									<th>备注</th>
								</tr>
							</thead>
							<tbody>
								<ilghar:List Object="Order.Detail" Condition="OrderID=Order[ID] and ParentID IS NULL">
									<ilghar:Record>
										<tr id="<%=InnerID%>" guid="<%=ID%>">
											<td><input type="text" id="Number<%=InnerID%>" size="10" maxlength="50" class="Text" /></td>
											<td class="<%=ContentLang%>Font" style="text-align:left">
												<%=Name%>
												<%If String.Equals(Kind, "AutoSite")%>
													<ilghar:List Object="Order.Detail" Condition="OrderID=Order[ID] and ParentID=ID">
														<ilghar:Record>
															<blockquote style="margin:0 0 0 20px">+ <%=Name%></blockquote>
														</ilghar:Record>
													</ilghar:List>
												<%EndIf%>
											</td>
											<td><%=Amount%></td>
											<td><%=DeliveredAmount%></td>
											<td>
												<%If Convert.ToInt32(DeliveredAmount) > 0%>
													<input type="text" id="Amount<%=InnerID%>" value="0" size="5" />
												<%Else%>
													0
												<%EndIf%>
											</td>
											<td>
												<textarea id="Memo<%=InnerID%>" cols="15" rows="3" wrap="off" class="Text <%=ContentLang%>Font"></textarea>
											</td>
										</tr>
										<%If String.Equals(Kind, "Set")%>
											<ilghar:List Object="Order.Detail" Condition="OrderID=Order[ID] and ParentID=ID">
												<ilghar:Record>
													<tr id="<%=InnerID%>" guid="<%=ID%>">
														<td><input type="text" id="Number<%=InnerID%>" size="10" maxlength="50" class="Text" /></td>
														<td style="padding-left:20px;text-align:left">+<%=Name%></td>
														<td><%=Amount%></td>
														<td><%=DeliveredAmount%></td>
														<td>
															<%If Convert.ToInt32(DeliveredAmount) > 0%>
																<input type="text" id="Amount<%=InnerID%>" value="0" size="5" />
															<%Else%>
																0
															<%EndIf%>
														</td>
														<td>
															<textarea id="Memo<%=InnerID%>" cols="15" rows="3" wrap="off" class="Text <%=ContentLang%>Font"></textarea>
														</td>
													</tr>
												</ilghar:Record>
											</ilghar:List>
										<%EndIf%>
									</ilghar:Record>
								</ilghar:List>
							</tbody>
						</table>
					</td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="2">
						<input type="submit" id="Submit" value="退货" />
					</td>
				</tr>
			</tfoot>
		</table>
	</form>
	<div class="Footer"></div>
</div>
</body>
</html>