<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");

	
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
$ajax = "webuserspool/_forms/friendPoints/ajax/ajax.friendPoints.php";
$compName = "friendGrid";

?>
var <?php echo $compName;?> = new Ext.grid.EditorGridPanel({     
	store: new Ext.data.Store({
		url: '<?php echo $ajax;?>',			
		baseParams:{
			action: 'getPrijatelja'
		},
		reader: new Ext.data.JsonReader({		
				root:'rows',
				totalProperty : "results"
			},
			new Ext.data.Record.create([
			 	{ name:"id", type: 'int' },	
				{ name:"email", type: 'string' },
				{ name:"Firstname", type: 'string' },
				{ name:"Surname", type:'string'},
				{ name:"Points", type:'int'}
			])
		)
	}),
    id: '<?php echo $compName;?>',
    border: false,
    autoHeight: true,
	title		: 'Friends',
	tbar:[{
		text: 'Delete',
		handler: function(){
			var grid = Ext.getCmp('<?php echo $compName;?>').getSelectionModel();
			if(grid.selection != null){
				Ext.Ajax.request({
					url: '<?php echo $ajax;?>',
					params:{
						action: 'DeleteFriend',
						friend_id: grid.selection.record.data.id
					},
					success: function(resp, opt){
						Ext.getCmp('pannelistGrid').getStore().reload();
						Ext.getCmp('friendGrid').getStore().reload();
					}
				});
			}
		}
	}],
  	columns: [
  		{header: "Email", width:300, sortable: false,dataIndex: 'email', editor: new Ext.form.TextField },
  		{header: 'Firstname',  sortable: true, dataIndex: 'Firstname' },
  		{header: 'Surname',  sortable: true, dataIndex: 'Surname' },
  		{header: 'Points',  sortable: true, dataIndex: 'Points', editor: new Ext.form.NumberField({ allowBlank: false, allowNegative: false, maxValue: 100000}) }   		
    ],		
   	listeners: {
   		afteredit : function(e) {
   			Ext.Ajax.request({
   				url:'<?php echo $ajax;?>',
   				params: { action: 'update' ,
   								
   							  id	: e.record.data.id,
						   field	: e.field,
							value 	: e.value						
   				},
				success : function(resp,opt){ 
					e.record.commit();
					Ext.getCmp('pannelistGrid').getStore().reload();
				}							  							
			})
		}	
	}     
});
