<!-- fréquentation par semaine par an (en lines) -->
<?php

$i=1;
$tabWeeks = array();
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		$tabNbPerWeek[$i] = array();
		//pas de sept jours
		foreach (new DatePeriod($jourP1date_evol[$i], new DateInterval('P7D'), $jourP2date_evol[$i]) as $dt) {
			$date = $dt->format('Y-m-d');
			$week = $dt->format('W');
			$datetoend = strtotime(date("Y-m-d", strtotime($date)) . " +6 day"); // ajouter 6 jours
			$dateend = date('Y-m-d',$datetoend);
			$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$dt->format('Y-m-d')."' AND freq.date_freq <= '".$dateend."' ".$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 >= '".$dt->format('Y-m-d')."' AND date_freq <= '".$dateend."' AND passage = '0' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			array_push($tabNbPerWeek[$i], $nbeleves);
			if(substr($_SESSION['nb_year'],1,1)=='p'&&$i==2) { array_push($tabWeeks, $week); }
			else if($i==1) { array_push($tabWeeks, $week); }
		}
		$i++;
	}
}
$bctab5 = '<table style="text-align:right;">
		<thead><tr><td colspan="'.$i.'" style="text-align:center;">'.FDIR_STATS_EVOL1.FSTATS_INF2.'</td></tr><tr><th></th>';
$nivi=0;
foreach($an as $eachyear) {
	$bctab5 .= '<th>'.$an[($nivi+1)].'</th>';
	$nivi++;
}
$bctab5 .= '</tr></thead><tbody>';
$w=0;
foreach($tabWeeks as $weekname) {
	$bctab5 .= '<tr><td>'.$weekname.'</td>';
	$m=1;
	foreach($an as $eachyear) {
		$bctab5 .= '<td>'.$tabNbPerWeek[$m][$w].'</td>';
		$m++;
	}
	$bctab5 .= '</tr>';
	$w++;
}	
$bctab5 .= '</tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbperweekperyear" class="charts" width="600" height="300"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($tabWeeks); ?>;
    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($tabNbPerWeek[$i]); ?>,
			fill:false,
			borderColor: <?php echo json_encode($tabYearColor[$i]); ?>,
			lineTension:0.1
		} <?php if($j<=count($an)) { echo ','; } $j++;} ?> ]
    };
new Chart(
    document.getElementById('chart_nbperweekperyear'),
    {
    type: 'line',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_EVOL1; ?> (<?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: 'end', // Alignement horizontal
              anchor: 'end', // 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>'.$bctab5;
$_SESSION['bctabs'] .= '<table></table>'.$bctab5;
?>