<?php
// fréquentation par niveau par an :
$i=1;
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		$tabNbPerNiv[$i] = array();
		$nbeleves_niv = 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."");
		$nbeleves = mysqli_num_rows($nbeleves_niv);
		if($grappes_check==1) {
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$firstday[$i]."' AND date_freq <= '".$lastday[$i]."' AND passage = '0' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$nbeleves += $elegrap['nombre'];
			}
		}
		array_push($tabNbPerNiv[$i], $nbeleves);
		foreach ($tabDebniveau as $divdeb) {
			$nivcolor = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$divdeb."'");
			$nivcolor_info = mysqli_fetch_assoc($nivcolor);
			$tabColor[$i] = $nivcolor_info['bgcolor'];
			$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$firstday[$i]."' AND freq.date_freq <= '".$lastday[$i]."' AND division LIKE '".$divdeb."%' ".$filtre_activs."");
			$nbeleves = mysqli_num_rows($nbeleves_niv);
			if($grappes_check==1) {
				$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$firstday[$i]."' AND date_freq <= '".$lastday[$i]."' AND niveau ='".$divdeb."' AND passage = '0' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			array_push($tabNbPerNiv[$i], $nbeleves);
		}
		$i++;
	}
}
$bctab1 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($i+3).'" style="text-align:center;">'.FDIR_STATS_EVOL3.'</td></tr><tr><th>'.YEAR.'</th><th>'.TOTAL.'</th>';
$nivi=0;
foreach($tabDebniveau as $divdeb) {
	$bctab1 .= '<th>'.$tabNiveau[$nivi].'</th>';
	$nivi++;
}
$bctab1 .= '</tr></thead><tbody>';
$w=1;
foreach($an as $eachyear) {
	$bctab1 .= '<tr><td>'.$an[$w].'</td>';
	$m=0;$tot=0;
	$bctab1 .= '<td>'.$tabNbPerNiv[$w][$m].'</td>';
	$m++;
	foreach($tabDebniveau as $divdeb) {
		$bctab1 .= '<td>'.$tabNbPerNiv[$w][$m].'</td>';
		$m++;
	}
	$bctab1 .= '</tr>';
	$w++;
}	
$bctab1 .= '</tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbperyearperniv" class="charts" width="600" height="300"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($taballNiveau); ?>;
    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($tabNbPerNiv[$i]); ?>,
			backgroundColor: <?php echo json_encode($tabBgallcolor); ?>,
			borderColor: <?php echo json_encode($tabBgallcolor); ?>,
			borderWidth: 1
		} <?php if($j<=count($an)) { echo ','; } $j++;} ?> ]
    };
new Chart(
    document.getElementById('chart_nbperyearperniv'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_EVOL3; ?> (<?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: function(context) {
			        var index = context.dataIndex;
			        var color = '#878787'; // Couleur par défaut

			        // Condition pour déterminer la couleur en fonction de l'index ou d'autres critères
			        if (index === 0) {
			            color = '#fff';
			        } 
			        return color; // Retourne la couleur déterminée
			    },
              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>'.$bctab1;
$_SESSION['bctabs'] .= $bctab1;
?>