<?php

	session_start ();
		
	if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
	{	
		header("Location: login.php");
	}
	
	header( 'content-type: text/html; charset=utf-8' );
	include('database.php');
	include('fonctions.php');	
	DB_connexion(); 

	$mois = $_GET['mois'];	
	$annee = date('Y');	
	$table = "reservation_dev" .$annee;

	if(isset($_GET['submit']))	
	{	
		if(isset($_GET['verrou']) && $_GET['verrou'] == "ok")	
		{	
						$nb_semaine = $_GET['nb_semaine'];
			
						$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and formule = 'longue' and mois ='$mois' and mode = 'actif' ";			
						//echo $sql;
						mysqli_query($connexion, "SET NAMES 'utf8'");		
						$result = mysqli_query($connexion, $sql );
						$tab_jour = array();
				
						while ($data = mysqli_fetch_assoc ($result))				
						{	
							$tab_jour[] = $data['rendezvous'];		
						}
			
						//echo '<pre>';			
						//print_r($tab_jour);				
						
						$tab_store = array();
						$tab_checkbox = array();
						
						for( $i=0; $i < $nb_semaine ; $i++)
						{
							${'text'.$i} = "cached_check".$i;
							$val = ${'text'.$i};
							$val = $_GET[$val];
							$tab_checkbox[] = $val;
						}
						
						//echo '<pre>';			
						//print_r($tab_checkbox);		

						for( $i=0; $i < $nb_semaine ; $i++)
						{	
							if($tab_checkbox[$i] == "actif")
							{
								${'text'.$i} = "select_debut".$i;
								$val1 = ${'text'.$i};
								$val1 = "start".$_GET[$val1];
								$tab_store[] = $val1;
				
								${'text'.$i} = "select_fin".$i;
								$val2 = ${'text'.$i};
								$val2 = "stop".$_GET[$val2];
								$tab_store[] = $val2;
				
								${'text'.$i} = "input_avant".$i;
								$val3 = ${'text'.$i};
								$tab_store[] = $_GET[$val3];
				
								${'text'.$i} = "input_apres".$i;
								$val4 = ${'text'.$i};
								$tab_store[] = $_GET[$val4];
				
								$sql = "UPDATE " .$table ." SET horairedebut = '$val1' , horairefin = '$val2' WHERE rendezvous ='$tab_jour[$i]' ";
								//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());
								}
							}	
						}
						
						//echo '<pre>';
						//print_r($tab_store);
			
						$step = 0;
			
						for( $i=0; $i < $nb_semaine ; $i++)
						{
													
							if($tab_checkbox[$i] == "actif")
							{	

								$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and formule = 'longue' and mois ='$mois' and mode = 'actif' and rendezvous ='$tab_jour[$i]' ";		
								//echo $sql;
								//echo '<br>';
								
								$result = mysqli_query($connexion, $sql );
								if(!$result)
								{	
									die('Impossible d\'exécuter la requête :' . mysql_error());
								}
								
								$tab_tmp = array();
								
								$data = mysqli_fetch_assoc ($result);
								
								$tab_tmp[1] = $data['seance1'];
								$tab_tmp[2] = $data['seance2'];
								$tab_tmp[3] = $data['seance3'];
								$tab_tmp[4] = $data['seance4'];
								$tab_tmp[5] = $data['seance5'];
								$tab_tmp[6] = $data['seance6'];
								$tab_tmp[7] = $data['seance7'];
								$tab_tmp[8] = $data['seance8'];
								
								echo '<pre>';
								print_r($tab_tmp);
							
								$val1 = $tab_store[$step];
								$val2 = $tab_store[$step+1];
								$val3 = $tab_store[$step+2];
								$val4 = $tab_store[$step+3];

								$step = $step + 4;
									
								$indice_debut = substr($val1, 5);					
								$indice_fin = substr($val2, 4);
								//echo $indice_debut ." - " .$indice_fin;				
								//echo '<br>';
						
								$tab_seance = array();		
								$verrou = false;				
								$tab_seance[] = "decalage";

								for( $k=1; $k <9 ; $k++)	
								{	
									$tab_seance[] = "unselect";							
								}	
						
								echo '<pre>';	
								print_r($tab_seance);
								for( $l = $indice_debut; $l <= $indice_fin ; $l++)				
								{ 
									$tab_seance[$l] = "";
								}

								$supplement_avant_debut = $indice_debut - $val3;	
								$supplement_avant_fin = $indice_debut - 1;				
								$supplement_apres_debut = $indice_fin + 1;	
								$supplement_apres_fin = $indice_fin + $val4;

//  tracking bug 14/04/2015
//	probléme d'effacement  - état figé							
								
								for( $l=1; $l <9 ; $l++)
								{
									if( $l >= $supplement_apres_debut && $l <= $supplement_apres_fin)		
									{	
										$tab_seance[$l] = "creneau fermer";	
									}	
									if( $l >= $supplement_avant_debut && $l <= $supplement_avant_fin)	
									{	
										$tab_seance[$l] = "creneau fermer";	
									}		
								
								}
//////////////////////////////// old //////////////////						

/*
								for( $l=1; $l <9 ; $l++)
								{			
									if( $l >= $indice_debut && $l <= $indice_fin)
									{	
									
									
										
//										if($tab_tmp[$l] != "creneau fermer" || $tab_tmp[$l] != "creneau ouvert")
//										{
//											$tab_seance[$l] = $tab_tmp[$l];  
//										}
//										else
//										{
//											$tab_seance[$l] = "";	
//										}
										
//										if($tab_tmp[$l] == "unselect")
//										{
//											$tab_seance[$l] = "";	
//										}
										/*else
										{
											$tab_seance[$l] = "";	
										}*/
										
//									}				
//									if( $l >= $supplement_apres_debut && $l <= $supplement_apres_fin)		
//									{	
//										$tab_seance[$l] = "creneau fermer";	
//									}	
//									if( $l >= $supplement_avant_debut && $l <= $supplement_avant_fin)	
//									{	
//										$tab_seance[$l] = "creneau fermer";	
//									}				
//								}
//////////////////////////////// old //////////////////						
								echo '<pre>';				
								print_r($tab_seance);					

								$sql = "UPDATE " .$table ." SET seance1 = '$tab_seance[1]' , 
																seance2 = '$tab_seance[2]' , 		
																seance3 = '$tab_seance[3]' ,
																seance4 = '$tab_seance[4]' , 			
																seance5 = '$tab_seance[5]' , 
																seance6 = '$tab_seance[6]' , 	
																seance7 = '$tab_seance[7]' , 	
																seance8 = '$tab_seance[8]' ,
																avant = '$val3' , 				
																apres = '$val4' 			
																WHERE rendezvous ='$tab_jour[$i]' ";						
						
								//echo $sql;
								//echo '<br>';				
	
								$result = mysqli_query($connexion, $sql );			
								if(!$result)
								{	
									die('Impossible d\'exécuter la requête :' . mysql_error());				
								}
							}	
						}
						
						echo "Les paramétres ont été pris en compte.";	
						echo "<br><br>";
						
		}	
		
	}
?>

<html>
<body>

<style>
table, th, td {	padding: 10px;    border: 1px ;	align: center;}
</style>
	
<script>

function valide(valeur)
{	
	var verif = true;   
	var msg_erreur = new Array();

	for (i = 0; i < valeur; i++)
	{ 	
		var index_start = "id_start"+i;	
		var index_stop = "id_stop"+i;	
		var index_avant = "id_avant"+i;
		var index_apres = "id_apres"+i;	
		var val1 = document.getElementById(index_start).selectedIndex;	
		var val2 = document.getElementById(index_stop).selectedIndex;		
		var val3 = document.getElementById(index_avant).value;	
		var val4 = document.getElementById(index_apres).value;

		val1 = parseInt(val1);
		val2 = parseInt(val2);
		val3 = parseInt(val3);
		val4 = parseInt(val4);		
		
		//var ligne = 0;
		var ligne = i + 1;
		
		if(val1 - val3 <= 0)	
		{	
			msg_erreur[i] = "Hors plage : ligne "+ligne;		
			verif = false;	
		}	
		if(val2 + val4 > 8)
		{	
			msg_erreur[i] = "Hors plage : ligne "+ligne;	
			verif = false;	
		}	
		if(val1 == 0 && val2 == 0 )	
		{ 	
			//verif = false;
			//alert("PB"+1);	
			msg_erreur[i] = "Heure de début et de fin non conforme : ligne "+ligne;	
			verif = false;	
		}
		if(val1 == 0 && val2 != 0 )	
		{ 	
			//alert("Début non conforme "+i);
			msg_erreur[i] = "Heure de début non conforme : ligne "+ligne;
			verif = false;	
		}
		if(val1 != 0 && val2 == 0 )
		{ 	
			//alert("Fin non conforme "+i);			
			msg_erreur[i] = "Heure de fin non conforme : ligne "+ligne;
			verif = false;
		}
		if(val1 != 0 && val2 != 0 )
		{ 	
			//alert("Probléme d'horaire "+i);		
			if(val2 <= val1)
			{	msg_erreur[i] = "Probléme d'horaire : ligne "+ligne;
				verif = false;	
			}		
		}
		if(isNaN(val3) == true )
		{	
			msg_erreur[i] = "Ce n'est pas un chiffre ligne  "+ligne;	
			verif = false;	
		}
		/*		else	
		{	if(val3 >= 3 )
			{ 	msg_erreur[i] = "Valeur possible: 0, 1 ou 2 ligne  "+i;	
			verif = false;
			}	
		}*/	
		if(isNaN(val4) == true )
		{	
			msg_erreur[i] = "Ce n'est pas un chiffre ligne  "+ligne;		
			verif = false;	
		}
		/*
		else
		{	if(val4 >= 3 )
			{ 	msg_erreur[i] = "Valeur possible: 0, 1 ou 2 ligne  "+i;	
				verif = false;
			}	
		}*/		
	} 
	
	if(verif == true)
	{ 

		var nb_semaine = document.getElementById('nb_semaine').value
		
		for (j = 0; j < nb_semaine; j++)
		{ 	
			var index_start = "id_start"+j;	
			var index_stop = "id_stop"+j;	
			var index_avant = "id_avant"+j;
			var index_apres = "id_apres"+j;	
			var index_checkbox = "ele"+j;	
			
			var val1 = document.getElementById(index_start).selectedIndex;	
			var val2 = document.getElementById(index_stop).selectedIndex;		
			var val3 = document.getElementById(index_avant).value;	
			var val4 = document.getElementById(index_apres).value;
			var val5 = document.getElementById(index_checkbox).checked;
		
			var element1 = "select_debut"+j;
			document.getElementById(element1).value = val1;
			
			var element2 = "select_fin"+j;
			document.getElementById(element2).value = val2;

			var element3 = "input_avant"+j;
			document.getElementById(element3).value = val3;			
			
			var element4 = "input_apres"+j;
			document.getElementById(element4).value = val4;
			
			var element5 = "cached_check"+j;	
			if(val5 == true)
			{  document.getElementById(element5).value = "actif";
			}
			else
			{	document.getElementById(element5).value = "non_actif";
			}

			document.getElementById('verrou').value="ok";	
		}
	}
	if(verif == false) 
	{ 	
		var affiche = msg_erreur.join("\n"); // n  <br>	 
		alert(affiche);  	
		document.getElementById('verrou').value="erreur";	
		// document.getElementById('message').innerHTML = affiche ;	
		//message.textContent = "my text";	
		// document.getElementById('message').innerHTML = "text1. \n text2."; 
	}	
}

</script>

<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><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>	

<?php	
		echo ucfirst($mois) ." " .$annee  .":  Scéance de 30min" ;	
		echo '<br><br>';
		// verifier que parametre 1 et parametre2 ne sont pas vide sinon on signal que la config est incompléte !!!
		$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and formule = 'longue' and mois ='$mois' and mode = 'actif' ";	

		//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());
		}

		$n = mysqli_num_rows($result);
		
		echo "Ouvertes: " .$n;

		echo '	<form action="#" name ="form_horaire" method="get">
		  		<input type="hidden" name="verrou" id="verrou" value="">';
?>

				<input type="hidden" name="mois" id="mois" value="<?php echo $mois; ?>">
				<input type="hidden" name="nb_semaine" id="nb_semaine" value="<?php echo $n; ?>">

<?php				
				for( $i=0; $i < $n ; $i++)
				{	$element = 'select_debut' .$i;
					echo "<input type='hidden' name='$element' id='$element' value=''>";
	
					$element = 'select_fin' .$i;
					echo "<input type='hidden' name='$element' id='$element' value=''>";
		
					$element = 'input_avant' .$i;
					echo "<input type='hidden' name='$element' id='$element' value=''>";

					$element = 'input_apres' .$i;
					echo "<input type='hidden' name='$element' id='$element' value=''>";
					
					$element = 'cached_check' .$i;
					echo "<input type='hidden' name='$element' id='$element' value=''>";
				}
				
		echo '	<table cellpadding="10">
				<tr>
				<th>Sem</th>	
				<th>Date</th>
				<th>Jour</th>	
				<th>Premier Crénau</th>
				<th>Dernier Créneau</th>
				<th>Avant</th>
				<th>Aprés</th>
				<th>Changer</th>
				<th></th>	
				</tr>';	

		$ptr = 0;
		
		if($n != 0)	
		{
			while ($data = mysqli_fetch_assoc ($result))
			{
			
					$semaine = $data['semaine'];
					$rendezvous = $data['rendezvous'];
					$jour = $data['jour'];
			
					$horairedebut = $data['horairedebut'];
					$horairefin = $data['horairefin'];
					$avant = $data['avant'];
					$apres = $data['apres'];
			
					//echo $horairedebut ."  " .$horairefin;
			
					$id_start = "id_start" .$ptr;
					$id_stop = "id_stop" .$ptr;
					$id_mess = "id_mess" .$ptr;
					$id_avant = "id_avant" .$ptr;
					$id_apres = "id_apres" .$ptr;
			
					echo '
							<tr>
							<td>' .$semaine .'</td>
							<td>' .$rendezvous .'</td>
							<td>' .$jour .'</td>
							<td>

							<select id = ' .$id_start .' = name = ' .$id_start .' ;>';
					echo '		<option value="">Selection:</option>';
				
							if($horairedebut == "start1")
							{
								echo '<option value="start1" selected>12H00</option>';
							}
							else
							{
								echo '<option value="start1">12H00</option>';
							}		
					
							if($horairedebut == "start2")
							{
								echo '<option value="start2" selected>12H30</option>';
							}
							else
							{
								echo '<option value="start2">12H30</option>';
							}	
			
							if($horairedebut == "start3")
							{
								echo '<option value="start3" selected>13H</option>';
							}
							else
							{
								echo '<option value="start3">13H</option>';
							}	
			
							if($horairedebut == "start4")
							{
								echo '<option value="start4" selected>13H30</option>';
							}
							else
							{
								echo '<option value="start4">13H30</option>';
							}	
			
							if($horairedebut == "start5")
							{
								echo '<option value="start5" selected>14H</option>';
							}
							else
							{
								echo '<option value="start5">14H</option>';
							}	
					
							if($horairedebut == "start6")
							{
								echo '<option value="start6" selected>14H30</option>';
							}
							else
							{
								echo '<option value="start6">14H30</option>';
							}	
			
							if($horairedebut == "start7")
							{
								echo '<option value="start7" selected>15H00</option>';
							}
							else
							{
								echo '<option value="start7">15H00</option>';
							}	
			
							if($horairedebut == "start8")
							{
								echo '<option value="start8" selected>15H30</option>';
							}
							else
							{
								echo '<option value="start8">15H30</option>';
							}	
			
					echo '
							</select>
							</td>
			
							<td>';
							echo '<select id = ' .$id_stop .' = name = ' .$id_stop . '>';
					
					echo '	<option value="">Selection:</option>';
					
							if($horairefin == "stop1")
							{
								echo '<option value="stop1" selected>12H00</option>';
							}
							else
							{
								echo '<option value="stop1">12H00</option>';
							}	
					
							if($horairefin == "stop2")
							{
								echo '<option value="stop2" selected>12H30</option>';
							}
							else
							{
								echo '<option value="stop2">12H30</option>';
							}	
					
							if($horairefin == "stop3")
							{
								echo '<option value="stop3" selected>13H</option>';
							}
							else
							{
								echo '<option value="stop3">13H</option>';
							}	
					
							if($horairefin == "stop4")
							{
								echo '<option value="stop4" selected>13H30</option>';
							}
							else
							{
								echo '<option value="stop4">13H30</option>';
							}	
					
							if($horairefin == "stop5")
							{
								echo '<option value="stop5" selected>14H00</option>';
							}
							else
							{
								echo '<option value="stop5">14H00</option>';
							}	
					
							if($horairefin == "stop6")
							{
								echo '<option value="stop6" selected>14H30</option>';
							}
							else
							{
								echo '<option value="stop6">14H30</option>';
							}	
					
							if($horairefin == "stop7")			
							{	
								echo '<option value="stop7" selected>15H00</option>';
							}
							else
							{
									echo '<option value="stop7">15H00</option>';	
							}	

							if($horairefin == "stop8")
							{	
								echo '<option value="stop8" selected>15H30</option>';	
							}
							else		
							{		
								echo '<option value="stop8">15H30</option>';
							}	
					echo '	</select>
							</td>';
					echo '	<td>
							<input  type ="text" size = "3" maxlength="1"  id = ' .$id_avant .' = name = ' .$id_avant .' value = ' .$avant  .'>	
							</td>';
					echo '	<td>
							<input  type ="text" size = "3" maxlength="1" id = ' .$id_apres .' = name = ' .$id_apres  .' value = ' .$apres  .'>	
							</td>';	

					$id_checkbox = "ele".$ptr;

					echo '	<td>
							<input type="checkbox" id=' .$id_checkbox  .' name=' .$id_checkbox .' value =""> 
							</td>';	
							
					echo '	<td><p id="' .$id_mess .'"></p></td>	
							</tr>';						
							$ptr = $ptr + 1;		
			}		

?>
				</table>

<?php			
		
				echo '<input type="submit" name="submit" value="Enregistrer" onclick="valide(' .$ptr .')" />';	
				echo '</form>';		
				
		}
?>	
</body>
</html>