<?php
/* Démarrage ou prolongation de la session */
session_start();

?>

<?php

function ajout($select)
{
    array_push($_SESSION['panier']['id'],$select['id']);
    array_push($_SESSION['panier']['qte'],$select['qte']);
    array_push($_SESSION['panier']['designation'],$select['designation']);
    array_push($_SESSION['panier']['prix'],$select['prix']);
} 


?>


<?php

 	
 	/* On vérifie l'existence du panier, sinon, on le crée */
	if(!isset($_SESSION['panier']))
	{
    		/* Initialisation du panier */
    		$_SESSION['panier'] = array();
    		/* Subdivision du panier */
    		$_SESSION['panier']['id'] = array();
    		$_SESSION['panier']['qte'] = array();
    		$_SESSION['panier']['designation'] = array();
    		$_SESSION['panier']['prix'] = array();
	}
 	
		  
		   $selection = $_GET[action]; 
		  
		   if($selection  == "purge")	
		      {   echo "purge";	
		    	  unset($_SESSION['panier']);
	    		  $display = "0";
		     }	
			     	
		   if($selection == "efface")
		     {   echo "efface";	
		         $indice =  $_GET[indice];  
		         array_splice($_SESSION['panier']['id'], $indice, 1);
			 array_splice($_SESSION['panier']['designation'], $indice, 1);
			 array_splice($_SESSION['panier']['qte'], $indice, 1);
			 array_splice($_SESSION['panier']['prix'], $indice, 1);
			 print_r($_SESSION['panier']);   
			 $nb_articles = count($_SESSION['panier']['id']);
			 //echo "Nombres articles:" .$nb_articles;
			 if($nb_articles == 0)
			   {  $display = "0";
			   }
			 else {
			        $display = "1";
			      }    
		     }				     	
			     	
		   if($selection == "add")	
		      {	  echo "ajout";	 
		      	  // verifier si l'article est deja dans le panier
		      	   if(in_array($_POST['id'], $_SESSION['panier']['id']))
                             { // oui - elle existe
	 	                 $position = array_search($_POST['id'], $_SESSION['panier']['id']);
	 	                 echo "t" .$position ."t";
	 	                 // modifie qte
	 	                 $_SESSION['panier']['qte'][$position] = $_SESSION['panier']['qte'][$position] + $_POST['qte'];
	 	                 print_r($_SESSION['panier']); 
		      	  	 $display = "1";
	 	              } 
		      	  else { 
		         	 $select['id'] = $_POST['id'];
	      	          	 $select['qte'] = $_POST['qte']; 
			  	 $select['designation'] = $_POST['designation'];	
			  	 $select['prix'] = $_POST['prix'];
			  	 ajout($select); 
			  	 var_dump($_SESSION['panier']);
			  	 $display = "1";
			  	 //print_r($_SESSION['panier']); 
			      }	  
		    
	 	     }     
	 	               
	 	      if($_POST[rafraichir] == "rafraichir")	
		      {   echo "rafraichir";
			  //var_dump($_SESSION['panier']);	
			  //$nb_articles = count($_SESSION['panier']['id']);
			  //echo "*****" .$nb_articles ."******";
			  for($i=0;$i<count($_SESSION['panier']['id']);$i++)
			     {	$pos = "qte".$i;
				$_SESSION['panier']['qte'][$i] = $_POST[$pos];
			     }
			   $display = "1";
			   //$nb_articles = count($_SESSION['panier']['id']);
			   // echo "*****" .$nb_articles ."******";
			   var_dump($_SESSION['panier']);
		       }
	        	
	        	
	        if($display == "1")
	          {	
			echo "	
			<form name=\"form1\" method=\"post\" action=\"new_panier.php\" >
			<h3>Votre panier</h3>
			<table border=\"1\"  >
			<tr>
			<th>Code Article</th>
			<th>Désignation</th>
			<th>Quantité</th>
			<th>Prix unitaire</th>
			<th>Prix total</th>  ";
	
			$total=0;
		
			for($i=0;$i<count($_SESSION['panier']['id']);$i++)
		   	{
		   		$val = $_SESSION['panier']['qte'][$i];
		   		$nom = "qte".$i;
			      	echo "<tr><td>",$_SESSION['panier']['id'][$i],"</td>
			      	<td>",$_SESSION['panier']['designation'][$i],"</td>
			      	
			      	<td>
			      	<input type=\"text\" name=\"$nom\" size=\"2\" maxlength=\"2\" value= $val  />
			      	<a href=\"new_panier.php\"></a>
			        <img src=\"ajoutplus_encartPanier.gif\" alt=\"Ajouter\" width=\"12\" border=\"0\" height=\"12\" />
	   			<a href=\"new_panier.php\"></a>
	   			<img src=\"ajoutmoins_encartPanier.gif\" alt=\"Ajouter\" width=\"12\" border=\"0\" height=\"12\" />		
			        </td>
			      	
			      	<td align=\"right\">",$_SESSION['panier']['prix'][$i],"</td>
			      	<td align=\"right\"> ", $_SESSION['panier']['prix'][$i]*$_SESSION['panier']['qte'][$i], "</td>
			      	<td><a href=\"new_panier.php?action=efface&indice=$i\">Enlever du panier</a></td></tr>";
			      	$total+=$_SESSION['panier']['prix'][$i]*$_SESSION['panier']['qte'][$i];	 
				
		   	}
	
		   	echo "<tr><td colspan=\"4\"><b>Prix total T.T.C</b></td><td align=\"right\"><b>$total</b></td></tr>";
		   	echo "</table>";
		   	/*echo "  <td><a href=\"new_panier.php?action=purge\">Vider le panier</a></td> ";*/
		   	/*echo "  <td><a href=\"new_panier.php?action=rafraichir\">Mise à jour</a></td> ";*/
		   	 
		   	
		   	echo " <input type=\"submit\" name=\"rafraichir\"  value=\"rafraichir\" /> ";
		   	echo "  <td><a href=\"new_panier.php?action=purge\">Vider le panier</a></td> ";
		   	echo "  <td><a href=\"../Commande/identification_commande.php\">Commander</a></td> ";
		   	//echo " <input type=\"submit\" name=\"purge\"  value=\"purge\" /> ";
		   	
		   	echo "</form>";
		   }	
		   	
	 	   if($display == "0")
	             { echo " Le panier est vide";
	             }	
	 		
	 		echo "  <td><a href=\"generateur.php\">Catalogue</a></td> ";
	 		
/*	<img src=\"ajoutplus_encartPanier.gif\" alt=\"Ajouter\" width=\"12\" border=\"0\" height=\"12\" />
	   	<img src=\"ajoutmoins_encartPanier.gif\" alt=\"Ajouter\" width=\"12\" border=\"0\" height=\"12\" />
*/			   

?> 