<!DOCTYPE html>
<html id="html">
	<head>
		<meta charset="utf-8" />
        <title>Commande</title>
		<link rel="stylesheet" href="style/style_jquery-ui.css">
		<link rel="stylesheet" type="text/css" href="style/style.css">
		<script type="text/javascript" src="script/scripts.js"></script>
		<script type="text/javascript" src="script/jquery.js"></script>
		<script type="text/javascript" src="script/jquery-ui.js"></script>
  		<script>
			$(function() {
				$( "#tabs" ).tabs();
			});
 		</script>
 		<script type="text/javascript" src="script/jsweetalert.js"></script>
		<link rel="stylesheet" type="text/css" href="style/style_sweetalert.css">

 		<?php Include('script/rotation.php'); ?>
	</head>

	<body onload="retourne_cookie()"><!-- Il faut ajouter l'appelle a la fonction retourne au chargement de la page pour eviter un bug d'appui sur le bouton -->
		<?php
			error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
			Include('../database.params.inc.php');
			$link = pg_connect("host=$machine user=$user password=$pwd dbname=$db")or die("Erreur de Connection !<br />".pg_last_error()) ;
		?>
		<div id="entete">
			<img src="img/accueil.png" class="bouton_entete" onclick="self.location.href='index.php'">
			<img src="img/rotation.png" class="bouton_entete" onclick="retourne()">
			<p id="titre">
				<!-- Titre de la page affichee dans l'entete -->
				Commande
			</p>
			<img src="img/aide.png" class="bouton_entete_help"  onclick="swal('Aide', 'Veuillez choisir au moins un produit afin de pouvoir passer à l\'étape du paiement.','info');">
		</div>
		<div id="contenu">
			<!-- Le contenu doit etre écrit ci-dessous -->

			<form method=post name="cmd" action="commande_2.php">
				<?php
					// récupération des type de conso
					$query = "SELECT DISTINCT type FROM PRODUIT WHERE dispo = 'true' ORDER BY type";
					$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
					$nb_type = pg_num_rows($result);
					$i = 0;
					while ($i<$nb_type) {
						$type[$i] = pg_fetch_result($result, $i, "type");
						$i++;
					}
				?>

				<!-- Systeme d'onglet -->
				<div id="tabs">
					<ul>
						<?php 
							$a = 1;
							while ( $a <= $nb_type) {?>
						        <li><a href="#tabs-<?php echo $a;?>"><?php echo $type[$a-1];?></a></li>
							<?php 
								$a++; 
							} 
						?>
					</ul>


			    	<?php 
			    		$b = 1;
			    		while ( $b <= $nb_type) {
			    			$val = $b-1;
			    			$query2 = "SELECT id_produit, nom, prix, information, img FROM PRODUIT WHERE type = '$type[$val]' AND dispo = 'true' ORDER BY id_produit";
			    			$result2 = pg_query($link, $query2) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
			    			$num = pg_num_rows($result2);

			    			?> 
		    				<div id="tabs-<?php echo $b;?>">
		    				<!--tab content-->
				    			<?php
				    				$c = 0;
				    				while ($c < $num) { 
				    					$nb_commande = pg_fetch_result($result2, $c, "id_produit");
				    					?>
							        
							            <!-- Affichage en forme du produit -->
										<table>
											<tr>
												<td rowspan="2" class="tab_1"><img src="../carte/<?php echo( pg_fetch_result($result2, $c, "img")); ?>" class="tab_1_input"></td>
												<td class="tab_2"><?php echo( pg_fetch_result($result2, $c, "nom")); ?></td>
												<td rowspan="2" class="tab_3"><img src="img/arrow_left.png" onClick="if (document.forms.cmd.prod_<?php echo $nb_commande; ?>.value > 0){ document.forms.cmd.prod_<?php echo $nb_commande; ?>.value--;}"></td>
												<td rowspan="2" class="tab_4"><input type=number id="prod_<?php echo $nb_commande; ?>" name="prod_<?php echo $nb_commande; ?>" min="0" max="20" step="1" value="0" class="tab_4_input"></td><!-- prod_ représente l'id_produit et la value le nombre, envoi double -->
												<td rowspan="2" class="tab_5"><img src="img/arrow_right.png" onClick="if (document.forms.cmd.prod_<?php echo $nb_commande; ?>.value < 20){ document.forms.cmd.prod_<?php echo $nb_commande; ?>.value++;}"></td>
												<td rowspan="2" class="tab_6"><?php echo( pg_fetch_result($result2, $c, "prix")); ?>€</td>
											</tr>
											<tr>
												<td class="tab_7"><?php echo( pg_fetch_result($result2, $c, "information")); ?></td>
											</tr>
										</table>
										<br>

										<!-- Transfert des données connexes -->
										<input type=hidden id="img_<?php echo $nb_commande; ?>" name="img_<?php echo $nb_commande; ?>" value="<?php echo( pg_fetch_result($result2, $c, "img")); ?>" >
										<input type=hidden id="nom_<?php echo $nb_commande; ?>" name="nom_<?php echo $nb_commande; ?>" value="<?php echo( pg_fetch_result($result2, $c, 'nom')); ?>" >
										<input type=hidden id="prix_<?php echo $nb_commande; ?>" name="prix_<?php echo $nb_commande; ?>" value="<?php echo( pg_fetch_result($result2, $c, 'prix')); ?>" >
										<input type=hidden id="info_<?php echo $nb_commande; ?>" name="info_<?php echo $nb_commande; ?>" value="<?php echo( pg_fetch_result($result2, $c, 'information')); ?>" >

							        
						        	<?php $c++; 
						    		}
				        		?> 		
			        		</div> 
				        	<?php
				        	$b++; 
						} 
					?>

				</div>
				<button id="valid_commande" onclick="submit();"> Valider </button><br>
				<button id="erase_commande" onclick="reset();"> Effacer </button>
			</form>
		</div>
	</body>
</html>
