<?php
require($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/inc/head.php');
require($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/inc/sidebar-eleve.php');
//require($_SERVER['DOCUMENT_ROOT'].dirname($_SERVER["PHP_SELF"]).'/submenu.inc.php');





// FILTRES 

$_SELECT['search']['type_contrat']		= str_replace('value="'.$_GET['type_contrat'].'"', 'value="'.$_GET['type_contrat'].'" selected ', $_SELECT['search']['type_contrat']);
$_SELECT['search']['secteur_activite']	= str_replace('value="'.$_GET['secteur_activite'].'"', 'value="'.$_GET['secteur_activite'].'" selected ', $_SELECT['search']['secteur_activite']);
$_SELECT['search']['commune']			= str_replace('value="'.$_GET['commune'].'"', 'value="'.$_GET['commune'].'" selected ', $_SELECT['search']['commune']);
$_SELECT['search']['niveau_formation']	= str_replace('value="'.$_GET['niveau_formation'].'"', 'value="'.$_GET['niveau_formation'].'" selected ', $_SELECT['search']['niveau_formation']);



?>
	<style>
		label{width:140px;;font-size:1em}
		.filtres{background:#f5f5f5;margin-bottom:50px;}
		.filtres td{border:0;text-align:left;}
		input[type=text],select {width:150px;}
	</style>

	<form>
		<table class="filtres">
			<tr>
				<td style="width:50%;">
					<label>Type d'emploi : </label>
					<?php echo $_SELECT['search']['type_contrat'];?>
				</td>
				<td  style="width:50%;">
					<label>Durée : </label> 
					<select name="duree">
						<option value="null">Indifférent</option>
						<option value="1" <?php if($_GET['duree'] == 1) echo 'selected'?>>1 mois</option>
						<option value="2" <?php if($_GET['duree'] == 2) echo 'selected'?>>2 mois</option>
						<option value="3" <?php if($_GET['duree'] == 3) echo 'selected'?>>3 mois</option>
						<option value="4" <?php if($_GET['duree'] == 4) echo 'selected'?>>4 mois</option>
						<option value="5" <?php if($_GET['duree'] == 5) echo 'selected'?>>5 mois</option>
						<option value="6" <?php if($_GET['duree'] == 6) echo 'selected'?>>6 mois</option>
						<option value="12" <?php if($_GET['duree'] == 12) echo 'selected'?>>6-12 mois</option>
						<option value="24" <?php if($_GET['duree'] == 24) echo 'selected'?>>12-24 mois</option>
						<option value="25" <?php if($_GET['duree'] == 25) echo 'selected'?>>+ de 24 mois</option>
					</select> 
				</td>
			</tr>
			<tr>
				<td>
					<label>Secteur d'activité : </label>
					<?php echo $_SELECT['search']['secteur_activite'];?>
				</td>
				<td>
					<label>Lieu d'exercice : </label>
					<?php echo $_SELECT['search']['commune'];?>
				</td>
			</tr>
			<tr>
				<td>
					<label>Niveau de formation : </label>
					<?php echo $_SELECT['search']['niveau_formation'];?>
				</td>
				<td>
					<label>Mot clé : </label>
					<input type="text" name="k"/>
				</td>
			</tr>
			<tr>
				<td>
					<label>N° de l'offre : </label>
					<input type="text" name="id_offre" style="width:40px;"/>
				</td>
				<td>
					<label></label>
					<input type="submit" class="bg-red" style="padding:5px 10px;" value="Rechercher">
				</td>
			</tr>
		</table>
	</form>
	

<?php
// REQUETE + TABLEAU RESULTAT
	
	$where = " WHERE validation = 'V' ";
	
	// DETECTION SI RECHERCHE PAR REGION
	if( $_GET['commune'] == 'NORD' || $_GET['commune'] == 'OUEST' || $_GET['commune'] == 'EST' || $_GET['commune'] == 'SUD' ) $region_search = 1;
	else																													  $region_search = 0;
	
	// DUREE
	if		($_GET['duree'] == '12')								$where .= " AND duree_contrat >= '6' AND duree_contrat <= '12' ";
	elseif	($_GET['duree'] == '24')								$where .= " AND duree_contrat >= '12' AND duree_contrat <= '24' ";
	elseif	($_GET['duree'] == '25')								$where .= " AND duree_contrat > '24' ";
	elseif	( isset($_GET['duree'])	 && $_GET['duree'] <> "null")	$where .= " AND duree_contrat = '".$_GET['duree']."' ";
	
	// KEYWORD
	if( isset($_GET['k']) AND strlen($_GET['k']) > 2 ){
		//echo "keyword ok";
		$_KEYWORD = explode(" ",$_GET['k']);
		foreach($_KEYWORD as $key => $value){
			if( strlen($value) > 2)
				$where .= " AND ( intitule LIKE \"%$value%\" OR descriptif LIKE \"%$value%\" OR objectifs LIKE \"%$value%\" OR missions LIKE \"%$value%\" ) ";

		}
	}

	if( isset($_GET['commune'])			 && $_GET['commune'] <> "null" && $region_search == 0 )	$where .= " AND commune				= '".$_GET['commune']."' ";
	if( isset($_GET['niveau_formation']) && $_GET['niveau_formation'] <> "null" )				$where .= " AND niveau_formation	= '".$_GET['niveau_formation']."' ";
	if( isset($_GET['type_contrat'])	 && $_GET['type_contrat'] <> "null" )					$where .= " AND type_contrat		= '".$_GET['type_contrat'] ."' ";
	if( isset($_GET['id_offre'])		 && $_GET['id_offre'] <> "" )							$where .= " AND id_offre			= '".$_GET['id_offre'] ."' ";
	

	//echo $q = "SELECT * FROM site_offres ".$where;
	
	$sql = dbquery("SELECT * FROM site_offres ".$where." ");
		
	$resultats_nb		= 0 ;
	$resultats_tableau	= ""; 

	for($i=0;$i<dbnumrows($sql);$i++){
		$id_offre				= dbresult($sql,$i,"id_offre");
		$id_entreprise			= dbresult($sql,$i,"id_entreprise");
		$intitule				= dbresult($sql,$i,"intitule");
		$type_contrat			= dbresult($sql,$i,"type_contrat");
		$duree_contrat			= dbresult($sql,$i,"duree_contrat");
		$commune				= dbresult($sql,$i,"commune");
		$niveau_formation		= dbresult($sql,$i,"niveau_formation");
		$date_enregistrement	= dbresult($sql,$i,"date_enregistrement");


		// EXCEPTIONS (secteur activité + region)

		$except = 0;
		
		$sql2				= dbquery("SELECT * FROM site_entreprises WHERE id_entreprise = '".$id_entreprise."'");	
		$secteur_activite	= dbresult($sql2,0,"secteur_activite");
		if( isset($_GET['secteur_activite']) && $_GET['secteur_activite']<>'null')	
			if( $_GET['secteur_activite'] <> $secteur_activite) $except++;

		$sql3				= dbquery("SELECT * FROM codes_postaux WHERE code_postal = '".$commune."'");
		$region				= dbresult($sql3,0,"region");
		if($region_search == 1)	
			if( $_GET['commune'] <> $region)					$except++;
		
		if($except == 0){
			$resultats_nb ++;

			//<td>'.$_TAB['secteur_activite'][$secteur_activite].'</td>
			
			$resultats_tableau .='
				<tr>
					<td>'.$id_offre.'</td>
					<td class="left">'.$intitule.'</td>
					<td>'.$_TAB['type_contrat'][$type_contrat].'</td>
					<td>'.$_TAB['niveau_formation'][$niveau_formation].'</td>
					<td>'.$duree_contrat.' mois</td>
					<td>'.$region.'</td>
					<td>'.date_iso_fr($date_enregistrement).'</td>
					<td><a class="overlay-ajax" rel="#overlay" href="/espace_personnel/overlay/apercu/offre/full.php?id_offre='.$id_offre.'"><img src="/espace_personnel/images/page_search.png"/></a></td>
				</tr>
			';
		}
	}
	$resultats_tableau .= '</tbody></table>';


	if( $resultats_nb == 0 )
		echo '</i>Aucun résultat.</i>';
	else{
?>

		<table class="tablesorter">
		<thead>
			<tr>
				<th style="width:5%">N°</th>
				<th class="left" style="width:30%">Intitulé</th>
				<!-- <th style="width:20%">Secteur<br>d'activité</th> -->
				<th style="width:20%">Type</th>
				<th style="width:12%">Niveau de<br>formation</th>
				<th style="width:10%">Durée</th>
				<th style="width:10%">Région</th>
				<th style="width:8%">Date</th>
				<th style="width:5%">Détails</th>
			</tr>
		</thead>
		<tbody>	
<?php
		echo $resultats_tableau;
	}	
?>
</div>

<div class="apple_overlay" id="overlay" style="max-height:70%;overflow:auto;" >	
	<div class="contentWrap"></div>
</div>

<script>
// TABLESORTER
	$(document).ready(function() 
    { 
        $(".tablesorter").tablesorter({
			headers: { 0: { sorter: false}, 1: {sorter: false}, 7: {sorter: false} }
		}); 
    } 
	);
</script>

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/inc/end.php');
?>