<?php
  switch($Etape)
  {
    case 0:
      Create_Recherche_Form(2,$NoVisu);
    break;
    case 1:
      Create_Recherche_Form(2,$NoVisu);
      Affiche_Resultat_Recherche_Form(2,$NoVisu);
    break;
    case 2:
      echo "  <form name='info'>\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='3'>\n";
      echo "   <input type='hidden' name='NoChasis' value='$NoChasis'>\n";
      echo "<TABLE border=1>\n";
      echo "<TR><TH>No châssis</TH><TD>$NoChasis</TD></TR>\n";
        if($ligne = Vehicule::getVehiculeByNoChassis(addslashes($NoChasis)))
        {
          echo "<TR><TH>Model</TH><TD>" . $ligne['Model'] . "</TD></TR>\n";
          echo "<TR><TH>Couleur</TH><TD>" . $ligne['Couleur'] . "</TD></TR>\n";
          echo "<TR><TH>NoPlaque</TH><TD><input type='text' name='NoPlaque' value='" . $ligne['NoPlaque'] . "'></TD></TR>\n";
        }
      echo "<TR><TH>Categorie</TH><TD><SELECT name='Categorie[]' multiple='multiple'>";
      $requette2 = "SELECT * FROM catloc WHERE NoCatLoc > 0 ORDER BY Position";
      $resultat2 = CatLoc::getCatloc();
      foreach($resultat2 as $ligne2)
      {
        echo "<OPTION value='".$ligne2['NoCatLoc']."'>".$ligne2['Description']."</OPTION>";
      }
      echo "</SELECT></TD></TR>\n";
	  echo "<TR><TH>Type</TH><TD><SELECT name='TypeVhc'><OPTION value='0'>Scooter</OPTION><OPTION value='1'>Moto non carénée</OPTION><OPTION value='2'>Moto carénée</OPTION></SELECT></TD></TR>";
      echo "<TR><TH>Etat vhc</TH><TD><textarea cols='50' rows='4' name='EtatVhc'></textarea></TD></TR>\n";
      echo "<TR><TH>Km actuel</TH><TD><input type='text' name='KmActuel' value='".$ligne2['Km']."'></TD></TR>\n";
      echo "<TR><TH>Caution</TH><TD><input type='text' name='Caution' value=''></TD></TR>\n";
      echo "<TR><TH>Article Location</TH><TD><input type='text' name='ArticleLocation' value='' readonly><A HREF='find.php?Type=11&Detail=0&Cible=ArticleLocation' target='recherche'>Rechercher</A></TD></TR>\n";
      echo "<TR><TH>Article km supplémentaires</TH><TD><input type='text' name='ArticleKm' value='' readonly><A HREF='find.php?Type=11&Detail=0&Cible=ArticleKm' target='recherche'>Rechercher</A></TD></TR>\n";
      echo "</TABLE>\n";
      echo "<TABLE border=1>\n";
      echo "<TR></TR><TD>&nbsp;</TD><TD>Prix unitaire</TD><TD>km max</TD><TD>km sup.</TD></TR>\n";
      echo "<TR><TH>Prix 1-2 jours</TH><TD><input type='text' name='Prix1_2' value='0'></TD><TD><input type='text' name='Kil1_2' value='0'></TD><TD><input type='text' name='Sup1_2' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix 3-6 jours</TH><TD><input type='text' name='Prix3_5' value='0'></TD><TD><input type='text' name='Kil3_5' value='0'></TD><TD><input type='text' name='Sup3_5' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix 7-12 jours</TH><TD><input type='text' name='Prix6_9' value='0'></TD><TD><input type='text' name='Kil6_9' value='0'></TD><TD><input type='text' name='Sup6_9' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix 13 + jours</TH><TD><input type='text' name='Prix10_12' value='0'></TD><TD><input type='text' name='Kil10_12' value='0'></TD><TD><input type='text' name='Sup10_12' value='0'></TD></TR>\n";
//      echo "<TR><TH>Prix 13-15 jours</TH><TD><input type='text' name='Prix13_15' value='0'></TD><TD><input type='text' name='Kil13_15' value='0'></TD><TD><input type='text' name='Sup13_15' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix mois</TH><TD><input type='text' name='PrixMois' value='0'></TD><TD><input type='text' name='KilMois' value='0'></TD><TD><input type='text' name='SupMois' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix Weekend 1</TH><TD><input type='text' name='PrixWE1' value='0'></TD><TD><input type='text' name='KilWE1' value='0'></TD><TD><input type='text' name='SupWE1' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix Weekend 2</TH><TD><input type='text' name='PrixWE2' value='0'></TD><TD><input type='text' name='KilWE2' value='0'></TD><TD><input type='text' name='SupWE2' value='0'></TD></TR>\n";
      echo "<TR><TH>Prix Weekend 3</TH><TD><input type='text' name='PrixWE3' value='0'></TD><TD><input type='text' name='KilWE3' value='0'></TD><TD><input type='text' name='SupWE3' value='0'></TD></TR>\n";
      echo "</TABLE>\n";
      echo "   <button type='submit'>Terminer</button>\n";
      echo "  </form>\n";
    break;
    case 3:
  $postdata = file_get_contents("php://input");
  $pos1 = strpos($postdata, "Categorie%5B%5D=");
	$Categorie = array();
  while ($pos1 !== FALSE)
  {
    $pos2 = strpos($postdata, "&", $pos1);
    if ($pos2 !== FALSE)
    {
      $tmp_no = substr($postdata, $pos1 + 16, $pos2 - $pos1 - 16);
    }
    else
    {
      $tmp_no = substr($postdata, $pos1 + 16);
    }
    array_push($Categorie, $tmp_no);
    $pos1 = strpos($postdata, "Categorie%5B%5D=", $pos1 + 16);
  }
     /* $requette = "INSERT INTO vehiculeslocation".ID_GARAGE." " .
                  "(NoChasis, Categorie, CategorieMulti, Prix1_2, Prix3_5, Prix6_9, Prix10_12, Prix13_15, PrixMois, PrixWE1, PrixWE2, PrixWE3, EtatVhc, Kil1_2, Kil3_5, Kil6_9, Kil10_12, Kil13_15, KilMois, KilWE1, KilWE2, KilWE3," .
                  " Sup1_2, Sup3_5, Sup6_9, Sup10_12, Sup13_15, SupMois, SupWE1, SupWE2, SupWE3, Caution, KmActuel, ArticleLocation, ArticleKm, NoPlaque)" .
                  " VALUES " .
                  "('" . addslashes($NoChasis) . "', '$TypeVhc', '".implode(",", $Categorie)."', '$Prix1_2', '$Prix3_5', '$Prix6_9', '$Prix10_12', '$Prix13_15', '$PrixMois', '$PrixWE1', '$PrixWE2', '$PrixWE3', '$EtatVhc', '$Kil1_2', '$Kil3_5', '$Kil6_9', '$Kil10_12', '$Kil13_15', '$KilMois', '$KilWE1', '$KilWE2', '$KilWE3', ".
                  "'$Sup1_2', '$Sup3_5', '$Sup6_9', '$Sup10_12', '$Sup13_15', '$SupMois', '$SupWE1', '$SupWE2', '$SupWE3', '$Caution', '$KmActuel', '$ArticleLocation', '$ArticleKm', '" . addslashes($NoPlaque) . "')";*/

                  
                  // Your original data
                  $data = array(
                      'NoChasis' => addslashes($NoChasis),
                      'Categorie' => $TypeVhc,
                      'CategorieMulti' => implode(",", $Categorie),
                      'Prix1_2' => $Prix1_2,
                      'Prix3_5' => $Prix3_5,
                      'Prix6_9' => $Prix6_9,
                      'Prix10_12' => $Prix10_12,
                      'Prix13_15' => $Prix13_15,
                      'PrixMois' => $PrixMois,
                      'PrixWE1' => $PrixWE1,
                      'PrixWE2' => $PrixWE2,
                      'PrixWE3' => $PrixWE3,
                      'EtatVhc' => $EtatVhc,
                      'Kil1_2' => $Kil1_2,
                      'Kil3_5' => $Kil3_5,
                      'Kil6_9' => $Kil6_9,
                      'Kil10_12' => $Kil10_12,
                      'Kil13_15' => $Kil13_15,
                      'KilMois' => $KilMois,
                      'KilWE1' => $KilWE1,
                      'KilWE2' => $KilWE2,
                      'KilWE3' => $KilWE3,
                      'Sup1_2' => $Sup1_2,
                      'Sup3_5' => $Sup3_5,
                      'Sup6_9' => $Sup6_9,
                      'Sup10_12' => $Sup10_12,
                      'Sup13_15' => $Sup13_15,
                      'SupMois' => $SupMois,
                      'SupWE1' => $SupWE1,
                      'SupWE2' => $SupWE2,
                      'SupWE3' => $SupWE3,
                      'Caution' => $Caution,
                      'KmActuel' => $KmActuel,
                      'ArticleLocation' => $ArticleLocation,
                      'ArticleKm' => $ArticleKm,
                      'NoPlaque' => addslashes($NoPlaque)
                  );
                  
                  // Build the placeholders for columns and values
                  $columns = implode(', ', array_keys($data));
                  $values = ':' . implode(', :', array_keys($data));
                  
                  // Prepare the SQL statement
                  $sql = "INSERT INTO vehiculeslocation" . ID_GARAGE . " ($columns) VALUES ($values)";
                  $statement = Connection::getStatement($sql);
                  
                  // Bind parameters
                  foreach ($data as $key => &$value) {
                      if(empty($value)) {
                        $value = null;
                      }
                      $statement->bindParam(":$key", $value);
                  }
                  
                  // Execute the query
                  
                              
      if ($statement->execute())
      {
      }
      else
      {
        echo "KO???";
      }
    break;
  }
?>
