<%@ 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="Config" Object="InstanceConfig" Operation="GetBasicSettings()" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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="<%=Manager.GetModulePath(null)%><%=Instance[IlgharCMS.SystemModule.Alias.Module.Namespace]%>/alias.js"></script>
<script type="text/javascript" src="<%=CommonPath%>ajax.js"></script>
<script type="text/javascript" src="<%=CommonPath%>xmldom.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 _products = new Array();
var _xmlDoc = CreateXmlDom();
var _orderID;
var _childIndex = 0;
var _parentID;
var _price = 0;

var _discount = new Array();
<ilghar:List Object="Discount">
	<ilghar:Record>
		_discount[_discount.length] = new Object();
		_discount[_discount.length - 1].RoleInnerID = "<%=RoleInnerID%>";
		_discount[_discount.length - 1].Amount = "<%=Amount%>";
	</ilghar:Record>
</ilghar:List>

function XmlHttpCallBack(xmlHttp)
{
	if (xmlHttp.status == 200)
	{
		if (_action == "Add")
		{
			_orderID = xmlHttp.responseText;
			window.status = "添加订单完成！";
			
			if (_productIndex < _products.length)
			{
				_action = "AddProduct";
				window.status = "正在添加产品...";
				_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order.Detail&Operation=Add", "OrderID=" + _orderID + "&ProductID=" + _products[_productIndex].ProductID + "&ProductNumber=" + _products[_productIndex].ProductNumber + "&Name=" + _products[_productIndex].Name + "&Kind=" + _products[_productIndex].Kind + "&Amount=" + _products[_productIndex].Amount + "&UnitID=" + _products[_productIndex].UnitID + "&Price=" + _products[_productIndex].Price, XmlHttpCallBack);
			}
			else
			{
				alert("添加成功！");
				document.getElementById("Submit").disabled = false;
				window.status = "";
			}
		}
		else if (_action == "AddProduct" || _action == "AddChild")
		{
			if (_childIndex < _products[_productIndex].Children.length)
			{
				if (_action == "AddProduct")
				{
					_childIndex = 0;
					_parentID = xmlHttp.responseText;
				}

				var child = _products[_productIndex].Children[_childIndex];
				_action = "AddChild";
				_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order.Detail&Operation=Add", "OrderID=" + _orderID + "&ParentID=" + _parentID + "&ProductID=" + child.ProductID + "&ProductNumber=" + child.ProductNumber + "&Name=" + child.Name + "&Kind=" + child.Kind + "&UnitAmount=" + child.UnitAmount + "&Amount=" + child.Amount + "&UnitID=" + child.UnitID + "&Price=" + child.Price + "&IsBinding=" + child.IsBinding + "&IsPresent=" + child.IsPresent, XmlHttpCallBack);
				_childIndex++;
			}
			else
			{
				_productIndex++;
				
				if (_productIndex < _products.length)
				{
					_action = "AddProduct";
					_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order.Detail&Operation=Add", "OrderID=" + _orderID + "&ProductID=" + _products[_productIndex].ProductID + "&ProductNumber=" + _products[_productIndex].ProductNumber + "&Name=" + _products[_productIndex].Name + "&Kind=" + _products[_productIndex].Kind + "&Amount=" + _products[_productIndex].Amount + "&UnitID=" + _products[_productIndex].UnitID + "&Price=" + _products[_productIndex].Price, XmlHttpCallBack);
				}
				else
				{
					alert("添加成功！");
					document.getElementById("Submit").disabled = false;
					window.status = "";
				}
			}
		}
		else if (_action == "BindingList")
		{
			var html = "";
			_xmlDoc.loadXML(xmlHttp.responseText);
			var items = _xmlDoc.documentElement.childNodes;
			for (var i = 0; i < items.length; i++)
			{
				if (typeof (items[i].selectSingleNode) == "undefined")
					continue;
				
				var id = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingID).text;
				var number = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingNumber).text;
				var name = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingName).text;
				var kind = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingKind).text;
				var price = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingPrice).text;
				var unitID = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingUnitID).text;
				var unitName = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.BindingUnitName).text;
				var amount = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.Amount).text;
				var isPresent = items[i].selectSingleNode(IlgharCMS.HostModule.Product_Binding.IsPresent).text;
				html += "<blockquote productID='" + id + "' productNumber='" + number + "' name='" + name + "' kind='" + kind + "' price='" + price + "' unitID='" + unitID + "' unitAmount='" + amount + "' amount='" + amount + "' isPresent='" + isPresent + "' style='margin:0 0 0 20px'> + " + name + " - <span unitAmount='" + amount + "'>" + amount + "</span>" + unitName + "</blockquote>";
			}
			
			if (html != "")
			{
				var table = document.getElementById("DetailTable");
				table.rows[1].cells[1].innerHTML += html;
			}
		}
	}
	else
	{
		if (_action == "Add")
			alert("添加失败！请查看日志！");
		else if (_action == "AddProduct")
			alert("添加产品失败！请查看日志！");
		else if (_action == "AddChild")
			alert("添加子产品失败！请查看日志！");
		else
			alert("失败！请查看日志！");
		document.getElementById("Submit").disabled = false;
		window.status = "";
	}
}

function OnSubmit(form)
{
	if (!form.PriceAdjustment.value.match(/^-?\d+(\.\d+)?$/))
	{
		alert("折扣或涨价不正确！");
		form.PriceAdjustment.select();
		return false;
	}
	
	if (document.getElementById("Member").memberID == "")
	{
		alert("请选择会员！");
		return false;
	}
	
	var detailTable = document.getElementById("DetailTable");
	if (detailTable.rows.length == 2)
	{
		alert("没有选择任何产品！");
		return false;
	}
	
	_productIndex = 0;
	_products.length = 0;
	for (var i = 2; i < detailTable.rows.length; i++)
	{
		var row = detailTable.rows[i];
		
		_products[_products.length] = new Object();
		_products[_products.length - 1].ProductID = row.productID;
		_products[_products.length - 1].ProductNumber = row.productNumber;
		_products[_products.length - 1].Name = row.name;
		_products[_products.length - 1].Kind = row.kind;
		_products[_products.length - 1].Amount = row.amount;
		_products[_products.length - 1].UnitID = row.unitID;
		_products[_products.length - 1].Price = row.price;
		
		var children = new Array();
		var bqs = row.cells[1].getElementsByTagName("BLOCKQUOTE");
		for (var j = 0; j < bqs.length; j++)
		{
			var child = new Object();
			child.ProductID = bqs[j].productID;
			child.ProductNumber = bqs[j].productNumber;
			child.Name = bqs[j].name;
			child.Kind = bqs[j].kind;
			child.UnitAmount = bqs[j].unitAmount;
			child.Amount = bqs[j].amount;
			child.UnitID = bqs[j].unitID;
			child.Price = bqs[j].price;
			child.IsBinding = true;
			child.IsPresent = bqs[j].isPresent;
			children[children.length] = child;
		}
		_products[_products.length - 1].Children = children;
	}

	var data = "Number=" + (form.Number.value == "" ? FormatDate(new Date(), "YYYYMMDDhhmmssSSS") : form.Number.value);
	data += "&Member=" + encodeURIComponent(document.getElementById("Member").member);
	data += "&MemberID=" + document.getElementById("Member").memberID;
	data += "&Discount=" + document.getElementById("Member").discount;
	data += "&NeedInvoice=" + document.getElementById("NeedInvoiceYes").checked;
	data += "&TaxRate=<%=Config[TaxRate]%>";
	data += "&PriceAdjustment=" + form.PriceAdjustment.value;
	data += "&Memo=" + encodeURIComponent(form.Memo.value);
	data += "&Price=" + _price;
	data += "&Sum=" + document.getElementById("SumTD").innerHTML;
	
	document.getElementById("Submit").disabled = true;
	window.status = "正在操作...";
	_action = "Add";
	_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Order&Operation=Add", data, XmlHttpCallBack);
	return false;
}

function ShowSelectDialog(type)
{
	var path;
	if (type == "Member")
		path = "select_member.aspx?Lang=<%=ContentLang%>";
	else if (type == "Product")
		path = "../product/select.aspx?Lang=<%=ContentLang%>&Type=" + type;
		
	path = HandleFake404(path);
	if (g_browser == "IE")
		window.showModalDialog(path, window, "status=0;dialogWidth=500px;dialogHeight=500px;center=1;help=0;resizable=1;scroll=1");
	else
		window.open(path,null,"width=500,height=500,menubar=no,toolbar=no,location=no,scrollbars=yes,status=no,modal=yes");
}

function SelectMember(id, name, roleInnerIDs)
{
	var member = document.getElementById("Member");
	member.innerHTML = name;
	member.memberID = id;
	member.member = name;
	
	var discount = 100;
	for (var i = 0; i < _discount.length; i++)
	{
		if (roleInnerIDs.indexOf("|" + _discount[i].RoleInnerID + "|") != -1)
		{
			if (parseInt(_discount[i].Amount) < discount)
				discount = parseInt(_discount[i].Amount);
		}
	}
	member.discount = discount;
	
	Compute();
}

function SelectProduct(control)
{
	var table = document.getElementById("DetailTable");
	var row = table.rows[1];
	row.cells[0].innerHTML = control.ProductNumber;
	row.cells[1].innerHTML = control.Name;
	row.cells[2].innerHTML = control.Price;
	
	row.productID = control.ProductID;
	row.productNumber = control.ProductNumber;
	row.name = control.Name;
	row.kind = control.Kind;
	row.price = control.Price;
	row.unitID = control.UnitID;

	document.getElementById("UnitName").innerHTML = control.UnitName;
	
	_action = "BindingList";
	_ajax.Post("<%=WebPath%>ilghar.aspx?Lang=<%=ContentLang%>&Object=Product.Binding&Operation=List&Condition=" + encodeURIComponent(IlgharCMS.HostModule.Product_Binding.ProductID + "=" + control.ProductID), null, XmlHttpCallBack);	
}

function AddProduct()
{
	var table = document.getElementById("DetailTable");
	var row1 = table.rows[1];
	for (var i = 2; i < table.rows.length; i++)
	{
		if (table.rows[i].productID == row1.productID)
		{
			alert("该产品已经添加！");
			return;
		}
	}
	
	if (!document.forms[0].Amount.value.match(/^[1-9]\d*$/))
	{
		alert("数量不正确！");
		document.forms[0].Amount.select();
		return;
	}
	
	if (typeof(row1.productNumber) == "undefined")
	{
		alert("请选择产品！");
		return;
	}
	
	var row = table.insertRow();
	row.productID = row1.productID;
	row.productNumber = row1.productNumber;
	row.name = row1.name;
	row.kind = row1.kind;
	row.price = row1.price;
	row.unitID = row1.unitID;
	row.amount = document.forms[0].Amount.value;
	
	var cell = row.insertCell();
	cell.innerHTML = row.productNumber;
	cell = row.insertCell();
	cell.innerHTML = row1.cells[1].innerHTML;
	cell.style.textAlign = "left";
	cell = row.insertCell();
	cell.innerHTML = row.price;
	cell = row.insertCell();
	cell.innerHTML = document.forms[0].Amount.value;
	cell = row.insertCell();
	cell.innerHTML = "<input type=\"button\" value=\"删除\" onclick=\"DeleteProduct(this)\" /> ";
	
	Compute();
}

function DeleteProduct(input)
{
	document.getElementById("DetailTable").deleteRow(input.parentNode.parentNode.rowIndex);
	Compute();
}

function Compute()
{
	var form = document.forms[0];
	var discount = parseFloat(document.getElementById("Member").discount);
	var taxRate = <%=Config[TaxRate]%>;
	var priceAdjustment = 0;
	_price = 0;
	
	if (!form.PriceAdjustment.value.match(/^-?\d+(\.\d{1,3})?$/))
	{
		alert("折扣或涨价格式不正确！");
		return;
	}
	priceAdjustment = parseFloat(form.PriceAdjustment.value);
	
	var detailTable = document.getElementById("DetailTable");
	if (detailTable.rows.length == 2)
	{
		document.getElementById("SumTD").innerHTML = "0";
		return;
	}
	
	for (var i = 2; i < detailTable.rows.length; i++)
	{
		var row = detailTable.rows[i];
		
		_price += parseFloat(row.price) * parseFloat(row.amount);
	}
	
	var beforeTax = _price - _price * (100 - discount) / 100;
	var afterTax = beforeTax;
	if (document.getElementById("NeedInvoiceYes").checked)
		afterTax = beforeTax + beforeTax * taxRate / 100;
	var sum = (afterTax + priceAdjustment).toFixed(2);
		
	document.getElementById("SumTD").innerHTML = sum;
}

function Amount_OnKeyUp(amount)
{
	if (!amount.match(/^[1-9]\d*$/))
	{
		alert("数量不正确！");
		return;
	}
	
	var table = document.getElementById("DetailTable");
	var spans = table.rows[1].cells[1].getElementsByTagName("SPAN");
	for (var i = 0; i < spans.length; i++)
	{
		amount = parseInt(spans[i].unitAmount) * parseInt(amount);
		spans[i].innerHTML = amount;
		spans[i].parentNode.amount = amount;
	}
}
</script>
</head>
<body>
<div class="WhiteHeader">
	<div class="Left"><img src="<%=ImagePath%>close_frame.gif" onclick="OpenCloseLeftFrame()" /></div>
	<div class="Title">添加订单</div>
	<div class="Right">
		<ilgharUC:LanguageMenu runat="server" />
		<img height="15" src="<%=ImagePath%>dropdown.gif" onmouseover="this.src='<%=ImagePath%>dropdown_over.gif';ShowLanguageMenu(this);" onmouseout="this.src='<%=ImagePath%>dropdown.gif'" />
		语言：<%=CurrentLanguage%>
	</div>
</div>
<div class="Block">
	<ul class="TabMenu">
		<li class="Left"></li>
		<li class="Middle"><a href="list.aspx?Lang=<%=ContentLang%>">订单列表</a></li>
		<li class="Right"></li>
		<li class="Left Selected"></li>
		<li class="Middle Selected">添加订单</li>
		<li class="Right Selected"></li>
		<li class="Left"></li>
		<li class="Middle"><a href="detail/list.aspx?Lang=<%=ContentLang%>">产品列表</a></li>
		<li class="Right"></li>
	</ul>
	<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><input type="text" name="Number" size="30" maxlength="50" class="Text" />如果留空，则会自动生成</td>
				</tr>
				<tr>
					<th>会员：</th>
					<td><span id="Member" member="" memberID="" discount="0" class="<%=ContentLang%>Font"></span><input type="button" value="选择" onclick="ShowSelectDialog('Member')" /></td>
				</tr>
				<tr>
					<th>发票：</th>
					<td>
						<table border="0" cellpadding="4" cellspacing="1">
							<tr>
								<th>是否需要：</th>
								<td>
									<input type="radio" name="NeedInvoice" id="NeedInvoiceYes" onclick="Compute()" /><label for="NeedInvoiceYes">是</label>
									<input type="radio" name="NeedInvoice" id="NeedInvoiceNo" checked="checked" onclick="Compute()" /><label for="NeedInvoiceNo">否</label>
								</td>
							</tr>
							<tr>
								<th>税率：</th>
								<td><%=Config[TaxRate]%>%</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<th>折扣或涨价：</th>
					<td><input type="text" name="PriceAdjustment" value="0" size="10" maxlength="20" class="Text" onchange="Compute()" /></td>
				</tr>
				<tr>
					<th>备注：</th>
					<td><textarea name="Memo" cols="70" rows="6" wrap="off" class="Text <%=ContentLang%>Font"></textarea></td>
				</tr>
				<tr>
					<td colspan="2">
						<table id="DetailTable" border="0" cellpadding="4" cellspacing="1">
							<thead>
								<tr>
									<th style="width:120px">货号</th>
									<th>名称</th>
									<th style="width:60px">价格</th>
									<th style="width:60px">数量</th>
									<th style="width:80px">添加/删除</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>&nbsp;</td>
									<td style="text-align:left">&nbsp;</td>
									<td>&nbsp;</td>
									<td><input type="text" name="Amount" value="1" size="5" onkeyup="Amount_OnKeyUp(this.value)" /><span id="UnitName" class="<%=ContentLang%>Font"></span></td>
									<td>
										<input type="button" value="添加" onclick="AddProduct(this.form)" />
										<input type="button" value="选择" onclick="ShowSelectDialog('Product')" />
									</td>
								</tr>
							</tbody>
						</table>
					</td>
				</tr>
				<tr>
					<th>订单总价：</th>
					<td id="SumTD"></td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="2">
						<input type="submit" id="Submit" value="添加" />
						<input type="reset" value="清空" />
					</td>
				</tr>
			</tfoot>
		</table>
	</form>
	<div class="Footer"></div>
</div>
<script type="text/javascript">
HandleFake404();
</script>
</body>
</html>