Ext.namespace('EMA.Alert.grid');

EMA.Alert.grid.Dealers = Ext.extend(EMA.GridPanel, {
	constructor : function(config){
		config = config || {};
		
		if (config.title) {
			this.title = config.title;
		} else {
			this.title = 'Dealers';
		}
/*		
		if (config.transactionsGrid) {
			this.transactionsGrid = config.transactionsGrid;
		} else {
			this.transactionsGrid = false;
		}
*/		
		this.userForm = new Ext.Window({
			title: 'User',
	        layout: 'fit',
	        width: 430,
	        height: 270,
	        closeAction: 'hide',
	        plain: true,
	        maximizable: false,
	        modal: true,
	        shim: false,
	        items: []
	    });
		
		this.sm = new Ext.grid.CheckboxSelectionModel({
			singleSelect: true
		});
		
		this.gridColumns = [
			{header: 'id', width: 50, dataIndex: 'User.id'},
			{header: 'Firstname', width: 120, dataIndex: 'User.firstname'},
			{header: 'Lastname', width: 120, dataIndex: 'User.lastname'},
			{header: 'Username', width: 120, dataIndex: 'User.username'},
			{header: 'Last login', width: 150, dataIndex: 'User.last_login', renderer: EMA.Util.dateTimeRenderer},
			{header: 'Login count', width: 70, dataIndex: 'User.login_count'},
			{header: 'Today Nb transaction', width: 70, dataIndex: 'Transaction.transaction_count'},
			{header: 'Created', width: 150, dataIndex: 'User.created', renderer: EMA.Util.dateTimeRenderer}
	    ];		
		
		this.filters = new Ext.ux.grid.GridFilters({
	        encode: false,
	        filters: [
	        	{
					dataIndex: 'User.firstname',
					type: 'string'
				}, {
					dataIndex: 'User.lastname',
					type: 'string'
				}
			]
	    });
		
		this.store = new Ext.data.JsonStore({
			autoDestroy: true,
			url: '/users/index',
			root: 'User',
			idProperty: 'User.id',
			autoLoad: false,
			remoteSort: true,
			fields: [
			 	{name: 'User.id'},
				{name: 'User.firstname'},
				{name: 'User.lastname'},
				{name: 'User.username'},
				{name: 'User.login_count'},
				{name: 'Transaction.transaction_count'},
				{name: 'User.last_login', type: 'date', dateFormat: 'Y-m-d H:i:s'},
				{name: 'User.created', type: 'date', dateFormat: 'Y-m-d H:i:s'}
			]
		});
		
	    Ext.apply(this, {
	    	limit: 30,
	        tbar: new Ext.Toolbar({
	        	items: [
						
						{
							text: 'Add',
							iconCls: 'beezid-action-add',
							listeners: {
								click: function() {
									this.addDealer();
								},
								scope: this
							}
						},
						'-',
						{
							text: 'Edit',
							iconCls: 'beezid-action-edit',
							listeners: {
								click: function() {
									this.editDealer();
								},
								scope: this
							}
						},
						'-',
						{
							text: 'Delete',
							iconCls: 'beezid-action-delete',
							listeners: {
								click: function() {
									this.deleteDealer();
								},
								scope: this
							}
						},
						'-',
						{
							text: 'Change Password',
							iconCls: 'beezid-action-password',
							listeners: {
								click: function() {
									this.passwordDealer();
								},
								scope: this
							}
						},
						'->', 
						{
							text: 'Logged in as  <span style="font-size: 14px;"><b>xxx</b></span>&nbsp;&nbsp;[Log Out]',
							iconCls: 'ppc-action-logout',
							listeners: {
								click: function() {
									location.href = '/users/logout';
								}
							}
						}
						
					]
	        }),
			rowclickFunction: function(grid, rowIndex, e) {
				if (this.commandsGrid) {
					var record = grid.getSelectionModel().getSelected();
					if(record && record.id) {
						this.commandsGrid.onDealerRowClick(record.id);
					}
				}
            }
	    });
	    
	    EMA.Alert.grid.Dealers.superclass.constructor.apply(this, arguments);
	},
	editDealer: function() {
		if (this.getSelectionModel().getSelected()) {
			var s = this.getStore();
			var sm = this.getSelectionModel();
			var parentWindow = this.userForm;
			var callback = function(id) {
				if (id) {
					// callback to select record after load
					var reloadCb = function() {
						sm.selectRecords([s.getById(id)]);
					};
					
					s.reload({callback: reloadCb});
					parentWindow.hide();
					
					Ext.Msg.show({
						title: 'Success',
						msg: 'User <span style="font-size: 16px;"><b>#' + id + '</b></span> was updated.',
						buttons: Ext.Msg.OK
					});
				}
			};
			this.userForm.removeAll();
			this.userForm.add(new EMA.Alert.form.User({
				title: 'Edit a player',
				populate: true,
				url: '/users/edit',
				parentWindow: this.userForm,
				data: this.getSelectionModel().getSelected().data,
				callback: callback
			}));
			this.userForm.show();
		}
	},
	addDealer: function() {
			var s = this.getStore();
			var parentWindow = this.userForm;
			var callback = function(id){
				s.reload();
				parentWindow.hide();
				Ext.Msg.show({
					title: 'Success',
					msg: 'User <span style="font-size: 16px;"><b>#' + id + '</b></span> was created.',
					buttons: Ext.Msg.OK
				});
			};
			this.userForm.removeAll();
			this.userForm.add(new EMA.Alert.form.User({
	            id: 'user-add-form',
				title: 'Add a Dealer',
				txnType: 'Add',
				populate: false,
				url: '/users/add',
				parentWindow: this.userForm,
				callback: callback
			}));
			this.userForm.show();
	},
	deleteDealer: function() {
		if (this.getSelectionModel().getSelected()) {
			var s = this.getStore();
			var sm = this.getSelectionModel();
			var callback = function(id){
				s.reload();
			};
			var record = this.getSelectionModel().getSelected().data;
			Ext.MessageBox.confirm('Confirm', 'Are you sure you want to delete the selected dealer#'+record['User.id']+' ?', function(btn) {
				if (btn === "yes"){
					//submit to server

					Ext.Ajax.request({
						url: '/users/delete/'+record['User.id'],						
						failure:function(response,options){
							Ext.MessageBox.alert('Warning', 'Lost connection to the server!');
						},												  
						success:function(o){
							s.reload();
						  },
						  scope: this
					});
				}
			 }, this);
		}
	},
	passwordDealer: function() {
		if (this.getSelectionModel().getSelected()) {
			var s = this.getStore();
			var sm = this.getSelectionModel();
			var parentWindow = this.userForm;
			var callback = function(id) {
				if (id) {
					// callback to select record after load
					var reloadCb = function() {
						sm.selectRecords([s.getById(id)]);
					};
					
					s.reload({callback: reloadCb});
					parentWindow.hide();
					
					Ext.Msg.show({
						title: 'Success',
						msg: 'Password for user <span style="font-size: 16px;"><b>#' + id + '</b></span> was updated.',
						buttons: Ext.Msg.OK
					});
				}
			};
			this.userForm.removeAll();
			this.userForm.add(new EMA.Alert.form.Password({
				title: 'Edit a player',
				populate: true,
				url: '/users/edit_password',
				parentWindow: this.userForm,
				data: this.getSelectionModel().getSelected().data,
				callback: callback
			}));
			this.userForm.show();
		}
	}
});
