<?php
if (!isset($brandingData)) {
    require_once 'db.php';
    $stmt = $pdo->query("SELECT * FROM branding LIMIT 1");
    $brandingData = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>

    <div class="d-flex">
<nav class="sidebar bg-light p-3" style="width: 250px; height: 100vh;">
<ul class="nav flex-column">

<!-- Top Navigation header (unchanged) -->
<div style="background-color: #d1f7da; border-bottom: 2px solid #32a24c; padding: 10px 16px; font-size: 14px; font-weight: 600; color: #14532d; border-radius: 10px 10px 0 0; margin-bottom: 10px;">
  <i class="fa fa-compass me-2"></i> Navigation
</div>

<!-- 🔶 Begin boxed menu container -->
<div class="sidebar-menu-box">



<?php
$currentPage = basename($_SERVER['PHP_SELF']);
$isActive = fn($page) => $currentPage === $page ? 'active' : '';
?>

<li class="nav-item"><a class="nav-link <?= $isActive('user-dashboard.php') ?>" href="user-dashboard.php"><i class="fa fa-home"></i> Dashboard</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('profile.php') ?>" href="profile.php"><i class="fa fa-user"></i> My Account</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('my_leads.php') ?>" href="my_leads.php"><i class="fa fa-address-book"></i> Leads</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('referrals.php') ?>" href="referrals.php"><i class="fa fa-users"></i> Referrals</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('settings.php') ?>" href="settings.php"><i class="fa fa-money-bill-wave"></i> Setup</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('lead-capture.php') ?>" href="lead-capture.php"><i class="fa fa-file-alt"></i> Lead Pages</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('training-center.php') ?>" href="training-center.php"><i class="fas fa-video"></i> Training</a></li>
<li class="nav-item"><a class="nav-link <?= $isActive('marketing-tools.php') ?>" href="marketing-tools.php"><i class="fas fa-box-open"></i> Vault</a></li>


            <?php if (session_status() === PHP_SESSION_NONE) session_start(); ?>
<?php if (isset($_SESSION['logged_in_user']) && $_SESSION['logged_in_user'] === 'edkeyte'): ?>
    <?php
    $jsonFile = 'admin-sidebar-links.json';
    if (file_exists($jsonFile)) {
        $adminLinks = json_decode(file_get_contents($jsonFile), true);

        $normalLinks = array_filter($adminLinks, fn($l) => empty($l['fixed']) && !empty($l['enabled']));
        usort($normalLinks, fn($a, $b) => ($a['order'] ?? 0) <=> ($b['order'] ?? 0));

        $fixedLinks = array_filter($adminLinks, fn($l) => !empty($l['fixed']) && !empty($l['enabled']));

        foreach (array_merge($normalLinks, $fixedLinks) as $link):
            if (!empty($link['divider_before'])) echo '<hr class="my-2">';
            $target = !empty($link['new_tab']) ? ' target="_blank"' : '';
            $isActiveClass = $currentPage === basename($link['url']) ? ' active' : '';
echo '<li class="nav-item"><a class="nav-link' . $isActiveClass . '" href="' . htmlspecialchars($link['url']) . '"' . $target . '>';
            echo '<i class="fa ' . htmlspecialchars($link['icon']) . '"></i> ' . htmlspecialchars($link['label']) . '</a></li>';
        endforeach;
    }
    ?>
<?php endif; ?>
</div>

<!-- ✅ Logout and Secure Footer Box -->
<div style="margin-top: 12px;">
  <hr class="my-2">
  <div style="background: #fffdfc; border: 1px solid #fef2e4; border-radius: 10px; padding: 10px; text-align: center;">
    <a class="nav-link" href="logout.php" style="color: #dc3545; font-weight: bold;">
      <i class="fa fa-power-off me-2"></i> Logout
    </a>
    <hr class="my-2">
    <div style="font-size: 12px; color: #b34709;">
      🔐 Secure Portal Access
    </div>
  </div>
</div>

</ul>
</nav>
