<?php

		session_start ();
		include('database.php');	
		include('fonctions.php');	
		
		error_reporting(0);
		
		DB_connexion(); 
	
		header( 'content-type: text/html; charset=utf-8' );
		//header('Content-Type: text/html; charset=iso-8859-1');
	
		if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
		{  
			header("Location: login.php");   
		}
		
		$mois = $_GET['mois'];
		//$annee = date('Y'); maintenant on peut choisir l'année
		
		$utilisateur = $_SESSION['nom']; // .' ' .$_SESSION['prenom'];
		
		$table = $_SESSION['table_gestion'];
		//echo $table;
		
		if(isset($_GET['submit']))
		{
			$indice = $_GET['indice'];
			$mois = $_GET['mois'];
			
//echo $_SERVER['QUERY_STRING'];
//echo htmlspecialchars($_SERVER['QUERY_STRING']);
//echo htmlentities($_SERVER['QUERY_STRING']);
//echo utf8_encode($_SERVER['QUERY_STRING']);
//echo mb_detect_encoding($_SERVER['QUERY_STRING']); 

			$url = urldecode($_SERVER['QUERY_STRING']);
			//echo $url;
			$champs = explode('&',$url);
			
			$nb_elements = count($champs) - 3 ; // ne pas prendre en compte: mois, indice et submit
						
			$j = 1;
			for( $i=1; $i <= $indice ; $i++)
			{	
				if($utilisateur == "admin")
				{
					$tab1 = explode('=', $champs[$j]);
					$tab2 = explode('=', $champs[$j+1]);
					$tab3 = explode('=', $champs[$j+2]);
					$id = substr($tab1[0],6);
				
					$j = $j + 3;
					$champ1 = $tab1[1];
					$champ2 = $tab2[1];
					$champ3 = $tab3[1];
					$sql = "UPDATE " .$table ." SET statut = '$champ1' , cheque = '$champ2', banque = '$champ3' WHERE id ='$id' ";
					//echo $sql;
					//echo '<br>';
				}
				else
				{
					$tab1 = explode('=', $champs[$j]);
					$tab2 = explode('=', $champs[$j+1]);
					$id = substr($tab1[0],6);
					// ne pas prendre en compte le champ statut qui n'est pas modifiable par un utilisateur	
					$j = $j + 2;
					$champ1 = $tab1[1];
					$champ2 = $tab2[1];
					
					$sql = "UPDATE " .$table ." SET cheque = '$champ1', banque = '$champ2' WHERE id ='$id' ";
					//echo $sql;
					//echo '<br>';
				}	
				mysqli_query($connexion, "SET NAMES 'utf8'");	
				$result = mysqli_query($connexion, $sql );
				
				if(!$result)	
				{	
					die('Impossible d\'exécuter la requête :' . mysql_error());	
				}
				
			}
			
			// enregistre le statut
			/*for( $i=1; $i <= $indice ; $i++)
			{		$id_select = "select" .$i;
					$id_cheque = "cheque" .$i;
					$id_banque = "banque" .$i;
					$champ1 = $_GET[$id_select];
					$champ2 = $_GET[$id_cheque];
					$champ3 = $_GET[$id_banque];
					$sql = "UPDATE " .$mois_actif ." SET statut = '$champ1' , cheque = '$champ2', banque = '$champ3' WHERE id ='$i' ";
					echo $sql;
					mysqli_query($connexion, "SET NAMES 'utf8'");	
					$result = mysqli_query($connexion, $sql );
					if(!$result)	
					{	die('Impossible d\'exécuter la requête :' . mysql_error());	
					}
			}*/			
			echo "Datas enregistrées";
			echo '<br><br>';
				
		}
?>

<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>
</head>		
<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 class='active'><a href='selection_gestion_mois.php'><span>Gestion</span></a></li>
		<li><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>
		<li><a href='#'><span><?php echo $_SESSION['year']; ?></span></a></li>
	</ul>
	</div>
	<br>
	<br>
	<br>
	
<body>

<style>
td {
    padding: 8px;
	height: 20px;
    vertical-align: middle;
}

	.titre {
	margin-left:50px;
	margin-top:50px;
	font-size: 26px;
	}

//#matable th:hover{
//  background-color:red;
}

</style>
<?php		

		/*$tmp_table = $_SESSION['table_reservation'] = "reservation" .$_SESSION['year'] ."_v3";
		echo $tmp_table;
		
		if(!(mysql_query("SELECT * FROM $tmp_table")))
		{
	
			$str = " La database " .$_SESSION['year'] ." n'existe pas encors !";
			die($str);
		} */
/*
		$sql = "SELECT * FROM " .$table;
		$result = mysqli_query($connexion, $sql );
	
		if($result == null)
		{
			$table_nom = substr( $table, 0, -3 );
			$str = "&nbsp;&nbsp; &nbsp;&nbsp;    La table " .$table_nom ." n'exite pas configurée!."; 
			echo "<p><font size='5'>";
			die($str);
			echo "</p>";
			echo "<br>";
		}
*/	
	
		$table = $_SESSION['table_reservation'] = "reservation" .$_SESSION['year'] ."_v3";
		// echo $table;
		 
		if(!(mysqli_query($connexion, "SELECT * FROM $table")))
		{
			//$str = 'Arnold a dit : "I\'ll be back"';
			echo "<div class ='titre' id ='titre'>";
			$str = " La database " .$_SESSION['year'] ." n'existe pas encore !";
			die($str);
			echo "</div>";
		} 
	

		$annee = $_SESSION['year'];

		$actif = "actif";
		//$sql = "SELECT * FROM gestion WHERE annee ='$annee' AND mois = '$mois' and mode = '$actif' ";
/////		$sql = "SELECT * FROM gestion WHERE annee ='$annee' AND mois = '$mois' and mode = '$actif' order by client ASC ";
		$sql = "SELECT * FROM gestion_v3 WHERE annee ='$annee' AND mois = '$mois' and mode = '$actif' order by client ASC ";
		
//		$sql = "SELECT * FROM " .$_SESSION['table_gestion'] ." WHERE annee = " .$annee ." AND mois = " .$mois ." and mode = " .$actif ." order by client ASC ";
		//echo $sql;
		
		//$sql = "SELECT * FROM " .$table ." annee ='$annee' AND mois = '$mois' and mode = '$actif' order by jour ASC ";
		//SELECT * FROM ta_table ORDER BY champ ASC //ORDER BY moyenne ASC 
		
		//echo $sql;
		mysqli_query($connexion, "SET NAMES 'utf8'");	
		$result = mysqli_query($connexion, $sql );
		
		$nbre_enregistrements = mysqli_num_rows($result);
		//echo $nbre_enregistrements;
		
		if($nbre_enregistrements != 0)
		{
		
			echo ucfirst($mois) ." " .$annee;
			echo '<br><br>';
		
			if(!$result)	
			{	die('Impossible d\'exécuter la requête :' . mysql_error());	
			}

			//$tarif_CE = 5.0;
			//$tarif_plein = 12.67;
			$i = 1;
		
			$tab_retour_courte = statistiques($annee,$mois,"courte");
			$tab_compteur_courte = $tab_retour_courte[0];
			$tab_client_courte = $tab_retour_courte[1];
			
		
	
			//echo '<pre>';
			//print_r($tab_compteur);
	
			//echo '<pre>';
			//print_r($tab_retour);
	
			//echo '<pre>';
			//print_r($tab_client); E6BA39  f1f3f5  d5d5d5   bgcolor="#1ddfff"
			
			$tab_retour_longue = statistiques($annee,$mois,"longue");
			$tab_compteur_longue = $tab_retour_longue[0];
			$tab_client_longue = $tab_retour_longue[1];
	
?>		
			<form action="#" name ="form_gestion" method="get">
			<input type="hidden" name="mois" id="mois" value="<?php echo $mois; ?>">
			<div id="list">
			<table  id="matable" align="left" border="0" cellpadding="5" cellspacing="1" width="100%">
			<tbody>
			<tr bgcolor="#f1f3f5">
			<th class="center" width="1%">Id</th>
			<th align="center" width="10%">Utilisateur</th>
			<th align="center" width="6%">Jour</th>
			<th align="center" width="4%">Inscription</th>
			<th align="center" width="4%">Formule</th>
			<th align="center" width="4%">Seance</th>
			<!--<th align="center" width="8%">Compteur</th>-->
			<th align="center" width="4%">Montant</th>
			<th align="center" width="1%">Réglement</th>
			<th align="center" width="2%">Etat</th>
			<th align="center" width="6%">Chéque</th>
			<th align="center" width="6%">Banque</th>
			</tr>			
<?php	
			$datas = mysqli_fetch_assoc ($result);
			//echo '<pre>';
			//print_r($datas);
						
			$result = mysqli_query($connexion, $sql );

			while ($datas = mysqli_fetch_assoc ($result))
			{	
					if($utilisateur == "admin")
					{		
							$formule = $datas['formule'];
					
							if($formule == "courte")
							{	
								//echo "Admin";
								$key_courte = array_search($datas['client'], $tab_client_courte);
								//echo $key;
						
								// une seule réservation
								if($tab_compteur_courte[$key_courte] == 0)
								{	$montant = $_SESSION['tarifnormal'];
									//echo "step1";
								}	
								if($tab_compteur_courte[$key_courte] == 1)
								{	$montant = $_SESSION['tarifCE'];
									//echo "step2";
								}
								// plusieures réservation
								if($tab_compteur_courte[$key_courte] > 1)
								{	$tab_compteur_courte[$key_courte] = 0;
									$montant = $_SESSION['tarifCE'];
									//echo "step3";
								}	
							}
							
							if($formule == "longue")
							{	
								//echo "Admin";
								$key_longue = array_search($datas['client'], $tab_client_longue);
								//echo $key;
						
								// une seule réservation
								if($tab_compteur_longue[$key_longue] == 0)
								{	$montant = $_SESSION['tarif30'];
									//echo "step1";
								}	
								if($tab_compteur_longue[$key_longue] == 1)
								{	$montant = $_SESSION['tarif30'];
									//echo "step2";
								}
								// plusieures réservation
								if($tab_compteur_longue[$key_longue] > 1)
								{	$tab_compteur_longue[$key_longue] = 0;
									$montant = $_SESSION['tarif30'];
									//echo "step3";
								}	
							}
							

							if ($i%2 == 1)
							  {
								$propriete = "impair";
								$prop = "#02E9FA";
							  }	
							else
							{
								$propriete = "impair";
								$prop = "#0CC2F5";
							}	
							
							echo '<tr>';
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$i .'</th>';
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['client'] .'</th>';
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['jour'] .'</th>';
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['date_inscription'] .'</th>';		
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['formule'] .'</th>';
							
							if($formule == "courte")
							{	
								switch ($datas['seance'])
								{
									case "1":
										$val = "12H20";
										break;
									case "2":
										$val = "12H40";
										break;	
									case "3":
										$val = "13H00";
										break;
									case "4":
										$val = "13H20";
										break;
									case "5":
										$val = "13H40";
										break;
									case "6":
										$val = "14H00";
										break;
									case "7":
										$val = "14H20";
										break;
									case "8":
										$val = "14H40";
										break;			
								}
							}
							
							if($formule == "longue")
							{	
								switch ($datas['seance'])
								{
									case "1":
										$val = "12H00";
										break;
									case "2":
										$val = "12H30";
										break;	
									case "3":
										$val = "13H00";
										break;
									case "4":
										$val = "13H30";
										break;
									case "5":
										$val = "14H00";
										break;
									case "6":
										$val = "14H30";
										break;
									case "7":
										$val = "15H00";
										break;
									case "8":
										$val = "15H30";
										break;			
								}
							}
							
							$id = $datas['id'];
							
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$val .'</th>';
							echo '<th align="center"' .'bgcolor=' .$prop .'>' .$montant . '&#8364' .'</th>';
							$select_objet = 'select' .$id;
							echo '<th align="center"' .'bgcolor=' .$prop .'>';
							echo '<select name=' .$select_objet .'>';
							if($datas['statut'] == "Payé")
							{
								echo '	<option value="Payé" selected>Payé</option>
										<option value="Non encaissé">Non encaissé</option></th>';
								//echo '  </select><img src="yes.png" alt="Smiley face"></td></td> ';	
								echo '<th align="center"' .'bgcolor=' .$prop .'>';
								echo '<img src="yes.png" alt="Icone"></th>';								
							}	
					
							if($datas['statut'] == "Non encaissé")
							{
								echo ' <option value="Non encaissé" selected>Non encaissé</option>
									   <option value="Payé" >Payé</option></th>';
							   //echo '  </select><img src="no.png" alt="Smiley face"></td></td> ';	
								echo '<th align="center"' .'bgcolor=' .$prop .'>';
								echo '<img src="no.png" alt="Icone"></th>';							   
							}	
							
							//echo '<td><img src="yes.png" alt="Smiley face"></td>';
							$cheque_objet = 'cheque' .$id;
?>							
							<th align="center" bgcolor="<?php echo $prop; ?>"><input type="text"  name="<?php echo $cheque_objet; ?>" value="<?php echo $datas['cheque']; ?>"></th> 								

<?php							
							$banque_objet = 'banque' .$id;
?>
							<th align="center" bgcolor="<?php echo $prop; ?>"><input type="text"  name="<?php echo $banque_objet; ?>" value="<?php echo $datas['banque']; ?>"></th> 								
							<?php
							//echo '
							//</select></td> ';
							echo '</tr>';
							$i = $i + 1;
				} 
				else { 	
							//echo "Utilisateur";
								
								$formule = $datas['formule'];
					
								if($formule == "courte")
								{
									$key_courte = array_search($datas['client'], $tab_client_courte);
									//echo $key;
						
									// une seule réservation
									if($tab_compteur_courte[$key_courte] == 0)
									{	$montant = $_SESSION['tarifnormal'];
										//echo "step1";
									}	
									if($tab_compteur_courte[$key_courte] == 1)
									{	$montant = $_SESSION['tarifCE'];
										//echo "step2";
									}
									// plusieures réservation
									if($tab_compteur_courte[$key_courte] > 1)
									{	$tab_compteur_courte[$key_courte] = 0;
										$montant = $_SESSION['tarifCE'];
										//echo "step3";
									}	
								}
								
								if($formule == "longue")
								{	
									//echo "Admin";
									$key_longue = array_search($datas['client'], $tab_client_longue);
									//echo $key;
							
									// une seule réservation
									if($tab_compteur_longue[$key_longue] == 0)
									{	$montant = $_SESSION['tarif30'];
										//echo "step1";
									}	
									if($tab_compteur_longue[$key_longue] == 1)
									{	$montant = $_SESSION['tarif30'];
										//echo "step2";
									}
									// plusieures réservation
									if($tab_compteur_longue[$key_longue] > 1)
									{	$tab_compteur_longue[$key_longue] = 0;
										$montant = $_SESSION['tarif30'];
										//echo "step3";
									}	
								}
									
								if ($i%2 == 1)
								{
									$propriete = "impair";
									$prop = "#02E9FA";
								}	
								else
								{
									$propriete = "impair";
									$prop = "#0CC2F5";
								}	
							
							
								echo '<tr>';
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$i .'</th>';
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['client'] .'</th>';
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['jour'] .'</th>';
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['date_inscription'] .'</th>';		
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$datas['formule'] .'</th>';
								
								$formule = $datas['formule'];
							
								if($formule == "courte")
								{	
									switch ($datas['seance'])
									{
										case "1":
											$val = "12H20";
											break;
										case "2":
											$val = "12H40";
											break;	
										case "3":
											$val = "13H00";
											break;
										case "4":
											$val = "13H20";
											break;
										case "5":
											$val = "13H40";
											break;
										case "6":
											$val = "14H00";
											break;
										case "7":
											$val = "14H20";
											break;
										case "8":
											$val = "14H40";
											break;			
									}
								}
							
								if($formule == "longue")
								{	
									switch ($datas['seance'])
									{
										case "1":
											$val = "12H00";
											break;
										case "2":
											$val = "12H30";
											break;	
										case "3":
											$val = "13H00";
											break;
										case "4":
											$val = "13H30";
											break;
										case "5":
											$val = "14H00";
											break;
										case "6":
											$val = "14H30";
											break;
										case "7":
											$val = "15H00";
											break;
										case "8":
											$val = "15H30";
											break;			
									}
								}
								
					
								$id = $datas['id'];
					
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$val .'</th>';
								echo '<th align="center"' .'bgcolor=' .$prop .'>' .$montant . '&#8364' .'</th>';
								$select_objet = 'select' .$id;
								echo '<th align="center"' .'bgcolor=' .$prop .'>';
								//echo '<select name=' .$select_objet .'>';
								echo '<select name=' .$select_objet .' disabled title="Reservé à l\'administrateur">';
								if($datas['statut'] == "Payé")
								{
									echo '	<option value="Payé" selected>Payé</option>
											<option value="Non encaissé">Non encaissé</option></th>';
									//echo '  </select><img src="yes.png" alt="Smiley face"></td></td> ';	
									echo '<th align="center"' .'bgcolor=' .$prop .'>';
									echo '<img src="yes.png" alt="Icone"></th>';								
								}	
					
								if($datas['statut'] == "Non encaissé")
								{
									echo ' 	<option value="Non encaissé" selected>Non encaissé</option>
											<option value="Payé" >Payé</option></th>';
									//echo '  </select><img src="no.png" alt="Smiley face"></td></td> ';	
									echo '<th align="center"' .'bgcolor=' .$prop .'>';
									echo '<img src="no.png" alt="Icone"></th>';							   
								}	
							
								//echo '<td><img src="yes.png" alt="Smiley face"></td>';
							
								$cheque_objet = 'cheque' .$id;
//								echo '<th align="center"' .'bgcolor=' .$prop .'><input type="text" name=' .$cheque_objet  .' value = ' .$datas['cheque']   .'></th>';
?>								
								<th align="center" bgcolor="<?php echo $prop; ?>"><input type="text"  name="<?php echo $cheque_objet; ?>" value="<?php echo $datas['cheque']; ?>"></th> 								

<?php
								$banque_objet = 'banque' .$id;
								//echo '<th align="center"' .'bgcolor=' .$prop .'><input type="text" name=' .$banque_objet  .' value = ' .$datas['banque']   .'></th>';
?>
								<th align="center" bgcolor="<?php echo $prop; ?>"><input type="text"  name="<?php echo $banque_objet; ?>" value="<?php echo $datas['banque']; ?>"></th> 								

<?php	
								//echo '
								//</select></td> ';
								echo '</tr>';
								$i = $i + 1;
								
								
								//echo ' <th align="center"><select name=' .$select_objet .' disabled title="Reservé à l\'administrateur">';
							//}
							
					 }
					 
					
					 
			}	// while
			
			$indice = $i - 1;
?>  
			<tr bgcolor="#f1f3f5">
			<td colspan="16" class="center">&nbsp;</td>
			</tr>
			
			<tr bgcolor="#f1f3f5">
			<td colspan="16" class="center"><input type="submit" name="submit" value="Enregistrer" /></td>
			</tr>
			
			
			</tbody>
			</table>
			
			</div>
	
			<input type="hidden" name="indice" id="indice" value="<?php echo $indice; ?>">	
			<br>
			<br>
			<br>
			
<?php
		/*if($_SESSION['nom'] == "admin")
		{
			echo '<input type="submit" name="submit" value="Enregistrer" />';
		} 
		else			
		{
			echo '<input type="submit" name="submit"  disabled value="Enregistrer" />';
		}*/
		
		
		//echo '<input type="submit" name="submit" value="Enregistrer" />';	
			
?>			
			</form>
<?php
		}
		else
		{ 	
			echo "Aucunes entrées en " .ucfirst($mois) ;
		}
			
		//echo '<br><br><br>';
		//echo '<a href="selection_gestion_mois.php">Retour ... </a>';
			
?>
</body>
</html>
