<?php
$i=1;
foreach($tabDebniveau as $divdeb) {
	$tab[$i] = array();
	$nivcolor = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$divdeb."'");
	$nivcolor_info = mysqli_fetch_assoc($nivcolor);
	$tabColor[$i] = $nivcolor_info['bgcolor'];
	$rack=0;
	for($z=0;$z<((count($per_pv)/2)-1);$z++) {
			if(isset($per_pv[$rack])) {
			$nbeleves_niv = mysqli_query($bdd,"SELECT freq_id FROM ".$prefix."_freq WHERE date_freq > '".$per_pv[$rack]."' AND date_freq <= '".$per_pv[($rack+1)]."' 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 > '".$per_pv[$rack]."' AND date_freq <= '".$per_pv[($rack+1)]."' AND niveau = '".$divdeb."' AND passage = '0' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
		}
		array_push($tab[$i], $nbeleves);
		$rack+=2;
	}
	$i++;
}
$bctab6 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($i+1).'" style="text-align:center;">'.FDIR_STATS_PER3.FDIR_STATS_PER4.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
$nivi=0;
foreach($tabDebniveau as $divdeb) {
	$bctab6 .= '<th>'.$tabNiveau[$nivi].'</th>';
	$nivi++;
}
$bctab6 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
$w=0;
for($z=0;$z<((count($per_pv)/2)-1);$z++) {
	$bctab6 .= '<tr><td>'.$tabPeriodes[$w].'</td>';
	$m=1;
	foreach($tabDebniveau as $divdeb) {
		$bctab6 .= '<td>'.$tab[$m][$w].'</td>';
		$m++;
	}
	$bctab6 .= '<td>'.$tabNbPerPeriode[$w].'</td></tr>';
	$w++;
}	
$bctab6 .= '<tr><td>'.TOTAL.'</td><td colspan="'.($i-1).'"></td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbperperiodeperniv" class="charts" width="600" height="300"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($tabPeriodes); ?>;
    const data = {
      labels: labels,
      datasets: [ 
        <?php $i=1;$j=1;
        foreach($tabDebniveau as $divdeb) { ?>
        {
            label: <?php echo json_encode($tabNiveau[$i-1]); ?>,
            data: <?php echo json_encode($tab[$i]); ?>,
            backgroundColor: '<?php echo $tabColor[$i] ?>',
            borderColor: '<?php echo $tabColor[$i] ?>',
            borderWidth: 1
        } <?php if($j<count($tabDebniveau)) { echo ','; } $i++; $j++;} ?> ]
    };
new Chart(
    document.getElementById('chart_nbperperiodeperniv'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_PER3.FDIR_STATS_PER4; ?>"
            },
            legend: {
                display:true
            },
            tooltip : {
                mode: 'point',
                intersect: false,
              }  ,    
            datalabels: {
              display: true, // Affiche les étiquettes de données
              color: '#fff', // Couleur du texte
              align: 'center', // Alignement horizontal
              anchor: 'center', // Alignement vertical
              font: {
                weight: 'bold', // Style de police
              },
              display: function(context) {
                  return false;
                },
            },
        },
        responsive: true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
      }
    }
);
})();
</script>