<?php
	$phase = isset($this->request->params[0]) ? 1 : 0;

	$page = isset($this->request->params[0])?$this->request->params[0] : 1; 
	if ($page== 'S') {$page=1;}
	
	echo "<center><h1>".decode_lng_text("SEARCH_TITLE")."</h1></center></br>";

					
	// recup variable du formulaire
		$nom_husband = $this->request->data->nom_husband;
		$nom_husband_exact = $this->request->data->nom_husband_exact;
		$prenom_husband = $this->request->data->prenom_husband;
		$prenom_husband_exact = $this->request->data->prenom_husband_exact;
		$nom_spouse = $this->request->data->nom_spouse;
		$nom_spouse_exact = $this->request->data->nom_spouse_exact;
		$prenom_spouse = $this->request->data->prenom_spouse;
		$prenom_spouse_exact = $this->request->data->prenom_spouse_exact;
		$start = $this->request->data->start;
		$end = $this->request->data->end;

	/*
	* affichage du panneau de selection des critères
	*/
	echo "<center><table id='recherche_selection' ><tr style=' background-color:#FAD278; text-align:center; font-weight:bold;'>";
	echo "<td colspan=3>Choisissez un type de recherches</center></td></tr>";
	echo "<tr style='border:none; text-align:center; font-weight:bold;'>";

	echo "<td><a href='".Router::url('recherches/individus')."'>".decode_lng_text('SEARCH_TITLE_INDIVIDUS')."</a></td>";
	echo "<td><a href='".Router::url('recherches/events/0')."'>".decode_lng_text('SEARCH_TITLE_EVENTS')."</a></td>";
	echo "<td >".decode_lng_text('SEARCH_TITLE_COUPLES')."</td>";
	
	echo "</tr></table></center></br>";
	/*
	* affichage du tableau de selection des critères "couples"
	*/

	echo "<div id='panneau-couples' style='visibility:display'>";

	echo "<center>";
	echo "<form action=".Router::url('recherches/couples/S')." method='post'>";
	echo "<table border=1>";
	echo "<tr><td><strong>".display_lng_text('SEARCH_HUSBAND')."<strong></td>";
	echo "<td colspan=4>".display_lng_text("SEARCH_LAST_NAME")."&nbsp;&nbsp;<input type=text name=nom_husband value='".$nom_husband."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp;";
		if ($nom_husband_exact==true) {
			echo "<input type=checkbox name=nom_husband_exact checked='checked'></td>"; 
		}
		else {
			echo "<input type=checkbox name=nom_husband_exact></td>"; 
		}
	echo "<td>".display_lng_text("SEARCH_FISRT_NAME")."&nbsp;<input type=text name=prenom_husband value='".$prenom_husband."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp;";
		if ($prenom_husband_exact==true) {
			echo "<input type=checkbox name=prenom_husband_exact checked='checked'></td>"; 
		}
		else {
			echo "<input type=checkbox name=prenom_husband_exact></td>"; 
		}
		
	echo "</td></tr>";
	echo "<tr><td><strong>".display_lng_text('SEARCH_SPOUSE')."</strong></td>";
	echo "<td colspan=4>".display_lng_text("SEARCH_LAST_NAME")."&nbsp;&nbsp;<input type=text name=nom_spouse value='".$nom_spouse."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp;";
		if ($nom_spouse_exact==true) {
			echo "<input type=checkbox name=nom_spouse_exact checked='checked'></td>"; 
		}
		else {
			echo "<input type=checkbox name=nom_spouse_exact></td>"; 
		}
	echo "<td>".display_lng_text("SEARCH_FISRT_NAME")."&nbsp;<input type=text name=prenom_spouse value='".$prenom_spouse."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp;";
		if ($prenom_spouse_exact==true) {
			echo "<input type=checkbox name=prenom_spouse_exact checked='checked'></td>"; 
		}
		else {
			echo "<input type=checkbox name=prenom_spouse_exact></td>"; 
		}
	echo "</td></tr>";
	echo "<td><strong>".decode_lng_text("EVNT_DATE")."</strong></td>";
	echo "<td colspan=8><center>".decode_lng_text("DETAIL_DATE_BEGIN")."&nbsp;<input type=text name=start value='".$start."' SIZE=4>&nbsp;&nbsp;&nbsp;&nbsp;".decode_lng_text("DETAIL_DATE_END")."&nbsp;<input type=text name=end value='".$end."' SIZE=4>";
	echo "</center></td></tr></table><br>";

	echo"<table border=0>";
//var_dump ($this->Form->errors);
	foreach ($this->Form->errors as $k=>$error) {
		if (!empty($error)) {
			echo "<tr><td colspan='3' style='color:red;'>".$error."</td></tr>";
			$phase=0;
		}
	}
	echo "</table></br>";
	
	if ($phase==0) {
		echo "<div id=tab><h2><strong><i>".display_lng_text('SEARCH_NO_CRITERIA')."</i></strong></h2></div>";
		
	}

	// vue d'attente pendant la recherche
		
	echo "<center><input type=submit value=".decode_lng_text('PRIVATE_ENTER')." onClick='AttenteVisibility();'></center>";
	echo "</form></br>";
	echo "<div><a href='".Router::url('avis/recherches_guide')."'>".display_lng_text("SEARCH_GUIDE")."</a></div></br>";
	echo "</center>";
	
	echo "<div id='attente_retour' class='attente box2 w500p' >
			<table style='text-align: center; height=300px; width:100%;'>
			  <tbody>

				<tr>
				  <td style='text-align: center;'>
					<img width='400px' height='25px' src='".IMAGES."attente.gif' alt='Attente  recherches' /> 
				  </td>
				</tr>
				<tr>
				  <td style='text-align: center;font-size:16px;'>
						<p><br/><i>".display_lng_text('SEARCH_ATTENTE')."</i> <br/>  </p>
				  </td>
				</tr>
				
			  </tbody>
			</table>
				
		</div>";
		
	
/*
*     Affichage du résultat
*/

	if ($phase==0) { return; }
	
	echo "<div id='recherches_resultats' style='visibility:display'>";
	echo "<div> <img src='".IMAGES."ligne_orange.png' width=100% height='2px'></div>";

	echo "<table>";
	$nb_res_max = count($_SESSION['t_recherches_resultats']);

	if ($nb_res_max==0 && $phase != 0) {
		echo "<br><strong>".decode_lng_text("SEARCH_NO_RESULTS")."</strong></br>";
		echo "<div> <img src='".IMAGES."ligne_orange.png' width=100% height='2px'></div>";
		return;
	}
	else {	
		echo "<p style='color:#cc0000'><b><i><color=red>". $nb_res_max.' '.display_lng_text("SEARCH_FOUND_RESULTS")."</i></b></p>";
	}


			// on determine le nombre de resultats vus par page
			if (empty($nb_res_par_page)) { 
				$nb_res_par_page = '30';
			}
		// mise en place de la pagination		
		// calcul des limites basse et haute d'affichage des résultats
			$total_pages = ceil($nb_res_max/$nb_res_par_page);
			$i_deb = ($page - 1) * $nb_res_par_page;
			$i_fin = $i_deb + $nb_res_par_page;
			if ($i_fin > $nb_res_max) {
				$i_fin = $nb_res_max;
			}
			


			if ($page == '-1') {
				$i_deb = 0;
				$i_fin = $nb_res_max;
				echo display_lng_text("SEARCH_RESULTS_NUMBERS")."&nbsp";
				echo $i_deb + 1 ." - ".$i_fin."<br>";
				echo "<a href=".Router::url('recherches/individus/1').">";
				echo display_lng_text("SEARCH_WITH_LIMITS");
				echo "</a></br></br>";
				
			}
			else {
				echo display_lng_text("SEARCH_RESULTS_NUMBERS")."&nbsp";
				echo $i_deb + 1 ." - ".$i_fin."<br>";
				$reload = 'recherches/individus/';

				
				echo '<div class="pagination"><ul>';	

				if ($total_pages > 1) {
					
					echo Pagination($reload, $page, $total_pages);
				}
				
				echo "</ul></div>"; 
				
				// lien pour ne pas mettre de limites de résultats
				echo "<a href=".Router::url('recherches/individus/-1').">";
				echo display_lng_text("SEARCH_NO_LIMITS");
				echo "</a></br></br>";
			}
		// fin de la pagination	
		
		
		echo "<table border=1>";
//var_dump($_SESSION['t_recherches_resultats']);		
		echo "<tr><th> Epoux </th><th> Epouse </th><th>Date </th><th> Lieu </th></tr>";
		for($i=$i_deb;$i<$i_fin;$i++) {
			
			$row = $_SESSION['t_recherches_resultats'][$i];
		//array('id_family'=>$row->id_family, 'id_husband'=>$row->id_husband, 'id_spouse'=>$row->id_spouse, 'place'=>$row->place, 'date'=>$row->date);
			echo "<tr><td>".$row['id_husband']."</td><td>".$row['id_spouse']."</td><td>".$row['date']."</td><td>".$row['place']."</td></tr>";		
		}
		
		echo "</table>";
		
		// // on reaffiche la pagination si besoin
		// echo '<div class="pagination"><ul>';	
		// if ($total_pages > 1) {
			// echo Pagination($reload, $page, $total_pages);
		// }		
		// echo "</ul></div><br/>";
		// // fin de la pagination	 de bas de page

		echo "</br><div> <img src='".IMAGES."ligne_orange.png' width=100% height='2px'></div>";
	
	echo "</div>";

?>
