<?php 
include '../lib/configuration.inc';

if(isset($_SESSION['SessionWeb_competition_id'])) {
	////////////////////////////////
	//		INFOS compétition      /
	////////////////////////////////
	$requete_prix_forfait = mysqli_query($connexion_mysql, 'SELECT prix_forfait, DATE_FORMAT(competition_date_debut, "%Y-%m-%d"), DATE_FORMAT(competition_date_fin, "%Y-%m-%d"), text_heb, text_heb_pro, text_heb_acc, text_heb_2, text_heb_3, text_heb_4, text_heb_pro_2, text_heb_pro_3, text_heb_2_en, text_heb_3_en, text_heb_4_en, text_heb_pro_2_en, text_heb_pro_3_en, text_heb_en, text_heb_pro_en, text_heb_acc_en, DATE_FORMAT(date_debut_hotel, "%Y-%m-%d"), DATE_FORMAT(date_fin_hotel, "%Y-%m-%d") FROM competitions WHERE competition_id = "'.$_SESSION['SessionWeb_competition_id'].'"');
	$req = mysqli_fetch_array($requete_prix_forfait);
	$competition_date_debut=$req[1];
	$competition_date_fin=$req[2];
	$text_heb=$req[3];
	$text_heb_pro=$req[4];
	$text_heb_acc = $req[5];
	$text_heb_2 = $req[6];
	$text_heb_3 = $req[7];
	$text_heb_4 = $req[8];
	$text_heb_pro_2 = $req[9];
	$text_heb_pro_3 = $req[10];
	$text_heb_2_en = $req[11];
	$text_heb_3_en = $req[12];
	$text_heb_4_en = $req[13];
	$text_heb_pro_2_en = $req[14];
	$text_heb_pro_3_en = $req[15];
	$text_heb_en = $req[16];
	$text_heb_pro_en = $req[17];
	$text_heb_acc_en = $req[18];
	$date_debut_heb = date("Y-m-d", strtotime('-0 days', strtotime($req[19])));
	$date_fin_heb = date("Y-m-d", strtotime('-0 days', strtotime($req[20])));
}


/**************************
*	INFOS & LOGIQUE HEBERGEMENT
***************************/
// condition "WHERE" de la requete SQL pour les héberbements
$part_where = '';
if(isset($_GET['vue_piscine']) and ($_GET['vue_piscine']=="0" or $_GET['vue_piscine']=="1"))
	$part_where .= ' AND vue_piscine = '.$_GET['vue_piscine'];
// requete SQL hebergement
$requete_heber_dispos = mysqli_query($connexion_mysql, 'SELECT * FROM hebergements WHERE qtte > 0'.$part_where);




//$requete_hebergements = mysqli_query($connexion_mysql, 'SELECT DISTINCT id_hotel FROM hebergements');
$requete_hebergements = mysqli_query($connexion_mysql, 'SELECT hotel, id_hotel FROM `hebergements` GROUP BY(id_hotel);');
$nb_hebergements = mysqli_num_rows($requete_hebergements);




// setting des booleans
$chbre_double_dispo=0;
$chbre_simple_dispo=0;
while ($row = $requete_heber_dispos->fetch_array()) {
	if(intval($row['chambre_double']) == 1)
		$chbre_double_dispo = 1;
	if(intval($row['chambre_double']) == 0)
		$chbre_simple_dispo = 1;
}
if($_SESSION['SessionWeb_joueur_profil'] == 'acc')
	$chbre_simple_dispo = 0;

////////////////////////////////////////////////////////////////////////////
// Construction du tableau des <option> pour le <select> "choix de chambre"/
////////////////////////////////////////////////////////////////////////////
$options_chambre_double = array();
if(!isset($_GET['chambre_double'])) {
	$selected_double = '';
	$selected_simple = '';
	array_push($options_chambre_double, "\t<option value='none' selected>".$choisissez_type_chambre."</option>\n");
}
else {
	$selected_double = intval($_GET['chambre_double']) == 1 ? 'selected' : '';
	$selected_simple = intval($_GET['chambre_double']) == 0 ? 'selected' : '';
}



if($chbre_double_dispo==1)
	array_push($options_chambre_double, "\t<option value='1' ".$selected_double.">".$text_type_chbre_double."</option>\n");
else
	array_push($options_chambre_double, "\t<option value='1' disabled>".$text_type_chbre_double_nodispo." </option>\n");

if($chbre_simple_dispo==1)
	array_push($options_chambre_double, "\t<option value='0' ".$selected_simple.">".$text_type_chbre_simple."</option>\n");
else
	array_push($options_chambre_double, "\t<option value='0' disabled>".$text_type_chbre_simple_nodispo."</option>\n");



////////////////////////////////////////////////////////////////////////////
// Construction du tableau des <option> pour le <select> "vue piscine"     /
////////////////////////////////////////////////////////////////////////////
$options_vue_piscine = array();
$part_where = '';
if(isset($_GET['chambre_double']) and ($_GET['chambre_double']=="0" or $_GET['chambre_double']=="1"))
	$part_where .= ' AND chambre_double = '.$_GET['chambre_double'];
$requete_heber_dispos = mysqli_query($connexion_mysql, 'SELECT * FROM hebergements WHERE qtte > 0'.$part_where);
// setting des booleans
$vue_piscine_avec=0;
$vue_piscine_sans=0;
while ($row = $requete_heber_dispos->fetch_array()) {
	if(intval($row['vue_piscine']) == 1)
		$vue_piscine_avec = 1;
	if(intval($row['vue_piscine']) == 0)
		$vue_piscine_sans = 1;
}
if(!isset($_GET['vue_piscine'])) {
	$selected_vue = '';
	$selected_novue = '';
	array_push($options_vue_piscine, "\t<option value='none' selected>".$choisir_vue."</option>\n");
}
else {
	$selected_vue = intval($_GET['vue_piscine']) == 1 ? 'selected' : '';
	$selected_novue = intval($_GET['vue_piscine']) == 0 ? 'selected' : '';
}
if($vue_piscine_avec==1)
	array_push($options_vue_piscine, "\t<option value='1' ".$selected_vue.">".$text_pisc."</option>\n");
else
	array_push($options_vue_piscine, "\t<option value='1' disabled>".$text_pisc_nodispo." </option>\n");

if($vue_piscine_sans==1)
	array_push($options_vue_piscine, "\t<option value='0' ".$selected_novue.">".$text_nopisc."</option>\n");
else
	array_push($options_vue_piscine, "\t<option value='0' disabled>".$text_nopisc_nodispo."</option>\n");



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?=$_SESSION['SessionWeb_langue']?>" xml:lang="<?=strtolower($_SESSION['SessionWeb_langue'])?>">
	<head>
		<title><?=$txt_page_titre?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=8">
		<meta name="language" content="<?=strtolower($_SESSION['SessionWeb_langue'])?>">
		<meta name="robots" content="index, follow">
		<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
		<link rel="shortcut icon" href="/favicon.png">
		<link rel="stylesheet" type="text/css" href="<?=$web_commun?>commun.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	</head>

	<body scroll="auto">
		<div id="page" align="center">
			<div id="contenu">
				<?php include './commun/entete.inc'; ?>
				<div id="formulaire">
					<form action="inscription_6_<?=$_SESSION['SessionWeb_joueur_profil']?>.php" method="GET">

						<!--  Hébergement -->
						<h1><?=$title_heb;?></h1>	
						<br clear="all">



						<div class="colonne_gauche_obligatoire" style="padding-top: 0px;" hidden><?=$besoin_heb;?></div>	<!--  Besoin d'un hébergement ? -->
						<div class="colonne_droite" style="height: 33px;" hidden>
							<?php
								$check_no = "";
								$check_yes = "checked";
								//if (isset($_GET["trip-start"])) {
								if (isset($_GET["hotel_id"])) {
										$check_no = "";
										$check_yes = "checked";
									} else {
										$check_no = "";
										$check_yes = "checked";
									}							
							?>
							<label for="yes">	<!-- Oui -->
								<?php
								echo '<input type="radio" id="radioyes" name="hebornot" value="yes" '.$check_yes.'/>'.$txt_pro_etape2_champ_membre_clubs_competition_oui."\n";
								?>
							</label>
							<label for="no">	<!-- Non -->
								<?php					
								echo '<input type="radio" id="radiono" name="hebornot" value="no" '.$check_no.'/>'.$txt_pro_etape2_champ_membre_clubs_competition_non."\n";
								?>
							</label>
						</div>
						<br clear="all">
						<div class="separateur to_hide_0"></div>

						<!--  Texte en dessous de "Besoin d'un hébergement ?" -->





						<!--  si amateur -->
						<?php if ($_SESSION['SessionWeb_joueur_profil'] == 'am') { ?>
							<br>
							<div id="texte" style="width:600px;" class="to_hide_0">
								<div id="calque_package_golfeur_infos" class="to_hide_0">
									<?php if( strtolower($_SESSION['SessionWeb_langue']) == 'fr' ) { 
											echo $text_heb;
										} else {
											echo $text_heb_en;
										}
									?>
								</div>
							</div>
						<?php } ?>
						<!-- si pro  -->
						<?php if ($_SESSION['SessionWeb_joueur_profil'] == 'pro') { ?>
							<br>
							<div id="texte" class="to_hide_0" style="width:600px;">
								<div id="calque_package_golfeur_infos to_hide_0">

									<?php if( strtolower($_SESSION['SessionWeb_langue']) == 'fr' ) { 
											echo $text_heb_pro;
										} else {
											echo $text_heb_pro_en;
										}
									?>

								</div>
							</div>
						<?php } ?>
						<!--  si accompagnateur -->
						<?php if ($_SESSION['SessionWeb_joueur_profil'] == 'acc') { ?>
							<br>
							<div id="texte" class="to_hide_0" style="width:600px;">
								<div id="calque_package_golfeur_infos to_hide_0">

									<?php if( strtolower($_SESSION['SessionWeb_langue']) == 'fr' ) { 
											echo $text_heb_acc;
										} else {
											echo $text_heb_acc_en;
										}
									?>
								</div>
							</div>
						<?php } ?>
						<br clear="all">
						<div class="separateur"></div>

						<!-- Votre hôtel -->
						<div class="colonne_gauche_obligatoire to_hide_0" style="height: 85px; padding-top: 0px;" hidden><?=$votre_hotel;?></div>
						<!-- Select de "Votre hôtel" -->
						<div class="colonne_droite to_hide_0" style="height: 33px;" hidden>
							<?php
							  $selected = '';
							  $seen = array();
							  echo '<select name="hotel_id">',"\n";
								if(!isset($_GET['hotel_id']))
								  echo "\t",'<option value="empty" selected>'.$choisir_hotel.'</option>',"\n";
								while($value = mysqli_fetch_assoc($requete_hebergements)) {
									$ID = $value["id_hotel"];
									$hotel = $value["hotel"];
									if(!in_array($hotel, $seen)) {
										array_push($seen, $hotel);
									    if(isset($_GET['hotel_id']) && (int)$_GET['hotel_id'] == (int)$ID)
									    	$selected = ' selected="selected"';
										echo "\t",'<option value="', $ID ,'"', $selected ,'>', $hotel.'</option>',"\n";
										$selected='';
									}
								}
							  echo '</select>',"\n";
							?>
						</div>

						<!-- text_heb_2 (voir dans admin) -->
						<?php /* si amateur */
						if (isset($_GET['hotel_id']) && $_SESSION['SessionWeb_joueur_profil'] == 'am') { ?>
								<br clear="all">
								<div class="separateur"></div>
								<div id="texte" class="to_hide_0" style="width:600px;">
									<div id="calque_package_golfeur_infos to_hide_0">
										<?php 
											$txt_to_display = strtolower($_SESSION['SessionWeb_langue']) == 'fr' ? $text_heb_2 : $text_heb_2_en ;
											echo $txt_to_display;
										?>
									</div>
								</div>
							<?php
						} ?>
						<?php /* si pro */
						if (isset($_GET['hotel_id']) && $_SESSION['SessionWeb_joueur_profil'] == 'pro') {
							 ?>
								<br clear="all">
								<div class="separateur"></div>
								<div id="texte" class="to_hide_0" style="width:600px;">
									<div id="calque_package_golfeur_infos">
										<?php $txt_to_display = strtolower($_SESSION['SessionWeb_langue']) == 'fr' ? $text_heb_pro_2 : $text_heb_pro_2_en; echo $txt_to_display; ?>
									</div>
								</div>
							<?php } ?>
							<?php
								if(isset($_GET['hotel_id'])) { ?>

									<!-- Type de Chambre -->
									<br clear="all">
									<div class="separateur to_hide2"></div>
									<div class="colonne_gauche_obligatoire to_hide2" style="height: 85px; padding-top: 0px;"><?=$type_chambre;?></div> 
									<!-- Select "Type de chambre" -->
									<div class="colonne_droite to_hide2" style="height: 33px;">
										<?php
										echo '<select name="chambre_double">',"\n";
										foreach ($options_chambre_double as $key => $value)
											echo $value;
										echo '</select>',"\n";
										?>
									</div>

									<!-- text_heb_3 (voir admin) -->
									<?php /* si amateur */
									if (isset($_GET['hotel_id']) && $_SESSION['SessionWeb_joueur_profil'] == 'am') {
										?>
											<br clear="all">
											<div class="separateur"></div>
											<div id="texte" class="to_hide_0" style="width:600px;">
												<div id="calque_package_golfeur_infos">
													<?php 

														$txt_to_display = strtolower($_SESSION['SessionWeb_langue']) == 'fr' ? $text_heb_3 : $text_heb_3_en;
														echo $text_heb_3;
													?>
												</div>
											</div>
										<?php } ?>
									<?php /* si pro */
									if ( isset($_GET['hotel_id']) && $_SESSION['SessionWeb_joueur_profil'] == 'pro' && isset($_GET['chambre_double']) ) {
										 ?>
											<br clear="all">
											<div class=""></div>
											<div id="texte" class="to_hide_0" style="width:600px;">
												<div id="calque_package_golfeur_infos ">
													<?php $txt_to_display = strtolower($_SESSION['SessionWeb_langue']) == 'fr' ? $text_heb_pro_3 : $text_heb_pro_3_en ; 
														echo $txt_to_display;
													?>
												</div>
											</div>
										<?php } 
									?>
								
									<!-- Arrivée / Départ -->
									<br clear="all">
									<div class="separateur to_hide_3"></div>
									<div style="display:flex;" class="to_hide_3">

										<!-- Arrivée -->
										<div class="colonne_gauche_obligatoire" style="height: 85px; padding-top: 0px;"><?=$arrivee;?></div>
										<!-- Select Arrivée -->
										<div class="colonne_droite" style="height: 33px;">
										<?php
										$val = "";
										if (isset($_GET["trip-start"]))
											$val = $_GET["trip-start"];
										else
											$val = $competition_date_debut;
										echo '<input type="date" id="start" name="trip-start" value="' . $val . '" min="'.$date_debut_heb.'" max="'.$competition_date_debut.'" />'."\n";
										?>
										</div>

										<!-- Départ -->
										<div class="colonne_gauche_obligatoire" style="height: 85px; padding-top: 0px;"><?=$depart;?></div>
										<!-- Select Départ -->
										<div class="colonne_droite" style="height: 33px;">
										<?php
										$val = "";
										if (isset($_GET["trip-departure"]))
											$val = $_GET["trip-departure"];
										else 
											$val = $competition_date_fin;
										
										echo '<input type="date" id="departure" name="trip-departure" value="' . $val . '" min="'.$competition_date_fin.'" max="'.$date_fin_heb.'" />'."\n";
										?>
										</div>

									</div>

									<!-- text_heb_4 (voir admin, uniquement pour amateurs) -->
									<?php
									if (isset($_GET['hotel_id']) && $_SESSION['SessionWeb_joueur_profil'] == 'am' && isset($_GET['chambre_double'])) {
										?>
											<br clear="all">
											<div class="separateur"></div>
											<div id="texte" class="to_hide_0" style="width:600px;">
												<div id="calque_package_golfeur_infos">
													<?php
														$txt_to_display = strtolower($_SESSION['SessionWeb_langue']) == 'fr' ? $text_heb_4 : $text_heb_4_en ;
														echo $txt_to_display;
													?>
												</div>
											</div>
										<?php } ?>
								<?php
							}
							?>

							<!-- Vue piscine -->
							<?php
							if(isset($_GET['chambre_double'])) {
							?>
								<br clear="all">
								<div class="separateur to_hide_4"></div>
								<?php 
								echo "\t",'<div class="colonne_gauche_obligatoire to_hide_4" style="height: 85px; padding-top: 0px;">'.$choisir_vue.'</div>',"\n"; ?>
								<!-- Select vue piscine -->
								<div class="colonne_droite to_hide_4" style="height: 33px;">
										<?php
											echo '<select name="vue_piscine">',"\n";
											foreach ($options_vue_piscine as $key => $value)
												echo $value;
											echo '</select>',"\n";
										?>
								</div>
							<?php
							}
							?>

							<!-- Bouton formulaire -->
							<br clear="all">
							<div class="separateur" hidden></div>
							<div style="float: left; width: 200px;"><input type="button" value="<?=$txt_bouton_annuler?>" onclick="window.location='./inscription_1.php';" class="bouton_annuler"></div>
							<div align="right" style="margin-right: 130px;"><input type="submit" value="<?=$txt_bouton_etape_suivante?>"></div>

					</form>

				</div>

			</div>

		</div>

	</body>
</html>


<script type="text/javascript">
	$( document ).ready(function() {

		/* Lors de la validation du formulaire */
        $("form").on("submit", function (e) {
        	// Si URL valide, on passe à l'étape suivante
        	if(
        		(window.location.href.indexOf("hotel_id") > 0 &&
        		window.location.href.indexOf("chambre_double") > 0 &&
        		window.location.href.indexOf("vue_piscine") > 0)
				||
        		(
        			( window.location.href == window.location.href.split('?')[0] )
        			&&
        			$("#radiono").is(':checked')
        		)
        		||
        		(
        			( /^langue=\w{2}$/.test(window.location.href.split('?')[1]) )
        			&&
        			$("#radiono").is(':checked')
        		)
        	)
        	{
        		return;
        	} // Sinon on reste sur la page
        	else 
        	{
        		e.preventDefault();
        	}

        });


        // Si hébergement ou non (au chargement de la page), on affiche ou cache le formulaire
		if ( $("#radioyes").is(':checked') || window.location.href.indexOf("hotel_id") > 0)
			$("[class*='to_hide']").show();
		else
			$("[class*='to_hide']").hide();
		
		// Si hébergement ou non (au click), on affiche ou cache le formulaire
		$( 'input[name=hebornot]' ).change(function(){
			if(this.value == "no"){
				$(".to_hide_0").show();
				//$(".to_hide_0").hide();
				let url = window.location.href;
				window.location = url.split('?')[0];
			}
			else {
				$(".to_hide_0").show();
			}
		});

		// à la section de l'hotel, on recharge la page et on maj l'url
		$( "[name='hotel_id']" ).change(function() {
		  window.location='http://app.duosdugolfagadir.com/inscription_hebergement.php?hotel_id=' + this.value;
		});

		// à la selection de la chambre, on recharge la page et on maj l'url
		$( "[name='chambre_double']" ).change(function() {
		  if(window.location.href.indexOf("chambre_double") > 0) {
		  	if(this.value == "1") {
		  		window.location = window.location.href.replace("chambre_double=0", "chambre_double=1");
		  	} else {
		  		window.location = window.location.href.replace("chambre_double=1", "chambre_double=0");
		  	}
		  } else if ( window.location.href.indexOf("hotel_id") > 0) {
		  	window.location = window.location.href + "&chambre_double="+this.value;
		  } else {
		  	window.location = window.location.href + "?chambre_double="+this.value;
		  }
		});

		// à la section de la date de départ, on recharge la page et on maj l'url
		$( "[name='trip-start']" ).change(function() {
			var pattern = /trip-start=\d{4}\-\d{2}\-\d{2}/;
			var newdate = this.value;
			if(window.location.href.indexOf("trip-start") > 0) {
				window.location = window.location.href.replace(pattern, 'trip-start='+newdate);
			} else {
				window.location = window.location.href + "&trip-start="+newdate;
			}
		});

		// à la section de la date d'arrivée, on recharge la page et on maj l'url
		$( "[name='trip-departure']" ).change(function() {
		  var pattern = /trip-departure=\d{4}\-\d{2}\-\d{2}/;
		  var newdate = this.value;
		  if(window.location.href.indexOf("trip-departure") > 0) {
		  		window.location = window.location.href.replace(pattern, 'trip-departure='+newdate);
		  } else {
		  	window.location = window.location.href + "&trip-departure="+this.value;
		  }
		});

		// à la section de la vue piscine, on recharge la page et on maj l'url
		$( "[name='vue_piscine']" ).change(function() {
		  if(window.location.href.indexOf("vue_piscine") > 0) {
		  	if(this.value == "1") {
		  		window.location = window.location.href.replace("vue_piscine=0", "vue_piscine=1");
		  	} else if (this.value == "0") {
		  		window.location = window.location.href.replace("vue_piscine=1", "vue_piscine=0");
		  	} else { // this.value = none
		  		window.location = window.location.href.replace("&vue_piscine=none", "");
		  	}
		  } else {
		  	window.location = window.location.href + "&vue_piscine="+this.value;
		  }		 
		});
	});
</script>