<?php
session_start();
if(!isset($_SESSION['auth'])) {
	header("location:index.php");
}

require_once("../inc/confNetisse.php");
require_once("./functions.php");

$APID = 0;

if(!isset($_GET["apid"])) {
	header("location:agencePrincipaleList.php");
} else {
	$APID = intval($_GET["apid"]);
}
$q = "SELECT ap.id_agence_principale, nom_agence_principale, id_secteur, GROUP_CONCAT(id_conseiller) directeurs FROM ca_agence_principales ap LEFT JOIN ca_directeur_aps d ON d.id_parent = ap.id_agence_principale WHERE ap.id_agence_principale=:id_agence_principale";
$values = array(':id_agence_principale' => $APID);
$result = excuteQuery($q, $values);
$r = $result['stmt'];
if($r->rowCount()== 0) {
	header("location:agencePrincipaleList.php");
} else {
	$o = $r->fetchObject();
}
$directeurs = explode(",",$o->directeurs);
$list = getConseillers(null,6,'secteur','array');
$conseillers = "<ul>";
for($i=0; $i<count($list);$i++){
$conseiller = $list[$i];
$conseillers .= "<li class='cns-item' data-parent='".$conseiller->id_parent."'><input type='checkbox' name='directeurs[]' value='".$conseiller->id_conseiller."' ";
$conseillers .= (in_array($conseiller->id_conseiller,$directeurs))?"checked":"";
$conseillers .= " /> ".$conseiller->nom.' '.$conseiller->prenom.' ('.$conseiller->id_agent.')';
}
$conseillers .= "</ul>";
?>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><?php echo $PROJECT_TITLE;?> : Liste des agences principales</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">
		function delAgencePrincipale() {
			if (confirm("Etes-vous sûr de vouloir supprimer cette agence principale ?")) {
				document.getElementById("supp").value = 1;
				document.formAgencyUpd.submit();
			} else {
				document.getElementById("supp").value = 0;
			}

		}

		function checkForm() {
			if (document.getElementById("nom").value == "") {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Nom'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("nom").focus();} });
				POP.dialog("open");
			} else if (document.getElementById("secteur").value == 0) {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Secteur'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("secteur").focus();} });
				POP.dialog("open");
			} else {
				document.formAgencyUpd.submit();
			}
		}
		function addAgence(){
			window.location.href = "agcNew.php?apid=<?php echo $APID;?>";
		}

		$(function(){
			$(".cns-item").hide();
			$(".cns-item[data-parent='<?php echo $o->id_secteur;?>']").show();
			$("#secteur").on("change", function(){
				var parentID= $(this).val();
				$(".cns-item").hide();
				$(".cns-item input").removeAttr("checked");
				$(".cns-item[data-parent='"+parentID+"']").show();
			});
		});
	</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">
				Edition d'une agence principale
			</div>
			<div id="form">
		<form name="formAgencyUpd" id="formAgencyUpd" action="do.php" method="post">
			<input type="hidden" name="act" value="agencePrincipaleUpd"/>
			<input type="hidden" name="apid" value="<?php echo $APID; ?>"/>
			<input type="hidden" name="supp" id="supp" value="0"/>

			<table align="center" class="form">
			<tr>
			<td>Secteur</td><td><select name="secteur" id="secteur"><option value="">-- Sélectionnez un secteur --</option><?php echo getSecteurs($o->id_secteur);?></select></td>
			</tr>
			<tr>
			<td width="100">Nom de l'agence principale</td>
			<td><input type="text" name="nom" id="nom" size="30" tabindex="1" value="<?php echo $o->nom_agence_principale; ?>"/></td>
			</tr>
			<tr>
			<td width="100">Directeur(s)</td>
			<td><?php echo $conseillers; ?></td>
			</tr>
			<tr>
			<td colspan="2">
				<br/><br/>
				<input type="button" class="formAct" name="supprimer" id="supprimer" value="Supprimer" onclick="delAgencePrincipale();" tabindex="9"/>
				&nbsp;&nbsp;<input type="button" class="formAct" name="valider" id="valider" value="Valider" onclick="checkForm();" tabindex="8"/>&nbsp;&nbsp;<input type="button" class="formAct2" name="ajouter" id="ajouter" value="Ajouter une agence" onclick="addAgence();" tabindex="9"/></td>
			</tr>
			</table>
		</form>
		</div>
	</div>
	<div id="nav">
		<a href='dashboard.php' class='formNav'>Retour au menu</a>
		<a href='secteurList.php' class='formNav'>Retour &#224; la liste des agences principales</a>
	</div>

</div>

<?php include 'footer.php';?>
<script>
document.getElementById('nom').focus();
</script>

</body>

</html>