<?php 

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

$idProject = intval($_POST['id_project']);
/*
echo "<pre>";
var_dump($_SESSION);
echo "</pre>";
exit;*/

$sql = "SELECT proj_name FROM _Projects WHERE id = ".$idProject;
$res = $db->SQLexecute($sql);
$zad = $db->fetchAssoc($res);
$projName = $zad['proj_name'];

$sql = "SELECT * FROM quota_layer WHERE id_project = $idProject AND layer IN ( SELECT id FROM quota_layer_addon WHERE  interviewer_show = 1 ) ORDER BY layer,zap_st";
$resLayers = $db->SQLexecute($sql);

// if their are no records, their is no quota on this project.
if (!$db->NumRows($resLayers)) exit;
	
while ($rowLayer = $db->fetchAssoc($resLayers))
{
	if($rowLayer['layer'] != $curLayerId)
	{
		if($curLayerId)//finish the grid
		{
		
			$Layerji[$curLayerId]['sql'] = "SELECT a.*,(SELECT count(*) FROM ".$projName."___quotaCalc b WHERE ".implode(' AND ',$countWhere). " ) AS q_done FROM ".$projName."___quotaLimits a ";
			$Layerji[$curLayerId]['Columns'] = $menuCols;
			
			$menuCols = array();
		}
		
		//array that holds the where clouse for inner select count
		$countWhere = array();
		$curLayerId = $rowLayer['layer'];
		$userCondition = "";
	}

	
	if ($rowLayer['q_name'] == "user") {
		
		$colName = 'QT_user_id';
	}
	elseif($rowLayer['source'] == 2 )
		$colName = 'QT_'.$rowLayer['q_name'];
	else 
		$colName = 'TP_'.$rowLayer['q_name'];
	
	$countWhere[] = "a.$colName = b.$colName";
	
	$menuCols[$colName] = $rowLayer['q_name'];
		
			
}

	$Layerji[$curLayerId]['sql'] = "SELECT a.*,(SELECT count(*) FROM ".$projName."___quotaCalc b WHERE ".implode(' AND ',$countWhere). " ) AS q_done FROM ".$projName."___quotaLimits a ";
	$Layerji[$curLayerId]['Columns'] = $menuCols;





foreach ($Layerji as $idLayer=>$layerData) {
	$userInQuota = 0;
?>
    // create the Grid
    var capiQuotaStatistics<?php echo $idLayer;?> = new Ext.grid.EditorGridPanel({
        
    	store: new Ext.data.Store({
			url: '../webcati/_ajax/quotas/ajax.quotaStat.php',
			baseParams: {
					database:'<?php echo DB_WARPIT_WEBCATI_BASE;?>', 
					action : 'select',
					RMcomp:'grid',
					RMtable : '<?php echo $projName.'___quotaLimits' ;?>',  
					SQL : "<?php echo $layerData['sql'];?> " ,
					SQLwhere : " QT_user_id = <?php echo $_SESSION['UserInfo']['id'];?> ", 
					start: 0,
					limit : 1000,
					id_layer : <?php echo $idLayer; ?>
			},
			reader: new Ext.data.JsonReader({		
				root:'rows',
				totalProperty : "results",
				idProperty:"id"
			},
				new Ext.data.Record.create(
					[
					 { name:"id", type: 'int' },	
					 { name:"q_done", type: 'int' },
					 { name:"q_value", type: 'int' }
					 <?php 
					 
					 foreach ($layerData['Columns'] as $idCol=>$cols) {
					 	$userInQuota = 1;
					 	if ($cols == "user") echo ", { name:\"QT_user_id\", type: 'string' }";
					 		else {
					 			echo ", { name:\"".$idCol."_lab\", type: 'string' }";
					 			echo ", { name:\"".$idCol."\", type: 'string' }";
					 		}
					 }
					 ?>
					]
				)

			)
    	}),
        id: 'capiQuotaStatistics<?php echo $idLayer;?>',
        border: false,
        title: '<?php echo implode("-",$layerData['Columns']);?>',
       // closable: true,
     	columns: [
     		 <?php 
					 
				foreach ($layerData['Columns'] as $idCol=>$cols) {
						if ($cols == "user") echo "{header: 'User', hidden:true, width: 150, sortable: true, dataIndex: 'QT_user_id' } ,";			
							else echo "{header: '".$cols."', width: 150, sortable: true, dataIndex: '".$idCol."', renderer: function(val,md,rec){return '<b>'+val+'</b>'+' '+rec.data.{$idCol}_lab}} ,";
				}
			?>
      		{header: 'Needed', width: 70, sortable: true, dataIndex: 'q_value'  } ,
      		{header: 'Done', width: 70, sortable: true, dataIndex: 'q_done' }  ,
      		{header: 'To Do', width: 70, sortable: true,  renderer: function(val,md,rec) { if (rec.data.q_done == rec.data.q_value) return '<font color=green>0</font>'; else return '<font color=red>' + (rec.data.q_value - rec.data.q_done) + '</font>'; } }     		
        ]
		
      
    });
   
   
   

capiQuotaStatistics<?php echo $idLayer;?>.getStore().load({
/*
callback: function() {
capiQuotaStatistics<?php echo $idLayer;?>.getStore().filter([
{
property     : 'QT_user_id',
value        : '<?php echo $_SESSION['UserInfo']['id']; ?>',
exactMatch  : true
}
]); 
}
*/
});

<?php 
$tabs[] = "capiQuotaStatistics".$idLayer;

} 

?>

QuotaTab = new Ext.TabPanel({
    title: 'Quota statistics  - <?php echo $projName; ?>',
    closable: true,
    activeTab: 0,
    items: [<?php echo implode(",",$tabs);?>]
});

mainTabPanel.add(QuotaTab).show();
