<?php
if($day1>=$rentree) {
	echo '<h3>'.FDIR_STATS_TIT25.DOTS.'</h3>';
	echo FDIR_STATS_INF1;
	//élèves sans freq
	$elevespres_select = "SELECT freq.freq_id, eleves.eleves_id, freq.eleves_id, eleves.division, eleves.nom, eleves.prenom FROM ".$prefix."_eleves eleves 
		LEFT OUTER JOIN ".$prefix."_freq freq 
		ON freq.eleves_id = eleves.eleves_id
		WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' ".$filtre_activs." GROUP BY eleves.eleves_id";
	$elevespres_nb = mysqli_num_rows(mysqli_query($bdd,$elevespres_select));
	$eleves_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT eleves_id, division, nom, prenom FROM ".$prefix."_eleves ORDER BY division ASC"));
	$elevesabs_nb = $eleves_nb-$elevespres_nb;
	if($elevesabs_nb>0) {
		echo '<p>'.$elevesabs_nb.' élève';
		if($elevesabs_nb>1) { echo 's'; }
		echo ' (sur '.$eleves_nb.').</p>';
		echo '<div id="pager4" class="pager4">
		<form>
			<i class="fas fa-fast-backward first"></i>
			<i class="fas fa-backward prev"></i>
			<!-- the "pagedisplay" can be any element, including an input -->
			<input type="text" class="pagedisplay inptxt"/>
			<i class="fas fa-forward next"></i>
			<i class="fas fa-fast-forward last"></i>
			<div class="box"><select class="pagesize">
				<option selected="selected"  value="15">15'.PERPAGE.'</option>
				<option value="30">30'.PERPAGE.'</option>
				<option  value="45">45'.PERPAGE.'</option>
				<option  value="60">60'.PERPAGE.'</option>
			</select></div>
		</form><br>
		</div>';
		$bctab8 = '<table><thead><tr><td colspan="2" style="text-align:center;">'.FDIR_STATS_TIT25.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.DIV.'</th><th>'.NAMECH2.'</th></tr></thead><tbody>';
		echo '<table  cellspacing="1" class="tablesorter table4"><thead><tr><th>'.DIV.'</th><th>'.NAMECH2.'</th></tr></thead><tbody>';
		//élèves avec freq et filtre
		$elevespres_select = "SELECT freq.freq_id, eleves.eleves_id, freq.eleves_id, eleves.division, eleves.nom, eleves.prenom FROM ".$prefix."_eleves eleves 
		LEFT OUTER JOIN ".$prefix."_freq freq 
		ON freq.eleves_id = eleves.eleves_id
		WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' ".$filtre_activs." GROUP BY eleves.eleves_id";
		$elevespres = mysqli_query($bdd,$elevespres_select);
		//élèves
		$eleves = mysqli_query($bdd,"SELECT eleves_id, division, nom, prenom FROM ".$prefix."_eleves ORDER BY division ASC");
		foreach($eleves as $eleves_info) {
			$elecheck = 0;
			foreach($elevespres as $freqcheck) {
				if($freqcheck['eleves_id']==$eleves_info['eleves_id']) { $elecheck =1; }
			}
			if($elecheck==0) {
				echo '<tr><td class="center">'.$eleves_info['division'].'</td><td>'.$eleves_info['nom'].' '.$eleves_info['prenom'].'</td></tr>';
				$bctab8 .= '<tr><td class="center">'.$eleves_info['division'].'</td><td>'.$eleves_info['nom'].' '.$eleves_info['prenom'].'</td></tr>';
			}
		}
		echo '</tbody></table>';
		$bctab8 .= '</tbody></table>';
		$_SESSION['bctabs'].= '<table></table>'.$bctab8;
		?>
		<!-- fréquentation par division -->
		<?php
		$bctab9 = '<table style="text-align:right;">
		<thead><tr><td colspan="4" style="text-align:center;">'.FDIR_STATS_TIT26.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th><th>'.NUMBER.'</th><th>Sur</th><th>%</th></tr></thead><tbody>';
		$tabNbPerDiv = array();
		$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division ASC");
		$tot1=0;
		foreach($liste_division as $listediv) {
			$div=0;
			//élèves avec freq et filtre
			$elevespres_select = "SELECT freq.freq_id, eleves.eleves_id, freq.eleves_id, eleves.division, eleves.nom, eleves.prenom FROM ".$prefix."_eleves eleves 
			LEFT OUTER JOIN ".$prefix."_freq freq 
			ON freq.eleves_id = eleves.eleves_id
			WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' ".$filtre_activs." GROUP BY eleves.eleves_id";
			$elevespres = mysqli_query($bdd,$elevespres_select);
			//élèves
			$eleves = mysqli_query($bdd,"SELECT eleves_id, division, nom, prenom FROM ".$prefix."_eleves WHERE division = '".$listediv['division']."'");
			foreach($eleves as $eleves_info) {
				$elecheck = 0;
				foreach($elevespres as $freqcheck) {
					if($freqcheck['eleves_id']==$eleves_info['eleves_id']) { $elecheck =1; }
				}
				if($elecheck==0) {
					$div++;					
				}				
			}
			$tot1+=$div;
			array_push($tabNbPerDiv, $div);
			$percent = round(($div * 100 / mysqli_num_rows($eleves)),2);
			$bctab9 .= '<tr><td>'.$listediv['division'].'</td><td>'.$div.'</td><td>'.mysqli_num_rows($eleves).'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
		}
		$tote = mysqli_num_rows(mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves"));
		$percent2 = round(($tot1 * 100 / $tote),2);
		$bctab9 .= '<tr><td>'.TOTAL.'</td><td>'.$tot1.'</td><td>'.$tote.'</td><td>'.number_format((float)$percent2,2,'.','').'</td></tbody></table>';		
		?>
		<div id="wrapper" class="statcadre">
		<canvas id="chart_nbperdiv" class="charts" width="600" height="300"></canvas>
		</div>
		<script type="text/javascript">
		(async function() {
		    
		    Chart.register(ChartDataLabels);
		    const labels = <?php echo json_encode($tabDivisions); ?>;
		    const data = {
		      labels: labels,
		      datasets: [{
					label: "<?php echo FDIR_STATS_INF2; ?>",
					data: <?php echo json_encode($tabNbPerDiv); ?>,
					backgroundColor: <?php echo json_encode($tabDivColors); ?>,
					borderColor: <?php echo json_encode($tabDivColors); ?>,
					borderWidth: 1
				}]
		    };
		new Chart(
		    document.getElementById('chart_nbperdiv'),
		    {
		    type: 'bar',
		    data: data,
		    options: {
		        plugins: {
		            title: {
		                display: true,
		                text: "<?php echo FDIR_STATS_TIT26; ?>"
		            },
		            legend: {
		                display:false
		            },
		            tooltip : {
		                mode: 'point',
		                intersect: false,
		              }  ,    
		            datalabels: {
		              display: true, // Affiche les étiquettes de données
		              color: '#878787', // 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>'.$bctab9;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab9;
	}
	else { echo FDIR_STATS_INF3; }
}
?>