<?php
  switch($Etape)
  {
    case 0:
      echo "  <form name='info' method='post'>\n";
      echo "   <input type='hidden' name='Type' value=$Type>\n";
      echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
      echo "   <input type='hidden' name='Etape' value='1'>\n";
      echo "<TABLE border=1>\n";
	  $resultat = ChassisModel::getAll();
      echo "   <TR><TH>Select</TH><TH>Model</TH><TH>Chassis</TH></TR>";
	  foreach ($resultat as $ligne)
	  {
		echo "<TR><TD><input type=\"checkbox\" name=\"".$ligne["Chassis"]."\" /></TD><TD>".$ligne["Model"]."</TD><TD>".$ligne["Chassis"]."</TD></TR>";
	  }
      echo "</TABLE>\n";
      echo "   <button type='submit'>Afficher</button>\n";
      echo "  </form>\n";  
    break;
    case 1:
	  $requette = "SELECT * FROM chassis_model ORDER BY Model";
	  $resultat = ChassisModel::getAll();
	  $first = true;
	  $requette = "SELECT c.*, v.* FROM vehicules AS v, clients AS c WHERE v.Client = c.NoClient AND (";
	  foreach ($resultat as $ligne)
	  {
		if (!empty($_POST[$ligne["Chassis"]]))
		{
		  if (!$first)
		  {
			$requette .= " OR ";  
		  }
		  $first = false;
		  //echo $ligne["Chassis"]."<BR/>";
		  $requette .= "v.NoChasis LIKE '".$ligne["Chassis"]."%'";
		}
	  }
	  $requette .= ")";
	  //echo $requette;
	  $resultat = Connection::executeRawQueryWithfetchAll($requette);
      echo "<TABLE border=1>\n";
      echo "   <TR><TH>No chassis</TH><TH>Model</TH><TH>Mise en circulation</TH><TH>Dernière facture vhc</TH><TH>Allocution</TH><TH>Nom</TH><TH>Prenom</TH><TH>Adresse</TH><TH>NPA</TH><TH>Localite</TH><TH>Email</TH><TH>Mobile</TH><TH>Telephone privé</TH><TH>Telephone pro</TH><TH>Dernière facture client</TH><TH>CA Total</TH></TR>";
	  foreach ($resultat as $ligne)
	  {
		if ($maxCommande = Commande::getMaxCommandByChassis($ligne["NoChasis"]))
		{
			$DateFactVhc = substr($maxCommande,8,2).".".substr($maxCommande,5,2).".".substr($maxCommande,0,4);
		}
		else
		{
			$DateFactVhc = "";
		}
		if ($max = Commande::getMaxCommandByNoClient($ligne["NoClient"]))
		{
			$DateFactClient = substr($max,8,2).".".substr($max,5,2).".".substr($max,0,4);
		}
		else
		{
			$DateFactClient = "";
		}
		$Total = Commande::getSumCommandByNoClient($ligne["NoClient"]);
		echo "  <TR><TD>".$ligne["NoChasis"]."</TD><TD>".$ligne["Model"]."</TD><TD>".substr($ligne["DateCircul"],8,2).".".substr($ligne["DateCircul"],5,2).".".substr($ligne["DateCircul"],0,4)."</TD><TD>".$DateFactVhc."</TD><TD>".$ligne["Allocution"]."</TD><TD>".$ligne["Nom"]."</TD><TD>".$ligne["Prenom"]."</TD><TD>".$ligne["Adresse"]."</TD><TD>".$ligne["NPA"]."</TD><TD>".$ligne["Localite"]."</TD><TD>".$ligne["Email"]."</TD><TD>".$ligne["Natel"]."</TD><TD>".$ligne["TelPriv"]."</TD><TD>".$ligne["TelProf"]."</TD><TD>".$DateFactClient."</TD><TD>".$Total."</TD></TR>";  
	  }
      echo "</TABLE>\n";
    break;
  }
?>