<?php

if(isset($_GET["s"])){
	$_SESSION['bctabs']='';
	if($_GET["s"]=="nbperespace"||$_GET["s"]=="percentperespace"||$_GET["s"]=="all") {
		$bctab1 = '<table style="text-align:right;">
		<thead><tr><td colspan="3" style="text-align:center;">'.FDIR_STATS_TIT31.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th><th>'.NUMBER.'</th><th>%</th></tr></thead><tbody>';
		$tabNbPerEsp = array();
		$tabPercentPerEsp = array();
		if(isset($_SESSION['espace0'])||$filtre_espaces=="") {
			$nbeleves_activ = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND (espace_id = '0' OR espace_id IS NULL) ".$filtre_activsonly."");
			$nbeleves = mysqli_num_rows($nbeleves_activ);
			//si grappes
			if($grappes_check==1) {
				$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND passage = '0' AND (espace_id = '0' OR espace_id IS NULL) ".$filtre_activsonly."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			if($nbeleves>0) {
				$tabEsp[] = 0;
				$tabEspNom[] = $esp0['valeur'];
				array_push($tabNbPerEsp, $nbeleves);
				$percent = round(($nbeleves * 100 / $totalfreq_nb),2);
				array_push($tabPercentPerEsp, $percent);
				$bctab1 .= '<tr><td>'.$esp0['valeur'].'</td><td>'.$nbeleves.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
			}
		}
		foreach($espacs as $espacs_info) {
			$esp_id = $espacs_info['espace_id'];
			if(($filtre_espaces==""||isset($_SESSION['espace'.$esp_id]))) { // on vérifie qu'elle n'est pas décochée
				$nbeleves_activ = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND espace_id = '".$esp_id."' ".$filtre_activsonly."");
				$nbeleves = mysqli_num_rows($nbeleves_activ);
				//si grappes
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND passage = '0' AND espace_id = '".$esp_id."' ".$filtre_activsonly."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				if($nbeleves>0) {
					$tabEsp[] = $espacs_info['espace_id'];
					$tabEspNom[] = $espacs_info['espace_nom'];
					array_push($tabNbPerEsp, $nbeleves);
					$percent = round(($nbeleves * 100 / $totalfreq_nb),2);
					array_push($tabPercentPerEsp, $percent);
					$bctab1 .= '<tr><td>'.$espacs_info['espace_nom'].'</td><td>'.$nbeleves.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
				}
			}
		}
		$bctab1 .= '<tr><td>'.TOTAL.'</td><td>'.$totalfreq_nb.'</td><td>100.00</td></tr></tbody></table>';
	}
	if($_GET["s"]=="nbperespace"||$_GET["s"]=="all") {
		include('freq_stats_espchart_nbperespace.php');
	}
	if($_GET["s"]=="percentperespace"||$_GET["s"]=="all") {
		include('freq_stats_espchart_percentperespace.php');
	}
	if($_GET["s"]=="nbperespace"||$_GET["s"]=="percentperespace"||$_GET["s"]=="all") {
		echo '<br> <br>'.$bctab1;
		$_SESSION['bctabs'] .= $bctab1;
	}
	if($_GET["s"]=="percentperespperniv"||$_GET["s"]=="all") {
		include('freq_stats_espchart_percentperespperniv.php');
	}
	if($_GET["s"]=="percentperesppersex"||$_GET["s"]=="all") {
		include('freq_stats_espchart_percentperesppersex.php');
	}
	if($_GET["s"]=="espperweek"||$_GET["s"]=="all") {
		include('freq_stats_espchart_espperweek.php');
	}

	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>';
}
?>
