Ext.define('TradePortal.controller.Statements', {
	extend: 'Ext.app.Controller',

	requires: [
	           'Ext.ux.grid.Printer'
	       ],

	stores: [
			'Statements',
			'StatementsDrillDownCustomer',
			'StatementsDrillDownBalances',
			'StatementsDrillDown',
			'StatementsDrillDownEmail'
	],

	models: [
			'Statements',
			'StatementsDrillDownCustomer',
			'StatementsDrillDownBalances',
			'StatementsDrillDown'
	],

	views: [
			'statements.Panel', 'statements.Grid',
			'statements.DrillDownWindow', 'statements.DrillDownPanel', 'statements.DrillDownCustomer',
			'statements.DrillDownBalances', 'statements.DrillDownGrid', 'statements.DrillDownEmail'
	],

	init: function() {
		if (typeof console !== "undefined") {
			console.log('Initialized Statements!');
		}

		this.control({
			'viewport > panel': {
				render: this.onPanelRendered
			},
			'#statementsDrilldownEmail': {
				click: this.onStatementsDrilldownEmail
			},
			'#statements_drilldown_email button[text="Send"]' : {
				click: this.onStatementsEmailSend
			},
			'#statementsDrilldownPrint' : {
				click: this.onStatementsDrilldownPrint
			},
			'#statements_grid': {
				beforeitemclick: this.onBeforeOpenStatement
			},
			'#statements_grid': {
				itemclick: this.onOpenStatement
			}
		});
	},

	onPanelRendered: function() {
		if (typeof console !== "undefined") {
			console.log('Statements panel was rendered');
		}
	},

	onStatementsDrilldownEmail: function() {
		Ext.widget('statements_drilldownemail');
		Ext.getCmp('statementsDrilldownEmails').setValue( Ext.getStore('CustomerDetails').data.last().data.customerEmail );
	},

	onStatementsEmailSend: function() {
		Ext.getCmp('statements_drilldown_email').setLoading('Loading...');

		Ext.getStore('StatementsDrillDownEmail').load({
			params: {
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				companyID: Ext.getStore('StatementsDrillDown').data.last().data.companyId,
				customerID: Ext.getStore('StatementsDrillDown').data.last().data.customerId,
				statementDate: Ext.getStore('StatementsDrillDown').data.last().data.dateStatement,
				emailAddress: Ext.getCmp('statementsDrilldownEmails').getValue()
			}
		});
	},

	onStatementsDrilldownPrint: function() {
		var grid = Ext.getCmp('statements_drilldown_grid');
		Ext.ux.grid.Printer.printStatement(grid, 0, Ext.getStore('Config').data.last().data.company_name, Ext.getStore('Config').data.last().data.company_address, Ext.getStore('Config').data.last().data.company_phone, Ext.getStore('Config').data.last().data.company_fax, Ext.getStore('Config').data.last().data.company_email, Ext.getStore('Config').data.last().data.abn, Ext.getStore('Config').data.last().data.trading_terms);
	},

	onBeforeOpenStatement: function(combo, record, index) {
		if (record.data.customerId == 0) {
			return false;
		}
	},

	onOpenStatement: function(item, record) {
		Ext.getCmp('viewport').setLoading('Loading...');

		Ext.getStore('StatementsDrillDown').load({
			params: {
				userName: Ext.getStore('HomeStatistic').data.last().data.id_user,
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				companyID: record.data.companyId,
				customerID: Ext.getStore('HomeStatistic').data.last().data.id_cust,
				statementDate: record.data.dateStatement
			}
		});
		Ext.getStore('StatementsDrillDownBalances').removeAll();
		Ext.getStore('StatementsDrillDownBalances').add({balanceType: 'Current', balanceAmount: record.data.currentBalance}, {balanceType: '30 Days', balanceAmount: record.data.day30Balance}, {balanceType: '60 Days', balanceAmount: record.data.day60Balance}, {balanceType: '90 Days', balanceAmount: record.data.day90Balance}, {balanceType: '120 Days', balanceAmount: record.data.day120Balance}, {balanceType: 'Total Balance', balanceAmount: record.data.dueBalance});

	}

});
