﻿var ViewCartPage =
{
	listData :
	{
		head : [
			{ name: "title", caption: "Sản phẩm", field: "title", type: ColummType.label, css: "" },
			{ name : "typeName", caption : "Dung lương", field : "typeName", type : ColummType.label, css : "w80px", width: 65 },
			{ name: "price", caption: "Giá", field: "price", type: ColummType.label, css: "w80px", width: 65 },
			{ name: "discountWhenOrder", caption: "chiết khấu", field: "discountWhenOrder", type: ColummType.label, css: "w80px", width: 65 },
			{ name: "quantity", caption: "SL", field: "quantity", type: ColummType.textBox, css: "w30px", width: 65 },
			{ name: "amount", caption: "Thành tiền", field: "amount", type: ColummType.label, css: "w60px", width: 65 }
		],
		data : [
		]
	},
	
	gv : null
};

ViewCartPage.showFee = function()
{
	if (utilObj.getElById("idContainerFeeContent").innerHTML.trim() != "") return;
	var action = new RegisterAction();
	var re = action.getFeeContent();
	if(re.hasErrors)
	{
		alert(re.errorsAlert);
	}
	else
	{
		if (re.data != null)
		{
			utilObj.getElById("idContainerFeeContent").innerHTML = re.data.info;
		}
	}
};

ViewCartPage.createGV = function()
{
	var charSep = '.';
	this.gv = new GridViewBTD("idDisplayProductInCart", this.listData, "gvtest");
	// function GridView 
	this.gv.isShowAddNew = false; // Show or Hide LinkButton Add New Record
	this.gv.isShowEdit = false; // Show or Hide LinkButton Edit Record
	this.gv.isShowDelete = true; // Show or Hide LinkButton Delete Record
	this.gv.isShowPaging = false; // Show or Hide Paging
	this.gv.isShowTotal = true; // Show or Hide total of Columm
	
	this.gv.addEventForFunction(EventName.deleteRecord, function(e)
	{
		var item = e.elementRow.item;
		if(!confirm("Bạn có chắc rằng muốn xóa \"" + item.title + "\" này?")) return;
		
		if (OrderProduct.action.removeProduct(item.productID, item.typeID))
		{
			ViewCartPage.gv.removeRow(e.elementRow);
			ViewCartPage.gv.calcTotal(ViewCartPage.listData.head[4].name, charSep);
		}
	});
	
	this.gv.addEvent("quantity", EventName.change, function(e)
	{
		var elCell = e.elementCell;			// Get current Cell
		var elRow = elCell.parentNode;		// Get current Row
		var elRow = e.elementRow;			// Get current Row
		var item = elRow.item;				// Get current Item
		var value = elCell.element.value.trim();
		if (value == "" || isNaN(value) || parseInt(value, 10) <= 0 || parseInt(value, 10) >= 2000000000)
		{
			alert("Giá trị không được = rỗng hoặc <= 0!");
			elCell.element.value = item.quantity;
			return;
		}
		if (OrderProduct.action.editQuantity(item.productID, item.typeID, value))
		{
			item.quantity = elCell.element.value;
			item.amount = (item.price - item.discountWhenOrder) * elCell.element.value;
			elRow.cells[5].element.innerHTML = utilObj.formatNumber(item.amount, charSep);
			ViewCartPage.gv.calcTotal("amount", charSep);
		}
	});
	
	this.gv.addEventForFunction(EventName.rowDataBound, function(e)
	{
		// không có cell:  e.elementCell
		var elRow = e.elementRow;			// Get current Row
		var item = elRow.item;				// Get current Item
		elRow.cells[2].className = "textAlignRight";
		elRow.cells[3].className = "textAlignRight";
		elRow.cells[5].className = "textAlignRight";
		elRow.cells[2].element.innerHTML = utilObj.formatNumber(item.price, charSep);
		elRow.cells[3].element.innerHTML = utilObj.formatNumber(item.discountWhenOrder, charSep);
		elRow.cells[5].element.innerHTML = utilObj.formatNumber(item.amount, charSep);
		// Lấy được trang Bound
		switch (e.boundStatus)
		{
			case BoundStatus.insert:
				break;
			case BoundStatus.remove:
				break;
			case BoundStatus.update:
				break;
		}
		// alert(e.boundStatus);
	});
	
	// Binding resource phải đặt sau cùng
	this.gv.dataBind();
	
	this.gv.calcTotal("amount", charSep);
};

ViewCartPage.init = function()
{
	this.listData.data = OrderProduct.action.getListProductInCart();
	this.createGV();
};

ViewCartPage.loginCustomer =
{
	formSubmit : function()
	{
		var o = utilObj.getElById("customerFormLoginDirect");
		
		this.disableBtn(o);
		
		var userName = o.elements["customerUserNameDirect"].value.trim();
		if (!utilObj.isUserName(userName))
		{
			alert(GetTextLang(20));
			o.elements["customerUserNameDirect"].select();
			o.elements["customerUserNameDirect"].focus();
			this.enableBtn(o);
			return;
		}
		
		var password = EnCodePass(o.elements["customerPasswordDirect"].value);
		
		var action = new RegisterAction();
		var re = action.login(userName, password);
		if(re.hasErrors)
		{
			alert(re.errorsAlert);
		}
		else
		{
			if (re.data != null)
			{
				alert(GetTextLang(24));
				if (utilObj.getElById("customerFullNameDirect") != null)
				{
					utilObj.getElById("customerFullNameDirect").innerHTML = re.data.fullName;
				}
				BKC.customerStatus = 3;
				OrderProduct.viewCart(null);
			}
		}
		this.enableBtn(o);
	},
	
	disableBtn : function(o)
	{
		o.elements["customerBtnSubmitDirect"].disabled = true;
		o.elements["customerBtnResetDirect"].disabled = true;
	},
	
	enableBtn : function(o)
	{
		o.elements["customerBtnSubmitDirect"].disabled = false;
		o.elements["customerBtnResetDirect"].disabled = false;
	}
};


ViewCartPage.orderCustomer =
{
	formSubmit : function()
	{
		var o = utilObj.getElById("customerOrderForm");
		
		this.disableBtn(o);
		
		if (ViewCartPage.listData.data.length == 0)
		{
			alert(GetTextLang(29));
			this.enableBtn(o);
			return;
		}
		
		var addressOrder = o.elements["regAddressOrder"].value.trim();
		if (addressOrder == "")
		{
			alert(GetTextLang(19));
			o.elements["regAddressOrder"].select();
			o.elements["regAddressOrder"].focus();
			this.enableBtn(o);
			return;
		}
		
		var noteOrder = o.elements["regNoteOrder"].value;
		
		var listProOrder = new Array();
		for (var i = 0; i < ViewCartPage.listData.data.length; i++)
		{
			var item = ViewCartPage.listData.data[i];
			listProOrder.push({ productID : item.productID,
				typeID: item.typeID,
				quantity : item.quantity,
				VAT: item.VAT,
				priceMain : item.priceMain,
				priceDiscount: item.priceDiscount
			});
		}
		
		if (OrderProduct.action.order(addressOrder, noteOrder, listProOrder))
		{
			alert(GetTextLang(28));
			otherLink_Click("#Home");
		}
		this.enableBtn(o);
	},
	
	disableBtn : function(o)
	{
		o.elements["regBtnSubmitOrder"].disabled = true;
	},
	
	enableBtn : function(o)
	{
		o.elements["regBtnSubmitOrder"].disabled = false;
	}
};