<?php

/*
	identification de  la phase de recherches en cours
		phase = 0  : phase initiale avec premier choix des critères 
		phase = 1  : pahases suivantes
*/		
	$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>";

		$nom = $this->request->data->nom;
		$nom_exact = $this->request->data->nom_exact;
		$prenom = $this->request->data->prenom;
		$prenom_exact = $this->request->data->prenom_exact;
		$sexe = $this->request->data->sexe;
		$lieu = $this->request->data->lieu;

		$start = $this->request->data->start;
		$end = $this->request->data->end;
		$choice = $this->request->data->choice;
		if ($choice=="") {$choice="all";}

	/*
	* 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 >".decode_lng_text('SEARCH_TITLE_INDIVIDUS')."</td>";
	echo "<td><a href='".Router::url('recherches/events/0')."'>".decode_lng_text('SEARCH_TITLE_EVENTS')."</a></td>";
	echo "<td ><a href='".Router::url('recherches/couples')."'>".decode_lng_text('SEARCH_TITLE_COUPLES')."</a></td>";
	echo "</tr></table></center></br>";

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

	echo "<form action=".Router::url('recherches/individus/S')." method='post'>";

	echo "<center>";
	echo "<table border=1 ><tr><td>".display_lng_text("SEARCH_LAST_NAME")."&nbsp;&nbsp;<input type='text' name='nom' value='".$nom."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp;";
	if ($nom_exact==true) {
		echo "<input type=checkbox name=nom_exact checked='checked'></td>"; 
	}
	else {
		echo "<input type=checkbox name=nom_exact></td>"; 
	}
	echo "<td>".display_lng_text("SEARCH_FISRT_NAME")."&nbsp;<input type='text' name='prenom' value='".$prenom."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp;";
	if ($prenom_exact==true) {
		echo "<input type=checkbox name=prenom_exact checked='checked'></td>"; 
	}
	else {
		echo "<input type=checkbox name=prenom_exact></td>"; 
	}
	echo "<td>".display_lng_text("SEARCH_SEXE")." &nbsp;<select name=sexe>";
	echo "<option value=>".display_lng_text("SEARCH_SEXE_NO");
	echo "</option>";
	echo "<option ";
	if ($sexe=="M") { echo "selected "; }
	echo "value='M'>".display_lng_text("SEARCH_SEXE_MALE");
	echo "</option>";
	echo "<option ";
	if ($sexe=="F") { echo "selected "; }
	echo "value='F'>".display_lng_text("SEARCH_SEXE_FEMALE");
	echo "</option>";
	echo "</select></td></tr>";

	echo "<tr><td><center>".decode_lng_text("INDEX_LNK_PLACES")."&nbsp;&nbsp;";
	echo "<input type=text name=lieu value='".$lieu."'>&nbsp;&nbsp;";
	echo display_lng_text("SEARCH_PLAIN")."&nbsp";
	if ($lieu_exact==true) {
		echo "<input type=checkbox name=lieu_exact checked='checked'></td>"; 
	}
	else {
		echo "<input type=checkbox name=lieu_exact></td>"; 
	}
	
	echo "<td colspan='2'><center>".decode_lng_text("EVNT_DATE")."&nbsp;";
	echo 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>";

	echo "<tr><td colspan='3'><center>".decode_lng_text("INDEX_EVENT")." &nbsp;&nbsp;";

	if ($choice=='birth') {$check='checked';} else {$check='';}
	echo "<input type='radio' name='choice' value='birth' ".$check.">&nbsp;".decode_lng_text("GEDTAG_BIRT")."&nbsp;&nbsp;&nbsp;";
	if ($choice=='wedding') {$check='checked';} else {$check='';}
	echo "<input type='radio' name='choice' value='wedding' ".$check.">&nbsp;".decode_lng_text("GEDTAG_MARR")."&nbsp;&nbsp;&nbsp;";
	if ($choice=='death') {$check='checked';} else {$check='';}
	echo "<input type='radio' name='choice' value='death' ".$check.">&nbsp;".decode_lng_text("GEDTAG_DEAT")."&nbsp;&nbsp;&nbsp;";
	if ($choice=='other') {$check='checked';} else {$check='';}
	echo "<input type='radio' name='choice' value='other' ".$check.">&nbsp;".decode_lng_text("PATRO_OTHER")."&nbsp;&nbsp;&nbsp;";
	if ($choice=='all') {$check='checked';} else {$check='';}
	echo "<input type='radio' name='choice' value='all' ".$check.">&nbsp;".decode_lng_text("IDX_LIEUX_ALL")."&nbsp;&nbsp;";
	echo "</center></td></tr></table></br>";
	
//	var_dump($this->Form->errors);

	echo "<table border=0>";

	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>";
		
	}
	
	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>";

	// vue d'attente pendant la recherche
	
	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><br>";
				$reload = 'recherches/individus/';	

				if ($total_pages > 1) {
					echo '<div class="pagination"><ul>';
					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=0>";
	//var_dump($_SESSION['t_recherches_resultats']);		

		for($i=$i_deb;$i<$i_fin;$i++) {
			
			$row = $_SESSION['t_recherches_resultats'][$i];
			
			echo "<tr><td>";
			echo display_lng_text("SEARCH_FICHE");		
			if ($row['sex']==1) { $flag=IMAGES."female.png"; }
			if ($row['sex']==0) { $flag=IMAGES."male.png"; }
			echo "  <img src=$flag> ";
			echo "</td><td>".$row['individu'];
			echo " </td><td> ";
			//affiche_lien_arbre($row['id_family'], $row['id_individual']);
			echo "</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>";