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

// Vérification si l'accès est valide
if(!isset($_SESSION['SessionADMIN_compte_id'])){
	header('Location: ../../auth.php');
	exit;
}

// Vérification si l'utilisateur a les droits d'accès
if($_SESSION['SessionADMIN_compte_type']=='utilisateur' AND in_array(7, $_SESSION['SessionADMIN_compte_acces_modules'])==false){
	header('Location: ../../');
	exit;
}


if(isset($action) AND $action=='modifier'){

	foreach($planning_horaires as $planning_golf=>$planning_tab){
		foreach($planning_tab as $planning_equipe_id=>$planning_horaire){

			if(strlen(trim($planning_horaire))!=0 AND preg_match('/:/', $planning_horaire)==true){

				// Requête de mise à jour du planning
				$requete_maj = mysqli_query($connexion_mysql, 'UPDATE planning SET planning_depart_heure="'.$planning_horaire.'" WHERE golf_id="'.$planning_golf.'" AND equipe_id="'.$planning_equipe_id.'"');

			}

		}
	}

	header('Location: ./?action=rechercher&gp='.$gp.'&classement='.$classement);
	exit;

}


if(isset($gp) AND strlen(trim($gp))!=0){
	$critere_groupes = explode('|', $gp);
	$where = ' AND planning_groupe IN ("'.implode('","', $critere_groupes).'")';
}

if(!isset($classement)){ $classement = 'equipe_libelle'; }
if(!isset($where)){ $where = ''; }
if(!isset($gp)){ $gp = ''; }

// Sélection de la dernière compétition enregistrée
$requete_competition = mysqli_query($connexion_mysql, 'SELECT competition_id, competition_nb_groupes, competition_intervalle_departs FROM competitions ORDER BY competition_annee DESC LIMIT 1');
$resultat_competition = mysqli_fetch_array($requete_competition);
$competition_id = $resultat_competition[0];
$competition_nb_groupes = $resultat_competition[1];
$competition_intervalle_departs = $resultat_competition[2];


// Sélection des départs du calendrier de la dernière compétition
$requete_groupes = mysqli_query($connexion_mysql, 'SELECT DISTINCT(planning_groupe) FROM planning WHERE competition_id="'.$competition_id.'"'.$where.' ORDER BY planning_groupe');
$nb_groupes = mysqli_num_rows($requete_groupes);
?>

<!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">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title><?=$admin_titre?></title>
	<link rel="shortcut icon" type="image/png" href="<?=$admin_domaine?>favicon.png">
	<link rel="stylesheet" type="text/css" href="<?=$admin_css_page?>">
	<script type="text/javascript" src="<?=$admin_js?>jquery/jquery.js"></script>
	<script type="text/javascript" src="<?=$admin_js?>commun.js"></script>
	<meta name="robots" content="none">
</head>

<body>

<?php 
include $general_path_relatif.'sources/entete.inc';
include $general_path_relatif.'sources/menu.inc';
?>

<div id="contenu_1"<?=$correction_contenu_1?>><div id="contenu_2">

	<div id="contenu_3" align="left" style="width: 665px;">

		<div id="titres">
			<span class="titre_1"><a href="./">Planning</a> <span class="separateur">/</span> <span class="titre_2">Modifier</span></span>
		</div>
		<br clear="all">

		<div id="bloc">

			<form name="modifier" action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">

				<input type="hidden" name="action" value="modifier">
				<input type="hidden" name="gp" value="<?=$gp?>">
				<input type="hidden" name="classement" value="<?=$classement?>">

				<?php 
				if($nb_groupes!=0){

					for($groupe_boucle=0; $groupe_boucle<$nb_groupes; $groupe_boucle++){
						$resultat_groupe = mysqli_fetch_array($requete_groupes);
						$planning_groupe = $resultat_groupe[0];

						if($groupe_boucle!=0){ $ligne_entete_complement = ' border-top: 1px solid #E9E8DF;'; }else{ $ligne_entete_complement = ''; }

						$tab_groupe_golfs = array();

						echo '<div id="ligne" style="height: 44px; padding-left: 0px;'.$ligne_entete_complement.'">'."\n";

							echo '<div align="center" style="float: left; width: 212px; background-color: #F6F6F6; font-size: 20px; font-weight: bold; color: #999999; line-height: 44px;">GROUPE '.$planning_groupe.'</div>'."\n";

							// Sélection des dates et des golfs correspondants
							$requete_dates_golfs = mysqli_query($connexion_mysql, 'SELECT DISTINCT(DATE_FORMAT(planning_depart_date, "%d/%m/%y")), golf_id, (SELECT golf_nom FROM golfs WHERE golf_id=planning.golf_id) FROM planning WHERE planning_groupe="'.$planning_groupe.'" ORDER BY planning_depart_date');
							$nb_dates_golfs = mysqli_num_rows($requete_dates_golfs);
							for($date_golf_boucle=0; $date_golf_boucle<$nb_dates_golfs; $date_golf_boucle++){
								$resultat_date_golf = mysqli_fetch_array($requete_dates_golfs);
								$planning_date = $resultat_date_golf[0];
								$planning_golf_id = $resultat_date_golf[1];
								$planning_golf = $resultat_date_golf[2];

								array_push($tab_groupe_golfs, $planning_golf_id);	// Tableau contenant l'ordre des golfs classé par date croissante

								echo '<div align="center" style="float: left; width: 112px; padding: 4px 0px 4px 0px; border-left: 1px solid #E6E5DA; background-color: #F6F6F6; color: #666666; line-height: 18px;">'.$planning_date.'<br>'.$planning_golf.'</div>'."\n";

							}

						echo '</div>'."\n\n";

						// Sélection des horaires de départ
						$requete_planning = mysqli_query($connexion_mysql, 'SELECT planning_id, equipe_id AS equipe_identifiant, (SELECT equipe_nom FROM equipes WHERE equipe_id=equipe_identifiant) AS equipe_libelle, (SELECT planning_depart_heure FROM planning WHERE equipe_id=equipe_identifiant AND golf_id='.$tab_groupe_golfs[0].') AS date_1, (SELECT planning_depart_heure FROM planning WHERE equipe_id=equipe_identifiant AND golf_id='.$tab_groupe_golfs[1].') AS date_2, (SELECT planning_depart_heure FROM planning WHERE equipe_id=equipe_identifiant AND golf_id='.$tab_groupe_golfs[2].') AS date_3, (SELECT planning_depart_heure FROM planning WHERE equipe_id=equipe_identifiant AND golf_id='.$tab_groupe_golfs[3].') AS date_4 FROM planning WHERE planning_groupe="'.$planning_groupe.'" GROUP BY equipe_identifiant ORDER BY '.$classement);
						$nb_planning = mysqli_num_rows($requete_planning);
						if($nb_planning!=0){
							for($planning_boucle=0; $planning_boucle<$nb_planning; $planning_boucle++){
								$resultat_planning = mysqli_fetch_array($requete_planning);
								$planning_id = $resultat_planning[0];
								$equipe_id = $resultat_planning[1];
								$equipe_nom = $resultat_planning[2];
								$planning_depart_heure_1 = substr($resultat_planning[3], 0, -3);
								$planning_depart_heure_2 = substr($resultat_planning[4], 0, -3);
								$planning_depart_heure_3 = substr($resultat_planning[5], 0, -3);
								$planning_depart_heure_4 = substr($resultat_planning[6], 0, -3);

								echo '<div id="ligne">'."\n";
									echo '<div style="float: left; width: 202px;" title="Nom de l\'équipe">'.$equipe_nom.'</div>'."\n";
									echo '<div align="center" style="float: left; width: 112px; border-left: 1px solid #E6E5DA; background-color: #F6F6F6;"><input type="text" name="planning_horaires['.$tab_groupe_golfs[0].']['.$equipe_id.']" value="'.$planning_depart_heure_1.'" class="input_petit" style="width: 40px; text-align: center;"></div>'."\n";
									echo '<div align="center" style="float: left; width: 112px; border-left: 1px solid #E6E5DA; background-color: #F6F6F6;"><input type="text" name="planning_horaires['.$tab_groupe_golfs[1].']['.$equipe_id.']" value="'.$planning_depart_heure_2.'" class="input_petit" style="width: 40px; text-align: center;"></div>'."\n";
									echo '<div align="center" style="float: left; width: 112px; border-left: 1px solid #E6E5DA; background-color: #F6F6F6;"><input type="text" name="planning_horaires['.$tab_groupe_golfs[2].']['.$equipe_id.']" value="'.$planning_depart_heure_3.'" class="input_petit" style="width: 40px; text-align: center;"></div>'."\n";
									echo '<div align="center" style="float: left; width: 112px; border-left: 1px solid #E6E5DA; background-color: #F6F6F6;"><input type="text" name="planning_horaires['.$tab_groupe_golfs[3].']['.$equipe_id.']" value="'.$planning_depart_heure_4.'" class="input_petit" style="width: 40px; text-align: center;"></div>'."\n";
								echo '</div>'."\n\n";

							}
						}

						echo '<br><br>'."\n\n";

					}

				}
				?>

				<div id="formulaire_boutons"><input type="image" src="<?=$admin_img?>btn_enregistrer.png" width="150" height="32" align="absmiddle" style="margin: 0px 0px 30px 0px;"></div>

			</form>

		</div>

	</div>
	<br clear="all">

</div></div>

<?php include $general_path_relatif.'sources/pied_page.inc'; ?>

</body>
</html>
