<?php

$sampleArray = array( "Geeks",  "for", "proto"); 

$semaines_details = array();

?>

<!DOCTYPE html>
<html>
<style>
<!--
table, th, td {
  border:1px solid black;
}
-->
table { 
            <!-- margin-left: auto; 
            margin-right: auto; 
			-->
            font-size: 20px; 
            height: 100%; 
            table-layout: fixed; 
        } 
  
        td { 
            border: 1px solid black; 
            text-align: center; 
            padding: 1px; 
        } 
  
        tr:nth-child(even) { 
            background-color: #00cf45; 
        } 

</style>

<body>

<script>

function blabla(val)
{
	///alert(val);
}

<!--
https://saturncloud.io/blog/how-to-pass-variables-and-data-from-php-to-javascript/#:~:text=The%20simplest%20way%20to%20pass,it%20to%20a%20JavaScript%20variable.
-->

/*******************************************************************************************/

function submitFormData() 
{
			// OK
			//var element = "col1-row1";
			//champ = document.getElementById(element).value;
			//alert("plouf " + champ );
			
			//var javaScriptVar = "<?php echo $nb_semaines ; ?>";
			
			//alert("  ++++++ " + javaScriptVar);
			
			//console.log(javaScriptVar);
			
			// OK
			//var indice = "col2" + "-" + "row1" ;
			//var titi = document.getElementById(indice).value;
			//console.log(titi);
			
			
			//var passedArray = <?php echo json_encode($semaines_details); ?>; 
			//console.log(passedArray); 
	   
	   
			//alert(passedArray);
	   
			// Display the array elements 
			/*for(var i = 0; i < passedArray.length; i++)
			{ 
				document.write("<p>" + passedArray[i] + "</p>"); 
			} */
			
			
			var nb_semaines = document.getElementById("nb_semaines").value;
			console.log(" ----> Nb de semaines  " + nb_semaines);
			
			//var c = 1; 
			//var r = 1
			//var element = "col" + c + "-" + "row" + r;
			//console.log(" ---->  " + element);
			//champ_date = document.getElementById(indice).value;
			//console.log(" ---->  " + champ_date);
			
			let text = "";
			var tableau = new Array();
			
			
			var tab2 = new Array();
			//for (let i = 0; i < 3; i++)
			//{
				var array_col = new Array(nb_semaines);  // n
				var array_col_date = new Array(nb_semaines);  // n
			
			
				for (let j = 0; j < nb_semaines; j++)		// n
				{
					var col = 2; //i + 1;
					var row = j + 1;
					var indice = "col" + col + "-" + "row" + row;
					var champ_valeur = document.getElementById(indice).value;
					
					//alert(champ_valeur);

					
					array_col[j] = champ_valeur; // + " *" + champ_date;
					
									
					text += champ_valeur + "<br>";
					
					col = 1; 
					indice = "col" + col + "-" + "row" + row;
					champ_date = document.getElementById(indice).value;
					array_col_date[j] = champ_date; // + " *" + champ_date;
					
					// OK pour déterminer si le champ est vide
					if (typeof champ_valeur === "string" && champ_valeur.length === 0) 
					{
						console.log(indice + " " + champ_valeur + " The string is empty");
					} 
					else if (champ_valeur === null) 
					{
						console.log(indice + "  " + champ_valeur + " The string is null");
					} 
					else 
					{
						console.log(indice + " The string is not empty or null");
					}
					
					var tab1 = new Array();
					
					tab1.push(champ_date);
					tab1.push(champ_valeur);
					
					//let myObject = { datation: champ_date, valeur: champ_valeur }; // create an object
					
					tab2.push(tab1);
					
					/*if (champ_valeur === null)
					{
						console.log(indice + "  " + champ_valeur );
					}*/						
					
					//text += indice + "<br>";
				}
				
				//tableau[i] = array_col; 
				//tableau[0] = array_col; 
				
			//}
			
			console.log(tab2);
			
			//document.getElementById("display_text").innerHTML = text;
			
			//document.getElementById("display_text").innerHTML = "Voici le tableau au format string " + tableau.toString();
			document.getElementById("display_text").innerHTML = "Voici le tableau au format string " + array_col.toString() + array_col_date.toString();
			
			
			if (window.XMLHttpRequest) 
			{
				xmlhttp = new XMLHttpRequest();
			}
			else 
			{	
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			//var JsonString = JSON.stringify(array_col);
			var JsonString = JSON.stringify(tab2);
			xmlhttp.onreadystatechange = respond;
			xmlhttp.open("POST", "envoi_json.php", true);
			xmlhttp.send(JsonString);
			
}

/******************************************************************************************/
function xxxxxxxxxxxsubmitFormData() 
{
			// OK
			//var element = "col1-row1";
			//champ = document.getElementById(element).value;
			//alert("plouf " + champ );
			
			//var javaScriptVar = "<?php echo $nb_semaines ; ?>";
			
			//alert("  ++++++ " + javaScriptVar);
			
			//console.log(javaScriptVar);
			
			// OK
			//var indice = "col2" + "-" + "row1" ;
			//var titi = document.getElementById(indice).value;
			//console.log(titi);
			
			
			//var passedArray = <?php echo json_encode($semaines_details); ?>; 
			//console.log(passedArray); 
	   
	   
			//alert(passedArray);
	   
			// Display the array elements 
			/*for(var i = 0; i < passedArray.length; i++)
			{ 
				document.write("<p>" + passedArray[i] + "</p>"); 
			} */
			
			
			var nb_semaines = document.getElementById("nb_semaines").value;
			console.log(" ----> Nb de semaines  " + nb_semaines);
			
			//var c = 1; 
			//var r = 1
			//var element = "col" + c + "-" + "row" + r;
			//console.log(" ---->  " + element);
			//champ_date = document.getElementById(indice).value;
			//console.log(" ---->  " + champ_date);
			
			let text = "";
			var tableau = new Array();
			
			//for (let i = 0; i < 3; i++)
			//{
				var array_col = new Array(nb_semaines);  // n
			
				for (let j = 0; j < nb_semaines; j++)		// n
				{
					var col = 2; //i + 1;
					var row = j + 1;
					var indice = "col" + col + "-" + "row" + row;
					var champ_valeur = document.getElementById(indice).value;
					
					text += champ_valeur + "<br>";
					
					//col = 1; 
					//indice = "col" + col + "-" + "row" + row;
					//champ_date = document.getElementById(indice).value;
					
					array_col[j] = champ_valeur; // + " *" + champ_date;
					
					// OK pour déterminer si le champ est vide
					if (typeof champ_valeur === "string" && champ_valeur.length === 0) 
					{
						console.log(indice + " " + champ_valeur + " The string is empty");
					} 
					else if (champ_valeur === null) 
					{
						console.log(indice + "  " + champ_valeur + " The string is null");
					} 
					else 
					{
						console.log(indice + " The string is not empty or null");
					}
					
					/*if (champ_valeur === null)
					{
						console.log(indice + "  " + champ_valeur );
					}*/						
					
					//text += indice + "<br>";
				}
				
				//tableau[i] = array_col; 
				//tableau[0] = array_col; 
				
			//}
			
			//document.getElementById("display_text").innerHTML = text;
			
			//document.getElementById("display_text").innerHTML = "Voici le tableau au format string " + tableau.toString();
			document.getElementById("display_text").innerHTML = "Voici le tableau au format string " + array_col.toString();
			
			
			if (window.XMLHttpRequest) 
			{
				xmlhttp = new XMLHttpRequest();
			}
			else 
			{	
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			var JsonString = JSON.stringify(array_col);
			xmlhttp.onreadystatechange = respond;
			xmlhttp.open("POST", "envoi_json.php", true);
			xmlhttp.send(JsonString);
			
}
/*******************************************************************************************/
function transfert()
{
	
}
/*******************************************************************************************/

function respond() 
{
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
            document.getElementById('result').innerHTML = xmlhttp.responseText;
        }
    
}
/*******************************************************************************************/
function testResults (form) 
{
    var inputValue = form.input.value;
    alert ("You typed: " + inputValue);
}
/*******************************************************************************************/



</script>

</body>

<p id="display_text"></p>

<div id="result"></div>

</br>
</br>
</br>


<html>

<?php 
	//$result = $_GET['data']; 
	//echo $result; 
	
	//$result = $_GET['custId1'] ."  " .$_GET['custId2']; 
	//$result = $_POST['date_start'] ."  " .$_POST['date_end']; 
	//echo $result; 
	
	include("calendrier_generation02.php"); 
	
	echo "<br>";
	
	
	
	// generation
	
	
	$date_debut = $_POST['date_start'];
	$date_fin = $_POST['date_end'];
	
	echo "Plage " .$date_debut ." " .$date_fin;
	echo "<br>";
	
	// 
	$pieces = explode("-", $date_debut);
	$annee_debut = $pieces[2];
	unset($pieces); 
	$pieces = explode("-", $date_fin);
	$annee_fin = $pieces[2];
	unset($pieces); 
	
	echo " >>>> " .$annee_debut . "  " .$annee_fin ;
	echo "<br>";	
	
	// etalement du planning
	$etalement = array();
	
	if( $annee_debut == $annee_fin)
	{
		$etalement[] = $annee_debut;
		echo "Sur la meme annee";
		echo "<br>";
	}
	else
	{
		// 2025 - 2024 = 1    sur 2 ans
		// 2026 - 2024 = 2 	  sur 3 ans 
		//$etalement = ($annee_fin - $annee_debut) + 1; 

		for( $i=$annee_debut; $i <= $annee_fin ; $i++)	
		{
			$etalement[] = $i;
		}
		
		echo "Sur plusieurs annees";
		echo "<br>";
	}
	
	$listing_jours = array();
	$listing_jours = generation($etalement);
	
	//echo "<pre>";
	//print_r($listing_jours);
	//echo "</pre>";
	
	$allDates = structuration_dates($listing_jours, $date_debut, $date_fin);
	
	//echo "<pre>";
	//print_r($allDates);
	//echo "</pre>";
	
	$semaines_details = traitement($allDates);

	//echo "<br>";
	//echo "Nb semaines " .count($semaines_details);
	//echo "<br>";

	echo "<pre>";
	print_r($semaines_details);
	echo "</pre>";


	affiche_horizontale_table_semaine($semaines_details);
	
	// verifier si la table existe ? sinon va la créé 
	
	
/**********************************************************************************************/
function generation($tab_annee)
{
			$allDates = array();
			
			for( $i=0; $i < count($tab_annee) ; $i++)
			{		
				//echo " <<<< >>>> " .$tab_annee[$i];
				//echo "<br>";
				$listing_jours = calendrier_bis($tab_annee[$i]);	
				$allDates[] = $listing_jours;
				unset($listing_jours);
			}
			
			return $allDates;
}
/**********************************************************************************************/
function structuration_dates($listing_jours, $date_debut, $date_fin)
{
	$allDates = array(); 
	$flag = false;
	
	for( $i=0; $i <= count($listing_jours) ; $i++)	
	{
			//echo "<pre>";
			//print_r($listing_jours[$i]);
			//echo "</pre>";
			$annee = $listing_jours[$i];
		
			for( $j=0; $j < count($annee) ; $j++)
			{
				/*echo "<pre>";
				print_r($annee[$j]);
				echo "</pre>";*/
				$mois = $annee[$j];
				for( $k=0; $k < count($mois) ; $k++)
				{
					$string = $mois[$k];
					$pieces = explode( "_", $string);
					//echo " ++++ " .$pieces[0] . "     " .$date_debut ."   " .gettype($pieces[0]) . "  " .gettype($date_debut);
					//echo "<br>";
					
					if( $pieces[0] == $date_debut )
					{
						$flag = true;
						//echo " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ";
						//echo "<br>";
					}						
					
					if( $pieces[0] == $date_fin )
					{
						$flag = false;
						$allDates[] = $mois[$k];
						//echo " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ";
						//echo "<br>";
					}

					if($flag)
					{
						$allDates[] = $mois[$k];
					}						
				}					
			}				
	}
	
	return $allDates;
	
}
	
/**********************************************************************************************/
function traitement($allDates)
{
	$tab_semaines_details = array(); 
	
	for( $i=0; $i < count($allDates) ; $i++)
	{		
		if( $i != 0 )
		{
			$element_courant = explode( "_", $allDates[$i]);
			$element_precedent = explode( "_", $allDates[$i-1]);
			
			$week_courant = $element_courant[3];
			$week_precedent = $element_precedent[3];
			
			if( $week_courant != $week_precedent)
			{
				$p++;
				$start  = $i - $p; 
				$end = $start + $p - 1;
				$element_debut = explode( "_", $allDates[$i-$p]);
				$element_fin = explode( "_", $allDates[$i-1]);
				//echo ">>>>> " .$start ."  " .$end  ." " .$p  ."  " .$week_precedent ." " .$element_debut[0] ." " .$element_fin[0] ."<br>";
				
				// construire le tableau coorespondant
				$tab_semaines_details[] = $start ."_" .$end ."_" .$p ."_" .$week_precedent ."_" .$element_debut[0] ."_" .$element_fin[0]; 
				$p = 0;
			}			
			else
			{
				$p++;
			}
			
		}
		if( $i == count($allDates) - 1)
		{
			//$p++;
			$start = $end + 1;; 
			$end = $i;
			$element_debut = explode( "_", $allDates[$start]);
			$element_fin = explode( "_", $allDates[$end]);
			$ecart = ($end - $start)+1;
			//echo "<< FIN >> " .$start ."  " .$end  ." " .$ecart ."  " .$element_debut[3] ."  " .$element_debut[0] ." " . $element_fin[0] ."<br>";
			$tab_semaines_details[] = $start ."_" .$end ."_" .$ecart ."_" .$element_debut[3] ."_" .$element_debut[0] ."_" .$element_fin[0]; 

		}
	}
	
	return $tab_semaines_details;
}		
/**********************************************************************************************/
function affiche_horizontale_table_semaine($semaines_details)
{	
	
	$nb_semaines = count($semaines_details);
	
	$array_val = array("  WEEK   ", " Valeur ");
?>	


	<form action="javascript:void(0);" id="frmdata" onsubmit="submitFormData()">
	

<!--
	<form name="myForm" action="#" method="post" onsubmit="submitFormData()" >

<!--

	<form action="" id="frmdata" onsubmit="submitFormData()">
-->	
<!--
	<input type="hidden" name="email" value="riri">
	
	 <input type="text" name="input" value="<?php echo $variableAPasser; ?>">
-->	 
	<input type="hidden" id="nb_semaines" value="<?php echo $nb_semaines; ?>">
<!--
  <input type="button" name="button" value="Click" onClick="testResults(this.form)">
-->	
	<!--
	<input type=hidden id=variableAPasser value=<?php echo $variableAPasser; ?>/>
	-->
<?php	
	echo "
	<table border='1px' >
	<tr 	style='color:white;  
                background-color:black;' >";

	for ($i = 0; $i < 2; $i++) 
	{
		echo  "<th> ".$array_val[$i] ."</th>";
	}
	
	echo '</tr>';
	echo '<tr>';

	for($row=0;$row< count($semaines_details) ;$row++)
	{
		echo '<tr>';
		
		for($col=0;$col < 2 ;$col++)
		{
			$indice_row = $row+1;
			$indice_col = $col+1;
			$indice = "col".$indice_col ."-" ."row".$indice_row ;
			
			$colonne = "1" ;
			$indice_bis = "col".$colonne ."-" ."row".$indice_row ;
			

			if( $col == 0 )
			{
				$pieces = explode( "_", $semaines_details[$row]);
				$week = $pieces[3];
				$date_debut = $pieces[4];
				$date_fin = $pieces[5];
				
				// get month from date
				setlocale(LC_TIME, "fr_FR"); 
				
				$month = date('M', strtotime($date_debut));
				
				if( $month == "Jan")
					$month = "Janvier";
				if( $month == "Feb")
					$month = "Fevrier";
				if( $month == "Mar")
					$month = "Mars";
				if( $month == "Apr")
					$month = "Avril";
				if( $month == "May")
					$month = "Mai";
				if( $month == "Jun")
					$month = "Juin";
				if( $month == "Jul")
					$month = "Juillet";
				if( $month == "Aug")
					$month = "Aout";
				if( $month == "Sep")
					$month = "Septembre";
				if( $month == "Oct")
					$month = "Octobre";
				if( $month == "Nov")
					$month = "Novembre";
				if( $month == "Dec")
					$month = "Decembre";
				
				$str = $month ."             " .$week ."      (" .$date_debut ." * " .$date_fin .")"; 
				////////////echo "<td width='400px' >$str</td>";
				
				/*echo '
				<input type="hidden" name = "<?php echo $str; ?>"  id="<?php echo $str; ?>" />';
				*/
				
				/*echo '
				<td><input type="text" name="<?php echo $indice_bis; ?>" name="<?php echo $indice_bis; ?>" value= "lulu" readonly /></td>';
				*/
				
?>				
				<td><input type="text" name = "<?php echo $indice_bis; ?>"  id="<?php echo $indice_bis; ?>"  value = "<?php echo $str ?>"  /></td>

<?php
			}		
			
			else
			{
				/*echo '
				<td  width="400px" ><input type="text" name = "<?php echo $indice; ?>"  id="<?php echo $indice; ?>" onblur="blabla(this.id)" /></td>
				';*/
?>				
				<td><input type="text" name = "<?php echo $indice; ?>"  id="<?php echo $indice; ?>" /></td>
<?php
				
			}	
		}	
		echo '</tr>';
	}
		echo '</table>';
?>		
		<button type="submit">Submit</button>
		</form>	
<?php		

}
/*********************************************************************************************/
?> 