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

if(!in_array("add_all_orientations",$_SESSION['permissions']) && !in_array("add_own_orientations",$_SESSION['permissions'])) {
	header("location:dashboard.php");
}
require_once("../inc/confNetisse.php");

$s2 = "";
$multilang = (isset($GLOBALS["BORNE_CONF"]["LANGUAGES"]) && count($GLOBALS["BORNE_CONF"]["LANGUAGES"])>1) ? true : false;

$AID = $EID = 0;
$READONLY = "";
if(($_GET["aid"] == 0) && ($_GET["eid"] == 0)) $READONLY = " readonly disabled";
$READONLYENTITY = "";
if($_GET["aid"] == 0) $READONLYENTITY = " readonly disabled";
if((!isset($_GET["aid"])) && (!isset($_GET["eid"]))) {
	//header("location:agcList.php");

	$qa = "SELECT id_agence, nom_agence, code_agence, initiales FROM ca_agences WHERE is_active=:is_active AND siege = 1 AND supp=:supp ORDER BY nom_agence ASC";
	$va = array(':is_active' => 1, ':supp' => 0);
	$result = excuteQuery($qa, $va);
	$ra = $result['stmt'];
	$listAgc = "";
	while ($oa = $ra->fetchObject()) {
		$listAgc .= "<option value='".$oa->id_agence."'>".$oa->nom_agence."</option>";
	}
} else {
	$AID = intval($_GET["aid"]);
	$EID = intval($_GET["eid"]);
}
if ($AID > 0) {
    $q = "SELECT a.nom_agence FROM ca_agences a WHERE a.id_agence=:id_agence";
    $values = array(':id_agence' => $AID);
	$result = excuteQuery($q, $values);
	$r = $result['stmt'];
    $o = $r->fetchObject();
    $NOMAGENCE = "l'agence " . $o->nom_agence;
} else {
    $q = "SELECT nom_agence FROM ca_agences WHERE id_agence=:id_agence";
    $values = array(':id_agence' => $AID);
    $result = excuteQuery($q, $values);
	$r = $result['stmt'];
    $o = $r->fetchObject();
}

$defaultMsgs = array();
$langs = "";
if($multilang){
	foreach($GLOBALS["BORNE_CONF"]["LANGUAGES"] as $lang){
		$langs .= "<option value='".strtolower($lang)."'>".$lang."</option>";
		$defaultMsgs[$lang] = $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"][$lang]["RDV"];
	}
}
else{
	$defaultMsgs["FR"] = $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["SITEMAP"];
}
?>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><?php echo $PROJECT_TITLE;?> : Edition d'un plan du site</title>
	<link rel="stylesheet" type="text/css" href="css/cans/jquery-ui-1.8.17.custom.css" />
	<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" />
	<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">
		function delAgc() {
			if (confirm("Etes-vous sûr de vouloir supprimer cette agence ?")) {
				document.getElementById("supp").value = 1;
				document.formSiteMapNew.submit();
			} else {
				document.getElementById("supp").value = 0;
			}

		}

		function generateCode() {
			if (document.getElementById("initials").value != "") {
				var initials = document.getElementById("initials").value;
				var liste = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0","1","2","3","4","5","6","7","8","9");
				var chaine ='';
				for(i = 0; i < 15; i++)
				{
					chaine = chaine + liste[Math.floor(Math.random()*liste.length)];
				}
				document.getElementById("code").value = initials+"_"+chaine;
			}
		}


		function checkMax(obj,maxlength){
		  if (obj.value.length > maxlength) {
		    obj.value = obj.value.substring(0, maxlength);
		    var POP = $('<div></div>').html("<center>Votre message est trop long. Merci de le raccourcir.</center>").dialog({autoOpen: false,title: 'Erreur'});
			POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById(obj).focus();} });
			POP.dialog("open");
		   }
		}

		function checkForm() {
			
			if (document.getElementById("agcID") && document.getElementById("agcID").value == "") {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Agence'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("label").focus();} });
				POP.dialog("open");
			}
			else if (document.getElementById("label").value == "") {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Titre'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("label").focus();} });
				POP.dialog("open");
			}
			else if (document.getElementById("picto").value == "") {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Picto'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("label").focus();} });
				POP.dialog("open");
			}
			else if (document.getElementById("salle").value == "") {
				var POP = $('<div></div>').html("<center>Vous devez renseigner le champ 'Plan'.</center>").dialog({autoOpen: false,title: 'Erreur'});
				POP.dialog({autoOpen:false, modal:true, draggable: false, width:360,close:function(ev, ui){document.getElementById("label").focus();} });
				POP.dialog("open");
			}
			 else {
			document.formSiteMapNew.submit();
			}
		}
		function toggleUpload(){
			$("#cnsUpload").show();
			$("#uploadLink").hide();
		}

	</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">
			Nouveau plan <?php if($AID >0){ echo "pour ".$NOMAGENCE;}?>
		</div>
		<div id="panelsedit">
			<div id="formCns">
			<form name="formSiteMapNew" id="formSiteMapNew" action="do.php" method="post" enctype="multipart/form-data">
				<input type="hidden" name="act" value="siteMapNew"/>
				<input type="hidden" name="aid" value="<?php echo $AID; ?>"/>
				<input type="hidden" name="eid" value="<?php echo $EID; ?>"/>
				<input type="hidden" name="supp" id="supp" value="0"/>
				<input type="hidden" name="type" id="type" value="2"/>
				<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />

				<table align="center" class="form">
				<?php
					if(!isset($_GET["aid"])) {
					?>
				<tr>
				<td>Agence</td>
				<td>
					<select style="width: 200px;" name="agc" id="agcID" required="true" onchange="selectAgency(this.value)">
						<option value="">Sélectionner une agence</option>
						<?php echo $listAgc; ?>
					</select>
				</td>
				</tr>
				<?php } ?>
				<tr>
				<td></td>
				<td class='agencyRub'><br/></td>
				</tr>
				<tr>
				<td>Titre</td>
				<td><input name="label" <?php echo $READONLY;?> id="label" type="text" style="width: 200px;margin:10px 0" tabindex="1"/></td>
				</tr>
				<tr>
				<td class="cnsPhoto">Picto</td>
					<td class="cnsPhoto">
						<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 : 120 pixels. | Hauteur : 120 pixels.
						</span>
						<br/><br/>
						<input type="file" name="picto" id="picto" size="30" tabindex="4" accept="image/*"/>
					</td>
				</tr>
				<tr>
				<td>Plan</td>
				<td>
					<a href="javascript:toggleUpload();" id="uploadLink">Ajouter une image du plan</a>
					<div id="cnsUpload" style="display:none">
						<span class="cnsPhotoText">
							<b><u>Caract&#233;ristiques du fichier</u> :</b>
							<br/>
							Poids maximum : 1 Mo.
							<br/>
							Type de fichier : JPG.
							<br/>
							Dimensions attendues :<br/>Largeur: <?php echo isset($GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["IMAGES"]["salle"]["width"]) ? $GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["IMAGES"]["site-map"]["width"] : "830";?> pixels. | Hauteur : <?php echo isset($GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["IMAGES"]["salle"]["height"]) ? $GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["IMAGES"]["site-map"]["height"] : 600;?> pixels.
						</span>
						<br/><br/>
						<input type="file" name="salle" id="salle" size="30" style="width: 200px;" tabindex="3" accept="image/*"/>
					</div>
				</td>
				</tr>
				<tr>
				<td></td>
				<td>
					<br/>
					<input type="button" class="formAct" name="valider" id="valider" value="Valider" onclick="checkForm();" tabindex="4"/></td>
				</tr>
				</table>
			</form>
			</div>
		</div>
	  </div>
	</div>

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

<script>
document.getElementById('texte').focus();
function getEntities(agence){
	$.get( "ajax/getList.php?type=entity&aid="+agence, function( data ) {
	 $("#listEnt").html( data );
	 <?php if($GLOBALS["BO_CONF"]["ENTITIES"]){?>
		$("#listEnt").attr( 'disabled', false );
		$("#listEnt").attr( 'required', false );
	 <?php
	}else{?>
		eid = $("#listEnt option:last").val();
		if(parseInt(eid)){
			selectEntity(eid, agence);
		}
	<?php } ?>
	});
}
function selectEntity(eid, aid){
	if(navigator.userAgent.indexOf('MSIE')!==-1 || navigator.appVersion.indexOf('Trident/') > -1){
		location.href = location.href + "?N=2&aid=" + aid + "&eid=" + eid;
	}else{
		var url = new URL(location.href);
		var c = url.searchParams.get("aid");
		var d = url.searchParams.get("eid");
		var n = url.searchParams.get("N"); 
		if((c != '') && (n != '') && (n != '')){
			var query_string = url.search;

			var search_params = new URLSearchParams(query_string); 
			search_params.set('aid', aid);
			search_params.set('eid', eid);
			search_params.set('N', 2);
			url.search = search_params.toString();
			var new_url = url.toString();
			location.href = new_url;
		}else{
			location.href = location.href + "?N=2&aid=" + aid + "&eid=" + eid;
		}
	}
}
function selectAgency(aid){
	location.href = location.href + "?aid=" + aid;
}
</script>

</body>

</html>