<?php
require_once("../inc/confNetisse.php");
session_start();
if(!isset($_SESSION['auth'])) {
	header("location:index.php");
}
if(!$GLOBALS["BO_CONF"]["ENTITIES"]){
	header("location:agcList.php");
}


$AID = 0;
$READONLY = "";
if($_GET["aid"] == 0) $READONLY = " readonly disabled";
$siege_config = isset($BO_CONF["AGENCE"]["SIEGE"]) && $BO_CONF["AGENCE"]["SIEGE"]["ENABLED"] ? true : false;
$siegeCol = $siege_config ? ', siege' : '';
if(!isset($_GET["aid"])) {
	//header("location:agcList.php");

	$qa = "SELECT id_agence, nom_agence, code_agence, initiales".$siegeCol." FROM ca_agences WHERE is_active=:is_active AND supp=:supp";
	$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"]);
}

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

$q = "SELECT nom_agence, code_agence, initiales".$siegeCol." FROM ca_agences WHERE id_agence=:id_agence";
$values = array(':id_agence' => $AID);
$result = excuteQuery($q, $values);
$r = $result['stmt'];
$o = $r->fetchObject();
$AGENCE = $o->nom_agence;
$AGENCECODE = $o->initiales;


$pilecolor = $siege_config && $o->siege == 1 ? $GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["RDV"]["COLOR"] : $GLOBALS["BORNE_CONF"]["COLORS"]["PRIMARYCOLOR"];

$RDV =  (count($GLOBALS["BORNE_CONF"]["LANGUAGES"]) > 1)?$GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["RDV"] : $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["FR"]["RDV"];
$SRDV =  (count($GLOBALS["BORNE_CONF"]["LANGUAGES"]) > 1)?$GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["SRDV"] : $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["FR"]["SRDV"];
$MYST = (count($GLOBALS["BORNE_CONF"]["LANGUAGES"]) > 1)?$GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["MYST"] : $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["FR"]["MYST"];

$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;
}
?>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><?php echo $PROJECT_TITLE;?> : Cr&#233;ation d'une nouvelle entité</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 checkForm() {
			if(document.getElementById("agence")){
				if (document.getElementById("agence").value == "") {
					var POP = $('<div></div>').html("<center>Veuillez sélectionner une agence.</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");
				}
			}else 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){ ?>
			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.formEntNew.submit();
				}
			}
			<?php } ?>
			 else {
				document.formEntNew.submit();
			}
		}

		$(document).ready(function(){
			$('#pilecolor').spectrum({
		    color: "<?php echo $pilecolor;?>",
		    showInput: true,
		    className: "full-spectrum",
		    showInitial: true,
		    showSelectionPalette: true,
		    preferredFormat: "hex",
			});
			$('#bornecolor').spectrum({
		    color: "<?php echo $pilecolor;?>",
		    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">
		Cr&#233;ation d'une nouvelle entité <?php if($_GET['aid']){ ?>pour l'agence de <span class="agcTitle"><?php echo $AGENCE; ?></span><?php } ?>
	</div>
	<div id="panelsCns">
		<div id="formEnt">
		<form name="formEntNew" id="formEntNew" action="do.php" method="post" enctype="multipart/form-data">
			<input type="hidden" name="act" value="entNew"/>
			<input type="hidden" name="AID" value="<?php echo $AID;?>"/>
			<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
			<input type="hidden" name="AGENCECODE" value="<?php echo $AGENCECODE;?>"/>

			<table align="center" class="form">
			<?php
				if(!isset($_GET["aid"])) {
				?>
			<tr>
				<td style="padding: 10px;">Agence</td>
				<td style="padding: 10px;">
					<select style="width: 220px;" name='agc' required="true" id="agence" onchange="selectAgency(this.value)">
						<option value="">Sélectionner une agence</option>
						<?php echo $listAgc; ?>
					</select>
				</td>
			</tr>
			<?php
				}
			?>
			<tr>
			<td style="padding: 10px;">Libellé</td>
			<td style="padding: 10px;"><input type="text" name="entite" id="entite" size="30" tabindex="1" value="" <?php echo $READONLY;?>/></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" <?php echo $READONLY;?> value="<?php echo $pilecolor;?>" 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" <?php echo $READONLY;?> name="pilecolor" id="pilecolor" value="<?php echo $pilecolor;?>" style="width: 229px;"></td>
			</tr>
			<tr id="rdvRub" class="<?php echo $multilang?"hidden":"";?>">
			<td></td>
			<td class="agencyRub"><br/>Message d'orientation Conseiller Inconnu</td>
			</tr>
			<tr id="rdvRubText1" class="<?php echo $multilang?"hidden":"";?>">
			<td>Texte</td>
			<td><textarea <?php echo $READONLY;?> name="mysttexte" id="mysttexte" onkeyup="javascript:checkMax(this, 280);"><?php echo $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["MYST"];?></textarea></td>
			</tr>
			<tr id="rdvRub" class="<?php echo $multilang?"hidden":"";?>">
			<td></td>
			<td class="agencyRub"><br/>Message d'orientation RDV</td>
			</tr>
			<tr id="rdvRubText1" class="<?php echo $multilang?"hidden":"";?>">
			<td>Texte</td>
			<td><textarea <?php echo $READONLY;?> name="rdvtexte" id="rdvtexte" onkeyup="javascript:checkMax(this, 280);"><?php echo $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["RDV"];?></textarea></td>
			</tr>
			<tr id="rdvRub" class="<?php echo $multilang?"hidden":"";?>">
			<td></td>
			<td class="agencyRub"><br/>Message d'orientation SRDV</td>
			</tr>
			<tr id="srdvRubText1" class="<?php echo $multilang?"hidden":"";?>">
			<td>Texte</td>
			<td><textarea <?php echo $READONLY;?> name="srdvtexte" id="srdvtexte" onkeyup="javascript:checkMax(this, 280);"><?php echo $GLOBALS["BORNE_CONF"]["ORIENTATIONS"]["DEFAULT_MESSAGES"]["SRDV"];?></textarea></td>
			</tr>
			
			<tr style="display:<?php echo $entity_icon?'table-row':'none';?>">
				<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 : <?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" name="picto" id="picto" size="30" tabindex="4" accept="image/*"/>
				</td>
			</tr>
			<tr>
			<td>&nbsp;</td>
			<td align="center"><input type="button" class="formAct" name="valider" id="valider" value="Valider" onclick="checkForm();" tabindex="2"/></td>
			</tr>
			</table>
		</form>
		</div>
	</div>
	<div id="nav">
		<a href='entList.php?aid=<?php echo $AID;?>' class='formNav'>Retour &#224; la liste des entités</a>
	</div>
	</div>
	</div>

</div>
<?php include 'footer.php';?>
<script>
document.getElementById('entite').focus();
function selectAgency(aid){
	location.href = location.href + "?aid=" + aid;
}
</script>

</body>

</html>