<%@ 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']" />
<ilghar:Get ID="Balance" Mark="Member" Object="Deposit" Operation="GetBalance((Guid)Order[MemberID])" />
<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" />
<script type="text/javascript">
var _action = null;
var _data;
function XmlHttpCallBack(xmlHttp)
{
	if (xmlHttp.status == 200)
	{
		if (_action == "Submit")
		{
			window.status = "付款完成！";
			
			_action = "MakePayment";
			window.status = "正在生成收款单...";
			_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Payment&Operation=Add", _data, XmlHttpCallBack);
		}
		else if (_action == "MakePayment")
		{
			alert("付款成功！");
			document.getElementById("Submit").disabled = false;
			window.status = "";
			
			if (g_browser == "IE")
				window.dialogArguments.OnSearch();
			else
				window.opener.OnSearch();
			window.close();			
		}
	}
	else
	{
		if (_action == "Submit")
			alert("付款失败！请查看日志！");
		else if (_action == "MakePayment")
			alert("生成收款单失败！请查看日志！");
		else
			alert("失败！请查看日志！");
		document.getElementById("Submit").disabled = false;
		window.status = "";
	}
}

function OnSubmit(form)
{
	if (!form.Amount.value.match(/^\d+(\.\d+)?$/))
	{
		alert("付款金额格式不正确！");
		form.Amount.select();
		return false;
	}
	
	if (parseFloat(form.Amount.value) <= 0)
	{
		alert("付款金额必须大于零！");
		form.Amount.select();
		return false;
	}
	
	if (parseFloat(form.Amount.value) > (<%=Order[Sum]%> - <%=Order[HasPayed]%>))
	{
		alert("付款金额不能大于订单总价！");
		form.Amount.select();
		return false;
	}
	
	if (parseFloat(form.Amount.value) > <%=Balance%>)
	{
		alert("预存款不够！");
		form.Amount.select();
		return false;
	}
	
	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 paymentNumber = (form.Number.value == "" ? FormatDate(new Date(), "YYYYMMDDhhmmssSSS") : form.Number.value);

	_data = "OrderID=<%=Order[ID]%>";
	_data += "&Number=" + paymentNumber;
	_data += "&Amount=" + form.Amount.value;
	_data += "&Time=" + form.Time.value;
	_data += "&Memo=" + encodeURIComponent(form.Memo.value);
	
	document.getElementById("Submit").disabled = true;
	window.status = "正在操作...";
	_action = "Submit";
	_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order&Operation=Pay&ID=<%=Request.QueryString["ID"]%>", "Amount=" + form.Amount.value + "&PaymentNumber=" + paymentNumber, 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 width="120">订单号：</th>
					<td><%=Order[Number]%></td>
				</tr>
				<tr>
					<th>收款单号：</th>
					<td><input type="text" name="Number" size="30" maxlength="50" class="Text" />如果留空，则会自动生成</td>
				</tr>
				<tr>
					<th>付款金额：</th>
					<td><input type="text" name="Amount" value="0" size="5" maxlength="10" class="Text" /></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>
					<th>备注：</th>
					<td><textarea name="Memo" cols="70" rows="6" wrap="off" class="Text <%=ContentLang%>Font"></textarea></td>
				</tr>
				<tr>
					<th>需付金额：</th>
					<td><%=Convert.ToDecimal(Order[Sum]) - Convert.ToDecimal(Order[HasPayed])%></td>
				</tr>
				<tr>
					<th>预存款：</th>
					<td><%=Balance%></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>