<?php

	session_start ();
	include('database.php');	
	DB_connexion(); 
	
	if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
	{  
			header("Location: login.php");   
	}
	
	header( 'content-type: text/html; charset=utf-8' );
		
	//$mois = 2;
	$mois = $_GET['mois'];
	
	$annee = date('Y');
	//$table = "reservation" .$annee;
	$table = $_SESSION['table_reservation'];
	
	$sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' and mode ='actif' ";	
	//$sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' and mode ='actif' order by rendezvous ASC  ";	
	 
	//$sql="SELECT * FROM " .$mois_actif;	
	mysqli_query($connexion, "SET NAMES 'utf8'");
	$result = mysqli_query($connexion, $sql );
		
	if(!$result)	
	{	die('Impossible d\'exécuter la requête :' . mysql_error());	
	}

		$tab_client = array();
		$tab_statistiques = array();
	
		while ($datas = mysqli_fetch_assoc ($result))
		{
			$seance1 = $datas['seance1'];
			$seance2 = $datas['seance2'];
			$seance3 = $datas['seance3'];
			$seance4 = $datas['seance4'];
			$seance5 = $datas['seance5'];
			$seance6 = $datas['seance6'];
			$seance7 = $datas['seance7'];
			$seance8 = $datas['seance8'];
		
			if(strlen($seance1) !=0 && $seance1 != "unselect" && $seance1 != "creneau ouvert" && $seance1 != "creneau fermer")
			{ $tab_client[] = $seance1;
			}
			if(strlen($seance2) !=0 && $seance2 != "unselect" && $seance2 != "creneau ouvert" && $seance2 != "creneau fermer")
			{ $tab_client[] = $seance2;
			}
			if(strlen($seance3) !=0 && $seance3 != "unselect" && $seance3 != "creneau ouvert" && $seance3 != "creneau fermer")
			{ $tab_client[] = $seance3;
			}
			if(strlen($seance4) !=0 && $seance4 != "unselect" && $seance4 != "creneau ouvert" && $seance4 != "creneau fermer")
			{ $tab_client[] = $seance4;
			}
			if(strlen($seance5) !=0 && $seance5 != "unselect" && $seance5 != "creneau ouvert" && $seance5 != "creneau fermer")
			{ $tab_client[] = $seance5;
			}
			if(strlen($seance6) !=0 && $seance6 != "unselect" && $seance6 != "creneau ouvert" && $seance6 != "creneau fermer")
			{ $tab_client[] = $seance6;
			}
			if(strlen($seance7) !=0 && $seance7 != "unselect" && $seance7 != "creneau ouvert" && $seance7 != "creneau fermer")
			{ $tab_client[] = $seance7;
			}
			if(strlen($seance8) !=0 && $seance8 != "unselect" && $seance8 != "creneau ouvert" && $seance8 != "creneau fermer")
			{ $tab_client[] = $seance8;
			}
		
			//echo '<pre>';
			//print_r($datas);
		}

			//echo "++++++++++++++++++++++++++++++++++++++";
			//echo '<pre>';
			//print_r($tab_client);
			
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Espace réservation</title>

<link rel="stylesheet" href="menu_style.css" type="text/css">

	<div id='cssmenu'>
	<ul>
		<li><a href='home.php'><span>Home</span></a></li>
		<li><a href='selection_planning_mois.php'><span>Planning</span></a></li>
		<li><a href='selection_gestion_mois.php'><span>Gestion</span></a></li>
		<li class='active'><a href='selection_statistique_mois.php'><span>Statistiques</span></a></li>
<?php
		if($_SESSION['nom'] == "admin")
		{ 	
			echo '<li><a href="deplacer_un_rendezvous_admin.php"><span>Déplacer</span></a></li>';
			echo '<li><a href="configuration.php"><span>Configuration</span></a></li>';
		}
		else
		{
			echo '<li><a href="deplacer_un_rendezvous_user.php"><span>Déplacer</span></a></li>';
		}
?>
		<li><a href='change_password.php'><span>Changer password</span></a></li>
		<li><a href='logout.php'><span>Déconnexion</span></a></li>
		<li><a href='#'><span><?php echo $_SESSION['nom'] .' ' .$_SESSION['prenom'] .' connecté' ?></span></a></li>
	</ul>
	</div>
	
	<br>
<?php	//echo  ucfirst($mois_actif); ?>
	<br>
	<br>
	
</head>			
			
			
<?php			

			$nbre_enregistrements = count($tab_client);	
			
			if($nbre_enregistrements != 0)
			{
	
				$tab_less_doublons = array_unique($tab_client);
				
				echo '<pre>';
				print_r($tab_less_doublons);
	
				$tab_reindex = array_values($tab_less_doublons);

				echo '<pre>';
				print_r($tab_reindex);


				for( $i=0; $i < count($tab_reindex) ; $i++)
				{
					$cpt = 0;
					for( $j=0; $j < count($tab_client) ; $j++)
					{
						if($tab_client[$j] == $tab_reindex[$i])
						{	$cpt = $cpt + 1;
						}	 
					}
					$tab_statistiques[] = $cpt;
				}
	
				echo '<pre>';
				print_r($tab_statistiques);

				$clength = count($tab_reindex);
				for($x = 0; $x <  $clength; $x++)
				{
					$valeur = $tab_reindex[$x];
					$tab_associatif[$valeur] = $tab_statistiques[$x];			
				}	

				echo '<pre>';
				print_r($tab_associatif);	

				sort($tab_reindex);

echo '<pre>';
print_r($tab_reindex);	

$p = $tab_reindex[0];
echo $tab_associatif[$p];
echo '<br><br>';

$p = $tab_reindex[1];
echo $tab_associatif[$p];
		
				echo ucfirst($mois) ." " .$annee;
				echo '<br><br>';

?>


	
<body>	
<!--
<form action="#" name ="form_gestion" method="get">
-->
	<div id="list">
	<table align="left" bgcolor="#d5d5d5" border="0" cellpadding="5" cellspacing="1" width="100%">
	<tbody>
	<tr bgcolor="#f1f3f5">
	<th class="center" width="1%">Id</th>
	<th align="center" width="5%">Utilisateur</th>
	<th align="center" width="3%">Total</th>
	
	
	
	<th align="center" width="3%">Subventionné</th>
	<th align="center" width="3%">Personnel</th>
	<th align="center" width="3%">Somme</th>
	</tr>	
	
<?php
	//$tarif_CE = 5.0;
	//$tarif_plein = 12.67;
	$total_creneau = 0;
	$total = 0;
	$personnel = 0;
	$subventionner_creneau = count($tab_reindex);
	
	for( $i=0; $i < count($tab_reindex) ; $i++)
	{  echo '<tr>';
	   $indice = $i + 1;		
       echo '<td align="center">' .$indice .'</td>';
       echo '<td align="center">' .$tab_reindex[$i] .'</td>';
	   
	   $p = $tab_reindex[$i];
	   $nb_seance = $tab_associatif[$p];	
	   
	   //$total_creneau = $total_creneau + $tab_statistiques[$i];
 $total_creneau = $total_creneau + $nb_seance;	
	   
	   //echo '<td align="center">' .$tab_statistiques[$i] .'</td>';
echo '<td align="center">' .$nb_seance .'</td>';
	   	   
	   echo '<td align="center">1</td>';
	   //$charge_utilisateur=$tab_statistiques[$i]-1;
$charge_utilisateur=$nb_seance-1;

	   $personnel = $personnel + $charge_utilisateur;
	   echo '<td align="center">' .$charge_utilisateur .'</td>';
	   $soustotal = $_SESSION['tarifCE'] + ($charge_utilisateur * $_SESSION['tarifnormal']);
	   
	   $total = $total + $soustotal;
	   
	   echo '<td align="center">' .$soustotal .' euros' .'</td>';
/*	
		$nom_objet = 'id' .$i;
	  echo ' <td align="center"><select name=' .$nom_objet .'>';
	  echo '<option value="Payé">Payé</option>
			<option value="Non encaissé">Non encaissé</option>
			</select></td> ';
	   */
	   echo '</tr>';

   }
   
  
?>  
  
	<tr bgcolor="#f1f3f5">
<!--	<td colspan="16" class="center">&nbsp;</td> -->

<?php	
		echo '<td align="center"></td>';
		echo '<td align="center"></td>';
		echo '<td align="center">' .$total_creneau .'</td>';
		echo '<td align="center">' .$subventionner_creneau .'</td>';
		echo '<td align="center">' .$personnel .'</td>';
		echo '<td align="center">' .$total .'</td>';
		
?>
	  </tr>
	</tbody>
	</table>
	</div>
<?php	
		//echo  $total_creneau .' ' .$subventionner_creneau  .' ' .$total;
?>	
<!--	
	<input type="submit" name="submit" value="Validation" />
	</form>
-->	
<?php


	}
	

	else { echo "Aucunes entrées en " .ucfirst($mois) ;
		   echo '<br><br>';	
		 }


	//echo '<br/><br/><a href="selection_statistique_mois.php">Retour ... </a>';
	
?>	
</body>	
</html>	

