<?php
echo '<p>'.FSTATS_TIT2.' '.$totalfreq_nb.' '.ELEVS1.'.</p>';

//récupération des niveaux et couleurs par niveau
include('freq_stats_nivcolors.php');

$hor_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires"));
		
// Gestion des dates :
include('./db/proc/edt_week.php');
include('./db/proc/freq_stats_crennb.php');
include('./db/proc/calweeknb.php');
$table_width = 75+(136*$nbJour);

$moyenele_cren = round(($totalfreq_nb/($nb_creneaux*$nb_week)),2);
echo '<p>'.$moyenele_cren.FSTATS_TIT6.'</p>';
					
// Gestion des créneaux :
include('db/proc/freq_stats_crenprep.php');
	 
echo '<br>';

// 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();
$tabNbPerJour = array();
$total_semaine = 0;
if(!isset($total_jour)) { $total_jour = 0; }
$bctab1 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.FSTATS_TIT7.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.JOUR1.'</th><th>'.MOYENNE.'</th></tr></thead><tbody>';
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 plages horaires :
	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.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.php');
				}
			}
		}
	}
	array_push($tabNbPerJour, round($total_jour,2));
	$bctab1 .= '<tr><td>'.$jourTexte[$j].'</td><td>'.round($total_jour,2).'</td></tr>';
	$total_semaine += round($total_jour,2);
	$total_jour = 0;	
	}
	echo '</div>';
}
echo '</div>';
array_push($tabJours,MOY2);
array_push($tabNbPerJour, round($total_semaine/$nbJour,2));
$bctab1 .= '<tr><td>'.MOY2.'</td><td>'.round($total_semaine/$nbJour,2).'</td></tr>';
$bctab1 .= '</tbody></table>';
echo '<div style="clear:both;padding-top:30px;">
<p>'.STATS_RECAP14.'</p>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbperjour" 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: [{
            data: <?php echo json_encode($tabNbPerJour); ?>,
            backgroundColor: 'grey',
            borderColor: 'grey',
            borderWidth: 1
        }]
    };
new Chart(
    document.getElementById('chart_nbperjour'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: '<?php echo FSTATS_TIT7; ?>'
            },
            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,
        maintainAspectRatio: false,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
      }
    }
);
})();
</script>
<?php
echo '<br> <br>'.$bctab1;
$_SESSION['bctabs'] = $bctab1;
echo '</div>';

echo '<br> <a onclick="submitods();" id="preview-ods"><i class="fa-solid fa-file-export" style="font-size:1.5em;"></i> '.STATS_DL1.'</a>';
if(isset($_POST['haveods'])) {
	include('stats_ods_loader.php');
}
echo '<form id="loadods" action="db/proc/stats/stats_ods_loader.php" method="post"><input type="hidden" name="haveods" value="1" /></form>';

echo '<script type="text/javascript">
function submitods(){
	$("#loadods").submit()
}
</script>';
?>