<?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>
		<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
		echo "SERVICE<BR />\n";
		if ($ligne = Atelier::getAtelierByNoAtelier($NoAtelier)) {
			$requetteOptions = "SELECT voa.* FROM atelier_option" . ID_GARAGE . " AS ao, vhc_option AS vo, vhc_option_action AS voa WHERE ao.Id_atelier = " . $NoAtelier . " AND ao.Id_option = vo.Id AND vo.Id_option = voa.Id_option";
			$resultatOptions = Connection::executeRawQueryWithfetchAll($requetteOptions);
			$req_options = "";
			foreach ($resultatOptions as $ligneOption) {
				if ($req_options != "") $req_options .= ", ";
				$req_options .= $ligneOption["Id_action"];
			}

			//echo $requetteOptions."<br/>\n";
			//echo $req_options."<br/>\n";
			if ($req_options != "") {
				$req_options = " OR (action.Id IN (" . $req_options . ") AND action.Id_Type = action_type.Id)";
			}
			$requetteRemplacement = "SELECT voa.* FROM atelier_option" . ID_GARAGE . " AS ao, vhc_option AS vo, vhc_option_action_remplacement AS voa WHERE ao.Id_atelier = " . $NoAtelier . " AND ao.Id_option = vo.Id AND vo.Id_option = voa.Id_option";
			$resultatRemplacement = Connection::executeRawQueryWithfetchAll($requetteRemplacement);
			$req_Remplacement = "";
			foreach ($resultatRemplacement as $ligneRemplacement) {
				if ($req_Remplacement != "") $req_Remplacement .= ", ";
				$req_Remplacement .= $ligneRemplacement["Id_action"];
			}
			//echo $requetteRemplacement."<br/>\n";
			//echo $req_Remplacement."<br/>\n";
			if ($req_Remplacement != "") {
				$req_Remplacement = " AND action.Id NOT IN (" . $req_Remplacement . ")";
			}
			//$req_Remplacement = "";
			//print_r($ligne);
			$requette2 = "SELECT DISTINCT action_type.Description, action_type.Non_Select, action_type.Avec_Article, action.Texte, action.Id AS Id_Action, action_type.Valeurs FROM service_action, action, action_type WHERE (service_action.Id_Service = " . $ligne['Id_Service'] . " AND (service_action.Id_Action = action.Id$req_Remplacement) AND service_action.Id_Action_Type = action_type.Id)$req_options ORDER BY action.Id_Rubrique, action.Position";
			//echo $requette2."<br/>\n";
			//$requette2 = "SELECT action_type.Description, action_type.Avec_Article, action.Texte, service_action.Id_Action, action_type.Valeurs FROM service_action, action, action_type WHERE service_action.Id_Service = ".$ligne['Id_Service']." AND service_action.Id_Action = action.Id AND service_action.Id_Action_Type = action_type.Id ORDER BY action.Id_Rubrique, action.Position";
			//echo $requette2."<br/>\n";
			$resultat2 = Connection::executeRawQueryWithfetchAll($requette2);
			echo "<form METHOD='GET' name='info' action='ok_service.php'>";
			echo "<input type='hidden' name='NoAtelier' value='$NoAtelier'><BR>\n";
			echo "<input type='hidden' name='Type' value='$Type'><BR>\n";
			echo "<input type='hidden' name='Genre' value='$Genre'><BR>\n";
			echo "<TABLE border='1'>\n";
			foreach ($resultat2 as $ligne2) {
				echo "<TR><TD>" . $ligne2['Description'] . " " . $ligne2['Texte'] . "</TD><TD><input type='checkbox' name='act_" . $ligne2["Id_Action"] . "'";
				if ($ligne2['Non_Select'] == 1) {
				} else {
					echo " checked='1'";
				}
				echo " /></TD>";
				$valeures = explode(";", $ligne2["Valeurs"]);
				//print_r($ligne2);
				if (count($valeures) > 1) {
					echo "<TD><select name='act_exec_" . $ligne2["Id_Action"] . "'>";
					$pos = 0;
					foreach ($valeures as $valeure) {
						$val = str_replace("(0)", "", $valeure);
						$val = str_replace("(1)", "", $val);
						echo "<option value='$pos'>" . $val . "</option>";
						$pos++;
					}
					echo "</select></TD>";
				} else {
					echo "<TD>-</TD>";
				}
				if ($ligne2["Avec_Article"] == 1) {
					$requette3 = "SELECT service_article.* FROM service_vhc, service_article WHERE service_vhc.Id = " . $ligne['Id_Service'] . " AND service_vhc.Id_vhc = service_article.Id_Vhc AND service_article.Id_Action = " . $ligne2["Id_Action"];
					$resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
					if ($ligne3 = $resultat3[0]) {
						echo "<TD><input type='text' name='act_quantite_" . $ligne2["Id_Action"] . "' value='" . $ligne3["Quantite"] . "' /></TD>";
					}
					$requette3 = "SELECT service_option_article.* FROM service_option_article WHERE service_option_article.Id_Vhc = " . $ligne["Id_Vhc"] . " AND service_option_article.Id_Action = " . $ligne2["Id_Action"];
					$resultat3 = Connection::executeRawQueryWithfetchAll($requette3);
					if ($ligne3 = $resultat3[0]) {
						echo "<TD><input type='text' name='act_quantite_" . $ligne2["Id_Action"] . "' value='" . $ligne3["Quantite"] . "' /></TD>";
					}
				} else {
					echo "<TD>-</TD>";
				}
				echo "</TR>\n";
			}
			echo "</TABLE>\n";
			echo "<input type='submit' value='valider' />\n";
			echo "</form>\n";
		}
		?>
	</body>

	</html>
<?php
}
?>