<?php
// all = toutes les commandes 
// etat1 = attente validation paiement commande 
// etat2 = commande validée
// etat3 = commande en cours de préparation
// etat4 = Commande expédiée
// etat5 = commande en cours de livraison
// etat6 = commande livrée

	session_start();
	
	$_SESSION['database']="local";
	
	if($_SESSION['database']=="local")
	  {
	     $db_host    = "localhost";
	     $db_user   = "root";
	     $db_pass   = "";
     	     $db_name   = "boutique";

	 }
	
	if($_SESSION['database']=="public")
	  {
  	     $db_host    = "sql2";
   	     $db_user   = "analogde";
 	     $db_pass   = "rainbow";
    	     //$db_type   = "mysql";
	     $db_name   = "analogde";
	 }
	 
	 $connexion = mysql_connect($db_host,$db_user,$db_pass); 
   	 mysql_select_db($db_name, $connexion);
	 
	 if( isset($_POST['statut']) && !empty($_POST['statut']) ) 
	   {     $statut = $_POST['statut'];
	         if($statut == "tous")	
	           {  $requete = "SELECT * FROM commandes";
	              $result = mysql_query($requete);
                      $nb_enregistrement = mysql_num_rows($result);
		      echo $nb_enregistrement;
		      echo "tous les états";
		   }  
	      else {
	             $requete = "SELECT * FROM commandes where etat='$statut'";	
	             //echo SELECT COUNT(*) FROM commandes where etat='$statut'";
	             $result = mysql_query($requete);
	             $nb_enregistrement = mysql_num_rows($result);
	             echo $nb_enregistrement;
	             echo "-->" .$statut;
	           }  
	  }
	  
	  
	 if(isset($_GET['position'])) 
   	   { 
   	     $statut = $_GET['position'];
   	     
   	     if($statut == "tous") 
   	       { $requete = "SELECT * FROM commandes";
                 $result = mysql_query($requete);
                 $nb_enregistrement = mysql_num_rows($result);
                 echo $nb_enregistrement;
	         echo "pagination tous";
   	       }	
   	     if($statut != "tous")   
   	       { $requete = "SELECT * FROM commandes where etat='$statut'";	
   	         $result = mysql_query($requete);
                 $nb_enregistrement = mysql_num_rows($result);
                 echo "pagination choix ";
               }  
   	    /*if($pos == "etat6")
   	      {	header("Location: zgsfazou.php");
   	      	echo "$$$$" .$pos ."$$$$";
   	      }*/
	 } 
	 
	 if( !isset($_POST['statut']) && !isset($_GET['position']) )
	   { $statut = "tous";
	     $requete = "SELECT * FROM commandes";
	     $result = mysql_query($requete);
             $nb_enregistrement = mysql_num_rows($result);
             echo "tous par défaut";
	  }	
	 
	
	 
	 
 if( isset($_POST['mot']) && !empty($_POST['mot']) ) 
   {  //echo $_POST['mot'];
      $mot = $_POST['mot'];	
      //$requete = "SELECT * FROM commandes WHERE id LIKE '%$mot%'  "; 
      $requete = "SELECT * FROM commandes WHERE id = '$mot' ";
      $result = mysql_query($requete);
      $nb_enregistrement = mysql_num_rows($result); 
      echo "bilout";
   }
	 
	 
	  //$result = mysql_query($requete);
	  
	  // pour la pagignation
	  
	  
	  if($nb_enregistrement > 8)
	    { 
	  
	  	$ligne_par_page = 8;
	 
   	  	//$contenu = "SELECT COUNT(*) AS total FROM commandes" ;
	 
	  	//$res = mysql_query($contenu) OR die(mysql_error());
	 
	  	//$res = mysql_query('SELECT COUNT(*) AS total FROM $nom_table' ) OR die(mysql_error());
	  	//$row = mysql_fetch_assoc($res);
	  
	  	//$nb_enregistrement = $row['total'];
	 
	  	//echo $nb_enregistrement; 
	  	$nombre_pages=ceil($nb_enregistrement/$ligne_par_page); 
	  	//echo $nombre_pages;  	
	 
	  	if(isset($_GET['page'])) 
            	  {
     			$pageActuelle=intval($_GET['page']);
     
     			// Si la valeur de $pageActuelle (le numéro de la page) est plus grande que $nombreDePages...
     			if($pageActuelle>$nombre_pages) 
     		 	  {
          			$pageActuelle=$nombre_pages;
     		  	  }
	    	   }

		else  {
     			$pageActuelle=1; // La page actuelle est la n°1    
	      	      }

		$premiereEntree=($pageActuelle-1)*$ligne_par_page; // On calcul la première entrée à lire 

		//echo $premiereEntree;
	  }
	 
?>	 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Administration :: Patrice Delpy</title>

<link rel="stylesheet" href="default.css" type="text/css">

</head>

<body>

<div id="header"><h1>Administration</h1>
<div class="membre">Vous Ãªtes connectÃ© en tant que <b><?php echo $_SESSION['admin_identifiant']; ?> </b></div>
</div>

<?php include('menu.php'); ?>		

			
		<div class="heading">Liste des commandes 
		
		<form action="commandes.php" method="POST">
		<select name="statut" size="1" onChange="submit()">
		<option value="X">--[ SELECTION ]--</option>
		
<?php

		

		if($statut == "tous")
		  { echo "<option value=\"tous\" selected>Toutes les commandes</option>";
	          }
		else {
		    echo "<option value=\"tous\">Toutes les commandes</option>";	
		}

		if($statut == "etat1")
		  { echo "<option value=\"etat1\" selected>Attente validation paiement</option>";
	          }
		else {
		    echo "<option value=\"etat1\"  >Attente validation paiement</option>";	
		}
		
		if($statut == "etat2")
		  { echo "<option value=\"etat2\" selected>Commande validée</option>";
		  }
		else  { 
			echo "<option value=\"etat2\">Commande validée</option>";
		      } 	  
		      
		if($statut == "etat3")
		  { echo "<option value=\"etat3\" selected>Commande en cours de pr&eacute;paration</option>";
		  }
		else  { 
			echo "<option value=\"etat3\" >Commande en cours de pr&eacute;paration</option>";
		      } 	
		      
		if($statut == "etat4")
		  { echo "<option value=\"etat4\" selected>Commande expédiée</option>";
		  }
		else  { 
			echo "<option value=\"etat4\">Commande expédiée</option>";
		      } 	            

		if($statut == "etat5")
		  { echo "<option value=\"etat5\" selected>Commande en cours de livraison</option>";
		  }
		else  { 
			echo "<option value=\"etat5\">Commande en cours de livraison</option>";
		      } 
		      
		if($statut == "etat6")
		  { echo "<option value=\"etat6\" selected>Commande livr&eacute;e</option>";
		  }
		else  { 
			echo "<option value=\"etat6\">Commande livr&eacute;e</option>";
		      }       
		      
?>		      
		  
		
		
		</select>

<?php
		/*if(!isset($_POST['mot']))
		  {
		     echo "<input type=\"text\" name=\"mot\"  value=\"\" size=\"15\"> ";
		  }
		  
		if(isset($_POST['mot']))
		  {  $mot = $_POST['mot'];
		     echo " <input type=\"text\" name=\"mot\"  value=\"\" size=\"15\"> ";
		  }*/
		  
		   echo " <input type=\"text\" name=\"mot\"  value=\"\" size=\"15\"> ";
		  
?>		    

		<input type="submit" value="Rechercher" alt="Lancer la recherche!">


		</form></div>

		
		<div id="list">
		<table class="list">
	        <tr>
	        <th align="center" width="1%">&nbsp;</th>
	        <th width="*%" align="left">Etat</th>
		<th width="*%" align="left">Client</th>
		<th width="*%" align="left">Montant total</th>
		<th width="*%" align="left">Date de commande</th>
		<th width="*%" align="left">Num Commande</th>
		<th width="*%" align="left">Paiement</th>
		<th width="*%" align="left">Supprimer</th>
		
		<th width="1%">&nbsp;</th>
		</tr>
        

<?php

		//echo "+++" .$statut ."+++";

		if($nb_enregistrement > 8 && $statut == "tous" )
	          { $result=mysql_query('SELECT * FROM commandes ORDER BY id DESC LIMIT '.$premiereEntree.', '.$ligne_par_page.'');
			//echo "par ici";
		  }
		
		if($nb_enregistrement > 8 && $statut != "tous" )
	          { echo "par la";

//$result=mysql_query('SELECT * FROM commandes where etat='$statut' ); // ORDER BY id DESC LIMIT '.$premiereEntree.', '.$ligne_par_page.'');
//$query=mysql_query('SELECT * FROM commandes where etat='.$statut.' ORDER BY id DESC LIMIT '.$premiereEntree.', '.$ligne_par_page.'');
		   	
   	$requete = "SELECT * FROM commandes where etat='$statut' ORDER BY id DESC LIMIT $premiereEntree,$ligne_par_page "; 		   	

// $query = "SELECT * FROM commandes where etat='$statut.' designation LIMIT ".$premiereEntree.",".$ligne_par_page; 		   	

//echo $query = "SELECT * FROM commandes where etat LIKE '$statut' LIMIT $premiereEntree, $ligne_par_page ";

$result = mysql_query($requete); 

		   	
		   // $requete = "SELECT * FROM commandes where etat='$statut'";	
		  //  $retour_messages=mysql_query('SELECT * FROM '.$nom_table.' ORDER BY id DESC LIMIT '.$premiereEntree.', '.$ligne_par_page.'');

		  }  
		
		if($nb_enregistrement < 8  )
		  { $result = mysql_query($requete);
		  }	
			

		
//mysql_fetch_array
		//while($data = mysql_fetch_assoc($result))
		while($data = mysql_fetch_array($result))
		     {
		     	
		     	$id = $data['id'];
		     	$email = $data['email'];
		     	$client = $data['prenom']. " " .$data['nom'];
			$infos = $data['date']. " " .$data['heure'];
			$montant = $data['total_ttc'];
		     	$reglement = $data['reglement'];
		     	$etat = $data['etat'];
		     	
		     	switch($etat)
			      {
				  case "etat1" :
					          $info_etat  = "Validation du paiement en cours";
					    	  break;
				  case "etat2" :
					          $info_etat  = "Commande validée";
					    	  break;
				  case "etat3" :
					          $info_etat  = "Préparation en cours";
					    	  break;
					    	  
				  case "etat4" :
					          $info_etat  = "Commande expédiée";
					    	  break;
					    	 
				  case "etat5" :
					          $info_etat  = "Livraison en cours";
					    	  break;
					    	  
				  case "etat6" :
					          $info_etat  = "Commande livrée";
					    	  break; 	    	  
				  case "" :	    	   
				  		  $info_etat  = "pour debug";
					    	  break; 	    	  					    	  
				  		  
					}	
		     	
		     	// all = toutes les commandes 
// etat1 = attente validation paiement commande 
// etat2 = commande validée
// etat3 = commande en cours de préparation
// etat4 = Commande expédiée
// etat5 = commande en cours de livraison
// etat6 = commande livrée
		     	
		     	
		   	echo "	
		       		<tr class=\"row2\" onmouseover=\"this.className='highlight'\" onmouseout=\"this.className='row2'\">
		       		<td align=\"center\"><a href=\"visualiser_commandes.php?email=$email&id=$id\">
		       		<img src=\"view.png\" border=\"0\" alt=\"View\" title=\"Voir le detail de la commande\" /></a></td>

		       		<td align=\"left\" nowrap=\"nowrap\">$info_etat</td>
		       		<td align=\"left\" nowrap=\"nowrap\">$client</td>
		       		<td align=\"left\" nowrap=\"nowrap\">$montant</td>
		       		<td align=\"left\" nowrap=\"nowrap\">$infos</td>
		       		<td align=\"left\" nowrap=\"nowrap\">$id</td>
		       		<td align=\"left\" nowrap=\"nowrap\">$reglement</td>
		       				       		
		       		
		       		
		       		<td class=\"center\" width=\"1%\">
		       		<a href=\"dsff.php\">
		       		<img src=\"delete.png\" alt=\"Supprimer\" title=\"Supprimer\" /></a>
		       		</td>
		       		</tr>  ";
		     }

			  
?>              
             
              </table>
		</div>

<?php

		//echo "hhhh" .$statut ."hhh";

		if($nb_enregistrement > 8)
		  {
		 	echo '<p align="center">Page : '; //Pour l'affichage, on centre la liste des pages
		
		 	for($i=1; $i<=$nombre_pages; $i++) //On fait notre boucle
		           {
     		      		//On va faire notre condition
     		       		if($i==$pageActuelle) //Si il s'agit de la page actuelle...
     			 	  {
         		   	     echo ' [ '.$i.' ] '; 
     		                  }	
     		             else {
				       echo ' <a href="commandes.php?position='.$statut.'&page='.$i.'">'.$i.'</a> ';

     			  	  }
		           }
		        echo '</p>';
        	  }
	       

//       echo ' <a href="commandes.php?position='.$statut.'&page='.$i.'">'.$i.'</a> ';
              
        
?>

</body>
</html>