<!DOCTYPE html>
<html>
	<head>
		<title>Accueil</title>
		<?php $titre = 'Accueil'; ?>
		<meta charset="UTF-8">
		<?php include 'session.inc.php'; check_login(); ?>
		<link rel="stylesheet" href="style/style.css" />
	</head>

	<body>
    	<?php include("menu.php"); ?>
    	<div  id='contenu'>
    		<?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() ) ;

			?>
			<form method=post name="index" action="index_2.php">
			<input type='hidden' id='action' name='action' value='null'>
			<input type='hidden' id='id' name='id' value='null'>
			<input type='hidden' id='cmd' name='cmd' value='null'>
			<?php

				// recup des cmd
				$query = "SELECT DISTINCT num_cmd FROM COMMANDE WHERE etat!='LIVREE' AND etat!='ANNULEE' ORDER BY num_cmd ASC";
				$result = pg_query($link, $query) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
				$num = pg_num_rows($result);
				if ($num == 0) {
					echo "<label>Il n'y a pas de commandes en attente</label>";
				} 
				else {
					$i = 0;
					while ($i < $num) {
						$num_cmd = pg_fetch_result($result, $i, "num_cmd");

						// recup des info de la cmd en cours
						$query2 = "SELECT DISTINCT id_commande, nom, type, temps, img, nombre, etat, paiement, type_paiement, suggestion, prix, conso, num_cmd FROM COMMANDE, PRODUIT WHERE num_cmd = '$num_cmd' AND id_produit = conso AND etat!='LIVREE' AND etat!='ANNULEE'";
						$result2 = pg_query($link, $query2) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
						$num2 = pg_num_rows($result2);

						$j = 0;
						$prix = 0;
						while ($j < $num2) {
							// Calcul du prix
							if (pg_fetch_result($result2, $j, "suggestion") == "FALSE") {
								$prix += pg_fetch_result($result2, $j, "prix");
							}
							else {
								$cmd_val = pg_fetch_result($result2, $j, "conso");
								$query3 = "SELECT  prix FROM SUGGESTION WHERE conso_sugg = '$cmd_val' ";
								$result3 = pg_query($link, $query3) or die("Erreur SQL !<br />$query<br />".pg_last_error()) ;
								$prix += pg_fetch_result($result3, '0', "prix");
							}
							$j++;
						}
						
						?>
						<br>
						<table class="aff_cmd">
							<thead>
								<td>produit</td>
								<td>type</td>
								<td>nombre</td>
								<td>Réalisé</td>
								<td>Etat</td>
								<td>Paiement</td>
								<td>Livraison</td>
							</thead>
							<tr>
								<?php
									$k = 0;
									while ($k < $num2) {
										$conso = pg_fetch_result($result2, $k, "conso");
										$num_cmd = pg_fetch_result($result2, $k, "num_cmd");
										?>
										<tr>
											<td class="tab_2terbis"><?php echo( pg_fetch_result($result2, $k, "nom")); ?></td>
											<td class="tab_2ter"><?php echo( pg_fetch_result($result2, $k, "type")); ?></td>

											<td class="tab_aff_cmd_time"><?php echo( pg_fetch_result($result2, $k, "nombre")); ?></td>
											<td><?php if ( pg_fetch_result($result2, $k, "etat") != "fait" ){?><button id="bt_index"onclick="document.forms.index.action.value='etat'; document.forms.index.id.value='<?php echo $conso ;?>'; submit();" ><img src="img/check.png" width="50px" height="50px"></button><?php }?></td>
											<?php 
												if ($k == 0) {
													?>
													<td rowspan="<?php echo $num2; ?>"><input type="button" id="index" value="tout<?php echo "\n"; ?>realiser" style="height:calc(50px*<?php echo $num2 ; ?>);" onclick="document.forms.index.action.value='etat_tot'; document.forms.index.cmd.value='<?php echo $num_cmd ;?>'; submit();"></td>
													<td rowspan="<?php echo $num2; ?>"><?php if ( pg_fetch_result($result2, $k, "paiement") == "FALSE" ){?><input type="button" id="index" value="A payer : <?php echo "\n" ;echo( pg_fetch_result($result2, $k, "type_paiement")); echo "\n" ; echo($prix); echo "€";?>" onclick="document.forms.index.action.value='paiement'; document.forms.index.cmd.value='<?php echo $num_cmd ;?>'; submit();" style="height:calc(50px*<?php echo $num2 ; ?>);"><?php } else {?><input type="button" id="index"  disabled value="Deja payé" style="height:calc(50px*<?php echo $num2 ; ?>);"><?php }?> </td>
													<td rowspan="<?php echo $num2; ?>"><input type="button" id="index" value="A<?php echo "\n"; ?>livrer" onclick="document.forms.index.action.value='livraison'; document.forms.index.cmd.value='<?php echo $num_cmd ;?>'; submit();" style="height:calc(50px*<?php echo $num2 ; ?>);"></td>
													<?php
												}
											?>
										</tr>

										<?php
										$k++;
									}	
								?>

							</tr>
						</table>

						<?php
						$i++;
					}
				}
				//header ("Refresh: 60;");


			?>
		</div>
	</body>
</html>