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

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

$EID = $AID = 0;
if(!isset($_GET["eid"])) {
	header("location:entList.php");
} else {
	$EID = intval($_GET["eid"]);
}

if(!isset($_GET["aid"])) {
	header("location:agcList.php");
} else {
	$AID = intval($_GET["aid"]);
}
$entity_icon = false;
$q_agency_siege = "SELECT siege FROM ca_agences WHERE id_agence = ".$AID." LIMIT 1";
$stmt_agency_siege = excuteQuery($q_agency_siege);
$res_agency_siege = $stmt_agency_siege["stmt"];
$o_agency_siege = $res_agency_siege->fetchObject();
if($o_agency_siege->siege == 1 || $GLOBALS['BO_CONF']['ENTITY_ICON']){
	$entity_icon = true;
}
$s = $s1 = $s2 = $s3 = "";
$pictoCol = $entity_icon ? ", picto" : "";
$q = "SELECT id_entite, id_agence, libelle, enabled, borne_color, pile_color".$pictoCol." FROM ca_entites WHERE id_entite=:id_entite AND supp=:supp ";
// print $q;
$values = array(':id_entite' => $EID, ':supp' => 0);
$result = excuteQuery($q, $values);
$r = $result['stmt'];
if($r->rowCount() == 0) {
	header("location:entList.php");
}
$o = $r->fetchObject();

$PICTO = $o->picto;
$entity_icon_required = ($PICTO == "" || $PICTO == null) ? true : false;
$q = "SELECT count(1) nb FROM ca_entites WHERE enabled = 1 AND id_agence=:id_agence AND supp=:supp ";
$values = array(':id_agence' => $o->id_agence, ':supp' => 0);
$result = excuteQuery($q, $values);
$r = $result['stmt'];
$row = $r->fetchObject();
$NBENABLED = $row->nb;

if($o->enabled == 0){
	$CHECKED = "";
}
else{
	$CHECKED = "checked='checked'";
}
$AID = $o->id_agence;
$BORNECOLOR = $o->borne_color;
$PILECOLOR = $o->pile_color;

$q2 = "SELECT o.id_orientation, o.texte, o.orientation, o.label FROM ca_orientations o WHERE o.id_entite=:id_entite AND o.supp=:supp ORDER BY o.orientation ASC, o.id_orientation ASC";
$v2 = array(':id_entite' => $EID, ':supp' => 0);
$result = excuteQuery($q2, $v2);
$r2 = $result['stmt'];
$MSG = "";
$i = 1;
$NBSRDV = $NBINCONNU = $NBRDV = 0;

while ($o2 = $r2->fetchObject())
{
	$TEXTE = $o2->texte;

	$TYPE = $o2->label;
	$MSG .= "<tr class='blocmsg'>
	<td class='agencyRub'><br/>".$TYPE."</td>
	<td>";
	if(in_array("edit_all_orientations",$_SESSION['permissions']) || in_array("edit_own_orientations",$_SESSION['permissions'])) {
		$MSG .= "<br/><input type='button' class='formAct2' name='editer' id='editer' value='Editer' onclick=\"document.location.href='agcMsgUpd.php?aid=".$AID."&eid=".$EID."&m=".$o2->id_orientation."';\"/>";
	}
	$MSG .= "</td>
	</tr>
	<tr class='blocmsg'>
	<td colspan='2'>- ".nl2br($TEXTE)."</td>
	</tr>
	<tr><td><br/></td><td><br/></td></tr>";
	$i++;
}


$entitiesFieldsDisplay = $GLOBALS["BO_CONF"]["ENTITIES"]?'display:none':'';
$activeDisplay = $GLOBALS["BO_CONF"]["ENTITIES"]?'':'display:none';

?>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><?php echo $PROJECT_TITLE;?> : Edition d'un utilisateur</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" />
	<link rel="stylesheet" media="screen" type="text/css" href="css/spectrum.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" src="js/spectrum.js"></script>
	<script type="text/javascript">
		function delEnt() {
			if (confirm("Etes-vous sûr de vouloir supprimer cette entité ?")) {
				document.getElementById("supp").value = 1;
				document.formEntitesUpd.submit();
			} else {
				document.getElementById("supp").value = 0;
			}

		}

		function checkForm() {
			if (document.getElementById("entite").value == "") {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Libellé'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("entite").focus();} });
				POP.dialog("open");
			}
			<?php if($entity_icon && $entity_icon_required){ ?>
			else if(document.getElementById("picto")){
				if (document.getElementById("picto").files.length == 0) {
					var POP = $('<div></div>').html("<center>Veuillez importer un picto.</center>").dialog({autoOpen: false,title: 'Erreur'});
					POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("picto").focus();} });
					POP.dialog("open");
				}else{
					document.formEntitesUpd.submit();
				}
			}
			<?php } ?>
			 else {
				document.formEntitesUpd.submit();
			}
			
		}

		$(document).ready(function(){
			$('#pilecolor').spectrum({
		    color: "<?php echo $PILECOLOR?$PILECOLOR:$GLOBALS["BORNE_CONF"]["COLORS"]["PRIMARYCOLOR"];?>",
		    showInput: true,
		    className: "full-spectrum",
		    showInitial: true,
		    showSelectionPalette: true,
		    preferredFormat: "hex",
			});
			$('#bornecolor').spectrum({
		    color: "<?php echo $BORNECOLOR?$BORNECOLOR:$GLOBALS["BORNE_CONF"]["COLORS"]["PRIMARYCOLOR"];?>",
		    showInput: true,
		    className: "full-spectrum",
		    showInitial: true,
		    showSelectionPalette: true,
		    preferredFormat: "hex",
			});
		});
	</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">
		<?php
		if($GLOBALS["BO_CONF"]["ENTITIES"]){
			echo "Edition d'une entité";
		}
		else{
			echo "Orientations";
		}
		?>

	</div>
	<div id="panelsCns">
		<div id="form">
		<form name="formEntitesUpd" id="formEntitesUpd" action="do.php" method="post" enctype="multipart/form-data">
			<input type="hidden" name="act" value="entitesUpd"/>
			<input type="hidden" name="aid" value="<?php echo $AID; ?>"/>
			<input type="hidden" name="eid" value="<?php echo $EID; ?>"/>
			<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
			<input type="hidden" name="supp" id="supp" value="0"/>
			<input type="hidden" name="label" id="label" value="<?php echo $o->libelle ; ?>"/>
			<br/>
			<table align="center" class="form">
			<tr style="<?php echo ($GLOBALS["BO_CONF"]["ENTITIES"])?'':'display:none;'?>">
			<td><h3>Entité</h3></td>
			<td><input type="text" name="entite" id="entite" size="30" tabindex="1" value="<?php echo $o->libelle ; ?>"/>
			</td>
			</tr>
			<tr style="display:<?php echo $GLOBALS['BO_CONF']['COLORBYENTITY']?'table-row':'none';?>">
			<td>Couleur Borne</td>
			<td>
				<input type="text" name="bornecolor" id="bornecolor" value="<?php echo $BORNECOLOR;?>" style="width: 229px;">
			</td>
			</tr>
			<tr style="display:<?php echo $GLOBALS['BO_CONF']['COLORBYENTITY']?'table-row':'none';?>">
			<td>Couleur Pile</td>
			<td>
				<input type="text" name="pilecolor" id="pilecolor" class="color-input" value="<?php echo $PILECOLOR;?>" style="width: 229px;">
			</td>
			</tr>
			<tr style="<?php echo $activeDisplay;?>">
			<td>Activé</td>
			<td><input type="checkbox" name="enabled" id="enabled"  <?php echo $CHECKED;?> onclick="toggleActive(event)"/></td>
			</tr>
			<tr style="display:<?php echo $entity_icon?'table-row':'none';?>">
				<td class="cnsPhoto">Picto</td>
				<td class="cnsPhoto" style="background-color:#cccccc">
					<?php if($PICTO != ""){?><center><img src="<?php echo $SERVER.$PICTO;?>" width="120" height="auto" border="0"/></center><?php } ?>
					<br/>
					<span class="cnsPhotoText">
						<b><u>Caract&#233;ristiques du fichier</u> :</b>
						<br/>
						Poids maximum : 1 Mo.
						<br/>
						Type de fichier : JPG / PNG.
						<br/>
						Dimensions attendues : Largeur : <?php echo $GLOBALS["BO_CONF"]["IMAGES"]["entite"]["width"]; ?> pixels. | Hauteur : <?php echo $GLOBALS["BO_CONF"]["IMAGES"]["entite"]["width"]; ?> pixels.
					</span>
					<br/><br/>
					<input type="file" tabindex="4" name="picto" id="picto" size="30" accept="image/*"/>
				</td>
			</tr>
			<table align="center" class="form" id="bloc" style="border-collapse:collapse;<?php echo $entitiesFieldsDisplay;?>">
			<?php
			echo $MSG;
			?>
			</table>
			<?php if(in_array("add_all_orientations",$_SESSION['permissions']) || in_array("add_own_orientations",$_SESSION['permissions'])) {?>
			<table align="center" class="form" style="<?php echo $entitiesFieldsDisplay;?>">
			<tr>
			<td><input type='button' class='formAct2' name='ajouter' id='ajouter' value='Ajouter un Bloc Message' onclick='document.location.href="agcMsgNew.php?aid=<?php echo $AID;?>&eid=<?php echo $EID;?>";'  /></td>
			</tr>
			</table>
			<?php } ?>
			<table align="center" class="form">
			<tr>
			<td colspan="2">
		   	<br/>
				<?php if($NBENABLED>1 && in_array("delete_all_agences",$_SESSION['permissions']) || (in_array("delete_own_agence",$_SESSION['permissions'])&& in_array($AID,$_SESSION['agences']))) {?>
					<input type="button" class="formAct" name="supprimer" id="supprimer" value="Supprimer" onclick="delEnt();" tabindex="9"/>
				<?php } ?>
				<input type="button" class="formAct" name="valider" id="valider" value="Valider" onclick="checkForm();" tabindex="10"/>
			</td>
			</tr>
			</table>
		</form>
		</div>
	</div>
	<div id="nav">
		<?php
		if($GLOBALS["BO_CONF"]["ENTITIES"]){
		?>
		<a href="#" onclick="goBack()" class='formNav2'>Retour &#224; la liste des entités</a><!--href='entList.php?aid=<?php print $_GET['aid'];?>' -->
		<?php
		}
		else{
		?>
		<a href="#" onclick="goBack()" class='formNav2'>Retour &#224; la liste des agences</a><!--href='agcList.php' -->
		<?php
		}
		?>

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

</div>
<?php include 'footer.php';?>
<script type="text/javascript">
	function delEnt() {
			$.post("ajax/deleteEnt.php", function(data) {
			var POP = $('<div></div>').html(data).dialog({ autoOpen: false, title: "Suppression d'une entité " });
				POP.dialog({
					autoOpen: false,
					modal: true,
					draggable: false,
					width: 'auto',
					height: 'auto',
					overflow: "auto",
					close: function(ev, ui) {
						$(this).empty().remove();
					},
					open: function() {
						$(this).parent().css('height', 'auto');
						$(this).css('height', '100%');
					}
				});

				POP.dialog("open");
			});

		}
		function delEntConfirmed(){
			document.getElementById("supp").value = 1;
			document.formEntitesUpd.submit();
		}
</script>
</body>

</html>