<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once (PATH_TO_ROOT."CRM/class/class.Permissions.php");

$LangPack = new LanguagePack($db, $aLanguage);
$gridName = "changeTerminsTab";

$ajax = CRM_RPATH."_forms/changeTermins/_ajax/ajax.termins.php";	

$permissions = new Permissions($db);
//<?php echo $LangPack->GetLabel("CRM_all", "loading_mask_text","Posodabljanje...");
?>
var <?php echo $gridName;?> = new Ext.grid.EditorGridPanel({
	title: '<?php echo $LangPack->GetLabel("CRM_change_termin", "grid_title","Zamenjava terminov"); ?>',
	closable: true,
	store: new Ext.data.Store({
		url: '<?php echo $ajax;?>',			
		baseParams:{
			action: 'get'
		},
		reader: new Ext.data.JsonReader({		
				root:'rows',
				totalProperty : "results"
			},
			new Ext.data.Record.create([
				{ name:"id", type: 'id' },
				{ name:"id_tel", type: 'int' },
				{ name:"ime", type:'string'},
				{ name:"spol", type:'string'},
				{ name:"posta", type:'string'},
				{ name:"kraj", type:'string'},
				{ name:"naslov", type:'string'},
				{ name:"prisel", type:'string'},
				{ name:"mogo_priti", type:'string'}
			])
		)
	}),
	sm: new Ext.grid.RowSelectionModel(),
	id: '<?php echo $gridName;?>',
    border: false,
    tbar:[{
		xtype: 'button',
		text: '<?php echo $LangPack->GetLabel("CRM_change_termin", "change_period_title","Zamenjaj termin"); ?>',
		iconCls: 'RMicon_contact',
		handler: function(){
			var sel = <?php echo $gridName;?>.getSelectionModel().selections.items;
    		if(sel[0] != null){
    			var d = sel[0].data;
    			Ext.Msg.confirm('<?php echo $LangPack->GetLabel("CRM_change_termin", "change_period_alert_title","Zamenjava"); ?>', '<?php echo $LangPack->GetLabel("CRM_change_termin", "change_period_alert_msg","Ali je avtomatska zamenjava uredi?"); ?>',function(btn){
    				if(btn == "no"){
    					CallScript('<?php echo CRM_RPATH; ?>_forms/changeTermins/window.changePeriod.php', {id_tel: d.id_tel, id: d.id, gridName: '<?php echo $gridName;?>'});	
    				}else{
    					Ext.Ajax.request({
    						url: '<?php echo $ajax;?>',
    						params:{
    							action: 'fixTermine',
    							id_tel: d.id_tel,
    							autoFix: true,
    							id: d.id
    						},
    						success: function(){
    							
    						}
    					});
    				}
    			});
			}
		}
	}],
  	columns: [{
  		header:'#ID',
  		hidden: true,
  		dataIndex: 'id'
  	},{
  		header:'#ID TEL',
  		hidden: true,
  		dataIndex: 'id_tel'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "name_column_title","Ime"); ?>',
  		dataIndex: 'ime'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "gender_column_title","Spol"); ?>',
  		width: 50,
  		dataIndex: 'spol'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "post_column_title","Posta"); ?>',
  		width: 50,
  		dataIndex: 'posta'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "city_column_title","Kraj"); ?>',
  		dataIndex: 'kraj'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "address_column_title","Naslov"); ?>',
  		dataIndex: 'naslov'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "attended_column_title","Prisel"); ?>',
  		width: 400,
  		dataIndex: 'prisel'
  	},{
  		header: '<?php echo $LangPack->GetLabel("CRM_change_termin", "should_attended_column_title","Mogel priti"); ?>',
  		width: 400,
  		dataIndex: 'mogo_priti'
  	}]
});
Ext.getCmp('<?php echo $gridName;?>').getStore().reload();
Ext.getCmp('mainTabPanel').add(<?php echo $gridName;?>).show();