<?php
	include('database.php');
	DB_connexion(); 
	include('agenda.php');
	include('fonctions.php');

//////////////////////////////////////////////////	
function creation_table_annuelle($annee)
{	
	$connexion = DB(); 
	
	$nom_table  = "reservation_dev" .$annee;
	
	$sql = "CREATE TABLE IF NOT EXISTS " .$nom_table ."
			(
				id int(11) NOT NULL AUTO_INCREMENT,
				annee varchar(30) NOT NULL,
				mois varchar(30) NOT NULL,
				rendezvous varchar(30) NOT NULL,
				ts varchar(30) NOT NULL,
				jour varchar(30) NOT NULL,
				semaine varchar(30) NOT NULL,
				mode varchar(30) NOT NULL,        
				indice varchar(30) NOT NULL,        
				normal varchar(30) NOT NULL,
				etendu varchar(30) NOT NULL,
				formule varchar(30) NOT NULL,
				seance1 varchar(50) NOT NULL,
				seance2 varchar(50) NOT NULL,
				seance3 varchar(50) NOT NULL,
				seance4 varchar(50) NOT NULL,
				seance5 varchar(50) NOT NULL,
				seance6 varchar(50) NOT NULL,
				seance7 varchar(50) NOT NULL,
				seance8 varchar(50) NOT NULL,
				horairedebut varchar(50) NOT NULL,
				horairefin varchar(50) NOT NULL,
				avant varchar(50) NOT NULL,
				apres varchar(50) NOT NULL,
				PRIMARY KEY (id))";
			
			//echo $sql;
			
			$result = mysqli_query($connexion, $sql );
			if(!$result)
			{
				die('Impossible d\'exécuter la requête :' . mysql_error());
			}

	echo "Table done : " .$nom_table; 
	echo '<br>';	
			
	return($nom_table);		
			
}
//////////////////////////////////////////////////	
function remplir_table_calendrier($table,$annee,$mois,$rendezvous,$ts,$jour,$semaine,$mode,$formule)
{
		
	$connexion = DB(); 	
		
	$sql = "INSERT INTO " .$table ." (id, annee, mois, rendezvous, ts, jour, semaine , mode, normal,etendu,formule,seance1,seance2,seance3,seance4,seance5,seance6,seance7,seance8 ) 
							VALUES('', '$annee' ,'$mois' , '$rendezvous' ,'$ts', '$jour', '$semaine', '$mode' , '0', '0', 'courte' ,'unselect','unselect','unselect','unselect','unselect','unselect','unselect','unselect')"; 
	
	echo $sql;
	
	$result = mysqli_query($connexion, $sql );
	if(!$result)
	{
		die('Impossible d\'exécuter la requête :' . mysql_error());
	}
	
}			
//////////////////////////////////////////////////	
function generateur_calendrier()
{	
	$liste_annee = array("2017");  //,"2017","2018","2019","2020","2021","2022","2023","2024","2025");
	$liste_mois = array("janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre");
		
	for( $i=0; $i < count($liste_annee) ; $i++)
	{		
			$table = creation_table_annuelle($liste_annee[$i]);

			$listing_jours = calendrier($liste_annee[$i]);	
		
			for( $j=0; $j < count($listing_jours) ; $j++)
			{ 
		         $tab_mois = $listing_jours[$j];
				 for( $k=0; $k < count($tab_mois) ; $k++)
			     { 
						$pieces = explode('_',$tab_mois[$k]);
						$annee = $liste_annee[$i];
						$mois = ucfirst($liste_mois[$j]);
						$rendezvous = $pieces[0];
						$jour = ucfirst($pieces[1]);
						$ts = $pieces[2];
						$semaine = date('W',$ts);
						remplir_table_calendrier($table,$annee,$mois,$rendezvous,$ts,$jour,$semaine,"noactif","courte");
				 }
			}
	 	
	}
}
//////////////////////////////////////////////////	
/*CREATE TABLE IF NOT EXISTS gestion (
  id int(11) NOT NULL AUTO_INCREMENT,
  annee varchar(30) NOT NULL,
  mois varchar(30) NOT NULL,
  jour varchar(30) NOT NULL,
  client varchar(30) NOT NULL,
  mode varchar(30) NOT NULL,
  seance varchar(30) NOT NULL,
  compteur varchar(30) NOT NULL,
  montant varchar(30) NOT NULL,
  statut varchar(30) NOT NULL,
  cheque varchar(30) NOT NULL,
  banque varchar(30) NOT NULL,
  PRIMARY KEY (id)
)*/


	generateur_calendrier()
?>