<?php
  switch($Etape)
  {
    case 0:
      if($resultat = VehiculesLocation::getAll())
      {
        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='1'>\n";
        echo "   <select name='NoChasis'>\n";
        foreach($resultat as $ligne)
        {
            if($ligne2 = Vehicule::getVehiculeByNoChassis(addslashes($ligne['NoChasis'])))
            {
              echo " <option value='".$ligne2['NoChasis']."'>" . str_replace(" ","&nbsp;",taille_text($ligne2['Model'], 10, 0)) . " " . str_replace(" ","&nbsp;",taille_text($ligne['NoPlaque'], 10, 0)) . " " . taille_text($ligne['NoChasis'], 20, 0) ."</option>\n";
            }
        }
        echo "  </select><BR>\n";
        echo "   <button type='submit'>Suivant</button>\n";
        echo "  </form>\n";
      }
    break;
    case 1:
        if($ligne = VehiculesLocation::getVehiculeLocationByNoChassis(addslashes($NoChasis)))
        {
          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='2'>\n";
          echo "   <input type='hidden' name='NoChasis' value='$NoChasis'>\n";
          echo "<TABLE border=1>\n";
          echo "<TR><TH>No chasis</TH><TD>$NoChasis</TD></TR>\n";
            if($ligne2 = Vehicule::getVehiculeByNoChassis(addslashes($NoChasis)))
            {
              echo "<TR><TH>Model</TH><TD>" . $ligne2['Model'] . "</TD></TR>\n";
              echo "<TR><TH>Couleur</TH><TD>" . $ligne2['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'>";
          $resultat2 = CatLoc::getCatloc();
          $array_cat = explode(",",$ligne['CategorieMulti']);
          foreach($resultat2 as $ligne2)
          {
            echo "<OPTION value='".$ligne2['NoCatLoc']."'";
            if(in_array($ligne2['NoCatLoc'],$array_cat))
			{
            //if($ligne['Categorie']==$ligne2['NoCatLoc'])
              echo " selected";
			}
            echo ">".$ligne2['Description']."</OPTION>";
          }
          echo "</SELECT></TD></TR>\n";
          echo "<TR><TH>Type vehicule</TH><TD><SELECT name='TypeVhc'>";
          echo "<OPTION value='0'";
          if($ligne['Categorie']==0)
	      {
            echo " selected";
		  }
          echo ">Scooter</OPTION>";
          echo "<OPTION value='1'";
          if($ligne['Categorie']==1)
	      {
            echo " selected";
		  }
          echo ">Moto non carénée</OPTION>";
          echo "<OPTION value='2'";
          if($ligne['Categorie']==2)
	      {
            echo " selected";
		  }
          echo ">Moto carénée</OPTION>";
          echo "</SELECT></TD></TR>\n";
          echo "<TR><TH>Etat vhc</TH><TD><textarea cols='50' rows='4' name='EtatVhc'>".$ligne['EtatVhc']."</textarea></TD></TR>\n";
          echo "<TR><TH>Km actuel</TH><TD><input type='text' name='KmActuel' value='".$ligne['KmActuel']."'></TD></TR>\n";
          echo "<TR><TH>Caution</TH><TD><input type='text' name='Caution' value='".$ligne['Caution']."'></TD></TR>\n";
          echo "<TR><TH>Article Location</TH><TD><input type='text' name='ArticleLocation' value='".$ligne['ArticleLocation']."' 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='".$ligne['ArticleKm']."' 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>Prix km sup.</TD></TR>\n";
          echo "<TR><TH>Prix 1-2 jours</TH><TD><input type='text' name='Prix1_2' value='".$ligne['Prix1_2']."'></TD><TD><input type='text' name='Kil1_2' value='".$ligne['Kil1_2']."'></TD><TD><input type='text' name='Sup1_2' value='".$ligne['Sup1_2']."'></TD></TR>\n";
          echo "<TR><TH>Prix 3-6 jours</TH><TD><input type='text' name='Prix3_5' value='".$ligne['Prix3_5']."'></TD><TD><input type='text' name='Kil3_5' value='".$ligne['Kil3_5']."'></TD><TD><input type='text' name='Sup3_5' value='".$ligne['Sup3_5']."'></TD></TR>\n";
          echo "<TR><TH>Prix 7-12 jours</TH><TD><input type='text' name='Prix6_9' value='".$ligne['Prix6_9']."'></TD><TD><input type='text' name='Kil6_9' value='".$ligne['Kil6_9']."'></TD><TD><input type='text' name='Sup6_9' value='".$ligne['Sup6_9']."'></TD></TR>\n";
          echo "<TR><TH>Prix 13 + jours</TH><TD><input type='text' name='Prix10_12' value='".$ligne['Prix10_12']."'></TD><TD><input type='text' name='Kil10_12' value='".$ligne['Kil10_12']."'></TD><TD><input type='text' name='Sup10_12' value='".$ligne['Sup10_12']."'></TD></TR>\n";
//          echo "<TR><TH>Prix 13-15 jours</TH><TD><input type='text' name='Prix13_15' value='".$ligne['Prix13_15']."'></TD><TD><input type='text' name='Kil13_15' value='".$ligne['Kil13_15']."'></TD><TD><input type='text' name='Sup13_15' value='".$ligne['Sup13_15']."'></TD></TR>\n";
          echo "<TR><TH>Prix mois</TH><TD><input type='text' name='PrixMois' value='".$ligne['PrixMois']."'></TD><TD><input type='text' name='KilMois' value='".$ligne['KilMois']."'></TD><TD><input type='text' name='SupMois' value='".$ligne['SupMois']."'></TD></TR>\n";
          echo "<TR><TH>Prix Weekend 1</TH><TD><input type='text' name='PrixWE1' value='".$ligne['PrixWE1']."'></TD><TD><input type='text' name='KilWE1' value='".$ligne['KilWE1']."'></TD><TD><input type='text' name='SupWE1' value='".$ligne['SupWE1']."'></TD></TR>\n";
          echo "<TR><TH>Prix Weekend 2</TH><TD><input type='text' name='PrixWE2' value='".$ligne['PrixWE2']."'></TD><TD><input type='text' name='KilWE2' value='".$ligne['KilWE2']."'></TD><TD><input type='text' name='SupWE2' value='".$ligne['SupWE2']."'></TD></TR>\n";
          echo "<TR><TH>Prix Weekend 3</TH><TD><input type='text' name='PrixWE3' value='".$ligne['PrixWE3']."'></TD><TD><input type='text' name='KilWE3' value='".$ligne['KilWE3']."'></TD><TD><input type='text' name='SupWE3' value='".$ligne['SupWE3']."'></TD></TR>\n";
          echo "</TABLE>\n";
          echo "   <button type='submit'>Terminer</button>\n";
          echo "  </form>\n";
        }
    break;
    case 2:
        $postdata = file_get_contents("php://input");
        $pos1 = strpos($postdata, "Categorie%5B%5D=");
        if ($pos1 !== FALSE)
        {
        $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 = "UPDATE vehiculeslocation".ID_GARAGE." SET Categorie='$TypeVhc', CategorieMulti='" . implode(",", $Categorie) . "', EtatVhc='$EtatVhc', KmActuel='$KmActuel', Caution='$Caution', " .
                    "Prix1_2='$Prix1_2', Kil1_2='$Kil1_2', Sup1_2='$Sup1_2', Prix3_5='$Prix3_5', Kil3_5='$Kil3_5', Sup3_5='$Sup3_5', Prix6_9='$Prix6_9', Kil6_9='$Kil6_9', Sup6_9='$Sup6_9', " .
                    "Prix10_12='$Prix10_12', Kil10_12='$Kil10_12', Sup10_12='$Sup10_12', Prix13_15='$Prix13_15', Kil13_15='$Kil13_15', Sup13_15='$Sup13_15', " .
                    "PrixMois='$PrixMois', KilMois='$KilMois', SupMois='$SupMois', PrixWE1='$PrixWE1', KilWE1='$KilWE1', SupWE1='$SupWE1', " .
                    "PrixWE2='$PrixWE2', KilWE2='$KilWE2', SupWE2='$SupWE2', PrixWE3='$PrixWE3', KilWE3='$KilWE3', SupWE3='$SupWE3', ArticleLocation='$ArticleLocation', ArticleKm='$ArticleKm', NoPlaque='".addslashes($NoPlaque)."' " .
                    "WHERE NoChasis='".addslashes($NoChasis)."'";*/

                $updateData = array(
                        'table' => 'vehiculeslocation' . ID_GARAGE,
                        'set' => array(
                            'Categorie' => $TypeVhc,
                            'CategorieMulti' => implode(",", $Categorie),
                            'EtatVhc' => $EtatVhc,
                            'KmActuel' => $KmActuel,
                            'Caution' => $Caution,
                            'Prix1_2' => $Prix1_2,
                            'Kil1_2' => $Kil1_2,
                            'Sup1_2' => $Sup1_2,
                            'Prix3_5' => $Prix3_5,
                            'Kil3_5' => $Kil3_5,
                            'Sup3_5' => $Sup3_5,
                            'Prix6_9' => $Prix6_9,
                            'Kil6_9' => $Kil6_9,
                            'Prix10_12' => $Prix10_12,
                            'Kil10_12' => $Kil10_12,
                            'Sup10_12' => $Sup10_12,
                            'Prix13_15' => $Prix13_15,
                            'Kil13_15' => $Kil13_15,
                            'Sup13_15' => $Sup13_15,
                            'PrixMois' => $PrixMois,
                            'KilMois' => $KilMois,
                            'SupMois' => $SupMois,
                            'PrixWE1' => $PrixWE1,
                            'KilWE1' => $KilWE1,
                            'SupWE1' => $SupWE1,
                            'PrixWE2' => $PrixWE2,
                            'SupWE2' => $SupWE2,
                            'PrixWE3' => $PrixWE3,
                            'KilWE3' => $KilWE3,
                            'SupWE3' => $SupWE3,
                            'ArticleLocation' => $ArticleLocation,
                            'ArticleKm' => $ArticleKm,
                            'NoPlaque' => addslashes($NoPlaque)
                        ),
                        'where' => array(
                            'NoChasis' => addslashes($NoChasis)
                        )
                    );
                    
                    // Build the SQL query
                    $sql = "UPDATE " . $updateData['table'] . " SET ";
                    
                    foreach ($updateData['set'] as $key => $value) {
                      if(!empty($value)) {
                        $sql .= "$key='$value', ";
                      }
                    }
                    
                    $sql = rtrim($sql, ', '); // Remove the trailing comma
                    
                    $sql .= " WHERE ";
                    
                    foreach ($updateData['where'] as $key => $value) {
                        $sql .= "$key='$value' AND ";
                    }
                    
                    $sql = rtrim($sql, ' AND '); // Remove the trailing "AND"
                    
                    // Now $sql contains your dynamically generated SQL query
                    //echo $sql; // Use this for testing or executing the SQL query in your application
                    
                //echo $requette;
                if($resultat = Connection::executeRawQuery($sql))
              {
              }
              else
              {
                echo "KO";
        }
          break;
          }
  
?>
