<?php
//par sexe:
foreach($tabDebgenre as $genre) {
	$activNiveau[$genre] = array();
	$tabActivites[$genre] = array();
	$tabActivitesNom[$genre] = array();

	//fréquentation par activité :
	$tabNbPerActiviteODS[$genre] = array();
	$tabPercentPerActivite[$genre] = array();
	$tabPercentPerActiviteODS[$genre] = array();
	$act=0;
	foreach($activ as $activ_info) {
		$activ_code = $activ_info['code_activ'];
		if(($filtre_activs==""||@$_SESSION['verif_activ'][0]==""||in_array($activ_code,$_SESSION['verif_activ']))&&$activ_code!='ZERO') { // 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 code_activ = '".$activ_code."' ".$filtre_espaces.""));
			if($nbeleves_nivact>0) {
				$tabActivites[$genre][] = $activ_info['code_activ'];
				$tabActivitesNom[$genre][] = $activ_info['nom_activ'];
				$percent_act = round(($nbeleves_nivact * 100 / $nbeleves_nivglob),2);
				array_push($tabPercentPerActivite[$genre],$percent_act);
			}
			$percent_actODS = round(($nbeleves_nivact * 100 / $nbeleves_nivglob),2);
			array_push($tabNbPerActiviteODS[$genre],$nbeleves_nivact);
			array_push($tabPercentPerActiviteODS[$genre],$percent_actODS);
			$act++;
		}
	}
}
$bctab4 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($act+2).'" style="text-align:center;">'.FDIR_STATS_TIT10.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
foreach($activ as $activ_info) {
	$bctab4 .= '<th>'.$activ_info['nom_activ'].'</th>';
}
$bctab4 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
$w=1;
foreach($tabDebgenre as $genre) {
	$bctab4 .= '<tr><td>'.$genre.'</td>';
	$m=0;
	$tot = 0;
	foreach($activ as $activ_info) {
		$bctab4 .= '<td>'.$tabNbPerActiviteODS[$genre][$m].'</td>';
		$tot += $tabNbPerActiviteODS[$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_TIT11.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
foreach($activ as $activ_info) {
	$bctab5 .= '<th>'.$activ_info['nom_activ'].'</th>';
}
$bctab5 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
$w=1;
foreach($tabDebgenre as $genre) {
	$bctab5 .= '<tr><td>'.$genre.'</td>';
	$m=0;
	foreach($activ as $activ_info) {
		$bctab5 .= '<td>'.number_format((float)$tabPercentPerActiviteODS[$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_percentperactiv<?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($tabActivitesNom[$genre]); ?>;
		    const data = {
		      labels: labels,
		      datasets: [{
		            data: <?php echo json_encode($tabPercentPerActivite[$genre]); ?>,
		            backgroundColor: <?php echo json_encode($basicColors); ?>,
		            borderColor: <?php echo json_encode($basicColors); ?>,
		        }]
		    };
		new Chart(
		    document.getElementById('chart_percentperactiv<?php echo $genre; ?>'),
		    {
		    type: 'pie',
		    data : data,
		    options: {
		        plugins: {
		            title: {
		                display: true,
		                text: "<?php echo FDIR_STATS_TIT12.' '.$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;  
?>