<?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(); 

	$id_ancienne = "";
	$id_nouvelle = "";	
	
	if(isset($_GET['submit']))	
	{
			if(isset($_GET['verrou']) && $_GET['verrou'] == "ok")	
			{
					//print_r($_GET);
					$date_origine = $_GET['id_ancienne'];
					$date_destination = $_GET['id_nouvelle'];
		
					$annee = date('Y');
					$table = "reservation" .$annee;
			
					$sql = "SELECT * FROM " .$table ." WHERE rendezvous = '$date_origine' ";
					//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());
					}
			
					$datas = mysqli_fetch_assoc($result);
		
					//echo '<pre>';
					//print_r($datas);
		
					$tab_selection = array();		
			 
					//$mode = $datas['mode'];
					//$indice = $datas['indice'];
					$normal = $datas['normal'];
					$etendu = $datas['etendu'];
					$tab_selection[1] = $datas['seance1'];
					$tab_selection[2] = $datas['seance2'];
					$tab_selection[3] = $datas['seance3'];
					$tab_selection[4] = $datas['seance4'];
					$tab_selection[5] = $datas['seance5'];
					$tab_selection[6] = $datas['seance6'];
					$tab_selection[7] = $datas['seance7'];
					$tab_selection[8] = $datas['seance8'];
					$horairedebut = $datas['horairedebut'];
					$horairefin = $datas['horairefin'];
					$avant = $datas['avant'];
					$apres = $datas['apres'];
	
					mysqli_query($connexion, "SET NAMES 'utf8'");	
					$sql = "UPDATE " .$table ." SET 
												normal = '$normal' ,
												etendu = '$etendu' ,
												seance1 = '$tab_selection[1]' , 
												seance2 = '$tab_selection[2]' , 		
												seance3 = '$tab_selection[3]' ,
												seance4 = '$tab_selection[4]' , 			
												seance5 = '$tab_selection[5]' , 
												seance6 = '$tab_selection[6]' , 	
												seance7 = '$tab_selection[7]' , 	
												seance8 = '$tab_selection[8]' ,
												horairedebut = '$horairedebut' , 
												horairefin = '$horairefin' , 	
												avant = '$avant' , 	
												apres = '$apres' 
												WHERE rendezvous ='$date_destination' ";						
	
					//echo $sql;
						
					$result = mysqli_query($connexion, $sql );
	
					if(!$result)
					{
						die('Impossible d\'exécuter la requête :' . mysql_error());
					}
		
					// soit on fait une remise à 0 ou bien on efface seulement les créneaux
					// mise à jour de la table gestion
					
					$tab_tmp = array();	
					
					for( $i=1; $i <= count($tab_selection) ; $i++)
					{
						if($tab_selection[$i] == "unselect" || $tab_selection[$i] == "creneau ouvert" || $tab_selection[$i] == "creneau fermer")
						{
							$tab_tmp[$i] = $tab_selection[$i];
						}
						else
						{
							$tab_tmp[$i] = "";
						}
					
					}
					
					//print_r($tab_tmp);
					
					mysqli_query($connexion, "SET NAMES 'utf8'");	
					$sql = "UPDATE " .$table ." SET 
												normal = '0' ,
												etendu = '0' ,
												seance1 = '$tab_tmp[1]' , 
												seance2 = '$tab_tmp[2]' , 		
												seance3 = '$tab_tmp[3]' ,
												seance4 = '$tab_tmp[4]' , 			
												seance5 = '$tab_tmp[5]' , 
												seance6 = '$tab_tmp[6]' , 	
												seance7 = '$tab_tmp[7]' , 	
												seance8 = '$tab_tmp[8]' ,
												horairedebut = '$horairedebut' , 
												horairefin = '$horairefin' , 	
												avant = '$avant' , 	
												apres = '$apres' 
												WHERE rendezvous ='$date_origine' ";						
	
					//echo $sql;
						
					$result = mysqli_query($connexion, $sql );
					if(!$result)
					{
						die('Impossible d\'exécuter la requête :' . mysql_error());
					}

					$tab_mois = array("","janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre");
					$pieces = explode("-", $date_destination);
					$mois = intval($pieces[1]);
					$mois = $tab_mois[intval($pieces[1])];
					
					$sql = "SELECT id FROM gestion WHERE jour ='$date_origine' ";	
					//echo $sql;
					$result = mysqli_query($connexion, $sql );	
					if(!$result)
					{
						die('Impossible d\'exécuter la requête :' . mysql_error());
					}
					
					while($datas = mysqli_fetch_assoc($result))
					{
							$id = $datas['id'];
							$sql = "UPDATE gestion SET mois = '$mois' , jour = '$date_destination' WHERE id ='$id' ";
							//echo $sql;
							$res = mysqli_query($connexion, $sql );	
							if(!$res)
							{
								die('Impossible d\'exécuter la requête :' . mysql_error());
							}
					}
					
					echo "Migration des rendez-vous : du $date_origine vers $date_destination";
					echo '<br><br>';

			}
	}
	
////////////////////////////////////////////////////////////////////////////////	
/*	
	$mois = "mars";
	$jour = "17-03-2015";
	$annee = date('Y');
		
	echo ucfirst($mois) ." " .$annee;
	echo '<br><br>';
	
	$table = "reservation" .$annee;
	
	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' and rendezvous = '$jour' ";
	//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());
	}
	
	$datas = mysqli_fetch_assoc($result);
		
	//echo '<pre>';
	//print_r($datas);
		     
	$tab_seance = array();		
			 
	$mode = $datas['mode'];
	$indice = $datas['indice'];
	$normal = $datas['normal'];
	$etendu = $datas['etendu'];
	$tab_seance[1] = $datas['seance1'];
	$tab_seance[2] = $datas['seance2'];
	$tab_seance[3] = $datas['seance3'];
	$tab_seance[4] = $datas['seance4'];
	$tab_seance[5] = $datas['seance5'];
	$tab_seance[6] = $datas['seance6'];
	$tab_seance[7] = $datas['seance7'];
	$tab_seance[8] = $datas['seance8'];
	$horairedebut = $datas['horairedebut'];
	$horairefin = $datas['horairefin'];
	$avant = $datas['avant'];
	$apres = $datas['apres'];
	
	$jour = "23-03-2015";
	
	mysqli_query($connexion, "SET NAMES 'utf8'");	
	$sql = "UPDATE " .$table ." SET 
									mode = '$mode' ,
									indice = '$indice' ,
									normal = '$normal' ,
									etendu = '$etendu' ,
									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]' ,
									horairedebut = '$horairedebut' , 
									horairefin = '$horairefin' , 	
									avant = '$avant' , 	
									apres = '$apres' 
									WHERE rendezvous ='$jour' ";						
	
	//echo $sql;
						
	$result = mysqli_query($connexion, $sql );
	
	if(!$result)
	{
		die('Impossible d\'exécuter la requête :' . mysql_error());
	}
*/
?>

<html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<head>
</head>

<body>

<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 class="active"><a href="configuration.php"><span>Configuration</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>

<script>
function valide()
{
	var verif = true;   
	var msg_erreur = new Array();

	var val1 = document.getElementById('id_ancienne').selectedIndex;	
	var val2 = document.getElementById('id_nouvelle').selectedIndex;	
	
	var val1 = document.getElementById('id_ancienne').value;	
	var val2 = document.getElementById('id_nouvelle').value;	
	
	//alert(val1);
	//alert(val2);
	
	if(val1 == "none_origine" || val2 == "none_destination")
	{	
			msg_erreur[0] = "Vous devez selectionner deux dates distinctes !.";		
			verif = false;	
	}
	else {
			var ele1 = document.getElementById("id_ancienne");
			var text1 = ele1.options[ele1.selectedIndex].text;
			//alert(text1);
	
			var ele2 = document.getElementById("id_nouvelle");
			var text2 = ele2.options[ele2.selectedIndex].text;
			//alert(text2);
	
			var tableau1=text1.split(" ");
			//alert(tableau1[4]);
	
			var tableau2=text2.split(" ");
			//alert(tableau2[4]);
		
			var n = val1.localeCompare(val2);
	
			if(n ==  0)	
			{	
				msg_erreur[1] = "La date origine est identique à la date de destination !.";		
				verif = false;	
			}	
			
			if(tableau1[4] == "Vide" && tableau2[4] == "Vide")
			{	msg_erreur[2] = "La premiére selection doit être remplie et la seconde vide : Copie impossible !.";		
				verif = false;	
			}	
			if(tableau1[4] == "Vide" && tableau2[4] == "Remplie" )
			{	msg_erreur[2] = "La premiére selection doit être remplie et la seconde vide : Copie impossible !.";		
				verif = false;	
			}	
			if(tableau1[4] == "Remplie" && tableau2[4] == "Remplie" )
			{	msg_erreur[2] = "La premiére selection doit être remplie et la seconde vide : Copie impossible !..";		
				verif = false;	
			}
			
			var champs1 = tableau1[5] + tableau1[6] + tableau1[7] + tableau1[8];
			var champs2 = tableau2[5] + tableau2[6] + tableau2[7] + tableau2[8];
			
			if(champs1 != champs2)
			{  
				msg_erreur[1] = "Pour faire une copie, les plages horaires doivent être identiques !.";		
				verif = false;	
			}
			
	}
	
	if(verif == true)
	{ 
		document.getElementById('verrou').value="00000ok";	
	}
	if(verif == false) 
	{ 	
		var affiche = msg_erreur.join("\n"); 
		document.getElementById('verrou').value="erreur";	
		alert(affiche);
		//document.getElementById('message').innerHTML = affiche ;	
		//message.textContent = "my text";	
		//document.getElementById('message').innerHTML = "text1 <br> text2"; 
	}	
	
}

</script>
	
<?php
	echo "Déplacer des rendez-vous";
	echo "<br><br>";
	
	$annee = date('Y');
	$table = "reservation" .$annee;
	$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' 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());
	}
	
	//$tab_jour = array();		
	//$tab_mois = array();	
	//$tab_semaine = array();	
		
	$ts_du_jour = strtotime(date("d-m-Y"));	
	
	while ($data = mysqli_fetch_assoc ($result))
	{
		if($ts_du_jour < $data['ts'])
		{
			$tab_jour[] = $data['rendezvous'];
			$tab_mois[] = $data['mois'];
			$tab_semaine[] = $data['semaine'];
			$tab_normal[] = $data['normal'];
			$tab_etendu[] = $data['etendu'];
			$tab_horairedebut[] = $data['horairedebut'];
			$tab_horairefin[] = $data['horairefin'];
			$tab_avant[] = $data['avant'];
			$tab_apres[] = $data['apres'];
		}	
	}	
	
	echo '<form action="#" name ="form_horaire" method="get">';
	echo '<input type="hidden" name="verrou" id="verrou" value="">';
	echo '	<table cellpadding="10">
				<tr>
				<th>Jour origine</th>	
				<th>Jour destination</th>
				<th></th>	
				</tr>';	
	echo '<tr>';	

	echo '<td>';
	echo '<select id = "id_ancienne"  name =  "id_ancienne"  size="10">';
	echo '<option value="none_origine">Selection:</option>';				
	
	for( $i=0; $i < count($tab_jour) ; $i++)
	{
			//${'text'.$i} = "cached".$i;
			//$val = ${'text'.$i};
			//$objet = 'selection_old' .$i;
			//$infos = "55 " .$tab_jour[$i] ." ".$tab_mois[$i] ." Sem. ".$tab_semaine[$i]; //." " .$tab_horairedebut[$i] ." " .$tab_horairefin[$i]  ." " .$tab_avant[$i]  ." " .$tab_apres[$i];
						
			$etat = "";
			
			if( $tab_normal[$i] != 0   ) //|| strlen($tab_etendu[$i] != 0 )
			{
				$etat = "Remplie";
			}
			else
			{
				$etat = "Vide";
			}
			
			$objet = 'selection_old' .$i;
			$infos = $tab_jour[$i] ." ".$tab_mois[$i] ." Sem. ".$tab_semaine[$i] ." " .$etat ." " .$tab_horairedebut[$i] ." " .$tab_horairefin[$i]  ." " .$tab_avant[$i]  ." " .$tab_apres[$i];
			
			if($id_ancienne == $tab_jour[$i])
			{
				echo '<option value=' .$tab_jour[$i] .' selected>' .$infos .'</option>';
			}
			else
			{
				echo '<option value=' .$tab_jour[$i] .'>' .$infos .'</option>';
			}	
	}	
						
	echo '</select>';
	echo '</td>';

	echo '<td>';
	echo '<select id = "id_nouvelle"  name =  "id_nouvelle" size="10">';
	echo '<option value="none_destination">Selection:</option>';	

	for( $i=0; $i < count($tab_jour) ; $i++)
	{
			//${'text'.$i} = "cached".$i;
			//$val = ${'text'.$i};
			$etat = "";
			
			if( $tab_normal[$i] != 0   ) //|| strlen($tab_etendu[$i] != 0 )
			{
				$etat = "Remplie";
			}
			else
			{
				$etat = "Vide";
			}
			
			$objet = 'selection_new' .$i;
			$infos = $tab_jour[$i] ." ".$tab_mois[$i] ." Sem. ".$tab_semaine[$i] ." " .$etat;
			$infos = $tab_jour[$i] ." ".$tab_mois[$i] ." Sem. ".$tab_semaine[$i] ." " .$etat ." " .$tab_horairedebut[$i] ." " .$tab_horairefin[$i]  ." " .$tab_avant[$i]  ." " .$tab_apres[$i];
						
			if($id_nouvelle == $tab_jour[$i])
			{
				echo '<option value=' .$tab_jour[$i] .' selected>' .$infos .'</option>';
			}
			else
			{
				echo '<option value=' .$tab_jour[$i] .'>' .$infos .'</option>';
			}	
	}	
		
	echo '</select>';
	echo '</td>';

	echo '</tr>';
	
	echo '</table>';
				
	echo '<input type="submit" name="submit" value="Enregistrer" onclick="valide()" />';	
	echo '</form>';		

//http://www.formget.com/form-validation-using-ajax/#
//http://coursesweb.net/jquery/validate-submit-form
//http://contactmetrics.com/blog/validate-contact-form-jquery
//http://yensdesign.com/tutorials/validateform/
//http://speckyboy.com/2009/12/17/10-useful-jqu
//http://raymondselda.com/demo/php-contact-form/contact.php
	
?>
	
	<p id="message"></p>
	
</body>
</html>