<?php
// Calcul initial
$i=1;
$exclu_nb = array();
$limit_nb = array();
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		$exclu = mysqli_query($bdd,"SELECT exclusion_id FROM ".$prefix."_exclusions WHERE date_exclusion >= '".$firstday[$i]."' AND date_exclusion <= '".$lastday[$i]."'");
		$exclu_nb[$i] = mysqli_num_rows($exclu);
		$limit = mysqli_query($bdd,"SELECT limit_id FROM ".$prefix."_limits WHERE date_debut >= '".$firstday[$i]."' AND date_debut <= '".$lastday[$i]."'");
		$limit_nb[$i] = mysqli_num_rows($limit);
	$i++;
	}
}
$totalexclu_nb = 0;
$i=1;
foreach($exclu_nb as $addexclu) {
	$totalexclu_nb += $addexclu;
	if(substr($_SESSION['nb_year'],1,1)=='p') { echo '<p><b>'.$an[$i+1].' : '; }
	else { echo '<p><b>'.$an[$i].' : '; }
	if($addexclu==0) { echo FREQ_NOEXC.'.</b></p>'; }
	else { 
		echo $addexclu.' '.FREQ_EXC;
		if($addexclu>1) { echo PLURIEL; }
		echo '.</b></p>'; }
	$i++;
}

// exclusions par semaine par an (en lines)
$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 exclusion_id FROM ".$prefix."_exclusions WHERE date_exclusion >= '".$dt->format('Y-m-d')."' AND date_exclusion <= '".$dateend."'");
			$nbeleves = mysqli_num_rows($nbeleves_niv);
			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++;
	}
}
$bctab6 = '<table style="text-align:right;">
		<thead><tr><td colspan="'.$i.'" style="text-align:center;">'.FSTATS_TIT4.FSTATS_INF2.'</td></tr><tr><th></th>';
$nivi=0;
foreach($an as $eachyear) {
	$bctab6 .= '<th>'.$an[($nivi+1)].'</th>';
	$nivi++;
}
$bctab6 .= '</tr></thead><tbody>';
$w=0;
foreach($tabWeeks as $weekname) {
	$bctab6 .= '<tr><td>'.$weekname.'</td>';
	$m=1;
	foreach($an as $eachyear) {
		$bctab6 .= '<td>'.$tabNbPerWeek[$m][$w].'</td>';
		$m++;
	}
	$bctab6 .= '</tr>';
	$w++;
}	
$bctab6 .= '</tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_excluperweekperyear" 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_excluperweekperyear'),
    {
    type: 'line',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FSTATS_TIT4; ?> (<?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>'.$bctab6;
$_SESSION['bctabs'] .= '<table></table>'.$bctab6;
$totallimit_nb = 0;
$i=1;
echo '<br>';
foreach($limit_nb as $addlimit) {
	$totallimit_nb += $addlimit;
	if(substr($_SESSION['nb_year'],1,1)=='p') { echo '<p><b>'.$an[$i+1].' : '; }
	else { echo '<p><b>'.$an[$i].' : '; }
	if($addlimit==0) { echo FREQ_NOLIMIT1.'.</b></p>'; }
	else { 
		echo $addlimit.' '.FREQ_LIMIT;
		if($addlimit>1) { echo PLURIEL; }
		echo '.</b></p>'; }
	$i++;
}
// limitations par semaine par an (en lines)
$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 limit_id FROM ".$prefix."_limits WHERE date_debut >= '".$dt->format('Y-m-d')."' AND date_debut <= '".$dateend."'");
			$nbeleves = mysqli_num_rows($nbeleves_niv);
			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++;
	}
}
$bctab7 = '<table style="text-align:right;">
		<thead><tr><td colspan="'.$i.'" style="text-align:center;">'.FSTATS_TIT5.FSTATS_INF2.'</td></tr><tr><th></th>';
$nivi=0;
foreach($an as $eachyear) {
	$bctab7 .= '<th>'.$an[($nivi+1)].'</th>';
	$nivi++;
}
$bctab7 .= '</tr></thead><tbody>';
$w=0;
foreach($tabWeeks as $weekname) {
	$bctab7 .= '<tr><td>'.$weekname.'</td>';
	$m=1;
	foreach($an as $eachyear) {
		$bctab7 .= '<td>'.$tabNbPerWeek[$m][$w].'</td>';
		$m++;
	}
	$bctab7 .= '</tr>';
	$w++;
}	
$bctab7 .= '</tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_limitperweekperyear" 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_limitperweekperyear'),
    {
    type: 'line',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: '<?php echo FSTATS_TIT5; ?> (<?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>'.$bctab7;
$_SESSION['bctabs'] .= '<table></table>'.$bctab7;
?>