<html>
<!--    cibl&eacute; par:  choix-ref1.php

		cible:  	../menu.php
 -->

<!-- param&egrave;tre &eacute;mis:        -session_id         le num&eacute;ro de la session

	 param&egrave;tre re&ccedil;evable :  -session_id         le num&eacute;ro de la session
 -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Gestion du mat&eacute;riel</title>
</head>
<body>

<?php 

require("../outils/gui.php");
require("../outils/mise_en_page.php");
require("../outils/session.php");

//V&eacute;rifier si la session est valide.
$Bsession_id = $_GET["Bsession_id"];
verifierSession($Bsession_id);

//V&eacute;rifier les droits de l'utilisateur.
$privilege_base = lirePrivilege($Bsession_id);
if ($privilege_base != 2) 
	die("Vous n'avez pas les droits requis!");


// *****************************************************************************************************
// &eacute;tape 0 *********************************************************************************************
// menu de base ****************************************************************************************
// *****************************************************************************************************

if (empty ($etape))
{
	// liste du materiel actifs
	$sql = "select base_ref1materiels.num_materiel, base_ref1materiels.nom, 
	               base_ref1materiels.base 
			from base_ref1materiels
			where base_ref1materiels.actif = 'O'
		 	order by base_ref1materiels.nom asc;";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die("Ne peut acc&eacute;der &agrave; la table base_ref1materiels!<br>".$sql);

	print ("<div align=\"center\"><b><u><font size=\"+2\">Gestion du materiel</font></u></b></div>");
	print ("<br><br>");

	print ("<b><u><font size=\"+1\">Liste du materiel actif</font></u></b>");

	print ("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"ref1materiel_o\">
			<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
			<input type=\"hidden\" name=\"etape\" value=1>
			<select name=\"num_materiel\">");

	while ($row = mysqli_fetch_row($result))
	{
		for ($i = 0; $i < (($___mysqli_tmp = mysqli_num_fields($result)) ? $___mysqli_tmp : false); $i++)
		{
			print("<option value=\"");
			print(htmlspecialchars($row[$i++]));
			print("\">");
			printf("%s,  %s", htmlspecialchars ($row[$i++]), htmlspecialchars ($row[$i]));
			print("</option><br>");
		}
	}

	((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);

	print ("</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Consulter\"></form>");

	print ("Cette section permet de s&eacute;lectionner un materiel actif dans le menu d&eacute;roulant et d'y apporter 
            des modifications. Chaque &eacute;l&eacute;ment du menu d&eacute;roulant se compose de son nom et de l'abr&eacute;viation de la base. 
			Une fois l'&eacute;l&eacute;ment s&eacute;lectionn&eacute;, cliquez sur \"Consulter\" afin de visualiser et modifier les donn&eacute;es.");

	print("\n\n<hr><br>");

	// liste du materiel inactifs

	$sql = "select base_ref1materiels.num_materiel, base_ref1materiels.nom, 
	               base_ref1materiels.base 
			from base_ref1materiels
			where base_ref1materiels.actif = 'N'
		 	order by base_ref1materiels.nom asc;";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die("Ne peut acc&eacute;der &agrave; la table base_ref1materiels!<br>".$sql);

	$num = mysqli_num_rows($result);

	print ("<b><u><font size=\"+1\">Liste du materiel inactif</font></u></b><br>");

	if (!empty ($num))
	{
		print ("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"ref1materiel_n\">
				<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
				<input type=\"hidden\" name=\"etape\" value=1>
				<select name=\"num_materiel\">");

		while ($row = mysqli_fetch_row($result))
		{
			for ($i = 0; $i < (($___mysqli_tmp = mysqli_num_fields($result)) ? $___mysqli_tmp : false); $i++)
			{
				print ("<option value=\"");
				printf ("%s", htmlspecialchars ($row[$i++]));
				print ("\">");
				printf ("%s, %s", htmlspecialchars ($row[$i++]), htmlspecialchars ($row[$i]));
				print ("</option><br>");
			}
		}

		print("</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Consulter\"></form><br>");

		print ("Cette section permet de s&eacute;lectionner un materiel inactif dans le menu d&eacute;roulant et d'y apporter 
    	        des modifications. Chaque &eacute;l&eacute;ment du menu d&eacute;roulant se compose de son nom et de l'abr&eacute;viation de la base. 
				Une fois l'&eacute;l&eacute;ment s&eacute;lectionn&eacute;, cliquez sur \"Consulter\" afin de visualiser et modifier les donn&eacute;es.");
	}
	else
	{
		print ("Il n'y a aucun materiel inactif.");
	}

	((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);

	print("\n\n<hr>");

// pour des nouvelles activit&eacute;es

    print ("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\">");
	print ("<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">");
	print ("<input type=\"hidden\" name=\"etape\" value=2>");
	print ("<input id=\"frm_nouvelle_materiel\" type=\"submit\" value=\"Nouveau mat&eacute;riel\">");
	print ("</form>");

// bouton de rtour au menu
	print ("<hr>");
	print ("<div align=\"center\">");
    print ("<form action=\"../menu-base.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\">");
	print ("<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">");
	print ("<input id=\"frm_menu\" type=\"submit\" value=\"Retour au menu\">");
	print ("</form></div>");

//	pageDebug("&Eacute;tape 0");
} 

// *****************************************************************************************************
// &eacute;tape 1 *********************************************************************************************
// &eacute;diter un secteur existant **************************************************************************
// *****************************************************************************************************
if ($etape == 1)
	{
	// faire les modifications dans la table base_ref1materiel *****************************************
	if (!empty ($mod))
		{
		// modifier le champ nom dans base_ref1materiels 
		if (!empty ($nom))
			{
			$sql = "update base_ref1materiels
				set base_ref1materiels.nom = '$nom',
					base_ref1materiels.mis_a_jour = now()
           	    WHERE base_ref1materiels.num_materiel = '$num_materiel';";

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

		// modifier le champ base dans base_ref1materiels 
		if (!empty ($base))
			{
			$sql = "update base_ref1materiels
				set base_ref1materiels.base = '$base',
					base_ref1materiels.mis_a_jour = now()
           	    WHERE base_ref1materiels.num_materiel = '$num_materiel';";

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

		// modifier le champ actif dans base_ref1materiels 
		if (!empty ($actif))
			{
			$sql = "update base_ref1materiels
				set base_ref1materiels.actif = '$actif',
					base_ref1materiels.mis_a_jour = now()
           	    WHERE base_ref1materiels.num_materiel = '$num_materiel';";

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

		// modifier le champ compte_budget dans base_ref1materiels 
		if (!empty ($compte_budget))
			{
			$sql = "update base_ref1materiels
				set base_ref1materiels.compte_budget = '$compte_budget',
					base_ref1materiels.mis_a_jour = now()
           	    WHERE base_ref1materiels.num_materiel = '$num_materiel';";

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

		}

	// remplir les variables ******************************************************************************
	$sql = "SELECT base_ref1materiels.nom, base_ref1materiels.base, 
				   base_ref1materiels.actif, base_ref1materiels.compte_budget  
    	    FROM base_ref1materiels
       	    WHERE base_ref1materiels.num_materiel = '$num_materiel';";

	// Ex&eacute;cute la requ&ecirc;te et mettre les r&eacute;sultats dans $result
	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die ("ne peut lire les donn&eacute;es.<br>".$sql);

	// obtenir le nombre d'enregistrement. 0 si invalide, 1 si valide.
	$num = mysqli_num_rows($result);

	// met la valeur dans les variables
	if ($num)
		{
		$i = 0;
		$row = mysqli_fetch_row($result);
		$nom = ($row[$i++]);
		$base = ($row[$i++]);
		$actif = ($row[$i++]);
		$compte_budget = ($row[$i]);
		}
	else
		{
		die ("Il y a un probl&egrave;me dans la base de donn&eacute;es !");
		}

	// fait le m&eacute;nage des r&eacute;sultat en m&eacute;moire
	((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);

	// afficher les r&eacute;sultats ****************************************************************************	

	print ("<div align=\"center\"><u><b><font size=\"+2\">Modification du mat&eacute;riel</font></b></u></div>");
	print("<p>");

	print("<table width=\"100%\" cellpadding=0 cellspacing=0 border=\"1\"><tr><td>");

	// nom

	print ("<b>Nom du mat&eacute;riel:</b>");

	print("</td><td><div align=right>");

	print("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_nom\"> 
		<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		<input type=\"hidden\" name=\"num_materiel\" value=".$num_materiel.">
		<input type=\"hidden\" name=\"mod\" value=O>
		<input type=\"hidden\" name=\"etape\" value=1>");

	print("<input type=\"text\" value=\"".$nom."\" name=\"nom\" align=\"top\" maxlength=\"40\" size=\"40\">
			<input type=\"image\" alt=\"nom\" src=\"bouton0.gif\" border=0 width=15 height=24>
			</form>");
	print ("<td></td>");
	print("</div></td></tr><tr><td>");

	// base

	print ("<b>Base:</b>");
	print("</td><td><div align=right>");

	print ("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_base\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_materiel\" value=".$num_materiel.">
		    <input type=\"hidden\" name=\"mod\" value=O>
		    <input type=\"hidden\" name=\"etape\" value=1>");

	print ("<select name=\"base\">
			  <option value=\"".$base."\">".$base."</option>
			  <option value=\"R\">Richelieu</option>
			  <option value=\"V\">Ville-Marie</option>
			  <option value=\"RV\">Les deux</option>
		    </select>");
		
	print ("<input type=\"image\" alt=\"base\" src=\"bouton0.gif\" border=0 width=15 height=24></form>");
	print ("<td><b>R</b>ichelieu, <b>V</b>ille-Marie, <b>RV</b> les deux</td>");
	print("</div></td></tr><tr><td>");

	// actif

	print ("<b>Actif:</b>");
	print("</td><td><div align=right>");

	print ("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_actif\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_materiel\" value=".$num_materiel.">
		    <input type=\"hidden\" name=\"mod\" value=O>
		    <input type=\"hidden\" name=\"etape\" value=1>");

	print ("<select name=\"actif\">
			  <option value=\"".$actif."\">".$actif."</option>
			  <option value=\"N\">Non</option>
			  <option value=\"O\">Oui</option>
		    </select>");
	print ("<input type=\"image\" alt=\"actif\" src=\"bouton0.gif\" border=0 width=15 height=24></form>");
	print ("<td><b>O</b>ui/<b>N</b>on)</td>");
	print("</div></td></tr><tr><td>");

	// compte budgetaire

	print ("<b>Poste budgetaire:</b>");

	print("</td><td><div align=right>");

	print("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_compte_budget\"> 
		<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_materiel\" value=".$num_materiel.">
		<input type=\"hidden\" name=\"mod\" value=O>
		<input type=\"hidden\" name=\"etape\" value=1>");

	print("<input type=\"text\" value=\"".$compte_budget."\" name=\"compte_budget\" align=\"top\" maxlength=\"4\" size=\"4\">
			<input type=\"image\" alt=\"compte_budget\" src=\"bouton0.gif\" border=0 width=15 height=24>
			</form>");

	print("</div></td></tr></table>");

//*****************************************************************************************************
// Bas de page 
//*****************************************************************************************************
	print ("<br>");
	print ("<hr><br>");
// Boutons pour les renvois vers d'autres pages
	print("<table border=0 width=100%>");
   		print("<td width=45% align=right>"); // afiche le bouton de retour &agrave; la gestion branche
		print("<form action="\ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
	   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	   			print("<input type=hidden name=etape value=0>");
	   			print("<input type=submit name=back value=\"Gestion du mat&eacute;riel\">");
	    print("</form>");
	print("</td>");
	print("<td width=5% align=center>"."&nbsp"."</td>"); //Espacement
	print("<td width=45% align=left>"); //Afficher le bouton de retour au menu principal.
		print("<form action="\../menu-base.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");  
	   		 	print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	  		 	print("<input type=submit name=back value=\"    Menu Principal    \">");
		print("</form>");
	print("</td>");
	print ("</table>");
	}

// *****************************************************************************************************
// &eacute;tape 2 *********************************************************************************************
// demande les info pour cr&eacute;er la branche **************************************************************
// *****************************************************************************************************
if ($etape == 2)
{
	print ("<div align=\"center\"><u><b><font size=\"+2\">Nouveau mat&eacute;riel</font></b></u></div>");
	print("<p>");

	print ("<form action=\"ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"creation\"> 
			<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
			<input type=\"hidden\" name=\"etape\" value=3>
			<input type=\"hidden\" name=\"pargestion\" value=".$pargestion.">");

	print("<table width=350>");

	print("<tr><td><b>");
	print ("<b>Nom</b>");
	print("</b></td><td><div align=right>");
	print("<input type=\"text\" value=\"".$nom."\" name=\"nom\" align=\"top\" maxlength=\"40\" size=\"40\">");
	print("</div></td></tr>");

	print ("<tr><td>");	
	print ("<b>Base</b></td>");
	print ("<td><div align=right>");
	print ("<select name=\"base\">
			  <option value=\"R\">Richelieu</option>
			  <option value=\"V\">Ville-Marie</option>
			  <option value=\"RV\">Les deux</option>
			</select>");
	print("</div></td></tr><tr>");

	print("<tr><td><b>");
	print ("<b>Poste budgetaire:</b>");
	print("</b></td><td><div align=right>");
	print("<input type=\"text\" value=\"".$compte_budget."\" name=\"compte_budget\" align=\"top\" maxlength=\"4\" size=\"4\">");
	print("</div></td></tr>");

	print ("<tr><td>");	
	print ("<b>Actif</b></td>");
	print ("<td><div align=right>");
	print ("<select name=\"actif\">
			  <option value=\"O\">Oui</option>
			  <option value=\"N\">Non</option>
			</select>");
	print("</div></td></tr><tr>");
	
	print ("<td>&nbsp;</td><td><br>");
	print("<div align=right><input type='submit' value='Soumettre'></div>");

	//<input type=\"image\" alt=\"etape3\" src=\"bouton0.gif\" border=0 width=15 height=24>
	print("</td></tr></table></form>");

//	pageDebug("&Eacute;tape 2");
}

//FIXME: Un reload ajoute une seconde fois les informations !
// *****************************************************************************************************
// &eacute;tape 3 *********************************************************************************************
// cr&eacute;e le secteur dans la table ref1branche ***********************************************************
// *****************************************************************************************************
if ($etape == 3)
{
	print ("<div align=\"center\"><b>Activit&eacute; cr&eacute;&eacute;e avec succ&egrave;s</b></div>");
	print("<p>");

	// mettre les info dans la table ref1branche
	$sql = "insert into base_ref1materiels 
		    set nom = '$nom',
				base = '$base',
				actif = '$actif',
				compte_budget = $compte_budget,
				mis_a_jour = now();";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die("Ne peut faire l'ajout!<br>".$sql);

	$sql = "select base_ref1materiels.num_materiel
			from base_ref1materiels
			where base_ref1materiels.nom = '$nom'
			  and base_ref1materiels.base = '$base'
			  and base_ref1materiels.compte_budget = '$compte_budget';";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("Ne peut lire dans base_ref1materiels!<br>".$sql);
	$row = mysqli_fetch_row($result);
	$num_materiel = ($row[0]);

	print("&nbsp;<p><center><table width=80%><tr><td><center>");

	if ($pargestion == 1)
	{
		// bouton de retour &agrave; la gestion de location
			print("<form action="\../contract/ajout-location.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
		   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	   				print("<input type=submit name=back value=\"Retour\">");
		    print("</form>");	
	}
	elseif ($pargestion == 2)
	{
		// bouton de retour &agrave; la gestion d'achat
			print("<form action="\../contract/ajout-achat.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
		   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	   				print("<input type=submit name=back value=\"Retour\">");
		    print("</form>");	
	}
	else
	{
	
		// consulter le materiel
		print("<form action="\ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
	   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	   			print("<input type=hidden name=etape value=1>");
	   			print("<input type=hidden name=num_materiel value=$num_materiel>");
	   			print("<input type=submit name=back value=\"Consulter le mat&eacute;riel\">");
	    print("</form>");

		print("</center></td><td><center>");
		// ajouter une activit&eacute;
		print("<form action="\ref1materiels.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
   			print("<input type=hidden name=etape value=2>");
   			print("<input type=submit name=back value=\"Cr&eacute;er un nouveau mat&eacute;riel\">");
    	print("</form>");

		print("</center></td><td><center>");
		// retour au menu pricipal
		print("<form action="\../menu-base.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
	   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	   			print("<input type=submit name=back value=\"Menu Principal \">");
	    print("</form>");
		print("</center></td></tr></table></center>");
	}
	
}
?>

</body>
</html>
