<!-- fréquentation par division par an -->
<?php
// Vérification de la dispo de ces stats
$i=1;
foreach($freq_nb as $addfreq) {
//foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p') { $ancheck = $an[$i+1]; }
	else { $ancheck = $an[$i]; }
	$eff_divs = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$ancheck."'"));
	if($eff_divs==0) { $dispo = '3'; } else { $dispo = '1'; }
	$nbeleves_niv = mysqli_num_rows(mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$firstday[$i]."' AND freq.date_freq <= '".$lastday[$i]."' ".$filtre_activs." AND freq.eleves_id = 0"));
	if($nbeleves_niv>0) { $dispo = '2'; }
	$i++;
}
if($dispo=='3') {
	echo '<p class="max-width" style="margin-top:30px;"><b>'.FDIR_STATS_EVOL7.'</b><br>'.FDIR_STATS_EVOL5.'<a href="param.php?page=etab">param.php?page=etab</a>.</p>';
}
else if($dispo=='2') {
	echo '<p class="max-width" style="margin-top:30px;"><b>'.FDIR_STATS_EVOL7.'</b><br>'.FDIR_STATS_EVOL6.'</p>';
}
else if($dispo=='1') {
	$i=1;
	foreach($an as $eachyear) {
		if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
		else {
			$tabNbPerDiv[$i] = array();
			$tabPercentPerDiv[$i] = array();
			$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_etab_effectifs GROUP BY division ORDER BY division ASC");
			$div=0;
			foreach($liste_division as $listediv) {
				if($listediv['division']!="") {
					$nbeleves_div = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$firstday[$i]."' AND date_freq <= '".$lastday[$i]."' AND division = '".$listediv['division']."' ".$filtre_activs." GROUP BY freq.eleves_id");
					$nbeleves = mysqli_num_rows($nbeleves_div);
					array_push($tabNbPerDiv[$i], $nbeleves);
					$eff_divs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$an[$i]."' AND division = '".$listediv['division']."'");
					$eff_divglob=0;
					foreach($eff_divs as $eff_div) {
						$eff_divglob += $eff_div['effectif'];
					}
					if($eff_divglob==0) {
						$percent=0;
					}
					else { $percent = round(($nbeleves * 100 / $eff_divglob),2); }
					if($percent>100) { $percent = 100; }
					array_push($tabPercentPerDiv[$i], $percent);
				}
				$div++;
			}
			$i++;
		}
	}
	$bctab4 = '<table style="text-align:right;">
	<thead><tr><td colspan="'.($div+2).'" style="text-align:center;">'.FDIR_STATS_EVOL7.'</td></tr><tr><th>'.YEAR.'</th>';
	$nivi=0;
	foreach($liste_division as $listediv) {
		$bctab4 .= '<th>'.$listediv['division'].'</th>';
		$nivi++;
	}
	$bctab4 .= '</tr></thead><tbody>';
	$w=1;
	foreach($an as $eachyear) {
		$bctab4 .= '<tr><td>'.$an[$w].'</td>';
		$m=0;$tot=0;
		foreach($liste_division as $listediv) {
			$bctab4 .= '<td>'.number_format((float)$tabPercentPerDiv[$w][$m],2,'.','').'</td>';
			$m++;
		}
		$bctab4 .= '</tr>';
		$w++;
	}	
	$bctab4 .= '</tbody></table>';
	?>
	<div id="wrapper" class="statcadre">
	<canvas id="chart_propperyearperdiv" class="charts" width="600" height="300"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabDivisionsFreq); ?>;
	    const data = {
	      labels: labels,
	      datasets: [
			<?php
			if(isset($an[5])) { $i=5; }
			else if(isset($an[4])) { $i=4; }
			else if(isset($an[3])) { $i=3; }
			else { $i=2; }
			$j=1;
			if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; $j=2; }
			for($i;$i>$nb_sans;$i--) { ?>
			{
				label: <?php echo json_encode($an[$i]); ?>,
				data: <?php echo json_encode($tabPercentPerDiv[$i]); ?>,
				backgroundColor: <?php echo json_encode($tabDivColorsFreq); ?>,
				borderColor: <?php echo json_encode($tabDivColorsFreq); ?>,
				borderWidth: 1
			} <?php if($j<=count($an)) { echo ','; } $j++;} ?> ]
	    };
	new Chart(
	    document.getElementById('chart_propperyearperdiv'),
	    {
	    type: 'bar',
	    data: data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: "<?php echo FDIR_STATS_EVOL7; ?> (<?php if(isset($an[5])) { $i=5; } else if(isset($an[4])) { $i=4; } else if(isset($an[3])) { $i=3; } else { $i=2; } $j=1; if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; $j=2; } for($i;$i>$nb_sans;$i--) { echo $an[$i]; if($j<count($an)) { echo ' - '; } $j++; } ?>)"
	            },
	            legend: {
	    			display:false
	    		},
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              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,
	        scales: {
	            yAxes: [{
	                ticks: {
	                    beginAtZero:true
	                }
	            }]
	        },
	      }
	    }
	);
	})();
	</script>
<?php
echo '<br> <br>'.$bctab4;
$_SESSION['bctabs'] .= '<table></table>'.$bctab4;
}