<?php
  switch($Etape)
  {
    case 0:
  	  if ($New == 1)
  	  {
        if ($Vhc_ref != 0)
        {
          if ($ligne = ChassisModel::getChassisModelById($Vhc_ref))
          {
      	    $requette2 = "INSERT INTO chassis_model (Chassis, Marque, Model, Cylindree, Homologation, Id_Service, Type_Service) VALUES ('$NoChassis', '".Connection::getConnection()->quote($Marque)."', '".Connection::getConnection()->quote($Model)."', '".Connection::getConnection()->quote($Cylindree)."', '".Connection::getConnection()->quote($Homologation)."', '".$ligne["Id_Service"]."', '".$ligne["Type_Service"]."')";
      	    Connection::executeRawQuery($requette2);
      	    $Id = Connection::getConnection()->lastInsertId();
            $resultat3 = ServiceVHC::getServiceVHCByID($Vhc_ref);
            foreach($resultat3 as $ligne3)
            {
			  switch ($ligne["Type_Service"])
			  {
			    case "Texte":
			      $requette4 = "INSERT INTO service_vhc (Id_vhc, Nom, Duree) VALUES ('$Id', '".$ligne3["Nom"]."', '".$ligne3["Duree"]."')";
				break;
				default:
			      $requette4 = "INSERT INTO service_vhc (Id_vhc, Km, Duree) VALUES ('$Id', '".$ligne3["Km"]."', '".$ligne3["Duree"]."')";
				break;
			  }
        Connection::executeRawQuery($requette4);
        	    $Id_service = Connection::getConnection()->lastInsertId();
              $requette5 = "SELECT service_action.* FROM service_action, action WHERE Id_Service = ".$ligne3["Id"]." AND service_action.Id_Action = action.Id ORDER BY action.Id_Rubrique, action.Position";
              $resultat5 = Connection::executeRawQueryWithfetchAll($requette5);
              foreach ($resultat5 as $ligne5)
              {
                $requette6 = "INSERT INTO service_action (Id_Service, Id_Action, Id_Action_Type) VALUES ('$Id_service', '".$ligne5["Id_Action"]."', '".$ligne5["Id_Action_Type"]."')";
          	    Connection::executeRawQuery($requette6);
              }
            }
            $requette3 = "SELECT * FROM service_article WHERE Id_Vhc = $Vhc_ref";
            $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
            foreach($resultat3 as $ligne3)
            {
              $requette4 = "INSERT INTO service_article (Id_Vhc, Id_Action, NoArticle, Quantite) VALUES ('$Id', '".$ligne3["Id_Action"]."', '".$ligne3["NoArticle"]."', '".$ligne3["Quantite"]."')";
        	    Connection::executeRawQuery($requette4);
            }
            $requette3 = "SELECT * FROM vhc_action WHERE Id_Vhc = $Vhc_ref";
            $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
            foreach($resultat3 as $ligne3)
            {
              $requette4 = "INSERT INTO vhc_action (Id_vhc, Id_action) VALUES ('$Id', '".$ligne3["Id_action"]."')";
        	    Connection::executeRawQuery($requette4);
            }
			
            $requette3 = "SELECT * FROM vhc_option WHERE Id_vhc = $Vhc_ref";
            $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
            foreach($resultat3 as $ligne3)
            {
			  $requette4 = "INSERT INTO vhc_option (Id_vhc, Id_option, Duree) VALUES ('$Id', '".$ligne3["Id_option"]."', '".$ligne3["Duree"]."')";
        Connection::executeRawQuery($requette4);
            }
            $requette3 = "SELECT * FROM vhc_option_action WHERE Id_vhc = $Vhc_ref";
            $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
            foreach($resultat3 as $ligne3)
            {
              $requette4 = "INSERT INTO vhc_option_action (Id_vhc, Id_option, Id_action) VALUES ('$Id', '".$ligne3["Id_option"]."', '".$ligne3["Id_action"]."')";
              Connection::executeRawQuery($requette4);
            }
            $requette3 = "SELECT * FROM service_option_article WHERE Id_Vhc = $Vhc_ref";
            $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
            foreach($resultat3 as $ligne3)
            {
		      $requette4 = "INSERT INTO service_option_article (Id_Vhc, Id_Action, NoArticle, Quantite) VALUES ('$Id', '".$ligne3["Id_Action"]."', '".$ligne3["NoArticle"]."', '".$ligne3["Quantite"]."')";
          Connection::executeRawQuery($requette4);
            }
            $requette3 = "SELECT * FROM vhc_option_action_remplacement WHERE Id_vhc = $Vhc_ref";
            $resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
            foreach($resultat3 as $ligne3)
            {
		      $requette4 = "INSERT INTO vhc_option_action_remplacement (Id_vhc, Id_option, Id_action) VALUES ('$Id', '".$ligne3["Id_option"]."', '".$ligne3["Id_action"]."')";
          Connection::executeRawQuery($requette4);
            }
          }
        }
        else
        {
      		if($ligne=ServiceKM::getByID($Km_service))
      		{
				$requette2 = "INSERT INTO chassis_model (Chassis, Marque, Model, Cylindree, Homologation, Id_Service, Type_Service) VALUES ('$NoChassis', '".Connection::getConnection()->quote($Marque)."', '".Connection::getConnection()->quote($Model)."', '".Connection::getConnection()->quote($Cylindree)."', '".Connection::getConnection()->quote($Homologation)."', '$Km_service', '".$ligne['Type']."')";
				Connection::executeRawQuery($requette2);
				$Id = Connection::getConnection()->lastInsertId();
				switch ($ligne["Type"])
				{
					case "Texte":
						$types = explode(";",$ligne["Km"]);
						foreach ($types as $type)
						{
							$requette = "INSERT INTO service_vhc (Id_vhc, Nom, Duree) VALUES ('$Id', '$type', '0')";
							Connection::executeRawQuery($requette);
						}
					break;
					default:
						$kms = explode(";",$ligne["Km"]);
						foreach ($kms as $km)
						{
							$requette = "INSERT INTO service_vhc (Id_vhc, Km, Duree) VALUES ('$Id', '$km', '0')";
							Connection::executeRawQuery($requette);
						}
					break;
				}
      		}
        }
    		header("Location: default.php?Type=$Type&Sous_Type=".($Sous_Type-1)."&Etape=1&Id=$Id");
  	  }
      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='0'>\n";
      echo "   <input type='hidden' name='New' value='1'>\n";
      echo "<TABLE border=1>\n";
      echo "   <TR><TH>Début no chassis</TH><TD><input type='texte' name='NoChassis'></TD></TR>\n";
      echo "   <TR><TH>Marque</TH><TD><input type='text' name='Marque'></TD></TR>\n";
      echo "   <TR><TH>Model</TH><TD><input type='text' name='Model'></TD></TR>\n";
      echo "   <TR><TH>Cylindrée</TH><TD><input type='text' name='Cylindree'></TD></TR>\n";
      echo "   <TR><TH>Homologation</TH><TD><input type='text' name='Homologation'></TD></TR>\n";
  	  $requette = "SELECT * FROM service_km";
  	  $resultat = ServiceKM::getAll();
      echo "   <TR><TH>Fréquence services</TH><TD><select name='Km_service'>";
  	  foreach($resultat as $ligne)
  	  {
    		echo "<option value='".$ligne["Id"]."'>".$ligne["Description"]."</option>";
  	  }
  	  echo "</TD></TR>\n";
  	  $resultat = ChassisModel::getAll();
      echo "   <TR><TH>Copie données</TH><TD><select name='Vhc_ref'>";
      echo "<option value='0'>-</option>";
  	  foreach($resultat as $ligne)
  	  {
    		echo "<option value='".$ligne["Id"]."'>".$ligne["Chassis"]." ".$ligne["Model"]." ".$ligne["Cylindree"]." ".$ligne["Homologation"]."</option>";
  	  }
  	  echo "</TD></TR>\n";
      echo "</TABLE>\n";
      echo "   <button type='submit'>Créer</button>\n";
      echo "  </form>\n";
    break;
    case 1:
    break;
  }
?>
