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

/** @id TCart */
function TCart(){
	
	this.mapping = {
		divTCart: "TCart",
		divTCartDialog:"TCartDialog",
		spanSum: "DueMomeySum",
		tbodyCartDetail: "TCartDetail",
		txtAmount:"OrderAmount",
		txtCarNums:"txtCarNums",
		txtCardIDs:"txtCardIDs",
		selDeliveryMethod:"DeliveryMethodID",
		btnAddToCart:"btnAddToCart",
		btnCancelAdd:"btnAddCancle",
		spanProductName:"Cart_ProductName",
		spanMaterial:"Cart_Material",
		spanSpec:"Cart_Spec",
		spanOrigin:"Cart_Origin",
		spanWarehouse:"Cart_Warehouse",
		spanAmount:"Cart_Amount",
		spanPrice:"Cart_Price",
		spanUnit:"Cart_Unit",
		spanTime:"Cart_Time",
		spanDue:"Cart_deuPay",
		hiddenTRID:"TResourceID"
	};
	
	
	this.TCartBox = {
		btnShow: $(this.mapping.btnShowCart),
		divCart: $(this.mapping.divTCart),
		Sum: $(this.mapping.spanSum),
		Body: $(this.mapping.tbodyCartDetail)
	};
	
	this.Dialog = {
		Form: new CommonDialog("购物车",$(this.mapping.divTCartDialog), true, true),
		Pay:$(this.mapping.spanDue),
		btnOK:$(this.mapping.btnAddToCart),
		btnCancel:$(this.mapping.btnCancelAdd),
		txtAmount:$(this.mapping.txtAmount)
	};
	
	this.Dialog.Info = {
		name:$(this.mapping.spanProductName),
		material:$(this.mapping.spanMaterial),
		spec:$(this.mapping.spanSpec),
		origin:$(this.mapping.spanOrigin),
		warehouse:$(this.mapping.spanWarehouse),
		amount:$(this.mapping.spanAmount),
		price:$(this.mapping.spanPrice),
		unit:$(this.mapping.spanUnit),
		time:$(this.mapping.spanTime)
	};
	
	this.Form = {};
	this.Form.sURL = "/service/TCartService.asmx/add";
	this.Form.oParams ={
		TResourceID:$(this.mapping.hiddenTRID),
		OrderAmount:this.Dialog.txtAmount
	};
	
	this.SetForm={};
	this.SetForm.sURL = "/service/TCartService.asmx/set";
	this.SetForm.oParams = this.Form.oParams;
	
	this.RemoveForm={};
	this.RemoveForm.sURL = "/service/TCartService.asmx/remove";
	this.RemoveForm.oParams ={
		TResourceID:-1
	};
	
	this.DataForm = {};
	this.DataForm.sURL = "/service/TCartService.asmx/show";
	this.DataForm.oParams = {};
	
	this.BO ={
		Add: new TSearchBO(this.Form),
		Set: new TSearchBO(this.SetForm),
		GetData: new TSearchBO(this.DataForm),
		Remove: new TSearchBO(this.RemoveForm,true),
		CurrentBO:null
	};
	
	this.IsShowed = false;
	
	if (typeof TCart._initialized == "undefined") {
		TCart._initialized = true;
		
		/** @id show */
		TCart.prototype.show = function(){
			if(!this.IsShowed){
				this.IsShowed = true;
				this.TCartBox.divCart.style.display = "block";
				this.getData();
			}
		};
		
		/** @id hide */
		TCart.prototype.hide = function(){
			this.IsShowed = false;
			this.TCartBox.divCart.style.display = "none";
		};
		
		/** @id showCommonDialog */
		TCart.prototype.showCommonDialog = function(oS){
			this.Dialog.Form.ask();
			for(var i in this.Dialog.Info){
				if(oS[i]){
					this.Dialog.Info[i].innerHTML = oS[i];
				}
				else{
					this.Dialog.Info[i].InnerHTML = "";
				}
			};
			this.Form.oParams.TResourceID.value = oS.id;
		};
		
		/** @id showAddDialog */
		TCart.prototype.showAddDialog = function(oS){
			this.Dialog.btnOK.value ="添加";
			this.Dialog.txtAmount.value = 0;
			this.BO.CurrentBO = this.BO.Add;
			this.showCommonDialog(oS);
		};
		
		/** @id showModifyDialog */
		TCart.prototype.showModifyDialog = function(oS){
			this.Dialog.btnOK.value ="修改";
			this.Dialog.txtAmount.value = oS.order;
			this.BO.CurrentBO = this.BO.Set;
			this.showCommonDialog(oS);
			this.Dialog.Pay.innerHTML = parseFloat(this.Dialog.Info.price.innerHTML) * parseFloat(this.Dialog.txtAmount.value);
		};
		
		/** @id registerResource */
		TCart.prototype.registerResource = function(btnM,btnD,t){
			var Root = this;
			uEvent.addBrowserHandler(btnM,"click",function(){
				Root.showModifyDialog(t);
			});
			uEvent.addBrowserHandler(btnD,"click",function(){
				Root.RemoveForm.oParams.TResourceID = t.id;
				Root.Dialog.Form.busy("正在更新购物车...");
				Root.BO.Remove.execute();
			});
		}
		
		
		/** @id addResource */
		TCart.prototype.addResource = function(){
			this.Dialog.Form.busy("正在更新购物车...");
			this.BO.CurrentBO.execute();
		};
		
		/** @id handleMessage
		 * @param {SystemMessage} msg
		 */
		TCart.prototype.handleMessage = function(msg){
			if(msg.code== Configuration.Symbol.TCartAdded || msg.code== Configuration.Symbol.TCartAltered){
				this.Dialog.Form.hide();
			}
			else{
				this.Dialog.Form.message(msg);
			}
		};
		
		/** 
		 * @id handleException
		 * @param {ServiceException} ex
		 */
		TCart.prototype.handleException = function(ex){
			if(ex.ErrorCode == Configuration.Symbol.SignInRequest){
				uEvent.Listener.notify("TradeMember","SignInRequest");
			}
			this.Dialog.Form.exception(ex.Message);
		};
		
		/** 
		 * @id handleCartData
		 * @param {Array} lst
		 */
		TCart.prototype.handleCartData = function(lst){
			this.clearView();
			this.Dialog.Form.hide();
			var Columns =["name","material","spec","origin","warehouse","amount","price","unit","time","pause","order","due"];
			var sum =0;
			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";
				
				/** @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 btnM = C$("input");
				btnM.type = "button";
				btnM.value = "修改";
				td.appendChild(btnM);
				
				var btnD = C$("input");
				btnD.type="button";
				btnD.value="删除";
				td.appendChild(btnD);
				
				tr.appendChild(td);
				this.TCartBox.Body.appendChild(tr);
				this.registerResource(btnM,btnD,t);
				sum+=parseFloat(t.due);
			}
			this.TCartBox.Sum.innerHTML = sum;
			this.TCartBox.divCart.style.display = "block";
		};
		
		/** @id clearView */
		TCart.prototype.clearView = function(){
			while(this.TCartBox.Body.firstChild){
				this.TCartBox.Body.removeChild(this.TCartBox.Body.firstChild);
			}
			this.TCartBox.Sum.innerHTML = "";
		};
		
		/** @id getData */
		TCart.prototype.getData = function(){
			this.Dialog.Form.busy("正在获取数据...");
			this.clearView();
			this.BO.GetData.execute();
		};
		
		/** @id initialize */
		TCart.prototype.initialize = function(Root){
			
			this.TCartBox.divCart.style.display = "none";
			
			
			uEvent.addBrowserHandler(this.Dialog.btnCancel,"click",function(){
				Root.Dialog.Form.hide();
			});
			
			uEvent.addBrowserHandler(this.Dialog.btnOK,"click",function(){
				Root.addResource();
			});
			
			uEvent.addBrowserHandler(this.Dialog.txtAmount,"keyup",function(){
				Root.Dialog.Pay.innerHTML = parseFloat(Root.Dialog.Info.price.innerHTML) * parseFloat(Root.Dialog.txtAmount.value);
			});
			
			uEvent.Listener.add("Cart","Add",function(oS){
				Root.showAddDialog(oS);
			});
			
			uEvent.Listener.add(this.BO.Add,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.Add,"Message",function(msg){
				Root.handleMessage(msg);
			});
			
			uEvent.Listener.add(this.BO.Set,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.Set,"Message",function(msg){
				Root.handleMessage(msg);
				Root.getData();
			});
			
			uEvent.Listener.add(this.BO.Remove,"Exception",function(ex){
				Root.handleException(ex);
			});
			uEvent.Listener.add(this.BO.Remove,"Message",function(msg){
				Root.handleMessage(msg);
				Root.getData();
			});
			
			uEvent.Listener.add(this.BO.GetData,"Executed",function(lst){
				Root.handleCartData(lst);
			});
			uEvent.Listener.add(this.BO.GetData,"Message",function(msg){
				Root.handleMessage(msg);
			});
			
			
		};
		
	}
	
	
	this.initialize(this);
}
