<?php
// Sécurité : On s'assure qu'une session est active pour récupérer les infos de l'admin
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Récupération de l'initiale de l'administrateur connecté pour l'avatar
$admin_name = isset($_SESSION['user_name']) ? $_SESSION['user_name'] : (isset($_SESSION['nom']) ? $_SESSION['nom'] : 'Admin');
$initiale_admin = strtoupper(substr($admin_name, 0, 1));

// Détermination de la page active pour appliquer la classe .active sur les liens
$current_page = basename($_SERVER['PHP_SELF']);
?>
<aside class="sidebar-admin">
    <div class="sidebar-brand">
        <div class="brand-logo">
            <i class="fa-solid fa-screwdriver-wrench"></i>
        </div>
        <div class="brand-text">
            <h3>ParcoTrip</h3>
            <span>Panel Admin</span>
        </div>
    </div>

    <div class="sidebar-user-profile">
        <div class="profile-avatar">
            <?= $initiale_admin ?>
        </div>
        <div class="profile-info">
            <h4><?= htmlspecialchars($admin_name) ?></h4>
            <span class="role-badge"><i class="fa-solid fa-shield-halved"></i> Principal</span>
        </div>
    </div>

    <nav class="sidebar-nav">
        <ul>
            <li class="<?= ($current_page === 'index.php' || $current_page === 'dashboard.php') ? 'active' : '' ?>">
                <a href="index.php">
                    <i class="fa-solid fa-chart-pie"></i>
                    <span>Vue d'ensemble</span>
                </a>
            </li>

            <li class="<?= ($current_page === 'addparks.php') ? 'active' : '' ?>">
                <a href="addparks.php">
                    <i class="fa-solid fa-circle-plus"></i>
                    <span>Ajouter un parc</span>
                </a>
            </li>

            <li class="<?= ($current_page === 'modifsparcswait.php') ? 'active' : '' ?>">
                <a href="modifsparcswait.php">
                    <i class="fa-solid fa-clock-rotate-left"></i>
                    <span>Temps & API Slug</span>
                </a>
            </li>

            <li class="dropdown <?= ($current_page === 'edituser.php') ? 'active' : '' ?>">
                <a href="#" class="dropdown-toggle" onclick="event.preventDefault(); this.parentElement.classList.toggle('open');">
                    <i class="fa-solid fa-bars-progress"></i>
                    <span>Gérer</span>
                    <i class="fa-solid fa-chevron-down arrow-icon"></i>
                </a>
                <ul class="dropdown-menu">
                    <li class="<?= ($current_page === 'edituser.php') ? 'sub-active' : '' ?>">
                        <a href="edituser.php">
                            <i class="fa-solid fa-user-gear"></i>
                            <span>Gérer les membres</span>
                        </a>
                    </li>
                </ul>
            </li>
        </ul>
    </nav>

    <div class="sidebar-footer">
        <a href="index.php" class="btn-back-vitrine">
            <i class="fa-solid fa-arrow-left-to-line"></i>
            <span>Retour au site</span>
        </a>
        <a href="../logout.php" class="btn-logout-admin" onclick="return confirm('Fermer la session administrative ?');">
            <i class="fa-solid fa-power-off"></i>
        </a>
    </div>
</aside>

<style>
/* Structure du Dropdown au sein de la navigation */
.sidebar-nav ul li.dropdown {
    display: flex;
    flex-direction: column;
}

.sidebar-nav ul li.dropdown .dropdown-toggle {
    display: flex;
    align-items: center;
    width: 100%;
}

.sidebar-nav ul li.dropdown .arrow-icon {
    font-size: 0.75rem;
    margin-left: auto;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Rotation de la flèche quand le menu est ouvert ou sur la page active */
.sidebar-nav ul li.dropdown.open .arrow-icon,
.sidebar-nav ul li.dropdown.active .arrow-icon {
    transform: rotate(180deg);
}

/* Conteneur du sous-menu caché par défaut */
.sidebar-nav ul li.dropdown .dropdown-menu {
    display: none;
    list-style: none;
    padding-left: 14px;
    margin-top: 5px;
    border-left: 2px solid rgba(255, 255, 255, 0.1);
}

/* Affichage forcé si la classe .open est présente ou si on est sur la page enfant */
.sidebar-nav ul li.dropdown.open .dropdown-menu,
.sidebar-nav ul li.dropdown.active .dropdown-menu {
    display: block;
}

/* Liens internes du sous-menu */
.sidebar-nav ul li.dropdown .dropdown-menu li a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    font-size: 0.88rem;
    color: rgba(255, 255, 255, 0.65);
    text-decoration: none;
    border-radius: 8px;
    transition: all 0.2s ease;
}

.sidebar-nav ul li.dropdown .dropdown-menu li a:hover,
.sidebar-nav ul li.dropdown .dropdown-menu li.sub-active a {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.06);
}
</style>