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

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

	stores: [ 'DrillDown', 'DrillDownInfo', 'DrillDownEmail', 'copyTranToCart', 'acceptQuote' ],

	views: [ 'drilldown.Window', 'drilldown.Panel',
			 'drilldown.Grid', 'drilldown.FirstView',
			 'drilldown.DeliveryView', 'drilldown.ChargeView',
			 'drilldown.TotalsView', 'drilldown.DiscountMessagesView',
		   'drilldown.EmailWindow', 'drilldown.DeliveryDateWindow' ],

	models: [ 'DrillDown', 'DrillDownInfo', 'Valid' ],

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

		this.control({
			'viewport > panel': {
				render: this.onPanelRendered
			},
			'drilldownpanel button[text="Email"]': {
				click: this.onDrillDownEmail
			},
			'drilldownpanel button[text="Change"]': {
				click: this.onDrillRequestChange
			},
			'#drilldown_email button[text="Send"]': {
				click: this.onDrillDownEmailSend
			},
			'#drilldownPrint': {
				click: this.onDrillDownPrint
			},
			'#drilldownEmailRetail': {
				click: this.onDrillDownEmailRetail
			},
			'#changeComment': {
				click: this.onChangeComment
			},
			'#drilldownCopy': {
				click: this.onDrillDownCopy
			},
			'#acceptQuote': {
				click: this.onAcceptQuote
			},
			'#deliverydate_email button[text="Accept"]': {
				click: this.onAcceptQuoteButton
			},
			'#statements_drilldown_grid': {
				beforeitemclick: this.onBeforeOpenDrilldown,
				itemclick: this.onOpenDrilldown
			}
		});
	},

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

	onDrillRequestChange: function() {
		Ext.widget('mailsupportwindow');
		Ext.getCmp('mailsupport_window').setTitle('Change request');
		var trans = Ext.getStore('DrillDownInfo').data.last().data.transactionNumber;

		Ext.getStore('sendEmailMessage').setExtraParam('companyID', Ext.getStore('DrillDownInfo').data.last().data.companyID);
		Ext.getStore('sendEmailMessage').setExtraParam('customerID', Ext.getStore('DrillDownInfo').data.last().data.customerID);
		Ext.getStore('sendEmailMessage').setExtraParam('numTran', trans);
		Ext.getStore('sendEmailMessage').setExtraParam('suffixTran', Ext.getStore('DrillDownInfo').data.last().data.transactionSuffix);

		Ext.getCmp('subject').setValue( 'Changes requested for Transaction ' + trans );
		Ext.getCmp('subject').disable();
		Ext.getCmp('toMail').setValue('');
		Ext.getCmp('toMail').hide();
	},

	onDrillDownEmail: function() {
		Ext.widget('drilldownemail');
		Ext.getCmp('drillDownEmails').setValue( Ext.getStore('CustomerDetails').data.last().data.customerEmail );
		Ext.getStore('DrillDownEmail').setExtraParam('transactionType', Ext.getStore('DrillDownInfo').data.items[0].data.transactionType);
	},

	onDrillDownEmailSend: function() {
		Ext.getCmp('drilldown_email').setLoading('Loading...');
		Ext.getStore('DrillDownEmail').load({
			params: {
				userName: Ext.getStore('HomeStatistic').data.last().data.id_user,
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				companyID: Ext.getStore('DrillDownInfo').data.last().data.companyID,
				customerID: Ext.getStore('DrillDownInfo').data.last().data.customerID,
				transactionType: Ext.getStore('DrillDownEmail').proxy.extraParams.transactionType,
				transactionNumber: Ext.getStore('DrillDownInfo').data.items[0].data.transactionNumber,
				transactionSuffix: Ext.getStore('DrillDownInfo').data.items[0].data.transactionSuffix,
				emailAddress: Ext.getCmp('drillDownEmails').getValue()
			}
		});
	},

	onDrillDownPrint: function() {
		var grid = Ext.getCmp('drilldowngrid');
		Ext.ux.grid.Printer.printInvoice(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);
	},

	onDrillDownEmailRetail: function() {
		Ext.widget('drilldownemail');
		Ext.getCmp('drillDownEmails').setValue( Ext.getStore('CustomerDetails').data.last().data.customerEmail );
		Ext.getStore('DrillDownEmail').setExtraParam('transactionType', 'RetailInvoice');
	},

	onChangeComment: function() {
		Ext.getCmp('viewport').setLoading('Loading...');
		Ext.getStore('CommentInfo').load({
			params: {
				userName: Ext.getStore('HomeStatistic').data.last().data.id_user,
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				companyID: Ext.getStore('Config').data.last().data.company_id,
				customerID: Ext.getStore('HomeStatistic').last().data.id_cust,
				transactionNumber: Ext.getStore('DrillDownInfo').data.items[0].data.transactionNumber,
				transactionSuffix: Ext.getStore('DrillDownInfo').data.items[0].data.transactionSuffix
			}
		});
	},

	onDrillDownCopy: function(btn) {
		btn.up('panel').up('window').close();
		Ext.getStore('copyTranToCart').load({
			params: {
				userName: Ext.getStore('HomeStatistic').data.last().data.id_user,
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				sessionID: Ext.getStore('createCart').last().data.sessionID,
				companyID: Ext.getStore('DrillDownInfo').data.last().data.companyID,
				customerID: Ext.getStore('DrillDownInfo').data.last().data.customerID,
				transactionType: Ext.getStore('DrillDownInfo').data.items[0].data.transactionType,
				transactionNumber: Ext.getStore('DrillDownInfo').data.items[0].data.transactionNumber,
				transactionSuffix: Ext.getStore('DrillDownInfo').data.items[0].data.transactionSuffix
			}
		});
	},

	onAcceptQuote: function() {
		Ext.widget('deliverydateemail');
	},

	onAcceptQuoteButton: function() {
		if (Ext.getCmp('deliveryDateRequired').getValue() != null){
			Ext.getCmp('deliverydate_email').setLoading('Loading...');
		Ext.getStore('acceptQuote').load({
			params: {
				tpaID: 'tportal',
				idUser: Ext.getStore('Config').data.last().data.device_id,
				companyID: Ext.getStore('DrillDownInfo').data.last().data.companyID,
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				custID: Ext.getStore('DrillDownInfo').data.last().data.customerID,
				numTran: Ext.getStore('DrillDownInfo').data.items[0].data.transactionNumber,
				suffixTran: Ext.getStore('DrillDownInfo').data.items[0].data.transactionSuffix,
				deliveryDate: Ext.getCmp('deliveryDateRequired').getValue(),
				comment1: Ext.getCmp('deliveryCommentRequired').getValue()
			}
		});
		}
		else{
			Ext.Msg.alert('Alert', 'The delivery date field is mandatory.', Ext.emptyFn);
		}
	},

	onBeforeOpenDrilldown: function(combo, record, index) {
		if (record.data.displayType != "Invoice" && record.data.displayType != "Credit") {
			return false;
		}
	},

	onOpenDrilldown: function(item, record) {
		Ext.getCmp('statements_drilldown_window').setLoading('Loading...');

		Ext.customParam.transactionNumbers = [];
		Ext.customParam.transactionSuffixes = [];
		Ext.customParam.userComments = [];
		Ext.customParam.currentInvoiceIndex = 1;

		Ext.getStore('DrillDownInfo').load({
			params: {
				userName: Ext.getStore('HomeStatistic').data.last().data.id_user,
				deviceID: Ext.getStore('Config').data.last().data.device_id,
				companyID: Ext.getStore('Config').data.last().data.company_id,
				customerID: Ext.getStore('HomeStatistic').last().data.id_cust,
				transactionNumber: record.data.transactionNumber,
				transactionSuffix: record.data.transactionSuffix,
				tranType: record.data.typeTran
			}
		});
	}
});
