
Hedera.Confirm = new Class
({
	Extends: Hedera.Form

	,open: function ()
	{
		this.close ();
		this.isOpen = true;

		Hedera.BasketChecker.check (this.conn, this.hash,
			this.onBasketCheck.bind (this));
	}
	
	,onBasketCheck: function (isOk)
	{
		if (isOk)
			this.loadUi ();
	}
	
	,onOrderReady: function (form)
	{
		if (form.row < 0)
			return;

		var order = form.params;
		
		if (order.method != 'PICKUP')
		{
			Vn.Node.show (this.$.address);
			Vn.Node.setText (this.$.method, _('Agency'));
		}
		else
		{
			Vn.Node.hide (this.$.address);
			Vn.Node.setText (this.$.method, _('Warehouse'));
		}

		var total = order.taxBase + order.vat;
		
		if (total === null)
			total = 0;

		var totalDebt = order.debt + total;
		var exceededCredit = totalDebt - order.credit;
		var creditExceededCond = exceededCredit > 0;
			
		if (creditExceededCond)
			Htk.Toast.showWarning (
				_('You have exceeded your credit.'));

		var lot = {
			totalDebt: totalDebt,
			exceededCredit: exceededCredit,
			payAmount: 'ALL'
		};
		
		if (order.credit > 0)
		{
			this.$.creditInfo.style.display = 'table-row';

			if (creditExceededCond)
			{
				this.$.amountSelector.style.display = 'block';
				this.$.exceededInfo.style.display = 'table-row';
				lot.payAmount = 'EXCEEDED';
			}
		}
		
		var methods = [];
		
		if (totalDebt <= 0)
		{
			methods = ['balance'];
			lot.payMethod = 'BALANCE';
		}
		else
		{
			methods = ['card', 'transfer', 'later'];

			if (!creditExceededCond)
			{
				methods.push ('credit');
				lot.payMethod = 'CREDIT';
			}
			else
				lot.payMethod = 'CARD';
		}
		
		for (var i = 0; i < methods.length; i++)
			this.$[methods[i] +'Method'].style.display = 'block';

		this.$.lot.assign (lot);
	}
	
	,onPayMethodChange: function (payMethod)
	{
		var id = this.displayedInfo;
	
		if (id)
			Vn.Node.removeClass (this.$[id], 'selected');
	
		switch (payMethod.value)
		{
			case 'BALANCE':
				id = 'balanceMethod';
				break;
			case 'CREDIT':
				id = 'creditMethod';
				break;
			case 'CARD':
				id = 'cardMethod';
				break;
			case 'TRANSFER':
				id = 'transferMethod';
				break;
			default:
				id = null;
		}
		
		this.displayedInfo = id;
		
		if (id)
			Vn.Node.addClass (this.$[id], 'selected');
	}
	
	,disableButtons: function (disable)
	{
		this.$.modify.disabled = disable;
		this.$.confirm.disabled = disable;
	}
	
	,onModifyClick: function ()
	{
		window.history.back();
	}
	
	,onConfirmClick: function ()
	{
		this.disableButtons (true);
		this.$.confirmQuery.execute ();
	}

	,onConfirm: function (query, resultSet)
	{
		this.disableButtons (false);

		if (resultSet.fetchResult ())
			this.$.successDialog.show ();
	}

	,onDialogResponse: function ()
	{
		var lot = this.$.lot.params;

		if (this.$.payMethod.value === 'CARD')
		{
			if (this.$.payAmount.value === 'EXCEEDED')
				var payAmount = lot.exceededCredit;
			else
				var payAmount = lot.totalDebt;
		
			var tpv = new Hedera.Tpv ({
				conn: this.conn,
				hash: this.hash
			});
			tpv.pay (payAmount, this.$.order.get ('company'));
		}
		else
			this.hash.params = {form: 'ecomerce/orders'};
	}
});

