/**
 * @namespace com.thesis.control.TOrderDetailControl
 * @author 钟军锐 August.R@263.net
 */

/** @id TOrderDetailControl */
function TOrderDetailControl(){
	this.mapping ={
		btnOK:"btnODADOK",
		btnCancel:"btnODADCancel",
		divOrderDetail:"TOrderDetail",
		divAlterDialog:"divOrderDetailAlterDialog",
		spanName:"spanODADName",
		spanMaterial:"spanODADMaterial",
		spanSpec:"spanODADSpec",
		spanOrigin:"spanODADOrigin",
		spanWarehouse:"spanODADWarehouse",
		spanUnit:"spanODADUnit",
		txtAmount:"txtODADAmount",
		tbodyDetailRow:"TOrderDetailRow"
	};
	
	M$(this.mapping);
	
	this.Box = {};
	this.Box.List = {};
	this.Box.List.Frame = this.mapping.divOrderDetail;
	this.Box.List.Body = this.mapping.tbodyDetailRow;
	this.Box.List.StateID = -1;
	
	this.Exe = {};
	this.Exe.btnOK = this.mapping.btnOK;
	this.Exe.btnCancel = this.mapping.btnCancel;
	
	this.Dialog = {};
	this.Dialog.Alter = {};
	this.Dialog.Alter.Form = new CommonDialog("订单明细", this.mapping.divAlterDialog, true, true);
	this.Dialog.Alter.OID = -1;
	this.Dialog.Alter.Name = this.mapping.spanName;
	this.Dialog.Alter.Material = this.mapping.spanMaterial;
	this.Dialog.Alter.Spec = this.mapping.spanSpec;
	this.Dialog.Alter.Origin = this.mapping.spanOrigin;
	this.Dialog.Alter.Warehouse = this.mapping.spanWarehouse;
	this.Dialog.Alter.Unit = this.mapping.spanUnit;
	this.Dialog.Alter.Amount = this.mapping.txtAmount;
	
	this.ListForm = {};
	this.ListForm.sURL = "/Service/TOrderService.asmx/getOrderDetailList";
	this.ListForm.oParams = {};
	this.ListForm.oParams.OrderID = -1;
	
	this.RemoveForm = {};
	this.RemoveForm.sURL = "/Service/TOrderService.asmx/removeOrderDetail";
	this.RemoveForm.oParams = {};
	this.RemoveForm.oParams.OrderID = -1;
	this.RemoveForm.oParams.OrderDetailID = -1;
	
	this.AlterForm = {};
	this.AlterForm.sURL = "/Service/TOrderService.asmx/alterDetail";
	this.AlterForm.oParams = {};
	this.AlterForm.oParams.OrderID = -1;
	this.AlterForm.oParams.DetailID = -1;
	this.AlterForm.oParams.Amount = -1;
	
	this.BO = {};
	this.BO.dlist = new BaseBO(OrderDetailObject,this.ListForm,true);
	this.BO.dalter = new BaseBO(SystemMessage,this.AlterForm,true);
	this.BO.dremove = new BaseBO(SystemMessage,this.RemoveForm,true);
	
	this.IsShowed = false;
	////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////
	if (typeof TOrderDetailControl._initialized == "undefined") {
		TOrderDetailControl._initialized = true;
		
		/** @id show */
		TOrderDetailControl.prototype.show = function(){
			if (!this.IsShowed) {
				this.IsShowed = true;
				this.Box.List.Frame.style.display = "block";
			}
		};
		
		/** @id hide */
		TOrderDetailControl.prototype.hide = function(){
			this.IsShowed = false;
			this.Box.List.Frame.style.display = "none";
		};
		
		/** @id clear */
		TOrderDetailControl.prototype.clear = function(){
			while(this.Box.List.Body.firstChild)
				this.Box.List.Body.removeChild(this.Box.List.Body.firstChild);
		};
		
		/** @id handleException */
		TOrderDetailControl.prototype.handleException = function(ex){
			this.Dialog.Alter.Form.exception(ex,true);
			if(ex.ErrorCode == Configuration.Symbol.SignInRequest){
				uEvent.Listener.notify("TradeMember","SignInRequest");
			}
		};
		
		/** @id handleMessage */
		TOrderDetailControl.prototype.handleMessage = function(msg){
			if(msg.code == Configuration.Symbol.TOrderDetailAltered || 
				msg.code == Configuration.Symbol.TOrderDetailRemoved){
				this.Dialog.Alter.Form.hide();
			}
			else{
				this.Dialog.Alter.Form.message(msg);
			}
		};
		
		/** @id refresh */
		TOrderDetailControl.prototype.refresh = function(sOID){
			this.ListForm.oParams.OrderID = sOID;
			this.Dialog.Alter.OID = sOID;
			this.Dialog.Alter.Form.busy("正在获取明细列表...");
			this.BO.dlist.execute();
		};
		
		/** @id alter */
		TOrderDetailControl.prototype.alter = function(){
			this.AlterForm.oParams.Amount = this.Dialog.Alter.Amount.value;
			this.Dialog.Alter.Form.busy("正在修改订单明细...");
			this.BO.dalter.execute();
		};
		
		/** @id showAlterDialog */
		TOrderDetailControl.prototype.showAlterDialog = function(od){
			this.Dialog.Alter.Name.innerHTML = od.PName?od.PName:"";
			this.Dialog.Alter.Material.innerHTML = od.Material?od.Material:"";
			this.Dialog.Alter.Spec.innerHTML = od.Space?od.Space:"";
			this.Dialog.Alter.Origin.innerHTML = od.Origin?od.Origin:"";
			this.Dialog.Alter.Warehouse.innerHTML = od.Warehouse?od.Warehouse:"";
			this.Dialog.Alter.Unit.innerHTML = od.Unit?od.Unit:"";
			this.Dialog.Alter.Amount.value = od.Amount?od.Amount:"";
			this.AlterForm.oParams.DetailID = od.DID;
			this.Dialog.Alter.Form.ask();
		};
		
		/** @id registerDetail */
		TOrderDetailControl.prototype.registerDetail = function(od,btnM,btnC){
			var Root = this;
			uEvent.addBrowserHandler(btnC,"click",function(){
				Root.RemoveForm.oParams.OrderID = Root.Dialog.Alter.OID;
				Root.RemoveForm.oParams.OrderDetailID = od.DID;
				Root.Dialog.Alter.Form.busy("正在移除订单明细...");
				Root.BO.dremove.execute();
			});
			uEvent.addBrowserHandler(btnM,"click",function(){
				Root.AlterForm.oParams.OrderID = Root.Dialog.Alter.OID;
				Root.AlterForm.oParams.DetailID = od.DID;
				Root.showAlterDialog(od);
			});
		};
		
		/** @id handleDList */
		TOrderDetailControl.prototype.handleDList = function(lst){
			this.clear();
			var Columns =["PName","Material","Space","Origin","Warehouse","Price","Unit","Amount","DMoney","DeliveryID"];
			for(var i=0; i<lst.length; i++){
				var tr = C$("tr");
				if(i%2!=0)
					tr.className ="tr_style1";
				else
					tr.className = "tr_style2";
				var t=lst[i];
				var td=null;
				for (var j in Columns) {
					td = C$("td");
					if (typeof t[Columns[j]] != "undefined") 
						td.appendChild(T$(t[Columns[j]]));
					tr.appendChild(td);
				}
				td = C$("td");
				if(this.Box.List.StateID == 4){
					var btnM = C$("Input");
					btnM.type = "Button";
					btnM.value = "修改";
					td.appendChild(btnM);
					var btnC = C$("Input");
					btnC.type = "Button";
					btnC.value = "取消";
					td.appendChild(btnC);
					this.registerDetail(t,btnM,btnC);
				}
				tr.appendChild(td);
				this.Box.List.Body.appendChild(tr);
			}
			this.Dialog.Alter.Form.hide();
			this.show();
		};
		
		/** @id initialize */
		TOrderDetailControl.prototype.initialize = function(Root){
			this.hide();
			
			uEvent.addBrowserHandler(this.Exe.btnCancel,"click",function(){
				Root.Dialog.Alter.Form.hide();
			});
			uEvent.addBrowserHandler(this.Exe.btnOK,"click",function(){
				Root.alter();
			});
			
			uEvent.Listener.add(this.BO.dlist,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.dlist,"Executed",function(lst){
				Root.handleDList(lst);
			});
			uEvent.Listener.add(this.BO.dalter,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.dalter,"Message",function(msg){
				Root.handleMessage(msg);
			});
			uEvent.Listener.add(this.BO.dremove,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.dremove,"Message",function(msg){
				Root.handleMessage(msg);
			});
			
			uEvent.Listener.add("OrderDetailList","show",function(odr){
				Root.Box.List.StateID = odr.StateID;
				Root.refresh(odr.ID);
			});
			uEvent.Listener.add("OrderDetailList","hide",function(odr){
				if(!odr || Root.Box.List.StateID != odr.StateID)
					Root.hide();
			});
		};
	}
	
	this.initialize(this);
	
}
