<?php
// STATS PAR CRENEAU PAR AN
echo '<br>'.FSTATS_TIT15;
// Gestion des semaines
$i=1;
$nb_week_evol = array();
$nb_close_week = array();
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		${'tabNbPerJour'.$i} = array();	
		if(!isset($total_jour[$i])) { $total_jour[$i] = 0; }
		$nb_week_evol[$i]=0;
		$nb_close_week[$i]=0;
		//pas d'un jour
		foreach(new DatePeriod($jourP1date_evol[$i], new DateInterval('P7D'), $jourP2date_evol[$i]) as $dt) {
			$nb_week_evol[$i]++;
			$date = $dt->format('Y-m-d');
			//on supprime l'addition si la semaine est fermée
			$fermeture = mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."' AND statut = 0");
			$fermeture_nb = mysqli_num_rows($fermeture);
			if($fermeture_nb==1) {
				$ferme=1;
				for($k = 1; $k < 7; $k++)
				{
					$fermeture = mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date("Y-m-d", strtotime($date." +".$k." days"))."' AND statut = 0");
					$fermeture_nb = mysqli_num_rows($fermeture);
					if($fermeture_nb==1) {
						$ferme++;
					}
				}
				if($ferme==7) {
					$nb_week_evol[$i]--;
					$nb_close_week[$i]++;
				}
			}
		}
		$i++;
	}
}
// Calcul et affichage des moyennes
$i=1;
include('db/proc/freq_stats_crennb.php');
$moyenele_cren_evol = array();
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		$moyenele_cren_evol[$i] = round(($freq_nb[$i]/($nb_creneaux*$nb_week_evol[$i])),2);
		echo '<p>'.$an[$i].' : '.$moyenele_cren_evol[$i].FDIR_STATS_CREN1.'</p>';
		$i++;
	}
}
// Gestion des créneaux :
include('db/proc/edt_week.php');
include('db/proc/freq_stats_crenprep.php');
$table_width = 75+(136*$nbJour);
// EDT CDI
echo '<div class="edt_grille" style="display:block;width:'.$table_width.'px;background-color:'.substr($cren_colors['valeur'],14,7).';">';
	 
echo '<div style="float:left;width:60px;">';
echo '<div class="th heure" style="height:22px;"><div> </div></div>';
for ($h = 0; $h < $hor_nb; $h++) {
	echo '<div class="th heure" style="height:'.$heighthor[$h].'px;background-color:#fff;">';
	echo '<div';
	if($heighthor[$h]>=30) { echo ' class="pdfmarg"'; }
	echo '>';
	if($heighthor[$h]<14) { echo '<div class="pdfmargnone"><span class="fontsmall">'; }
	if($heighthor[$h]>=12.75) { echo $plageH[$h]; }
	if($heighthor[$h]<14) { echo '</span></div>'; }
	echo '</div></div>';
}
echo '</div>';
$tabJours = array();
for($j = 0; $j < $nbJour+1; $j++) {
	if($j>0) {
	array_push($tabJours,$jourTexte[$j]);
	echo '<div style="display:inline-block;position:relative;top:auto;float:left;width:138px;"><div class="th thday" style="height:21px;position:relative;background-color:#fff;">'.$jourTexte[$j].'</div>'; 
	 
	// les réceptions de briques :
	for ($h = 0; $h < $hor_nb; $h++) {
		if($plage_open[$dayl[$j]][$h]==0) {
			echo '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$heighthor[$h].'px;"></div>';
		}
		else {
			$usercode = "";
			$userid = 0;
			if($plage_demijour[$h]=='M'||$plage_demijour[$h]=='A'||$plage_demijour[$h]=='P') {
				if($plage_demijour[$h]=='M') { $demijourAP = '_am'; }
				else if($plage_demijour[$h]=='A') { $demijourAP = '_pm'; }
				else if($plage_demijour[$h]=='P'||$plage_demijour[$h]=='H'||$plage_demijour[$h]=='T'||$plage_demijour[$h]=='C') {
					if(substr($plageH[$h],0,2)<12) {
						$demijourAP = '_am'; }
					else if(substr($plageH[$h],0,2)>=12) {
						$demijourAP = '_pm'; }
				}
				$demijourverif = mysqli_query($bdd,"SELECT statut FROM ".$prefix."_hordemis WHERE demijour = '".$demijourGestion[$j].$demijourAP."'");
				$demijourverif_info = mysqli_fetch_assoc($demijourverif);
				if($demijourverif_info['statut'] == 0) {
					echo '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$heighthor[$h].'px;"></div>';
				}
				else {
					include('db/proc/freq_stats_crencell_evol.php');
				}
			}			
			else if($plage_demijour[$h]=='R') {
				$demijourverif = mysqli_query($bdd,"SELECT statut FROM ".$prefix."_hordemis WHERE demijour LIKE '".$demijourGestion[$j]."%'");
				$midiclose=0;
				while($demijourverif_info = mysqli_fetch_assoc($demijourverif)) {
					if($demijourverif_info['statut'] == 0) {
						$midiclose = 1;
						break;
					}
				}
				if($midiclose==1) {
					echo '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$heighthor[$h].'px;"></div>';
				}
				else {
					include('db/proc/freq_stats_crencell_evol.php');
				}
			}
		}
	}}
	echo '</div>';
	if(isset($an[5])) { $i=5; }
	else if(isset($an[4])) { $i=4; }
	else if(isset($an[3])) { $i=3; }
	else { $i=2; }
	for($i;$i>$nb_sans;$i--) { 
		array_push(${'tabNbPerJour'.$i}, round($total_jour[$i],2));
	}
}
if(isset($an[5])) { $i=5; }
else if(isset($an[4])) { $i=4; }
else if(isset($an[3])) { $i=3; }
else { $i=2; }
for($i;$i>$nb_sans;$i--) { 
	${'tabNbPerJourDef'.$i} = array();
	for($j=1;$j<=$nbJour;$j++) {
		if(isset(${'tabNbPerJour'.$i}[$j-1])) {
			$check = (${'tabNbPerJour'.$i}[$j]-${'tabNbPerJour'.$i}[$j-1]);
		}
		else {
			$check = ${'tabNbPerJour'.$i}[$j];
		}
		array_push(${'tabNbPerJourDef'.$i}, round($check,2));
	}
}
echo '</div>';
echo '<div style="clear:both;padding-top:30px;">';
echo '<br><h3>'.FDIR_STATS_CREN2.'</h3>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbperyearperjour" class="charts" width="600" height="300"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($tabJours); ?>;
    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(${'tabNbPerJourDef'.$i}); ?>,
			backgroundColor: 'grey',
            borderColor: 'grey',
			borderWidth: 1
		} <?php if($j<=count($an)) { echo ','; } $j++;} ?> ]
    };
new Chart(
    document.getElementById('chart_nbperyearperjour'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_CREN3; ?> (<?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: '#fff', // Couleur du texte
              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 '</div>';
?>