<?php
global $myUser,$conf;
if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour accéder à cette fonctionnalité",401);

require_once(__DIR__.SLASH.'MenuItem.class.php');


if($conf->get("navigation_allow_custom_menu")==0 && !$myUser->can('navigation','configure')) throw new Exception("Vous n'avez pas la permission d'afficher cette page", 403);
?>

<div class="row">
	<div class="col-md-12">
		<br>
        <div onclick="navigation_setting_save();" class="btn btn-success right"><i class="fas fa-check"></i> Enregistrer</div>
        <h3>Réglages Navigation</h3>
        <hr>
	</div>
	

	<div class="col-md-12 navigation-tree">
		<br>

        <h3>Arborescence du menu 
        	<select class="navigation-menu-selector text-primary" id="navigation-menu" onchange="navigation_tree_search()">
        		<?php 

        		$filters = array('menu'=>'0');
        		if($myUser->can('navigation','configure')){
        			//l'admin menu voit ses menu et les menu non attaché a un user
        			$filters['user:IN'] = ','.$myUser->login;
        		}else{
        			//les non admins menu ne voient que leurs menus
        			$filters['user'] = $myUser->login;
        		}
        		$menus = MenuItem::loadAll($filters,array('label'));

                //Récuperation du menu raccourcis du user
                $shortcutMenu = MenuItem::load(array('user'=>$myUser->login,'slug'=>'shortcut-menu'));
        		//On créé un menu raccourcis pour l'utilisateur courant si celui ci n'existe pas
        		if(!$shortcutMenu){
        			$shortcutMenu = new MenuItem();
        			$shortcutMenu->label = 'Menu raccourcis';
        			$shortcutMenu->slug = 'shortcut-menu';
        			$shortcutMenu->user = $myUser->login;
        			$shortcutMenu->menu = '0';
        			$shortcutMenu->save();
        			$menus[] = $shortcutMenu;
        		}
        		foreach($menus as $menu): ?>
        			<option <?php echo (isset($_['menu']) && $_['menu']=="mine" &&  $shortcutMenu->id == $menu->id ? 'selected="selected"': ''); ?> value="<?php echo $menu->id ?>"><?php echo $menu->label ?></option>
        		<?php endforeach; ?>
        	</select>

        </h3>
        <hr>
        <div onclick="navigation_tree_new();" class="btn btn-info"><i class="fas fa-plus"></i> Nouvel élément</div>
        <div class="btn btn-light" data-toggle="modal" data-target="#navigation-page-modal"><i class="fas fa-plus"></i> Ajouter une page existante</div>
     

        <ul class="navigation-tree-list">
        	<li class="navigation-line {{class}} hidden" data-id="{{id}}" >

        		<div class="navigation-item">
        			<div class="navigation-item-header">
	        			<span class="navigation-item-icon"><i class="{{icon}}"></i></span> 
	        			<h1 class="navigation-item-label">{{label}}</h1> 
	        			<small class="navigation-item-url">{{#url}}- {{url}}{{/url}}</small> 
	        		
	        		
	        		<h3 class="navigation-item-type"><span>{{target.label}}</span> <i  onclick="navigation_tree_toggle(this)" title="Afficher/Cacher" class="navigation-item-toggle fas fa-angle-right"></i></h3> 
        			</div>

					<div class="navigation-item-form hidden">


						<h4>Titre</h4>
						<div class="input-group mb-3">
							<input type="text" class="item-icon" data-type="icon" value="{{icon}}">
							<input type="text" class="form-control item-label" value="{{label}}" placeholder="Libellé du lien">
						</div>

						<h4>Adresse <small class="text-muted">- Optionnel</small></h4>
						<div class="input-group mb-3">
							<input type="text" class="form-control item-url" value="{{url}}"  placeholder="Adresse du lien">
						</div>
						<h4>Type d'ouverture <small class="text-muted">- Optionnel</small></h4>
						<!-- la balise form est obligatoire pour contrer le bug de jquery ui sortable, ne pas enlever -->
						<form>
							<?php foreach (MenuItem::target() as $slug => $target): ?>
							<label><input type="radio" name="item-target" value="<?php echo $slug; ?>" data-value="{{target.slug}}"> <i class="<?php echo $target['icon']; ?>"></i> <?php echo $target['label']; ?></label>
							<?php endforeach;  ?>
						</form>

						<h4>Visibilité <small class="text-muted">- Laisser vide pour rendre public</small></h4>
						<input type="text" class="item-visibility form-control" data-type="user-tpl" data-types="rank,user" data-multiple="true" value="{{visibility}}">
		

						<i title="Supprimer" class="navigation-item-delete far fa-trash-alt" onclick="navigation_tree_remove(this)"></i>
					</div>
        		</div>
        		<ul></ul>
        	</li>
        </ul>
 	</div>

	<?php if($myUser->can('navigation','configure')): ?>
	<div class="col-md-12 mt-3">
		<h3>Options générales</h3>
		<?php echo Configuration::html('navigation'); ?>
	</div>
	<?php endif; ?>



</div>


<!-- Modale page interne -->
<div class="modal fade" id="navigation-page-modal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Ajouter une page</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <ul class="list-group">
		<?php 

		$pages = array();
		Plugin::callHook("menu_main", array(&$pages));
		uasort ($pages , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
	
		
		foreach($pages as $page): 
			$icon = isset($page['icon'])? $page['icon'] : 'far fa-bookmark';
		?>
		<li class="list-group-item d-flex justify-content-between align-items-center" data-label="<?php echo $page['label']; ?>"  data-icon="<?php echo $page['icon']; ?>" data-url="<?php echo $page['url']; ?>">
			<div>
			<i class="<?php echo $icon; ?>"></i> <?php echo $page['label']; ?> <a class="text-muted" href="<?php echo $page['url']; ?>"> -<?php echo $page['url']; ?></a>
			</div>
			<span class="badge badge-success pointer" title="Ajouter au menu" onclick="navigation_tree_new(this);"><i class="fas fa-plus"></i></span>
		</li>

		<?php endforeach; ?>
		</ul>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
      </div>
    </div>
  </div>
</div>