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

$p = new Permissions($db);
$LangPack = new LanguagePack($db, $aLanguage);
	
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

$ajax = CRM_RPATH."_forms/prisotnost/_ajax/ajax.prisotnost.php";

$projectId = StringChecker::Check($_POST['projectId']);

$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE."._Projects WHERE id = $projectId";
$res = $db->SQLexecute($sql);
$data = $db->fetchAssoc($res);
$projectName = $data['proj_name'];

$title = $LangPack->GetLabel("CRM_attendance_panel", "panel_title","Prisotnost")." - ". $projectName;

$zipId = StringChecker::Check($_POST['zipId']);
$dan = StringChecker::Check($_POST['dan']);
$ura = StringChecker::Check($_POST['ura']);
$filter = StringChecker::Check($_POST['tabFilter']);
$cati_location = StringChecker::Check($_POST['cati_location']);


$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = ".$cati_location." AND id_project=".$projectId;

$res = $db->SQLexecute($sql);
$data = $db->fetchAssoc($res);
$tmp = explode(",", $data['presentation_location']);
$hotel = $tmp[0];

$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI.".`".$projectName."_language` WHERE `id_question` IN ( 9, 10 ) AND `id_text_type` = 3";
$res = $db->SQLexecute($sql);
while($row = $db->fetchAssoc($res)){
	$lang[] = explode("/", $row['lngText']);
}
foreach ($lang[0] as $day) {
	$tmp = explode(" ", $day);
	$days[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $day);	
}
foreach ($lang[1] as $t) {
	$tmp = explode(" ", $t);
	$time[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $t);
}

$title .= " - ".$days[$dan]['label']." - ". $time[$ura]['label']. " - ". $hotel;
$tabId = StringChecker::Check($_POST['uniqueTabId']);

$suffix = "_".$projectId."_".$zipId."_".$dan."_".$ura;
include('forms.userGrid.php');
include('forms.subUserGrid.php');
?>
Ext.override(Ext.grid.GridView, {
    holdPosition: false,
    onLoad : function(){
        if (!this.holdPosition) this.scrollToTop();
        this.holdPosition = false
    }
});

Ext.ns('Ext.ux.grid');
Ext.ux.grid.CheckColumn = Ext.extend(Ext.grid.Column, {
	processEvent: function(name, e, grid, rowIndex, colIndex){
		if(name == 'mousedown'){
			var record = grid.store.getAt(rowIndex);
			record.set(this.dataIndex, !record.data[this.dataIndex]);
			return false;
		}else{
			return Ext.grid.ActionColumn.superclass.processEvent.apply(this, arguments);
		}
	},
	renderer: function(v, p, record){
		p.css += ' x-grid3-check-col-td';
		return String.format('<div class="x-grid3-check-col{0}">&#160;</div>',v ? '-on' : '');
	},
	init: Ext.emptyFn
});
Ext.preg('checkcolumn', Ext.ux.grid.CheckColumn);
Ext.grid.CheckColumn = Ext.ux.grid.CheckColumn;
Ext.grid.Column.types.checkcolumn = Ext.ux.grid.CheckColumn;

var <?php echo $tabId;?> = new Ext.Panel({
	id:'<?php echo $tabId;?>',
	title:'<?php echo $title;?>',
	defaults:{split:true,border:false},
	layout:'border',
	border: false,
	<?php if(isset($_POST['CRM'])) {?>
	listeners:{
		close: function(panel){
			if(!Ext.getCmp('west-panel').collapsed){
				Ext.getCmp('west-panel').collapse();
			}
		}
	},
	<?php } ?> 
	closable:true,
	items:[
	{
		layout:{
			type: 'vbox',
			align: 'stretch'
		},
		region:'center',
		items:[ PrisotnostUserGrid<?php echo $suffix;?>, SubUserGrid<?php echo $suffix;?> ],
		width:520
	}
	]
});
Ext.getCmp('mainTabPanel').add(<?php echo $tabId;?>).show();
<?php if(isset($_POST['CRM'])) {?>
Ext.getCmp('mainTabPanel').on('tabchange',function(panel, tab){
	if(tab != null){
		if(tab.id == "OrdersTab" || tab.id == "DeliveryTab" || tab.id == "Financial"){
			if(!Ext.getCmp('west-panel').collapsed){
				Ext.getCmp('west-panel').collapse();
			}	
		}else{
			if(Ext.getCmp('west-panel').collapsed){
				Ext.getCmp('west-panel').expand();
			}
			Ext.getCmp('StatisticGrid').getEl().mask('<?php echo $LangPack->GetLabel("CRM_all", "loading_mask_text","Posodabljanje..."); ?>');
			Ext.getCmp('StatisticGrid').getStore().load({
				params: { 
					action: 'get',
					project_id: <?php echo $projectId;?>,
					dan: <?php echo $dan;?>,
					ura: <?php echo $ura;?>,
					cati_location: <?php echo $cati_location;?>
				},
				callback: function(store, opt, success){
					if(success){
						Ext.getCmp('StatisticGrid').getEl().unmask();
					}
				}
			});
		}
	}
});
<?php }?>