<?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'];
	foreach(${$codepays.'_months'} as $month) {
		if($codepays=='fnc'&&$month=='01') {
			$y = $year+1;
		}
		if(($codepays=='fra'||$codepays=='bel'||$codepays=='aut')&&($month>='01'&&$month<='07')) {
			$y = substr($year,4,4);
		}
		else if($codepays=='fra'||$codepays=='bel'||$codepays=='aut') {
			$y = substr($year,0,4);
		}
		$nbeleves_niv = mysqli_query($bdd,"SELECT freq_id FROM ".$prefix."_freq WHERE date_freq LIKE '".$y."-".$month."%' 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 LIKE '".$y."-".$month."%' AND niveau = '".$divdeb."' AND passage = '0' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$nbeleves += $elegrap['nombre'];
			}
		}
		array_push($tab[$i], $nbeleves);
	}
	$i++;
}
$bctab2 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($i+1).'" style="text-align:center;">'.FDIR_STATS_PER1.FDIR_STATS_PER4.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
$nivi=0;
foreach($tabDebniveau as $divdeb) {
	$bctab2 .= '<th>'.$tabNiveau[$nivi].'</th>';
	$nivi++;
}
$bctab2 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
$w=0;
foreach(${$codepays.'_months'} as $month) {
	$bctab2 .= '<tr><td>'.$tabMonths[$w].'</td>';
	$m=1;
	foreach($tabDebniveau as $divdeb) {
		$bctab2 .= '<td>'.$tab[$m][$w].'</td>';
		$m++;
	}
	$bctab2 .= '<td>'.$tabNbPerMonth[$w].'</td></tr>';
	$w++;
}	
$bctab2 .= '<tr><td>'.TOTAL.'</td><td colspan="'.($i-1).'"></td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbpermonthperniv" class="charts" width="600" height="300"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($tabMonths); ?>;
    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_nbpermonthperniv'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_PER1.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>