<?php 
global $myUser,$_,$conf;
if(!$myUser->can('activedirectory','configure')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'ActiveDirectoryGroup.class.php');
?>
<div class="row">
	<div class="col-md-12">
		<br>
		<h3>Réglages Active Directory</h3>
		<hr/>
		<?php 
		if($myUser!=false){ ?>
			<div id="active-directory-form-settings">
				<form action="action.php?action=activedirectory_activedirectory_settings_save" method="POST">
					<button type="submit" class="btn btn btn-success float-right"><i class="fas fa-check"></i> Enregistrer</button>
					<legend>Configuration de l'AD</legend><br>
					<div class="clear"></div>
					<div class="row">
						<div class="col-md-4">
							<label>Serveur :</label>
							<input type="text" name="ad-server" value="<?php echo $conf->get('plugin_activedirectory_server'); ?>" placeholder="ad.server.fr" class="form-control">
						</div>
						<div class="col-md-4">
							<label>Port :</label>
							<input type="text" name="ad-port" value="<?php echo $conf->get('plugin_activedirectory_port'); ?>" placeholder="389" class="form-control">
						</div>
						<div class="col-md-4">
							<label>Port SSL :</label>
							<input type="text" name="ad-ssl-port" value="<?php echo $conf->get('plugin_activedirectory_ssl_port'); ?>" placeholder="636" class="form-control">
						</div>
					</div><br>
					<div class="row">
						<div class="col-md-8">
							<label>Racine des utilisateurs :</label>
							<input type="text" name="ad-user-root" value="<?php echo $conf->get('plugin_activedirectory_user_root'); ?>" placeholder="ou=people,dc=idleman,dc=fr" class="form-control">
						</div>
					</div><br>
					<div class="row">
						<div class="col-md-8">
							<label>Racine des groupes :</label>
							<input type="text" name="ad-group-root" value="<?php echo $conf->get('plugin_activedirectory_group_root'); ?>" placeholder="ou=people,dc=idleman,dc=fr" class="form-control">
						</div>
						<div class="col-md-4">
							<label>Domaine :</label>
							<input type="text" name="ad-domain" value="<?php echo $conf->get('plugin_activedirectory_domain'); ?>" placeholder="@MONSITE.LOCAL" class="form-control">
						</div>
					</div><br>
					<div class="row">
						<div class="col-md-8">
							<label>CN Lecture seule :</label>
							<small class="text-muted">utilisé pour acceder aux groupes et aux utilisateurs</small>
							<input type="text" name="ad-reader-login" value="<?php echo $conf->get('plugin_activedirectory_reader_login'); ?>" placeholder="ou=people,dc=idleman,dc=fr" class="form-control">
						</div>

						<div class="col-md-4">
							<label>Mot de passe lecture seule :</label>
							<small class="text-muted">(Optionnel)</small>
							<input type="password" data-type="password" name="ad-reader-password" value="<?php echo $conf->get('plugin_activedirectory_reader_password'); ?>" placeholder="" class="form-control">
						</div>
					</div><br>
					<div class="row">
						<div class="col-md-8">
							<label>CN Administrateur :</label>
							<small class="text-muted">(Optionnel), utilisé pour les changements de mots de passe uniquement</small>
							<input type="text" name="ad-admin-login" value="<?php echo $conf->get('plugin_activedirectory_admin_login'); ?>" placeholder="ou=people,dc=idleman,dc=fr" class="form-control">
						</div>

						<div class="col-md-4">
							<label>Mot de passe Administrateur :</label>
							<small class="text-muted">(Optionnel)</small>
							<input type="password" data-type="password" name="ad-admin-password" value="<?php echo $conf->get('plugin_activedirectory_admin_password'); ?>" placeholder="" class="form-control">
						</div>
					</div><br>

					<div class="row mt-3">
						<!-- search results -->
						<div class="col-xl-12">
						 <h4>Meta informations</h4>
						 <p>Vous pouvez remplir des méta champs pour les utilisateurs, ces méta champs sont requis par certains plugins et peuvent être renseigné
						 depuis l'AD via la syntaxe : <code>Libellé:Type:nom-champ-ad:nom-meta</code> avec une ligne par champs</p>
						 <textarea class="form-control" name="ad-metafields"><?php echo $conf->get('plugin_activedirectory_metafields'); ?></textarea>
						</div>
					</div>
					
				</form>
				<hr/>
				<form id="ad-correspondance" data-id="" class="" data-action="activedirectory_activedirectory_save_link" action="action.php?action=activedirectory_activedirectory_save_link" method="POST">
					<legend>Correspondance Groupe / Rang / Établissement</legend><br>
					<div class="row">
						<div class="col-md-4">
							<label>Nom du groupe AD :</label>
							<input type="text" name="ad-group" id="ad-group" placeholder="Nom du groupe" class="form-control"> 
						</div>
						<div class="col-md-3">
							<label>Rang :</label>
							<select name="ad-rank" id="ad-rank" class="form-control">
								<?php 
								$ranks = new Rank();
								$ranks = $ranks->populate();
								foreach($ranks as $rank)
									echo '<option value="'.$rank->getId().'">'.$rank->getLabel().'</option>';
								?>
							</select>
						</div>
						<div class="col-md-3">
							<label>Établissement :</label>
							<select name="ad-firm" id="ad-firm" class="form-control">
								<?php 
								$firms = new Firm();
								$firms = $firms->populate();
								foreach($firms as $firm)
									echo '<option value="'.$firm->id.'">'.$firm->label.'</option>';
								?>
							</select>
						</div>
						<div class="col-md-2 position-relative text-right">
							<div class="btn btn-success noLabel" onclick="activedirectory_activedirectory_save_link();"><i class="fas fa-check"></i> Valider</div>
						</div>
					</div>
				</form>

				<table id="group-rank-firm" class="table table-striped table-bordered table-hover table-group-rank-firm ">
					<thead>
						<tr>
							<th>Groupe</th>
							<th>Rang</th>
							<th>Établissement</th>
							<th></th>
						</tr>
					</thead>
					<?php 
					$rank_groups = ActiveDirectoryGroup::loadAll(array());
					$firms = Firm::loadAll(array());

					$rankManager = new Rank();
					$rankManager = $rankManager->populate();
					$rankAnnuary = array();

					foreach($rankManager as $rank)
						$rankAnnuary[$rank->getId()] = $rank->getLabel();

					foreach($rank_groups as $rank_group){
						?>
						<tr data-id="<?php echo $rank_group->id; ?>">
							<td><?php echo $rank_group->adgroup ?></td>
							<td><?php echo $rankAnnuary[$rank_group->rank] ?></td>
							<td><?php echo Firm::load(array('id'=>$rank_group->firm))->label ?></td>
							<td class="text-center action-buttons">
								<div class="btn btn-info btn-squarred btn-mini" onclick="activedirectory_activedirectory_edit_link(this);"><i class="fas fa-pencil-alt"></i></div>
								<a class="btn btn-danger btn-squarred btn-mini" onclick="return confirm('Voulez-vous vraiment supprimer cet élément ?')" href="action.php?action=activedirectory_activedirectory_delete_link&groupId=<?php echo urlencode($rank_group->id); ?>"><i class="fas fa-times"></i></a>
							</td>
						</tr>
					<?php } ?>
				</table>
			</div>
		<?php } else { ?>
		<div id="main" class="wrapper clearfix">
			<article>
					<h3>Vous devez être connecté</h3>
			</article>
		</div>
		<?php } ?>
	</div>
</div>

	