<?php
session_start();
if(!isset($_SESSION['auth'])){
	header("location:index.php");
}
if($_SESSION['profil']!=100 && !in_array("view_all_users",$_SESSION['permissions']) && !in_array("edit_all_users",$_SESSION['permissions']) && !in_array("delete_all_users",$_SESSION['permissions']) && !in_array("edit_agence_users",$_SESSION['permissions']) && !in_array("view_agence_users",$_SESSION['permissions']) && !in_array("delete_agence_users",$_SESSION['permissions'])) {
	header("location:dashboard.php");
}
require_once("../inc/confNetisse.php");
$values = array(':supp' => 0);
$FILTER = "";
if ($_SESSION['profil']!=100 && !in_array("view_all_users",$_SESSION['permissions']) && !in_array("edit_all_users",$_SESSION['permissions'])) {
	$FILTER = " lnk.id_agence = :id_agence";
	$values[':id_agence'] = intval($_SESSION['agence']);
}
$DATA = "";
/*
$q = "SELECT GROUP_CONCAT(lnk.id_agence separator '-') as ids,adm.id_admin, adm.nom, adm.prenom, adm.email, adm.profil, adm.inactif, prf.nom as nom_profil FROM ca_admins as adm
LEFT JOIN ca_profils as prf ON prf.profil = adm.profil
LEFT JOIN ca_links as lnk ON lnk.id_admin = adm.id_admin
WHERE supp=0 ";
$q .= $FILTER;
$q .= " GROUP BY adm.id_admin ORDER BY adm.nom ASC";
*/

$q = "SELECT lnk.ids,adm.id_admin, adm.nom, adm.prenom, adm.email, adm.profil, adm.inactif, prf.nom as nom_profil FROM ca_admins as adm
LEFT JOIN ca_profils as prf ON prf.profil = adm.profil
LEFT JOIN (SELECT l.id_admin, GROUP_CONCAT(l.id_agence separator '-') as ids FROM ca_links l GROUP BY l.id_admin) as lnk ON lnk.id_admin = adm.id_admin
WHERE supp=:supp";
$q .= $FILTER;
//echo "<li>".$q;

$result = excuteQuery($q, $values);
$r = $result['stmt'];
if($result['status']){
	while($o = $r->fetchObject()) {
		if ($o->inactif == 0) {
			//$INACTIF = "<input type='checkbox' name='inactif' checked/>";
			$INACTIF = "Actif<br/><img src='img/green.gif' width='15' height='15' border='0'/>";
		} else {
			//$INACTIF = "<input type='checkbox' name='inactif' />";
			$INACTIF = "Inactif<br/><img src='img/red.gif' width='15' height='15' border='0'/>";
		}

		$DATA .= "<tr>\r\n<td>".$o->nom."</td>\r\n<td>".$o->prenom."</td>\r\n<td>".$o->email."</td>\r\n<td>".$o->nom_profil."</td>\r\n<td>".$INACTIF."</td>";
		if($_SESSION['profil']==100 || in_array("edit_all_users",$_SESSION['permissions']) || (in_array("edit_agence_users",$_SESSION['permissions']) && in_array($_SESSION['agence'],explode('-',$o->ids)))) {
			if($o->profil==100 && $_SESSION['profil']!=100){
				$DATA .= "\r\n<td></td>";
			}
			else{
				$DATA .= "\r\n<td><a href='admUpd.php?uid=".$o->id_admin."' ".$o->prenom." ".$o->nom."'>Editer</a></td>";
			}
		}
		else{
			$DATA .= "\r\n<td></td>";
		}
		$DATA .= "\r\n</tr>\r\n";
	}
}
?>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><?php echo $PROJECT_TITLE;?> : Liste des utilisateurs</title>
	<link rel="stylesheet" href="css/font-awesome.min.css" />
	<link rel="stylesheet" type="text/css" href="css/cadif.css" />
	<link rel="stylesheet" type="text/css" href="css/dashboard.css" />
	<link rel="stylesheet" type="text/css" href="css/cans/jquery-ui-1.8.17.custom.css" />
	<!--<link rel="stylesheet" type="text/css" href="css/tableTools_jui.css" />-->
	<link rel="stylesheet" type="text/css" href="css/datatable_jui.css" />


	<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.22.custom.min.js"></script>
	<script type="text/javascript" src="js/jquery.dataTables.js"></script>

	<script type="text/javascript">
	$(document).ready( function () {

		oTable = $('#AdminsDataTable').dataTable( {
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"sDom": '<"H"Tfr>t<"F"ip>',

			"oTableTools": {
				"sSwfPath": "./swf/copy_csv_xls_pdf.swf",
				"aButtons": [
						{
	                    "sExtends":    "xls",
	                    "sButtonText": "Excel"
	                    },
	                    {
	                    "sExtends":    "pdf",
	                    "sButtonText": "PDF"
	                    }
				]
			},

			"oLanguage": {
				"sProcessing":     "Traitement en cours...",
			    "sLengthMenu":     "Afficher _MENU_ &eacute;l&eacute;ments",
			    "sZeroRecords":    "Aucun &eacute;l&eacute;ment &agrave; afficher",
			    "sInfo":           "Affichage : _START_ &agrave; _END_ sur _TOTAL_ &eacute;l&eacute;ments",
			    "sInfoEmpty":      "Affichage de l'&eacute;lement 0 &agrave; 0 sur 0 &eacute;l&eacute;ments",
			    "sInfoFiltered":   "(filtr&eacute; de _MAX_ &eacute;l&eacute;ments au total)",
			    "sInfoPostFix":    "",
			    "sSearch":         "Rechercher&nbsp;/&nbsp;Filtrer&nbsp;:",
			    "sLoadingRecords": "T&#233;l&#233;chargement...",
			    "sUrl":            "",
			    "oPaginate": {
			        "sFirst":    "Premier",
			        "sPrevious": "Pr&eacute;c&eacute;dent",
			        "sNext":     "Suivant",
			        "sLast":     "Dernier"
			    }
			}

		});

	});
	</script>
</head>


<body>

<div id="global">

	<div id="home">
		<div id="logo"><a href="dashboard.php"><img src="<?php echo $SERVER; ?>assets/img/logo.png" height="110" border="0"/></a></div>
		<div id="username">
			<b><?php echo isset($_SESSION["nom"]) ? $_SESSION["nom"] : $_SESSION["matricule"];?></b> &nbsp; | &nbsp; <a href="close.php" class="link" title="Fermer et clore votre session">D&#233;connexion</a>
			<br/>
		</div>
	</div>
	<div id="panels">
	  <?php include "menu.php";?>
	  <div class="main-content">
		<div id="title">
			Liste des utilisateurs
		</div>
		<div>
			<?php if(in_array("add_all_users",$_SESSION['permissions']) || in_array("add_agence_users",$_SESSION['permissions'])) {?>
				<a href="admNew.php" class="formNav2"><i class="fa fa-plus-circle" style="color:#ED1B2F;font-size:150%"></i> <span style="color:#ED1B2F;">Ajouter un utilisateur</span></a>
				<br/><br/>
			<?php } ?>

		</div>
		<div id="list">
			<table cellpadding="0" cellspacing="0" border="0" class="display" id="AdminsDataTable">
				<thead>
				<tr>
					<td>Nom</td>
					<td>Pr&#233;nom</td>
					<td>Email</td>
					<td>Profil</td>
					<td>Statut</td>
					<td>Action</td>
				</tr>
				</thead>
				<tbody>
					<?php
						echo $DATA;
					?>
			</tbody>

			</table>
		</div>
	  </div>
	</div>

</div>

<?php include 'footer.php';?>
</body>

</html>