<?php
include('include/entete.php');
include('include/entete_adm.php');
include('config/config.inc.php');
include('config/lib.inc.php');

$maCleDeCryptage = getCleDeCryptage();	

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>FAFIH - DECIDAY</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/fonctions.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="css/style.css" rel="stylesheet" type="text/css"><script>
jQuery(document).ready(function(){
	<?php
	if(isset($_SESSION['msg']) && !empty($_SESSION['msg'])){
		?>
	alert('<?php print addslashes($_SESSION['msg']);?>');
	<?php
		$_SESSION['msg']="";
	}
	?>
	$("#export").click(function(e){
		e.preventDefault;
		window.location.href='export-suivi-cfa-adm.php';
	})
});
	
</script>
</head>

<body>
	
	<?php include('include/header.php');?>
	
	<div class="container contentPage">
	
		<?php include('include/admin-header.php');?>
		
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs bgTitrePage"></div>
		<div class="col-lg-8 col-md-8 col-sm-10 col-xs-12 titreContenuPage">Suivi des CFA/LP</div>
		<div class="col-lg-2 col-md-2 col-sm-1 hidden-xs bgTitrePage"></div>
		<div class="clearfix"></div>
		<br/><br/>
		<button type="button" class="btn btn-default btn-lg" id="export" title="Télécharger le fichier Excel">
  			<span class="glyphicon glyphicon-save-file" aria-hidden="true"></span> Export Excel
		</button>
	<?php
	$query="select * from eta";
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->execute();
	if($queryRequest->rowCount()>0){
		
		
				
		
		$data=$queryRequest->fetchAll();
		echo '<table cellpadding="0" cellspacing="0" class="table tabAdm">';
			echo "<tr><th>Etablissement</th><th>Coordonnées</th><th>Inscriptions</th><th>Sollicitations/Rendez-vous</th><th>Classes/Elèves</th><th>Date dernière connexion</th></tr>";
			foreach($data as $val){
				$count=0;
				echo "<tr><td><a href='detail-cfa-adm.php?id=".$val['id_eta']."'>".decrypter($maCleDeCryptage,$val['nom_eta'])."</a></td>";
				echo "<td>".decrypter($maCleDeCryptage,$val['adresse1'])."<br/>".decrypter($maCleDeCryptage,$val['cp'])." ".decrypter($maCleDeCryptage,$val['ville'])."</td>";
				
				//NB Trinômes Valide
//				$query="select * from capitaines,equipes where id_cap=num_capitaine and capitaines.num_eta=:id_eta and nom_eleve!='' and prenom_eleve!='' and nom_pro!='' and prenom_pro!=''";
//				$db2 = connect_base();
//				$queryRequest2 = $db2->prepare( $query );
//				$queryRequest2->bindParam( ':id_eta', $val['id_eta'], PDO::PARAM_INT );
//				$queryRequest2->execute();
//				$count=$queryRequest2->rowCount();
				
				//NB Trinômes Total
				$query="select * from capitaines,equipes where id_cap=num_capitaine and capitaines.num_eta=:id_eta";
				$db2 = connect_base();
				$queryRequest2 = $db2->prepare( $query );
				$queryRequest2->bindParam( ':id_eta', $val['id_eta'], PDO::PARAM_INT );
				$queryRequest2->execute();
				$count2=$queryRequest2->rowCount();
				
				if($count2 == 0 ){
					$count2 = "<span style='color:#A33;'>Non inscrit</span>";
				} else {
					if ($count2 == 1 ) $count2 = '<span style="color:#3A3;">'.$count2.' trinôme</span>';
					else $count2 = '<span style="color:#3A3;">'.$count2.' trinômes</span>';
				}
				
				//NB de RDV, de classes et d'élèves
				$query3="SELECT count(*) as rdvs, sum(nbClasses) as classes, sum(nbEleves) as eleves  
						FROM agenda
						INNER JOIN collegelycee ON idCoLy=numCoLy
						WHERE statut= 'reserve' AND numEta=:numEta";
				$db3 = connect_base();
				$queryRequest3 = $db3->prepare( $query3 );
				$queryRequest3->bindParam( ':numEta', $val['id_eta'], PDO::PARAM_INT );
				$queryRequest3->execute();
				$data3=$queryRequest3->fetchAll();
				if(isset($data3[0]['rdvs']))	$countRDV = $data3[0]['rdvs'];	else	$countRDV = 0;
				if(isset($data3[0]['classes']))	$classes = $data3[0]['classes'];	else	$classes = 0;
				if(isset($data3[0]['eleves']))	$eleves = $data3[0]['eleves'];	else	$eleves = 0;
				

				//NB total de sollicitations, nb total de classes et nb total d'élèves
				$query4="SELECT count(*) as sollicitations, sum(nbClasses) as classes, sum(nbEleves) as eleves
						FROM sollicitations
						WHERE traitement<>1 AND chxNumEta=:chxNumEta";
				$db4 = connect_base();
				$queryRequest4 = $db4->prepare( $query4 );
				$queryRequest4->bindParam( ':chxNumEta', $val['id_eta'], PDO::PARAM_INT );
				$queryRequest4->execute();
				$data4=$queryRequest4->fetchAll();
				if(isset($data4[0]['sollicitations']))	$countSol = $data4[0]['sollicitations'];	else	$countSol = 0;
				if(isset($data4[0]['classes']))	$classes = $classes + $data4[0]['classes'];
				if(isset($data4[0]['eleves']))	$eleves = $eleves + $data4[0]['eleves'];					
		
				
				// Date de dernière connexion
				$derniereCo = MysqlToDateFr($val['date_co']);
				if($derniereCo == "01/01/1970") $derniereCo = "";
				
			
				echo "<td>".$count2."</td>";
				echo "<td align=\"center\">".$countSol.'/'.$countRDV."</td>";
				echo "<td align=\"center\">".$classes.'/'.$eleves."</td>";
				echo "<td>".$derniereCo."</td>";
				
				echo "</tr>";
			}
		echo '</table>';
	}
	?>
		<br /><div class="btnDeci"><a href="index-adm.php">Retour</a></div>		
	</div>
	
	<?php include('include/footer.php');?>

</body>
</html>