<?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'];
		 
		//+++++++++++++++ COURTE +++++++++++++++++++++++";	
		 
		 $sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' and formule = 'courte' and mode ='actif' ";

		 mysqli_query($connexion, "SET NAMES 'utf8'");
		 $result = mysqli_query($connexion, $sql );
		 
		 if(!$result)	
		 {	die('Impossible d\'exécuter la requête :' . mysql_error());	
		 }

		 $n = mysqli_num_rows($result);	
		 
		 $tab_client_courte = array();
		 
		 if($n != 0)
		 {
	
			 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_courte[] = $seance1;
				}
				if(strlen($seance2) !=0 && $seance2 != "unselect" && $seance2 != "creneau ouvert" && $seance2 != "creneau fermer")
				{ $tab_client_courte[] = $seance2;
				}
				if(strlen($seance3) !=0 && $seance3 != "unselect" && $seance3 != "creneau ouvert" && $seance3 != "creneau fermer")
				{ $tab_client_courte[] = $seance3;
				}
				if(strlen($seance4) !=0 && $seance4 != "unselect" && $seance4 != "creneau ouvert" && $seance4 != "creneau fermer")
				{ $tab_client_courte[] = $seance4;
				}
				if(strlen($seance5) !=0 && $seance5 != "unselect" && $seance5 != "creneau ouvert" && $seance5 != "creneau fermer")
				{ $tab_client_courte[] = $seance5;
				}
				if(strlen($seance6) !=0 && $seance6 != "unselect" && $seance6 != "creneau ouvert" && $seance6 != "creneau fermer")
				{ $tab_client_courte[] = $seance6;
				}
				if(strlen($seance7) !=0 && $seance7 != "unselect" && $seance7 != "creneau ouvert" && $seance7 != "creneau fermer")
				{ $tab_client_courte[] = $seance7;
				}
				if(strlen($seance8) !=0 && $seance8 != "unselect" && $seance8 != "creneau ouvert" && $seance8 != "creneau fermer")
				{ $tab_client_courte[] = $seance8;
				}
			
				//echo '<pre>';
				//print_r($datas);
			}
		
			//echo "20min";
			//echo '<br>';
			//echo '<pre>';
			//print_r($tab_client_courte);
		 	//print_r(array_count_values($tab_client_courte));
			
			$tab_courte = array_count_values($tab_client_courte);
		}
		
		//"+++++++++++++++ LONGUE +++++++++++++++++++++++";
			
		$sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' and formule = 'longue' and mode ='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_longue = 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_longue[] = $seance1;
			}
			if(strlen($seance2) !=0 && $seance2 != "unselect" && $seance2 != "creneau ouvert" && $seance2 != "creneau fermer")
			{ $tab_client_longue[] = $seance2;
			}
			if(strlen($seance3) !=0 && $seance3 != "unselect" && $seance3 != "creneau ouvert" && $seance3 != "creneau fermer")
			{ $tab_client_longue[] = $seance3;
			}
			if(strlen($seance4) !=0 && $seance4 != "unselect" && $seance4 != "creneau ouvert" && $seance4 != "creneau fermer")
			{ $tab_client_longue[] = $seance4;
			}
			if(strlen($seance5) !=0 && $seance5 != "unselect" && $seance5 != "creneau ouvert" && $seance5 != "creneau fermer")
			{ $tab_client_longue[] = $seance5;
			}
			if(strlen($seance6) !=0 && $seance6 != "unselect" && $seance6 != "creneau ouvert" && $seance6 != "creneau fermer")
			{ $tab_client_longue[] = $seance6;
			}
			if(strlen($seance7) !=0 && $seance7 != "unselect" && $seance7 != "creneau ouvert" && $seance7 != "creneau fermer")
			{ $tab_client_longue[] = $seance7;
			}
			if(strlen($seance8) !=0 && $seance8 != "unselect" && $seance8 != "creneau ouvert" && $seance8 != "creneau fermer")
			{ $tab_client_longue[] = $seance8;
			}
		
			//echo '<pre>';
			//print_r($datas);
		}

			//echo "30min";
			//echo '<br>';
			//echo '<pre>';
			//print_r($tab_client_longue);
			//print_r(array_count_values($tab_client_longue));
			
			$tab_longue = array_count_values($tab_client_longue);
			
			//echo "********************";
			//echo '<pre>';
			//print_r($tab_courte);
			//echo '<pre>';
			//print_r($tab_longue);
			
			
?>

<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>
	<br>
	<br>
	
</head>			
			
			
<?php			
			
			$nbre_enregistrements = count($tab_client_courte) + count($tab_client_longue);
			
			if($nbre_enregistrements != 0)
			{

				$sql = 'SELECT * FROM client';
				mysqli_query($connexion, "SET NAMES 'utf8'");
				$result = mysqli_query($connexion, $sql );
				$tab_nom = array();
				$tab_prenom = array();
				$tab_clients = array();
	
				$ptr = 0;
			
				while ($data = mysqli_fetch_assoc ($result))
				{
					if(strlen($data['prenom']) != 0)
					{
						$tab_nom[$ptr] = $data['nom'];
						$tab_prenom[$ptr] = $data['prenom'];
						$ptr = $ptr + 1;
					}	
				}	

				for( $i=0; $i < count($tab_nom) ; $i++)
				{
					$tab_clients[$i] = $tab_nom[$i] . " " .$tab_prenom[$i];
				}

				//echo '<pre>';
				//print_r($tab_clients);
			
				$shop = array( array("rose", 1.25 , 15),
							array("daisy", 0.75 , 25),
						   array("orchid", 1.15 , 7) 
					); 
			
			
				$store_statistiques = array();
				$cpt = 0;
				
				for( $i=0; $i < count($tab_nom) ; $i++)
				{
					 // les 2 existes
					 if(array_key_exists($tab_clients[$i] , $tab_courte) && array_key_exists($tab_clients[$i] , $tab_longue) )
					 {
						//echo $tab_clients[$i] ."-". $tab_courte[$tab_clients[$i]] ."-". $tab_longue[$tab_clients[$i]];
						//echo "<br>";
						$store_statistiques[$cpt] = $tab_clients[$i] ."-". $tab_courte[$tab_clients[$i]] ."-". $tab_longue[$tab_clients[$i]];
						$cpt = $cpt + 1;
					 }	
					 
					 // seulement court
					 if(array_key_exists($tab_clients[$i] , $tab_courte) && !array_key_exists($tab_clients[$i] , $tab_longue) )
					 {
						//echo $tab_clients[$i] ."-". $tab_courte[$tab_clients[$i]] ."-0"; 
						$store_statistiques[$cpt] = $tab_clients[$i] ."-". $tab_courte[$tab_clients[$i]] ."-0"; 
						//echo "<br>";
						$cpt = $cpt + 1;
					 }	
					 
					 // seulement long
					 if( !array_key_exists($tab_clients[$i] , $tab_courte) && array_key_exists($tab_clients[$i] , $tab_longue) )
					 {
						//echo $tab_clients[$i] ."-0". $tab_longue[$tab_clients[$i]];
						$store_statistiques[$cpt] = $tab_clients[$i] ."-0". $tab_longue[$tab_clients[$i]];
						//echo "<br>";
						$cpt = $cpt + 1;
					 }	
				}

			//echo '<pre>';
			//print_r($store_statistiques);
			
				echo ucfirst($mois) ." " .$annee;
				echo '<br><br>';
	
?>


	
<body>	

	<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="5%">30Min</th>
	<th align="center" width="3%">Subventionné</th>
	<th align="center" width="3%">Personnel</th>
	

	<th align="center" width="5%">20Min</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_20min = 5.0;
				$tarif_plein_20min = 12.67;
				
				$tarif_CE_30min = 9;
				$tarif_plein_30min = 14.5;
				
				$cpt_courte = 0;
				$cpt_longue = 0;
				
				$benefice = 0;
				
				$cpt_longue_subvention = 0;
				$cpt_courte_subvention = 0;
				$cpt_longue_perso = 0;
				$cpt_courte_perso = 0;
				
				for( $i=0; $i < count($store_statistiques) ; $i++)
				{  
					echo '<tr>';
					$indice = $i + 1;		
					echo '<td align="center">' .$indice .'</td>';
					
					$store_statistiques[$i];
					
					$pieces = explode("-", $store_statistiques[$i]);
					$courte = $pieces[1]; 
					$longue = $pieces[2]; 
					
					$cpt_courte = $cpt_courte + $courte;
					$cpt_longue = $cpt_longue + $longue;
					
					echo '<td align="center">' .$pieces[0] .'</td>'; // client
					
					echo '<td align="center">' .$longue .'</td>';	// nombre de sceance de 30min


					if($longue == 0 )
					{
						echo '<td align="center">0</td>';	// Subvention
						echo '<td align="center">0</td>';   // Personnel
						$total_longue = 0;
					}
					if($longue != 0 )
					{
						
						echo '<td align="center">1</td>';	// Subvention
						$perso_longue = $longue - 1;
						$total_longue = $perso_longue * $tarif_plein_30min + $tarif_CE_30min;
						echo '<td align="center">' .$perso_longue .'</td>'; // Personnel
						$cpt_longue_perso = $cpt_longue_perso + $perso_longue; 
						$cpt_longue_subvention = $cpt_longue_subvention  + 1;
					}
					
					echo '<td align="center">' .$courte .'</td>';	// nombre de sceance de 30min


					if($courte == 0 )
					{
						echo '<td align="center">0</td>';	// Subvention
						echo '<td align="center">0</td>';   // Personnel
						$total_courte = 0;
					}
					if($courte != 0 )
					{
						
						echo '<td align="center">1</td>';	// Subvention
						$perso_courte = $courte - 1;
						$total_courte = $perso_courte * $tarif_plein_20min + $tarif_CE_20min ;
						echo '<td align="center">' .$perso_courte .'</td>'; // Personnel
						$cpt_courte_perso = $cpt_courte_perso + $perso_courte; 
						$cpt_courte_subvention = $cpt_courte_subvention  + 1;
						
					}
					
					//$euro = "&#128;";
					//$total_somme = $total_longue + $total_courte + $euro;    // " &euro";
					$total_somme = $total_longue + $total_courte  ;
					$benefice = $benefice + $total_somme;
					echo '<td align="center">' .$total_somme .'</td>'; // Somme
				}

				//echo '<tr>';
				echo '<tr bgcolor="#f1f3f5">';
				echo '<td align="center"></td>';
				echo '<td align="center">' . count($store_statistiques) . " clients" .'</td>'; // Somme
				echo '<td align="center">' .$cpt_longue .'</td>'; // Somme
				
				echo '<td align="center">' .$cpt_longue_subvention .'</td>'; // cpt_longue_subvention
				echo '<td align="center">' .$cpt_longue_perso .'</td>'; // cpt_longue_subvention
				
				echo '<td align="center">' .$cpt_courte .'</td>'; // Somme
				echo '<td align="center">' .$cpt_courte_subvention .'</td>'; // cpt_longue_subvention
				
				echo '<td align="center">' .$cpt_courte_perso .'</td>'; // cpt_longue_subvention
				echo '<td align="center">' .$benefice .'</td>'; // Somme
				//  echo '<td align="center">' .$total_courte .' euros' .'</td>';
   
  
?>  
  
	<!--		
	  </tr> -->
	</tbody>
	</table>
	</div>


<?php

		}
	

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


	
	
?>	
</body>	
</html>	

