<?php
require 'database.inc.php';
if (Test_User($lg_user, $lg_password) == TRUE) {
?>
  <html>

  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8'>
    <SCRIPT language=JavaScript src='valideformulaire.js' type=text/javascript></SCRIPT>
    <SCRIPT language=JavaScript>
      this.window.resizeTo(800, 600);
    </SCRIPT>
    <title>WebBike V<?php echo VERSION; ?></title>
    <?php
    $size = 2;
    if ($ligne = Poste::getPostesByIP()) {
      $size = $ligne['Size'];
    }
    echo "<link href='css/style$size.css' rel='stylesheet' media='screen'>\n";

    echo "<link href='" . CSS_FOLDER . "style.css' rel='stylesheet' media='screen'>\n"
    ?>
  </head>

  <body>
    <?php
    if (!isset($Etape)) {
      $Etape = 0;
    }
    if (!isset($NoVisu)) {
      $NoVisu = 0;
    }
    switch ($Etape) {
      case 0:
        echo "  <form name='info'>\n";
        echo "   <input type='hidden' name='Etape' value='1'>\n";
        echo "   <input type='hidden' name='NoClient' value='$NoClient'>\n";
        echo "   <input type='hidden' name='NoVisu' value='$NoVisu'>\n";
        echo "   <input type='hidden' name='Mode' value='$Mode'>\n";
        Create_Recherche(2, $NoVisu);
        echo "   <button type='submit'>Rechercher</button>\n";
        echo "  </form>\n";
        break;
      case 1:
        echo "  <form name='info'>\n";
        echo "   <input type='hidden' name='Etape' value='2'>\n";
        echo "   <input type='hidden' name='NoVisu' value='$NoVisu'>\n";
        echo "   <input type='hidden' name='Mode' value='$Mode'>\n";
        echo "   <input type='hidden' name='NoClient' value='$NoClient'>\n";
        $nb_results = Affiche_Resultat_Recherche(2, 10, $NoVisu);
        if ($nb_results != 0) {
          echo "   <button type='submit'>Transferer</button><button type='button' onClick=\"document.info.Etape.value = '3';document.info.submit();\">Attribuer</button>\n";
        }
        echo "  </form>\n";
    ?>
        <form action="recherche_vhc_create.php">
          <input type="hidden" name="NoClient" value="<?php echo $NoClient; ?>" />
          <input type="hidden" name="Mode" value="<?php echo $Mode; ?>" />
          <BUTTON type='submit'>Nouveau véhicule</BUTTON>
        </form>
        <form action="recherche_vhc_find.php">
          <input type="hidden" name="NoClient" value="<?php echo $NoClient; ?>" />
          <input type="hidden" name="Mode" value="<?php echo $Mode; ?>" />
          <BUTTON type='submit'>Rechercher véhicule</BUTTON>
        </form>
    <?php
        echo "<SCRIPT language=JavaScript>\n";
        echo "document.info.NoChasis.focus();\n";
        echo "this.document.onkeydown = formkeydown;\n";
        echo "actif = 0;\n";
        echo "</SCRIPT>\n";
        break;
      case 2:
        if ($ligne = Vehicule::getVehiculeByNoChassis($NoChasis)) {
          $result = Affecte_Resultat_Vehicule($ligne, $Mode);
          if ($Mode == 0) {
            if ($ligne2 = ChassisModel::getChassisModelByNoChassis($ligne['NoChasis'])) {
              $resultat3 = ServiceVHC::getServiceVHCByVhc($ligne2["Id"]);
              $pos = 0;
              $pos_service = 0;
              foreach ($resultat3 as $ligne3) {
                $pos++;
                switch ($ligne2['Type_Service']) {
                  case "Texte":
                    break;
                  default:
                    if (($pos_service == 0) && ($ligne['Km'] + 500 < $ligne3['Km'])) {
                      $pos_service = $pos;
                    }
                    break;
                }
              }
              echo "Service ?<br/>\n";
              echo "<button id=\"bt_oui\" onclick=\"transfert_vhc_lse();window.opener.document.forms['info'].service.selectedIndex = $pos_service;window.opener.duree_service();window.close();\">OUI</button><button onclick=\"transfert_vhc_lse();window.close();\">NON</button>\n";
              echo "<SCRIPT language=JavaScript>\n";
              echo "function transfert_vhc_lse() {\n";
              echo "window.opener.local_eval(\"$result\");\n";
              echo "}\n";
              echo "var bt_oui = document.getElementById(\"bt_oui\");\n";
              echo "bt_oui.focus();\n";
              echo "</SCRIPT>\n";
            } else {
              echo "<SCRIPT language=JavaScript>\n";
              echo "window.opener.local_eval(\"$result\");\n";
              echo "window.close();\n";
              echo "</SCRIPT>\n";
            }
          } else {
            echo "<SCRIPT language=JavaScript>\n";
            echo "window.opener.local_eval(\"$result\");\n";
            echo "window.close();\n";
            echo "</SCRIPT>\n";
          }
        }
        break;
      case 3:
        $requette = "UPDATE vehicules SET Client = $NoClient WHERE NoChasis = '$NoChasis'";
        Connection::executeRawQuery($requette);
        if ($ligne = Vehicule::getVehiculeByNoChassis($NoChasis)) {
          echo "<SCRIPT language=JavaScript>\n";
          $result = Affecte_Resultat_Vehicule($ligne, $Mode);
          echo "window.opener.local_eval(\"$result\");\n";
          echo "window.close();\n";
          echo "</SCRIPT>\n";
        }
        break;
    }
    ?>
  </body>

  </html>
<?php
}
?>