<?php
	
    $db_host    = "localhost";
    $db_user   = "root";
    $db_pass   = "";
   	$db_name   = "";
	
	//if(isset($_POST['soumettre']) && $_POST['soumettre'] == "Soumettre")
	if(isset($_POST['valide']) && $_POST['valide'] == "ok")
 	  {						
					$nom_bloc = $_POST['nom_bloc'];
					$lien_id = $_POST['selection_projet'];
					
					$connexion = mysql_connect($db_host,$db_user,$db_pass); 
		 			mysqltest_select_db($db_name, $connexion);
		 			$sql = "select * from projet where id='$lien_id' "; 
		 			$result = mysql_query($sql);
		 			$data = mysql_fetch_array($result);
	        			$lien_projet = $data['nom_projet'];
					
					$date = $_POST['date_schema'];
	        			$date_schema = date_fr_us($date);
	        			
	        			$date = $_POST['date_debut'];
	        			$date_debut = date_fr_us($date);
	        			
	        			$date = $_POST['date_fin'];
	        			$date_fin = date_fr_us($date);
	        			
	        			$jours_tache = $_POST['duree_jours'];
	        			
	        			/*$depart = strtotime($date_debut);
     					$fin = strtotime($date_plan);
     					$nb_jours_ouvres = calcul_jours_ouvres($depart, $fin);
					if($nb_jours_ouvres  == 0)
     					  { $nb_jours_ouvres = 1;
     					  }
					$date = $_POST['date_effective'];
	        			$date_effective = date_fr_us($date);*/
					
					$id_membre = $_POST['selection_membre'];
	        			
	        			$connexion = mysql_connect($db_host,$db_user,$db_pass); 
		 			mysql_select_db($db_name, $connexion);
		 			$sql = "select * from equipe where id='$id_membre' "; 
		 			$result = mysql_query($sql);
		 			$data = mysql_fetch_array($result);
	        			$membre = $data['membre'];
					
					$notes =$_POST['notes']; 
					
					$heure = date("H:i");
					$date = date("Y-m-d"); 
					
					
					$sql = "UPDATE bloc SET 
						nom_bloc = '$nom_bloc' ,
						lien_projet = '$lien_projet' ,
						heure = '$heure' ,
						date = '$date',
						date_schema = '$date_schema' ,
						date_debut = '$date_debut' ,
						jours_tache = '$jours_tache' ,
						date_fin = '$date_fin' ,
						affectation = '$membre' ,
						notes = '$notes' ,
						nb_jours = '$nb_jours_ouvres' WHERE id='$id' ";
						
					$requete = mysql_query( $sql, $connexion ) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
		 			//echo "<script>alert(\"Les modifications ont été prises en compte.\")</script>"; 
		 			//header("Location: rafraichir.php?action=editer_bloc&id=$id");
		 			
	}				
	
	$id = $_GET['id'];
		
	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	mysql_select_db($db_name, $connexion);
	
	$requete = "SELECT * FROM bloc WHERE id='$id' ";
	$result = mysql_query($requete);
	$data = mysql_fetch_array($result);
	
	$id = $data['id'];
	$nom_bloc = $data['nom_bloc'];
	$lien_projet = $data['lien_projet'];
	$heure = $data['heure']; 
	
	$date = $data['date']; 
        $date_creation = date_us_fr($date);
        
	$date = $data['date_schema']; 
	$date_schema = date_us_fr($date);
	
	
	$date = $data['date_debut']; 
	$date_debut = date_us_fr($date);

	$jours_tache = $data['jours_tache']; 

	$date = $data['date_fin']; 
	$date_fin = date_us_fr($date);
	
	// init
	if( $date == "0000-00-00" ) 
	  { 	$annee = date("Y");
	  	$date_fin = "01-01-" .$annee;
	  }	

	$membre = $data['affectation'];
	$owner = $data['owner']; 
	$notes = $data['notes']; 
	
	if(isset($_POST['calculer']) && $_POST['calculer'] == "ok")
	  {  
	     
	     // rafraichir
	     $nom_bloc = $_POST['nom_bloc'];	

	     $lien_id = $_POST['selection_projet'];
	     $connexion = mysql_connect($db_host,$db_user,$db_pass); 
	     mysql_select_db($db_name, $connexion);
	     $sql = "select * from projet where id='$lien_id' "; 
	     $result = mysql_query($sql);
	     $data = mysql_fetch_array($result);
	     $lien_projet = $data['nom_projet'];
	     
	     $date_schema = $_POST['date_schema'];
	     $date_debut = $_POST['date_debut'];
	     $jours_tache = $_POST['duree_jours'];
	     $membre = $_POST['affectation'];
	     
	     $id_membre = $_POST['selection_membre'];
      	     $connexion = mysql_connect($db_host,$db_user,$db_pass); 
	     mysql_select_db($db_name, $connexion);
	     $sql = "select * from equipe where id='$id_membre' "; 
	     $result = mysql_query($sql);
	     $data = mysql_fetch_array($result);
	     $membre = $data['membre'];
	     
	     $owner = $_POST['owner']; 
	     $notes =$_POST['notes']; 
	      
	     $action = "run";
	     
	     $date = $_POST['date_debut'];
	     $date = date_fr_us($date);
	     $timestamp = strtotime($date);
	  
	     while($action == "run")
	          {  $timestamp = $timestamp +  3600*24;
	             if( date( 'w' , $timestamp ) != 0 && date( 'w' , $timestamp ) !=6 )
	               { $cpt = $cpt + 1;
	                  if($cpt == $jours_tache)
	                    { $action = "stop";
	                    } 	
	               }	
                   } 
	     $date_fin = date( 'j-n-Y' , $timestamp );
	     
	  } 
	
	      
        if($_SESSION['logged_droits'] ==  "User")
	   {   include('menu_user.php');
	   }
	if($_SESSION['logged_droits'] ==  "Leader")
	   {   include('menu_leader.php');
	   }
	if($_SESSION['logged_droits'] ==  "Root")
	   {   include('menu_root.php');
	   }	      
	      
	
	echo "<script type=\"text/javascript\" src=\"traitement_formulaire.js\"></script>";

?>
		
	        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
	        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" /> 

		<div class="heading">Edition des propiétées du bloc</div>

		<form action="" method="post" id="editer_bloc" name="editer_bloc" onSubmit="return maFonction()" >


		<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
		<tr bgcolor="#F1F3F5">
		<td colspan="2">&nbsp;</td>
		</tr>
		</tr>
		
		</td>
		</tr>
						
		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Nom du bloc :</td>
		<td align="left" valign="middle" colspan="1">
		<input type="text" name="nom_bloc" size="70" maxlength="20" value="<?php echo $nom_bloc; ?>" id="nom_bloc" >&nbsp;</td>
		</tr>
		
		<input type="hidden" name="valide" id="valide" value="">
		<input type="hidden" name="calculer" id="calculer" value="">
		
		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Choix du projet :</td>
		<td align="left" valign="middle" colspan="1">
		<select name="selection_projet" size="1" > <!-- onChange="submit()" -->
		
<?php

		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
		mysql_select_db($db_name, $connexion);
		$sql = "SELECT * FROM projet";
		$option = mysql_query($sql) ;
		
		while($val=mysql_fetch_array($option))
		     {  $element = $val['nom_projet'];
			$id = $val['id'] ;
   		        $tab_projet[]= $element;
			
   		        if($element == $lien_projet)
   		          {	 echo "<option value=$id selected >$element</option>";
   		          }
   		        else {
   		        	 echo "<option value=$id >$element</option>";
   		             }
		     }     
?>		
		</select>
		</tr>
		
		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">SchÃ©ma disponible :</td>
		<td align="left" valign="middle" colspan="1">
		<!-- <input type="text" name="date_schema" size="30" maxlength="40" value="" id="">&nbsp;</td> -->
		<input type="text" size="12" id="date_schema" READONLY name="date_schema" value="<?php echo $date_schema; ?>" title="DD-MM-YYYY" > 
		<input type="button" value="=" onclick="displayCalendarFor('date_schema');"></td>
		</tr>
		
		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">DÃ©but Layout :</td>
		<td align="left" valign="middle" colspan="1">
		<!-- <input type="text" name="date_debut" size="30" maxlength="40" value="" id=""> en jours  -->
		<input type="text" size="12" id="date_debut" READONLY name="date_debut" value="<?php echo $date_debut; ?>" title="DD-MM-YYYY" > 
		<input type="button" value="=" onclick="displayCalendarFor('date_debut');"></td>
		</tr>
		
		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Durée prévue:</td>
		<td align="left" valign="middle" colspan="1">
		<input type="text" size="12" id="duree_jours" name="duree_jours" value="<?php echo $jours_tache; ?>" > en jours
		<input type="button" name="traitement_bouton" value="Calculer" onclick="calculer_date() "/>
		<!-- type="submit"  value="Calculer" -->
		</td>
		</td>
		</tr>
		

	    	<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Estimation:</td>
		<td align="left" valign="middle" colspan="1">
		<input type="text" size="12" id="date_fin"  READONLY name="date_fin" value="<?php echo $date_fin; ?>" > 
		</td>
		</tr>
		
		<tr bgcolor="#FFFFFF">
		<td align="right">Affectation :</td>
		<td align="left">
		<select name="selection_membre" size="1">
<?php		
	
		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
		mysql_select_db($db_name, $connexion);
		$sql = "SELECT * FROM equipe";
		$option = mysql_query($sql) ;
		
		while($val=mysql_fetch_array($option))
		     {  $element = $val['membre'];
			$id = $val['id'] ;
   		        $tab_affectation[]= $element;
			
   		        if($element == $membre)
   		          {	 echo "<option value=$id selected >$element</option>";
   		          }
   		        else {
   		        	 echo "<option value=$id >$element</option>";
   		             }
		     }   
	
?>
		
		</select></td>
		</tr>

		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Owner :</td>
		<td align="left" valign="middle" colspan="1">
		<input type="text" name="owner" size="30" maxlength="40" style="background-color: grey" onFocus="javascript: this.blur()" value="<?php echo $owner; ?>" id="owner">&nbsp;</td>
		</tr>

		<tr bgcolor="#FFFFFF">
		<td align="right" valign="top">Recommandations :</td>
		<td align="left" valign="middle" colspan="1">
		<textarea name="notes" rows="12" cols="90" id="notes"><?php echo $notes; ?>
		</textarea>&nbsp;</td>
		</tr>
				
		<tr bgcolor="#F1F3F5">
		<td colspan="2" align="center">
		<input type="submit" name="soumettre" value="Soumettre" />
		</td>
		</tr>
		
		</table>
		</div>
		</div>
		
		</form>


		</body>
		</html>