<?php

			session_start();
			include("corps_html.php");
			include('database.php');
			DB_connexion(); 
			
			if(isset($_POST['ajouter']) && $_POST['ajouter'] == "Ajouter")
			{ 
					//header("Location: tertertrafraichir.php");
					
					$nom_projet = $_POST['nom_projet'];
					$owner = $_POST['owner'];
					$date_debut = "00 00 00";
					$duree = "1";
	        		$famille = $_POST['famille'];
	        		$status = $_POST['status_projet'];
					$commentaires = $_POST['commentaires'];
					
					$heure = date("H:i");
					$date = date("d-m-Y"); 
					$infos = $heure ." " .$date;
					
	        		switch($famille)
  					{
					  case 1 :
					          $famille  = "Power management";
					    	  break;
					  case 2 :
					          $famille = "Audio";
					    	  break;
					  case 3 :
					          $famille = "Autre";
					    	  break;  	  		  
					}	
					
					switch($status)
  					{
					  case "choix1" :
					          $status  = "actif";
					    	  break;
					  case "choix2" :
					          $status = "inactif";
					    	  break;		  
					}
	  				
					$results = mysql_query("SHOW TABLES LIKE '$nom_projet'");
					$nb_resultats = mysql_num_rows($results);
 
     				if( $nb_resultats > 0 )
     				{
           			    echo "<script>alert(\"Ce projet existe déjà dans la base de donnée. \")</script>"; 
        			}
        			else
					{
						//nom_projet 
						//owner 
						//date_debut 
						//duree 
						//famille 
						//status 
						//commentaires 
						// infos
							
						$sql = "INSERT INTO projet (id, nom_projet, owner, date_debut, duree, famille, 
						status, commentaires, infos ) 
						VALUES ( '0', '$nom_projet' , '$owner', '$date_debut', '$duree', '$famille', 
						'$status', '$commentaires', '$infos' ) " ;
						$requete = mysql_query($sql); 
						
						//echo $sql;
						
						if (!$requete) 
						{
							die('Requête invalide : ' . mysql_error());
						}
						
						//header("Location: plouf.php");	
						//header("Location: rafraichir.php?action=ajouter_projet");
						
						$table = $nom_projet ."_data";
			
						$sql = "CREATE TABLE IF NOT EXISTS $table(
								id INT NOT NULL AUTO_INCREMENT, 
								PRIMARY KEY(id),
								nom_bloc VARCHAR(50), 
								owner VARCHAR(50),
								recommandations text,
								infos VARCHAR(50) )";
			
						$result = mysql_query($sql);
						if (!$result) 
						{
							die('Requête invalide : ' . mysql_error());
						}
						
						$_SESSION['creer_projet'] = "projet";		
				    }
			}
	
?>	

<script>

function selection_bloc()
{
	
}

</script>

<body>

	<div class="layout">

<?php
	include("header.php");
?>	
		
<!-- Main -->
		<div class="main">
			<div class="container">
				<div class="row">
				
				<div class="col-md-8">
						<!-- Content -->
						<div class="content">
							<div class="page-header">
								<!--<h3>Interface du gestionnaire de projets</h3> -->
								<h2><i class="icon-bullhorn"></i> <span>Créer un projet</span></h2>
							</div>
								
							<!-- Single Page -->
							<div class="page">
								
							<div class="examples">
							
							<!------>
							<form action="" method="post" id="form_ajouter_projet" name="form_ajouter_projet" > <!-- onsubmit="return valider()"-->
							
							<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="800">
											
							<tr bgcolor="#FFFFFF">
							<td align="right" valign="top">Nom du projet :</td>
							<td align="left" valign="middle" colspan="1">
							<input type="text" name="nom_projet" size="40" maxlength="20" value="" id="" >&nbsp;</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="40" maxlength="20" value="" id="" >&nbsp;</td>
							</tr>
							
							<input type="hidden" name="valide" id="valide" value="">
							
							<tr bgcolor="#FFFFFF">
							<td align="right" valign="top">Date de début :</td>
							<td align="left" valign="middle" colspan="1">
							<input type="text" size="12" id="date_debut_projet" READONLY name="date_debut_projet" title="DD-MM-YYYY" > 
							<input type="button" value="=" onclick="displayCalendarFor('date_debut_projet');"></td>
							</tr>
							
							<tr bgcolor="#FFFFFF">
							<td align="right">Durée :</td>
							<td align="left">
							<select name="duree" size="1">
							<option value="1" selected="selected">1 mois</option>
							<option value="2">2 mois </option>
							<option value="3">3 mois </option>
							<option value="4">4 mois </option>
							<option value="5">5 mois </option>
							<option value="6">6 mois </option>
							</select></td>
							</tr>
							
							<tr bgcolor="#FFFFFF">
							<td align="right">Famille :</td>
							<td align="left">
							<select name="famille" size="1">
							<option value="1" selected="selected">Power management</option>
							<option value="2">Audio</option>
							<option value="3">Autre</option>
							</select></td>
							</tr>
							
							<tr bgcolor="#FFFFFF">
							<td align="right">Status :</td>
							<td align="left">
							<select name="status_projet">
							<option value="choix1" selected="selected">Produit actif</option>
							<option value="choix2">Produit inactif</option>
							</select>
							</td>
							</tr>
							
							<tr bgcolor="#FFFFFF">
							<td align="right" valign="top">Commentaires :</td>
							<td align="left" valign="middle" colspan="1">
							<textarea name="notes" rows="12" cols="70" id="">
							</textarea></td>
							</tr>
									
							</table>
							
							<br/><br/><br/><br/><br/><br/>
							<input class="btn btn-primary" type="submit" value="Ajouter" name="ajouter" />
							
							</form>

<?php							
							if($_SESSION['creer_projet'] == "projet")
							{
								$_SESSION['creer_projet'] = "";		
								echo " Projet insérer dans la database.";
							}			
?>
							<!------->
							
							
							</div>	
								
							<div class="clear"></div>

							<div class="clear"></div>
							</div>
							<!-- End Single Page -->
						</div>
						<!-- End Content -->
				</div>
					
				</div>
			</div>
		</div>
<!-- End Main -->
		
<?php
	include("footer.php");
?>		

	</div>

</body>
</html>
