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

/** @id TradeSearchControl */
function TradeSearchControl(){
	this.mapping={
		btnSearch:"btnSearch",
		btnFirstPage:"btnPageFirst",
		btnPreviousPage:"btnPagePrevious",
		btnNextPage:"btnPageNext",
		btnLastPage:"btnPageLast",
		txtSearchExpression:"txtExpression",
		txtSearchProductName:"txtProductName",
		txtSearchMaterial:"txtMaterial",
		txtSearchSpec:"txtSpec",
		txtSearchOrigin:"txtOrigin",
		txtSearchWarehouse:"txtWarehouse",
		txtSearchPriceMin:"txtPriceMin",
		txtSearchPriceMax:"txtPriceMax",
		divSearchInterface:"SearchInterface",
		divSearchResult:"SearchResult",
		spanSearchMsg:"SearchMsg",
		spanSearchExceptionMsg:"SearchExceptionMsg",
		tbodySearchResultSet:"SearchResultSet",
		
		sortProducrName:"SortProductName",
		sortPrice:"SortPrice",
		sortAmount:"SortAmount",
		sortTime:"SortTime"
	}
	
	this.Box = {};
	this.Box.SearchInterface = $(this.mapping.divSearchInterface);
	this.Box.SearchResult = $(this.mapping.divSearchResult);

	
	this.MSG ={};
	this.MSG.StateMsg = $(this.mapping.spanSearchMsg);
	this.MSG.ExceptionMsg = $(this.mapping.spanSearchExceptionMsg);
	
	this.Exe = {};
	this.Exe.btnSearch = $(this.mapping.btnSearch);
	this.Exe.btnShow = $(this.mapping.btnShow);
	
	this.Input = {};
	this.Input.Exp = $(this.mapping.txtSearchExpression);
	this.Input.ProductName = $(this.mapping.txtSearchProductName);
	this.Input.Material = $(this.mapping.txtSearchMaterial);
	this.Input.Spec = $(this.mapping.txtSearchSpec);
	this.Input.Origin = $(this.mapping.txtSearchOrigin);
	this.Input.Warehouse = $(this.mapping.txtSearchWarehouse);
	this.Input.MinPrice = $(this.mapping.txtSearchPriceMin);
	this.Input.MaxPrice = $(this.mapping.txtSearchPriceMax);
	
	this.Result = {};
	this.Result.divResult = $(this.mapping.divSearchResult);
	this.Result.tbody = $(this.mapping.tbodySearchResultSet);
	
	this.Page ={};
	this.Page.First = $(this.mapping.btnFirstPage);
	this.Page.Previous=$(this.mapping.btnPreviousPage);
	this.Page.Next=$(this.mapping.btnNextPage);
	this.Page.Last=$(this.mapping.btnLastPage);
	
	this.Sort = {};
	this.Sort.Name = $(this.mapping.sortProducrName);
	this.Sort.Price = $(this.mapping.sortPrice);
	this.Sort.Amount = $(this.mapping.sortAmount);
	this.Sort.Time = $(this.mapping.sortTime);
	
	this.SearchForm = {};
	this.SearchForm.sURL = "/service/TResourceService.asmx/count";
	this.SearchForm.oParams ={
		Expression:this.Input.Exp
	};
	
	this.DataForm ={};
	this.DataForm.sURL ="/service/TResourceService.asmx/search";
	this.DataForm.oParams ={
		Expression:"",
		PageIndex:1,
		PageSize:10,
		Orderby:"a",
		sort:"d"
	};
	this.DataForm.Count = 0;
	
	this.BO = {
		Search:new TSearchBO(this.SearchForm),
		GetData:new TSearchBO(this.DataForm,true)
	};
	
	this.IsShowed = false;
	
	if(typeof TradeSearchControl._initialized == "undefined"){
		TradeSearchControl._initialized = true;
		
		/** @id show */
		TradeSearchControl.prototype.show = function(){
			if(!this.IsShowed){
				this.IsShowed = true;
				this.Box.SearchInterface.style.display = "block";
				this.Box.SearchResult.style.display = "block"
			}
		};
		
		/** @id hide */
		TradeSearchControl.prototype.hide = function(){
			this.IsShowed = false;
			this.Box.SearchInterface.style.display = "none";
			this.Box.SearchResult.style.display = "none"
		};
		
		/** @id clearResult */
		TradeSearchControl.prototype.clearResult = function(){
			while(this.Result.tbody.firstChild)
				this.Result.tbody.removeChild(this.Result.tbody.firstChild);
		};
		
		/** @id beginSearch */
		TradeSearchControl.prototype.beginSearch = function(){
			this.MSG.ExceptionMsg.innerHTML = "";
			this.MSG.StateMsg.innerHTML = "查询中. . .";
			this.Result.divResult.style.display = "none";
			this.clearResult();
			this.Exe.btnSearch.disabled = true;
			for(var i in this.Input){
				this.Input[i].disabled = true;
			}
			this.DataForm.oParams.Expression = "";
			this.DataForm.oParams.PageIndex = 1;
			this.DataForm.Count =0;
		}
		
		/** @id endSearch */
		TradeSearchControl.prototype.endSearch = function(){
			this.MSG.ExceptionMsg.innerHTML = "";
			this.MSG.StateMsg.innerHTML = "";
			this.Exe.btnSearch.disabled = false;
			for(var i in this.Input){
				this.Input[i].disabled = false;
			}
			this.Result.divResult.style.display = "block";
		}
		
		/** @id executeExpSearch */
		TradeSearchControl.prototype.executeExpSearch = function(){
			this.beginSearch();
			this.DataForm.oParams.Expression = this.SearchForm.oParams.Expression.value;
			this.BO.Search.execute();
		};
		
		/** @id executeAdvSearch */
		TradeSearchControl.prototype.executeAdvSearch = function(){
			var Exp ="t<0";
			var str ="";
			var log ="";
			
			str=this.Input.ProductName.value;
			log="*";
			if(Exp.length==0) log="";
			if(str.trim() != "") Exp+=log + "n=" + str.trim();
			
			str=this.Input.Material.value;
			log="*";
			if(Exp.length==0) log="";
			if(str.trim() != "") Exp+=log + "m=" + str.trim();
			
			str=this.Input.Spec.value;
			log="*";
			if(Exp.length==0) log="";
			if(str.trim() != "") Exp+=log + "s=" + str.trim();
			
			str=this.Input.Origin.value;
			log="*";
			if(Exp.length==0) log="";
			if(str.trim() != "") Exp+=log + "o=" + str.trim();
			
			str=this.Input.Warehouse.value;
			log="*";
			if(Exp.length==0) log="";
			if(str.trim() != "") Exp+=log + "w=" + str.trim();
			
			str=this.Input.MinPrice.value;
			if(str.trim() != "") str="p>" +str.trim();
			if(this.Input.MaxPrice.value.trim()!=""){
				if(str.length>0) str+="*";
				str += "p<" + this.Input.MaxPrice.value.trim();
			}
			log="*";
			if(Exp.length==0) log="";
			if(str.trim() != "") Exp+=log + "(" + str.trim() + ")";
			this.beginSearch();
			this.Input.Exp.value = Exp;
			this.DataForm.oParams.Expression = Exp;
			this.BO.Search.execute();
		};
		
		/** @id getData */
		TradeSearchControl.prototype.getData = function(){
			if(((this.DataForm.oParams.PageIndex-1)*this.DataForm.oParams.PageSize)>=this.DataForm.Count) return;
			this.clearResult();
			this.BO.GetData.execute();
		};
		
		/** @id handleException */
		TradeSearchControl.prototype.handleException = function(ex){
			this.endSearch();
			this.MSG.ExceptionMsg.innerHTML = ex.Message;
			if(ex.ErrorCode == Configuration.Symbol.SignInRequest){
				uEvent.Listener.notify("TradeMember","SignInRequest");
			}
		};
		
		/** @id handleMessage */
		TradeSearchControl.prototype.handleMessage = function(msg){
			if(msg.code=="Count"){
				this.endSearch();
				this.clearResult();
				this.MSG.StateMsg.innerHTML = "查询结果：共找到 " + msg + " 项";
				this.DataForm.Count = msg.toString();
				this.DataForm.oParams.PageIndex =1;
				this.refreshPageControl();
				this.getData();
			}
		};
		
		/** @id changeSortingMethod */
		TradeSearchControl.prototype.changeSortingMethod = function(sMethod){
			this.DataForm.oParams.Orderby = sMethod;
			this.getData();
		};
		
		/** @id registerResource */
		TradeSearchControl.prototype.registerResource = function(btn,t){
			uEvent.addBrowserHandler(btn,"click",function(){
				uEvent.Listener.notify("Cart","Add",t);
			});
			uEvent.addBrowserHandler(btn, "mouseout", function(){
				btn.className = "btnAdd2Cart Normal";
			});
			uEvent.addBrowserHandler(btn, "mouseover", function(){
				btn.className = "btnAdd2Cart Active";
			});
		}
		
		/** @id handleSearchResult */
		TradeSearchControl.prototype.handleSearchResult = function(lst){
			this.clearResult();
			var Columns =["name","material","spec","origin","warehouse","amount","price","unit","time","sold"];
			var iStart = (this.DataForm.Count <= this.DataForm.oParams.PageSize)?0:this.DataForm.oParams.PageSize * this.DataForm.oParams.PageIndex - this.DataForm.Count;
			for(var i=(iStart<0?0:iStart);i<lst.length;i++){
				var tr = C$("tr");
				if(i%2!=0)
					tr.className = "tr_style1";
				else
					tr.className = "tr_style2";
				
				/** @type {TResourceForSale} */
				var t=lst[i];
				var td = undefined;
				
				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");
				var btn = C$("input");
				btn.type = "button";
				btn.value = "";
				btn.className = "btnAdd2Cart Normal";
				td.appendChild(btn);
				tr.appendChild(td);
				this.Result.tbody.appendChild(tr);
				this.registerResource(btn,t);
			}
			this.endSearch();
			var sIndex = (this.DataForm.oParams.PageIndex-1)*this.DataForm.oParams.PageSize+1;
			var eIndex = this.DataForm.oParams.PageIndex * this.DataForm.oParams.PageSize;
			this.MSG.StateMsg.innerHTML = "查询结果：共找到 " + this.DataForm.Count + " 项, 显示" + sIndex + "~" + eIndex + "项";
		};
		
		/** refreshPageControl */
		TradeSearchControl.prototype.refreshPageControl = function(){
			if(this.DataForm.oParams.PageIndex<=1){
				this.Page.First.disabled = true;
				this.Page.Previous.disabled = true;
			}
			else{
				this.Page.First.disabled = false;
				this.Page.Previous.disabled = false;
			}
			if(this.DataForm.oParams.PageIndex * this.DataForm.oParams.PageSize >= this.DataForm.Count){
				this.Page.Last.disabled = true;
				this.Page.Next.disabled = true;
			}
			else{
				this.Page.Last.disabled = false;
				this.Page.Next.disabled = false;
			}
		};
		
		/** @id changeResultPage */
		TradeSearchControl.prototype.changeResultPage = function(nType){
			var flag = true;
			var length = this.DataForm.Count / this.DataForm.oParams.PageSize;
			length = Math.ceil(length);
			switch(nType){
				case -2://First
					this.DataForm.oParams.PageIndex = 1;
					break;
				case 2://Last
					this.DataForm.oParams.PageIndex = length;
					break;
				case -1://Previous
					this.DataForm.oParams.PageIndex -=1;
					if(this.DataForm.oParams.PageIndex<1)
						this.DataForm.oParams.PageIndex =1;
					break;
				case 1://Next
					this.DataForm.oParams.PageIndex +=1;
					if(this.DataForm.oParams.PageIndex > length)
						this.DataForm.oParams.PageIndex = length;
					break;
				default :
					flag = false;
					break;
			}
			if(flag){
				this.refreshPageControl();
				this.getData();
			}
		};
		
		/** @id initialize */
		TradeSearchControl.prototype.initialize = function(Root){
			
			this.show();
			
			this.MSG.StateMsg.innerHTML = "";
			this.MSG.ExceptionMsg.innerHTML = "";
			
			this.Sort.Amount.style.cursor = "pointer";
			this.Sort.Name.style.cursor = "pointer";
			this.Sort.Price.style.cursor = "pointer";
			this.Sort.Time.style.cursor = "pointer";
			
			
			uEvent.Listener.add(this.BO.Search,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.Search,"Message",function(msg){
				Root.handleMessage(msg);
			});
			uEvent.Listener.add(this.BO.GetData,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.GetData,"Executed",function(lst){
				Root.handleSearchResult(lst);
			});
			
			
			uEvent.addBrowserHandler(this.Exe.btnSearch,"click",function(){
				if(!Root.Exe.btnSearch.disabled)
					Root.executeAdvSearch();
				uEvent.getBrowserEvent().preventDefault();
			});
			
			uEvent.addBrowserHandler(this.Page.First,"click",function(){
				Root.changeResultPage(-2);
			});
			uEvent.addBrowserHandler(this.Page.Previous,"click",function(){
				Root.changeResultPage(-1);
			});
			uEvent.addBrowserHandler(this.Page.Next,"click",function(){
				Root.changeResultPage(1);
			})
			uEvent.addBrowserHandler(this.Page.Last,"click",function(){
				Root.changeResultPage(2);
			});
			
			
			for(var i in this.Input){
				if(this.Input[i]){
					uEvent.addBrowserHandler(this.Input[i],"keydown",function(){
						if (uEvent.getBrowserEvent().keyCode == 13) {
							Root.executeExpSearch();
							uEvent.getBrowserEvent().preventDefault();
						}
					});
				}
			}
			
			uEvent.addBrowserHandler(this.Sort.Amount,"click",function(){
				Root.changeSortingMethod("a");
			});
			uEvent.addBrowserHandler(this.Sort.Name,"click",function(){
				Root.changeSortingMethod("n");
			});
			uEvent.addBrowserHandler(this.Sort.Price,"click",function(){
				Root.changeSortingMethod("p");
			});
			uEvent.addBrowserHandler(this.Sort.Time,"click",function(){
				Root.changeSortingMethod("d");
			});
			
			this.endSearch();
			this.Result.divResult.style.display = "none";
			this.SearchForm.oParams.Expression.value = "t<0";
			this.executeExpSearch();
		};
	}
	
	this.initialize(this);

}
