<?php
//par sexe:
foreach($tabDebgenre as $genre) {
	$activNiveau[$genre] = array();
	$tabEsp[$genre] = array();
	$tabEspNom[$genre] = array();
	$tabEspODS[$genre] = array();
	$tabEspNomODS[$genre] = array();

	//fréquentation par activité :
	$tabNbPerEsp[$genre] = array();
	$tabPercentPerEsp[$genre] = array();
	$tabNbPerEspODS[$genre] = array();
	$tabPercentPerEspODS[$genre] = array();
	$act=0;
	if(isset($_SESSION['espace0'])||$filtre_espaces=="") {
		$nbeleves_nivglob = mysqli_num_rows(mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND sexe = '".$genre."' ".$filtre_activs.""));
		$nbeleves_nivact = mysqli_num_rows(mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND sexe = '".$genre."' AND (espace_id = '0' OR espace_id IS NULL) ".$filtre_activsonly.""));
		if($nbeleves_nivact>0) {
			$tabEsp[$genre][] = 0;
			$tabEspNom[$genre][] = $esp0['valeur'];
			$percent_act = round(($nbeleves_nivact * 100 / $nbeleves_nivglob),2);
			array_push($tabPercentPerEsp[$genre],$percent_act);
		}
		$tabEspODS[$genre][] = 0;
		$tabEspNomODS[$genre][] = $esp0['valeur'];
		$percent_act = round(($nbeleves_nivact * 100 / $nbeleves_nivglob),2);
		array_push($tabNbPerEspODS[$genre],$nbeleves_nivact);
		array_push($tabPercentPerEspODS[$genre],$percent_act);
		$act++;
	}
	foreach($espacs as $espacs_info) {
		$esp_id = $espacs_info['espace_id'];
		if(($filtre_espaces==""||isset($_SESSION['espace'.$esp_id]))) { // on vérifie qu'elle n'est pas décochée
			$nbeleves_nivglob = mysqli_num_rows(mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND sexe = '".$genre."' ".$filtre_activs.""));
			$nbeleves_nivact = mysqli_num_rows(mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND sexe = '".$genre."' AND espace_id = '".$esp_id."'  ".$filtre_activsonly.""));
			if($nbeleves_nivact>0) {
				$tabEsp[$genre][] = $espacs_info['espace_id'];
				$tabEspNom[$genre][] = $espacs_info['espace_nom'];
				$percent_act = round(($nbeleves_nivact * 100 / $nbeleves_nivglob),2);
				array_push($tabPercentPerEsp[$genre],$percent_act);
			}
			$tabEspODS[$genre][] = $espacs_info['espace_id'];
			$tabEspNomODS[$genre][] = $espacs_info['espace_nom'];
			$percent_act = round(($nbeleves_nivact * 100 / $nbeleves_nivglob),2);
			array_push($tabNbPerEspODS[$genre],$nbeleves_nivact);
			array_push($tabPercentPerEspODS[$genre],$percent_act);
			$act++;
		}
	}
}
$bctab4 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($act+2).'" style="text-align:center;">'.FDIR_STATS_INF19.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
$bctab4 .= '<th>'.$esp0['valeur'].'</th>';
foreach($espacs as $espacs_info) {
	$bctab4 .= '<th>'.$espacs_info['espace_nom'].'</th>';
}
$bctab4 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
$w=1;
foreach($tabDebgenre as $genre) {
	$bctab4 .= '<tr><td>'.$genre.'</td>';
	$m=0;
	$tot = 0;
	$bctab4 .= '<td>'.$tabNbPerEspODS[$genre][$m].'</td>';
	$tot += $tabNbPerEspODS[$genre][$m];
	$m++;
	foreach($espacs as $espacs_info) {
		$bctab4 .= '<td>'.$tabNbPerEspODS[$genre][$m].'</td>';
		$tot += $tabNbPerEspODS[$genre][$m];
		$m++;		
	}
	$bctab4 .= '<td>'.$tot.'</td></tr>';
	$w++;
}	
$bctab4 .= '</tbody></table>';
$bctab5 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($act+2).'" style="text-align:center;">'.FDIR_STATS_INF20.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
$bctab5 .= '<th>'.$esp0['valeur'].'</th>';
foreach($espacs as $espacs_info) {
	$bctab5 .= '<th>'.$espacs_info['espace_nom'].'</th>';
}
$bctab5 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
$w=1;
foreach($tabDebgenre as $genre) {
	$bctab5 .= '<tr><td>'.$genre.'</td>';
	$m=0;
	$bctab5 .= '<td>'.number_format((float)$tabPercentPerEspODS[$genre][$m],2,'.','').'</td>';
	$m++;
	foreach($espacs as $espacs_info) {
		$bctab5 .= '<td>'.number_format((float)$tabPercentPerEspODS[$genre][$m],2,'.','').'</td>';
		$m++;
	}
	$bctab5 .= '<td>100.00</td></tr>';
	$w++;
}	
$bctab5 .= '</tbody></table>';
$i=0;
foreach($tabDebgenre as $genre) { ?>
	<div id="wrapper" class="statcadre">
	<canvas id="chart_percentperesp<?php echo $genre; ?>" class="charts" width="400" height="400"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabEspNom[$genre]); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            data: <?php echo json_encode($tabPercentPerEsp[$genre]); ?>,
	            backgroundColor: <?php echo json_encode($basicColors); ?>,
	            borderColor: <?php echo json_encode($basicColors); ?>,
	        }]
	    };
	new Chart(
	    document.getElementById('chart_percentperesp<?php echo $genre; ?>'),
	    {
	    type: 'pie',
	    data : data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: "<?php echo FDIR_STATS_INF21.' '.$tabGenre[$i]; ?>"
	            },
	            legend: {
	                display:true
	            },
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              formatter: function(value, context) {
	                    return value + ' %'; // Ajoute le symbole "%" après la valeur
	                },
	              color: '#878787', // Couleur du texte
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'bold', // Style de police
	              },
	              display: function(context) {
	                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
	                },
	            },
	        },
	        responsive: true,
	      }
	    }
	);
	})();
	</script>
<?php 
$i++; 
} 
echo '<br> <br>'.$bctab4;
$_SESSION['bctabs'] .= '<table></table>'.$bctab4; 
echo '<br> <br>'.$bctab5;
$_SESSION['bctabs'] .= '<table></table>'.$bctab5; 
?>