/**
 * @namespace com.thesis.control.TOrderControl
 * @author 钟军锐 August.R@263.net
 */

/** @id TOrderControl */
function TOrderControl(){
	
	this.mapping ={
		btnCreate:"btnOrderCreate",
		btnUp:"btnOrderUp",
		btnCancel:"btnOrderUpCancel",
		btnDoCancel:"btnOrderDoCancel",
		btnUndoCancel:"btnOrderUndoCancel",
		btnFirst:"btnOrderListFirstPage",
		btnPrevious:"btnOrderListPreviousPage",
		btnNext:"btnOrderListNextPage",
		btnLast:"btnOrderListLastPage",
		btnOrderUpYes:"btnConfirmOrderUpYes",
		btnOrderUpNo:"btnConfirmOrderUpNo",
		divAlterDialog:"TOrderUpDialog",
		divCancelControl:"divCancelControl",
		divMEAccount:"MEAccountList",
		divDMDesc:"divDeliveryDescription",
		divDMDetail:"divDeliveryDetail",
		divOrderList:"TOrderList",
		divCancelDialog:"divOrderCancelDialog",
		divConfirmOrderUp:"TConfirmOrderUpDialog",
		hiddenOrderID:"TOrderID",
		spanOrderID:"spanTOrderID",
		spanOrderNum:"TOrderNum",
		spanOrderMoney:"TOrderMoney",
		spanOrderAmount:"TOrderAmount",
		spanCancelOrderID:"spanCancelOrderID",
		spanCancelOrderNum:"spanCancelOrderNum",
		spanCancelOrderMoney:"spanCancelOrderMoney",
		spanCancelOrderState:"spanCancelOrderState",
		spanPageIndex:"OrderListPageIndex",
		spanPageCount:"OrderListPageCount",
		selPurchaser:"TPurchaser",
		txtLinkMan:"TLinkMan",
		txtPhone:"TLinkPhone",
		selDeliveryMethod:"DeliveryMethodID",
		divDelDesc:"divDeliveryDescription",
		divDelDetail:"divDeliveryDetail",
		txtTruckNums:"txtCarNums",
		txtPersonIDs:"txtCardIDs",
		txtCancelReason:"txtCancelReason",
		radioBank:"MEAccount",
		tbodyOrderListRow:"TOrderListRow"
	};
	
	M$(this.mapping);
	
	this.Box = {};
	this.Box.OrderList = {};
	this.Box.OrderList.Frame = this.mapping.divOrderList;
	this.Box.OrderList.Body = this.mapping.tbodyOrderListRow;
	
	this.Pagination = {};
	this.Pagination.Size = 5;
	this.Pagination.Index =1;
	this.Pagination.Count =0;
	this.Pagination.List = new Array();
	this.Pagination.spanIndex = this.mapping.spanPageIndex;
	this.Pagination.spanCount = this.mapping.spanPageCount;
	
	this.Exe = {};
	this.Exe.btnShow = this.mapping.btnShow;
	this.Exe.btnCreate = this.mapping.btnCreate;
	this.Exe.btnUp = this.mapping.btnUp;
	this.Exe.btnCancel = this.mapping.btnCancel;
	this.Exe.btnCancelOrder = this.mapping.btnDoCancel;
	this.Exe.btnCancelUndo = this.mapping.btnUndoCancel;
	this.Exe.btnFirst = this.mapping.btnFirst;
	this.Exe.btnPrevious = this.mapping.btnPrevious;
	this.Exe.btnNext = this.mapping.btnNext;
	this.Exe.btnLast = this.mapping.btnLast;
	this.Exe.btnOrderUpYes = this.mapping.btnOrderUpYes;
	this.Exe.btnOrderUpNo = this.mapping.btnOrderUpNo;
	
	this.Dialog = {};
	this.Dialog.Alter = {};
	this.Dialog.Alter.Form = new CommonDialog("订单信息", this.mapping.divAlterDialog, true, false);
	this.Dialog.Alter.OID = this.mapping.hiddenOrderID;
	this.Dialog.Alter.sOID = this.mapping.spanOrderID;
	this.Dialog.Alter.ONum = this.mapping.spanOrderNum;
	this.Dialog.Alter.Purchase = this.mapping.selPurchaser;
	this.Dialog.Alter.LinkMan = this.mapping.txtLinkMan;
	this.Dialog.Alter.Phone = this.mapping.txtPhone;
	this.Dialog.Alter.AccountID = new MEAccountControl(this.mapping.divMEAccount);
	this.Dialog.Alter.DMID = this.mapping.selDeliveryMethod;
	this.Dialog.Alter.TruckNums = this.mapping.txtTruckNums;
	this.Dialog.Alter.PersonIDs = this.mapping.txtPersonIDs;
	this.Dialog.Alter.Money = this.mapping.spanOrderMoney;
	this.Dialog.Alter.Amount = this.mapping.spanOrderAmount;
	this.Dialog.Alter.Cancelable = this.mapping.divCancelControl;
	this.Dialog.Alter.DMDesc = this.mapping.divDMDesc;
	this.Dialog.Alter.DMDetail = this.mapping.divDMDetail;
	
	this.Dialog.Cancel = {};
	this.Dialog.Cancel.Form = new CommonDialog("取消订单", this.mapping.divCancelDialog, true, true);
	this.Dialog.Cancel.OID = this.mapping.spanCancelOrderID;
	this.Dialog.Cancel.ONum = this.mapping.spanCancelOrderNum;
	this.Dialog.Cancel.Money = this.mapping.spanCancelOrderMoney;
	this.Dialog.Cancel.State = this.mapping.spanCancelOrderState;
	this.Dialog.Cancel.Reason = this.mapping.txtCancelReason;
	
	this.Dialog.Confirm = {};
	this.Dialog.Confirm.Form = new CommonDialog("确认下订", this.mapping.divConfirmOrderUp, true, true);
	
	
	this.Dialog.Current = null;
	
	
	

	this.IsShowed = false;
	
	
	this.CreateForm = {};
	this.CreateForm.sURL = "/Service/TOrderService.asmx/createOrder";
	
	this.UpForm = {};
	this.UpForm.sURL = "/Service/TOrderService.asmx/OrderUp";
	this.UpForm.oParams = {};
	this.UpForm.oParams.OrderID = this.Dialog.Alter.OID;
	this.UpForm.oParams.Purchaser = this.Dialog.Alter.Purchase;
	this.UpForm.oParams.LinkMan = this.Dialog.Alter.LinkMan;
	this.UpForm.oParams.Phone = this.Dialog.Alter.Phone;
	this.UpForm.oParams.MEAccountID = this.Dialog.Alter.AccountID;
	this.UpForm.oParams.DeliveryMethodID = this.Dialog.Alter.DMID;
	this.UpForm.oParams.TruckNums = this.Dialog.Alter.TruckNums;
	this.UpForm.oParams.PersonCardIDs = this.Dialog.Alter.PersonIDs;

	
	this.AlterForm = {};
	this.AlterForm.sURL = "/Service/TOrderService.asmx/alter";
	this.AlterForm.oParams = this.UpForm.oParams;
	
	this.ListForm = {};
	this.ListForm.sURL = "/Service/TOrderService.asmx/getOrderList";
	
	this.CancelForm = {};
	this.CancelForm.sURL = "/Service/TOrderService.asmx/cancel";
	this.CancelForm.oParams = {};
	this.CancelForm.oParams.OrderID = "";
	this.CancelForm.oParams.Reason = "";
	
	
	this.BO = {};
	this.BO.create = new BaseBO(OrderObject, this.CreateForm);
	this.BO.up = new BaseBO(SystemMessage, this.UpForm);
	this.BO.alter = new BaseBO(SystemMessage, this.AlterForm);
	this.BO.list = new BaseBO(OrderObject, this.ListForm);
	this.BO.cancel = new BaseBO(SystemMessage, this.CancelForm, true);
	this.BO.current = null;
	
	////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////
	if(typeof TOrderControl._initialized == "undefined"){
		TOrderControl._initialized = true;
		
		/** @id show */
		TOrderControl.prototype.show = function(){
			if (!this.IsShowed) {
				this.IsShowed = true;
				this.refresh();
				this.Box.OrderList.Frame.style.display = "block";
			}
		};
		
		/** @id hide */
		TOrderControl.prototype.hide = function(){
			this.IsShowed = false;
			this.Box.OrderList.Frame.style.display = "none";
		};
		
		/** @id clear */
		TOrderControl.prototype.clear = function(){
			while(this.Box.OrderList.Body.firstChild)
				this.Box.OrderList.Body.removeChild(this.Box.OrderList.Body.firstChild);
			if(TimeRemainBar.release) TimeRemainBar.release("TOrderControl");
		};
		
		/** @id clearSelection */
		TOrderControl.prototype.clearSelection = function(odr){
			var t = this.Box.OrderList.Body;
			for(var i=0; i<t.childNodes.length; i++){
				if(i%2!=0){
					t.childNodes[i].className = "tr_style1";
				}
				else{
					t.childNodes[i].className = "tr_style2";
				}
			}
			uEvent.Listener.notify("OrderDetailList","hide",odr);
		};
		
		/** @id create */
		TOrderControl.prototype.create = function(){
			this.Dialog.Alter.Form.busy("正在生成订单...");
			this.Dialog.Current = this.Dialog.Alter;
			this.BO.create.execute();
		};
		
		/** @id cancel */
		TOrderControl.prototype.cancel = function(){
			this.CancelForm.oParams.Reason = this.Dialog.Cancel.Reason.value;
			this.Dialog.Cancel.Form.busy("正在取消订单...");
			this.BO.cancel.execute();
		};
		
		
		/** @id refresh */
		TOrderControl.prototype.refresh = function(){
			this.Dialog.Alter.Form.busy("获取订单列表...");
			this.Dialog.Current = this.Dialog.Alter;
			this.BO.list.execute();
		};
		
		/** @id handleException */
		TOrderControl.prototype.handleException = function(ex){
			var flag = true;
			if(ex.ErrorCode == Configuration.Symbol.TOrderMissInfo) flag = false;
			if(this.Dialog.Current) this.Dialog.Current.Form.exception(ex, flag);
			if(ex.ErrorCode == Configuration.Symbol.SignInRequest){
				uEvent.Listener.notify("TradeMember","SignInRequest");
			}
			this.clearSelection();
		};
		
		/** @id handleMessage */
		TOrderControl.prototype.handleMessage = function(msg){
			if(msg.code == Configuration.Symbol.TOrderUP || msg.code == Configuration.Symbol.TOrderAltered || 
				msg.code == Configuration.Symbol.TOrderCanceled){
				this.Dialog.Cancel.Form.hide();
				if(this.IsShowed) this.refresh();
				else uEvent.Listener.notify(this,"show");
			}
			else{
				if(this.Dialog.Current) this.Dialog.Current.Form.message(msg);
			}
			this.clearSelection();
		};
		
		
		/** @id changeDeliveryMethod */
		TOrderControl.prototype.changeDeliveryMethod = function(){
			if(this.Dialog.Alter.DMID.value==2){
				this.Dialog.Alter.DMDesc.style.display = "none";
				this.Dialog.Alter.DMDetail.style.display = "block";
			}
			else{
				this.Dialog.Alter.DMID.value =1;
				this.Dialog.Alter.DMDesc.style.display = "block";
				this.Dialog.Alter.DMDetail.style.display = "none";
			}
		};
		
		/** @id loadPurchasers */
		TOrderControl.prototype.loadPurchasers = function(lst){
			Clear$(this.mapping.selPurchaser);
			var flag = false;
			var op = C$("option");
			op.value = "";
			op.innerHTML = "您没有通过审核的购货单位";
			if (lst && lst.length > 0) {
				for (var i in lst) {
					if (lst[i].AudConditionID == 2) {
						op = C$("option");
						op.value = lst[i].Name;
						op.innerHTML = lst[i].Name;
						this.mapping.selPurchaser.appendChild(op);
						flag = true;
					}
				}
			}
			if(!flag){
				this.mapping.selPurchaser.appendChild(op);
			}
		};
		
		/** @id showEditDialog */
		TOrderControl.prototype.showEditDialog = function(odr){
			this.Dialog.Alter.Form.ask();
			this.Dialog.Alter.OID.value = odr.ID?odr.ID:"";
			this.Dialog.Alter.sOID.innerHTML = odr.ID?odr.ID:"";
			this.Dialog.Alter.Money.innerHTML = odr.Money?odr.Money:"";
			this.Dialog.Alter.Amount.innerHTML = odr.Amount?odr.Amount:"";
			this.Dialog.Alter.ONum.innerHTML = odr.OrderNum?odr.OrderNum:"";
			this.Dialog.Alter.Purchase.value = odr.Purchaser?odr.Purchaser:"";
			this.Dialog.Alter.LinkMan.value = odr.LinkMan?odr.LinkMan:"";
			this.Dialog.Alter.Phone.value = odr.Phone?odr.Phone:"";
			this.Dialog.Alter.DMID.value = odr.DMID?odr.DMID:"";
			this.Dialog.Alter.TruckNums.value = odr.TruckNums?odr.TruckNums:"";
			this.Dialog.Alter.PersonIDs.value = odr.PersonIDs?odr.PersonIDs:"";
			this.Dialog.Alter.AccountID.setAccountID(odr.Meac);
			this.changeDeliveryMethod();
		};
		
		/** @id hideEditDialog */
		TOrderControl.prototype.hideEditDialog = function(){
			this.Dialog.Alter.Form.hide();
		};
		
		/** @id showUpDialog */
		TOrderControl.prototype.showUpDialog = function(odr){
			this.Dialog.Alter.Cancelable.style.display = "none";
			this.showEditDialog(odr);
			this.BO.current = this.BO.up;
		};
		
		/** @id showAlterDialog */
		TOrderControl.prototype.showAlterDialog = function(odr){
			this.Dialog.Alter.Cancelable.style.display = "inline";
			this.showEditDialog(odr);
			this.BO.current = this.BO.alter;
		};
		
		/** @id showCancelDialog */
		TOrderControl.prototype.showCancelDialog = function(odr){
			this.Dialog.Cancel.OID.innerHTML = odr.ID?odr.ID:"";
			this.Dialog.Cancel.ONum.innerHTML = odr.OrderNum?odr.OrderNum:"";
			this.Dialog.Cancel.Money.innerHTML = odr.Money?odr.Money:"";
			this.Dialog.Cancel.State.innerHTML = odr.SRemark?odr.SRemark:"";
			this.Dialog.Cancel.Reasonvalue = "";
			this.CancelForm.oParams.OrderID = odr.ID;
			this.Dialog.Current = this.Dialog.Cancel;
			this.Dialog.Cancel.Form.ask();
		};
		
		/** @id hideCancelDialog */
		TOrderControl.prototype.hideCancelDialog = function(){
			this.Dialog.Cancel.Form.hide();
		};
		
		/** @id OrderCreated */
		TOrderControl.prototype.OrderCreated = function(odr){
			uEvent.Listener.notify(this,"OrderCreated");
			this.showUpDialog(odr);
		};
		
		/** @id registerOrder */
		TOrderControl.prototype.registerOrder = function(odr,btnDetail,btnAlter,btnCancel){
			var Root = this;
			uEvent.addBrowserHandler(btnDetail,"click",function(){
				Root.clearSelection();
				btnDetail.parentNode.parentNode.className = "tr_style3";
				uEvent.Listener.notify("OrderDetailList","show",odr);
			});
			if(btnAlter)
			uEvent.addBrowserHandler(btnAlter,"click",function(){
				Root.clearSelection(odr);
				btnDetail.parentNode.parentNode.className = "tr_style3";
				if(odr.StateID==1)
					Root.showUpDialog(odr);
				else
					Root.showAlterDialog(odr);
			});
			if(btnCancel)
			uEvent.addBrowserHandler(btnCancel,"click",function(){
				Root.clearSelection(odr);
				btnDetail.parentNode.parentNode.className = "tr_style3";
				Root.showCancelDialog(odr);
			});
		};
		
		/** @id refreshPaginationControl */
		TOrderControl.prototype.refreshPaginationControl = function(){
			if(this.Pagination.Index <= 1){
				this.Exe.btnFirst.disabled = true;
				this.Exe.btnPrevious.disabled = true;
			}
			else{
				this.Exe.btnFirst.disabled = false;
				this.Exe.btnPrevious.disabled = false;
			}
			if(this.Pagination.Index * this.Pagination.Size >= this.Pagination.Count){
				this.Exe.btnNext.disabled = true;
				this.Exe.btnLast.disabled = true;
			}
			else{
				this.Exe.btnNext.disabled = false;
				this.Exe.btnLast.disabled = false;
			}
			var Length = this.Pagination.Count / this.Pagination.Size;
			Length = Math.ceil(Length);
			this.Pagination.spanCount.innerHTML = Length;
			this.Pagination.spanIndex.innerHTML = this.Pagination.Index;
			
		};
		
		/** @id changeResultPage */
		TOrderControl.prototype.changeResultPage = function(nType){
			var flag = true;
			var Length = this.Pagination.Count / this.Pagination.Size;
			Length = Math.ceil(Length);
			switch(nType){
				case -2://First
					this.Pagination.Index = 1;
					break;
				case -1://Previous
					this.Pagination.Index -= 1;
					if(this.Pagination.Index < 1)
						this.Pagination.Index = 1;
					break;
				case 1://Next
					this.Pagination.Index += 1;
					if(this.Pagination.Index > Length)
						this.Pagination.Index = Length;
					break;
				case 2://Last
					this.Pagination.Index = Length;
					break;
				default:
					flag = false;
					break;
			}
			if(flag){
				this.refreshPaginationControl();
				uEvent.Listener.notify("OrderDetailList","hide");
				this.showOrderList();
			}
		};
		
		/** @id handleOrderList */
		TOrderControl.prototype.handleOrderList = function(lst){
			this.Pagination.List = lst;
			this.Pagination.Count = lst.length;
			this.changeResultPage(-2);
		};
		
		/** @id showOrderList */
		TOrderControl.prototype.showOrderList = function(){
			this.clear();
			var Columns = ["ID","SRemark","STime","Amount","Money","Purchaser","LinkMan","Phone","DMDesc"];
			var lst = this.Pagination.List;
			var iStart = (this.Pagination.Index - 1) * this.Pagination.Size;
			var iEnd = this.Pagination.Index * this.Pagination.Size;
			iEnd = iEnd > this.Pagination.Count? this.Pagination.Count : iEnd;
			for(var i=iStart;i<iEnd;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;
				var tdTime = null;
				for (var j in Columns) {
					td = C$("td");
					if (typeof t[Columns[j]] != "undefined") 
						td.appendChild(T$(t[Columns[j]]));
					tr.appendChild(td);
					if(j==2) {
						tdTime = td;
						td.innerHTML = td.innerHTML.replace(" ","<br>");
					}
				}
				
				td = C$("td");
				if(typeof t.Meac != "undefined"){
					td.appendChild(T$(t.Bank));
					td.appendChild(C$("br"));
					td.appendChild(T$(t.Meac));
				}
				tr.appendChild(td);
				
				td = C$("td");
				var btnViewDetail = null;
				var btnAlter = null;
				var btnCancel = null;
				
				btnViewDetail = C$("Input");
				btnViewDetail.type = "Button";
				btnViewDetail.value = "查看明细";
				btnViewDetail.className = "BigButton";
				td.appendChild(btnViewDetail);
				td.appendChild(C$("br"));
				if(t.StateID==1 || t.StateID==3 || t.StateID==7 || t.StateID==8){
					btnAlter = C$("Input");
					btnAlter.type = "Button";
					btnAlter.value = "修改";
					btnAlter.className = "SmallButton";
					td.appendChild(btnAlter);
					btnCancel = C$("Input");
					btnCancel.type = "Button";
					btnCancel.value = "取消";
					btnCancel.className = "SmallButton";
					td.appendChild(btnCancel);
				}
				tr.appendChild(td);
				
				if(t.StateID==3 || t.StateID==7){
					var trb = new TimeRemainBar(t.EndTime,"TOrderControl");
					tdTime.appendChild(trb.Frame);
				}
				
				this.Box.OrderList.Body.appendChild(tr);
				this.registerOrder(t,btnViewDetail,btnAlter,btnCancel);
			}
			this.Dialog.Alter.Form.hide();
		};
		
		/** @id initialize */
		TOrderControl.prototype.initialize = function(Root){
			
			this.hide();
			this.refreshPaginationControl();
			
			uEvent.addBrowserHandler(this.Exe.btnCreate,"click",function(){
				//Root.create();
				Root.Dialog.Confirm.Form.ask();
			});
			uEvent.addBrowserHandler(this.Exe.btnOrderUpNo, "click", function(){
				Root.Dialog.Confirm.Form.hide();
			});
			uEvent.addBrowserHandler(this.Exe.btnOrderUpYes, "click", function(){
				Root.Dialog.Confirm.Form.hide();
				Root.create();
			});
			uEvent.addBrowserHandler(this.Exe.btnUp,"click",function(){
				Root.Dialog.Alter.AccountID.getAccountID();
				Root.Dialog.Alter.Form.busy("正在提交订单...");
				Root.BO.current.execute();
			});
			uEvent.addBrowserHandler(this.Dialog.Alter.DMID,"change",function(){
				Root.changeDeliveryMethod();
			});
			uEvent.addBrowserHandler(this.Exe.btnCancel,"click",function(){
				Root.clearSelection();
				Root.hideEditDialog();
			});
			uEvent.addBrowserHandler(this.Exe.btnCancelUndo,"click",function(){
				Root.clearSelection();
				Root.hideCancelDialog();
			});
			uEvent.addBrowserHandler(this.Exe.btnCancelOrder,"click",function(){
				Root.cancel();
			});
			uEvent.addBrowserHandler(this.Exe.btnFirst,"click",function(){
				Root.changeResultPage(-2);
			});
			uEvent.addBrowserHandler(this.Exe.btnPrevious,"click",function(){
				Root.changeResultPage(-1);
			});
			uEvent.addBrowserHandler(this.Exe.btnNext,"click",function(){
				Root.changeResultPage(1);
			});
			uEvent.addBrowserHandler(this.Exe.btnLast,"click",function(){
				Root.changeResultPage(2);
			});
			
			
			uEvent.Listener.add(this.BO.create,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.create,"Message",function(msg){
				Root.handleMessage(msg);
			});
			uEvent.Listener.add(this.BO.create,"Executed",function(lst){
				Root.OrderCreated(lst[0]);
			});
			
			uEvent.Listener.add(this.BO.up,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.up,"Message",function(msg){
				Root.handleMessage(msg);
			});
			
			uEvent.Listener.add(this.BO.alter,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.alter,"Message",function(msg){
				Root.handleMessage(msg);
			});
			
			uEvent.Listener.add(this.BO.list,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.list,"Executed",function(lst){
				Root.handleOrderList(lst);
			});
			
			
			uEvent.Listener.add(this.BO.cancel,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.cancel,"Message",function(msg){
				Root.handleMessage(msg);
			});
		};
		
	}
	
	this.initialize(this);
	
}

