<?php

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

$dbConfigCapi = configLoader::loadConfig('capi');
	
$sql = "SELECT  translate_key, translate_text 
				FROM  ".DB_WARPIT_MAIN."._languagePack 
					WHERE id_programme = 2 AND id_language = ".$dbConfigCapi['default-language']." AND sub_page = 'capi'";
	//echo $sql;
	$res = $db->SQLexecute($sql);
	while ($zad = $db->fetchAssoc($res)) $languagePack[$zad['translate_key']] = $zad['translate_text'];
	

?>
 	

    // create the Grid
    var CapiAppointmentsGrid = new Ext.grid.EditorGridPanel({
        
    	store: new Ext.data.Store({
			url: '_ajax/ajax.capi.appointments.php',
			remoteGroup:true,
			
			reader: new Ext.data.JsonReader({		
				root:'rows',
				totalProperty : "results",
				idProperty:"id",
				messageProperty: "sumaUr"
			},
				new Ext.data.Record.create(
					[
					 { name:"id", type: 'int' },						 
					 { name:"ponovni_dogovor" , type: 'string' }, 
					 { name:"priimek_gosp" , type: 'string'},
					 { name:"komentar" , type: 'string'},					 
					 { name:"time_diff" , type: 'int'}
					]
				)

			)
    	}),
        id: 'CapiAppointmentsGrid',
        border: false,
        title: '<?php echo $languagePack['appointments'];?>',
    	sm: new Ext.grid.RowSelectionModel({
             singleSelect: true   
       	}),
  	
     
    
        viewConfig: {    
        			getRowClass: function(record, rowIndex, rp, ds){    	
		        			str = '';
		        			/*if (record.get('barvaj') == 0) str = 'statusNew'; 
		        			if (record.get('stop_date') == '0000-00-00 00:00:00') str = 'statusAdded'; */
		        			return 'x-grid3-row-collapsed ' + str;   
	        		}
        },
       	
       	
      	columns: [
      		{header: '<?php echo $languagePack['date-time'];?>',  width:70, sortable: true, dataIndex: 'ponovni_dogovor'  } ,
      		{header: '<?php echo $languagePack['contact-person'];?>',  width:100, sortable: true, dataIndex: 'priimek_gosp',editor:  new Ext.form.TextField()  } ,
      		{header: '<?php echo $languagePack['comment'];?>',  width:180,  id: 'idComment', sortable: true, dataIndex: 'komentar',editor:  new Ext.form.TextField() }
        ],
        autoExpandColumn: 'idComment',
		
    
       listeners: {
       		
       		afteredit : function(e) {
       		
       			Ext.Ajax.request({
       				url:'_ajax/ajax.capi.appointments.php',
       				params: { action: 'update' ,
       								
       							  id: e.record.data.id,
							   field: e.field,
								value : e.value
								
       				},
					success : function(resp,opt){ 
						
						e.record.commit();  
						
					}							  							
				})
			
					
			},
			
			cellcontextmenu : function(grid, rowIndex, cellIndex,e) {
				 sm = grid.getSelectionModel();
				 sm.selectRow(rowIndex);
				 
				 e.stopEvent();
				 new Ext.menu.Menu({
					items: [
					 	{ text: 'Continue with interviewing', 
					 			handler : function(){
					 				rec = sm.getSelected();
					 				CallScript("_ajax/ajax.project-actions.php",{ action: 'continue-with-interview',  id_record: rec.get('id')  }) 
					 			}
					 	}					 	
					]
				}).showAt(e.xy)
			}			
		},
		 tbar: [{
        		
        			 text: '<?php echo $languagePack['refresh'];?>',
		           	 handler: function() {
		           	 	CapiAppointmentsGrid.getStore().load();
		             }
			 },{
        		
        			 text: 'Continue',
		           	 handler: function() {
		           	 	rec = CapiAppointmentsGrid.getSelectionModel().getSelected();
		           	 	CallScript("_ajax/ajax.project-actions.php",{ action: 'continue-with-interview',  id_record: rec.get('id')  }) 
		             }
			 }]
        	
    });
    
    
CapiAppointmentsGrid.getStore().load();
Ext.getCmp('west-panel').add(CapiAppointmentsGrid).show();