<?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 src='js/calendrier.js' type=text/javascript></SCRIPT>
		<SCRIPT language=JavaScript src='atelier.js' type=text/javascript></SCRIPT>
		<link rel="stylesheet" href="jquery/themes/ui-lightness/jquery-ui-1.9.2.custom.min.css">
		<script src="jquery/jquery-1.8.3.min.js"></script>
		<script src="jquery/jquery-ui-1.9.2.custom.min.js"></script>
		<script src="jquery/jquery.ui.datepicker-fr-CH.min.js"></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"
		?>
		<link href='css/calendar.css' rel='stylesheet' media='screen'>
	</head>

	<body>
		<?php
		//  $annee=date("Y");
		if ($new == 0) {
			CheckAlertAtelier($NoAtelier);
			if ($ligne = Atelier::getAtelierByNoAtelier($NoAtelier)) {
				echo "<form METHOD='GET' name='info' action='ok_atelier.php'>";
				echo "<TABLE border=1>\n";
				atelier_bloc_client_vehicule($ligne);
				atelier_bloc_service($ligne);
				atelier_bloc_date_heure_mecano($ligne);
				atelier_bloc_description($ligne);
				if ($ligne["VhcPret"] == 1) {
					$chkPret = " checked='checked'";
				} else {
					$chkPret = "";
				}
				echo "<TR><TH>Vehicule de prêt</TH><TD colspan='3'><Input type='checkbox' name='VhcPret' value='1' $chkPret></TD></TR>\n";
				if ($ligne['Etape'] < 2) {
					if ($ligne['Acompte'] != 0) {
						$ligne_acompte = DBCash::getByNoFacture($ligne['NoCommande']);
						echo "<TR><TH>Acompte</TH><TD colspan='3'>" . $ligne_acompte["Somme"];
						echo " <BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", " . $ligne['NoClient'] . ", 1);'>Facture " . $ligne['NoCommande'] . "</button><BR>\n";
						echo "</TD></TR>";
					} else {
						atelier_bloc_acompte(null);
					}
				}
				if ($ligne['Etape'] == 2 && As_Right_Fonction(6, 31)) {
					echo "<TR><TH>NoFacture</TH><TD colspan='3'><Input type='text' name='NoCommande' value='" . $ligne['NoCommande'] . "'></TD></TR>\n";
				} else if ($ligne['NoCommande'] != 0) {
					echo "<TR><TH>NoFacture</TH><TD colspan='3'><Input type='text' name='NoCommande' value='" . $ligne['NoCommande'] . "' readonly='readonly' ></TD></TR>\n";
				} else {
					echo "<input type='hidden' name='NoCommande' value='" . $ligne['NoCommande'] . "'>\n";
				}
				echo "</TABLE>\n";
				echo "<input type='hidden' name='pri' value='0'><BR>\n";
				echo "<input type='hidden' name='prientete' value='0'>\n";
				echo "<input type='hidden' name='prientete2' value='0'>\n";
				echo "<input type='hidden' name='NoAtelier' value='" . $ligne['NoAtelier'] . "'>\n";
				echo "<input type='hidden' name='etape' value='" . $ligne['Etape'] . "'>\n";
				echo "<input type='hidden' name='fact' value='0'>\n";
				echo "<input type='hidden' name='vhc' value='" . $ligne['Id_Vhc'] . "'>\n";
				echo "<input type='hidden' name='web' value='1'>\n";
				if ($ligne['Etape'] == 99) {
					atelier_bouton_web($ligne);
				} else {
					atelier_bouton_valider($ligne);
					atelier_bouton_fiche_atelier($ligne);
					atelier_bouton_entete($ligne);
					if ($ligne['Etape'] == 0) {
						echo "<BR /><BR /><BIG>Recevoir véhicule</BIG><BR /><BR />\n";
						atelier_bouton_reception_vhc($ligne);
						atelier_bouton_fiche_atelier_reception($ligne);
					}
					if ($ligne['Etape'] == 1) {
						echo "<BR /><BR /><BIG>Travaux terminés</BIG><BR /><BR />\n";
						if ($ligne['Acompte'] != 0 || $ligne['NoCommande'] != 0) {
							atelier_bouton_travaux_termines($ligne);
						} else {
							atelier_bouton_facture($ligne);
							atelier_bouton_gf($ligne);
							atelier_bouton_gg($ligne);
							atelier_bouton_devis($ligne);
							atelier_bouton_facture_interne($ligne);
							atelier_bouton_sans_facture($ligne);
							if (As_Right_Groupe(6))
								atelier_bouton_facture_existante($ligne);
							echo "<BR>\n";
						}
					}
				}
				echo "</form>";
				if ($ligne['Etape'] < 2) {
					echo "<form METHOD='GET' name='infodel' action='del_atelier.php'>";
					echo "<input type='hidden' name='NoAtelier' value='$NoAtelier'>\n";
					echo "<BUTTON TYPE='button' onClick='valide_suppresion();'>Supprimer</button><BR>\n";
					echo "</form>";
				}
				if ($ligne['NoCommande'] != 0 && $ligne['Etape'] != 1) {
					echo "<form>";
					$ligne2 = Commande::getCommandeByNoCommande($ligne['NoCommande']);
					$Sous_Type = 1;
					echo "<br/><BUTTON TYPE='button' onClick='go_facture(" . $ligne['NoCommande'] . ", " . $ligne['NoClient'] . ", $Sous_Type)'>Facture</button><BR>\n";
					echo "</form>";
				}
				if ($ligne['Etape'] >= 1 && $ligne['Etape'] != 99) {
					echo "<br/><button id='SMS_button' type='button'";
					if ($ligne["Sms"] == 1) {
						echo " style='background-color:#FF0000;'";
					}
					echo " onClick='send_sms(" . $ligne['NoAtelier'] . ");'>Envoi SMS</button><BR>\n";
				}
			}
		} else {
			echo "<div id=\"div_alert\" style=\"display: none; color: red; width: 100%; background-color: rgba(255,255,255,0.5);\"></div>";
			echo "<form METHOD='GET' name='info' action='ok_atelier.php'>";
			echo "<TABLE border=1><input type='hidden' name='etape' value='0'><input type='hidden' name='new' value='1'>\n";
			atelier_bloc_client_vehicule(null);
			atelier_bloc_service(null);
			atelier_bloc_date_heure_mecano(null);
			atelier_bloc_description(null);
			atelier_bloc_retour_vhc_pret(null);
			atelier_bloc_acompte(null);
			echo "</TABLE>\n";
			echo "<input type='hidden' name='pri' value='0'><BR>\n";
			echo "<input type='hidden' name='prientete' value='0'>\n";
			echo "<input type='hidden' name='prientete2' value='0'>\n";
			echo "<input type='hidden' name='vhc' value='0'>\n";
			atelier_bouton_valider(null);
			atelier_bouton_fiche_atelier(null);
			atelier_bouton_entete(null);
			echo "<BR /><BR /><BIG>Recevoir véhicule</BIG><BR /><BR />\n";
			atelier_bouton_reception_vhc($ligne);
			atelier_bouton_fiche_atelier_reception($ligne);
			echo "</form>";
		}
		?>
		<div id="dialog" title="Recherche client">Dialog</div>
		<script>
			$(function() {
				$("#dialog").dialog({
					autoOpen: false
				});
			});

			function dialog_historique() {
				var nochasis = $('#NoChasis').val();
				if (nochasis.length > 0) {
					$.get("historique_atelier.php?nochasis=" + nochasis, function(data) {
						var dialog = $('#dialog');
						dialog.html(data);
						dialog.dialog({
							//resizable: false,
							//height:140,
							width: 800,
							title: "Historique",
							buttons: {
								"Fermer": function() {
									$(this).dialog("close");
								}
							}
						});
						dialog.dialog("open");
						//alert( "Load was performed." );
					});
					//window.open("historique_atelier.php?nochasis="+nochasis);
				}
			}

			function ajouter_date_autre() {
				var nbDates = $("#NbDatesAutre").val();
				nbDates++;
				$("#NbDatesAutre").val(nbDates);
				//alert(nbDates);
				var $tableBody = $("#TableDatesAutre tbody");
				var elementContent = "<tr id=\"trDateAutre" + nbDates + "\"><td><a href=\"javascript:supprimer_date_autre(" + nbDates + ");\"><img style=\"background-color:white;\" src=\"images/b_drop.png\" border=\"0\"></a></td>";
				elementContent += "<td><input type=\"hidden\" name=\"NoSuite" + nbDates + "\" value=\"New\"><input type=\"text\" size=\"10\" name=\"Date" + nbDates + "\" value=\"\"><span class=\"span_err\" id=\"errDate" + nbDates + "\" style=\"visibility:hidden;\">*</span></td>";
				elementContent += "<td><input type=\"text\" name=\"Temps" + nbDates + "\"></td>";
				var mecano = $("select[name=\"Mecano\"]").parent().html();
				mecano = mecano.replace("name=\"Mecano\"", "name=\"Mecano" + nbDates + "\"");
				elementContent += "<td>" + mecano + "</td></tr>";
				$tableBody.append(elementContent);
				jQuery("input[name=\"Date" + nbDates + "\"]").datepicker($.datepicker.regional["fr-CH"]).change(function() {
					test_date_new(this.value, "errDate" + nbDates);
				});
			}

			function supprimer_date_autre(cpt) {
				$("#trDateAutre" + cpt).remove();
			}
		</script>
	</body>

	</html>
<?php
}
?>