<?php
$userModel = new \App\Models\User();
$currentUser = $userModel->find(isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0);
$avatarSrc = $currentUser && $currentUser->avatar
    ? $baseUrl . '/' . ltrim($currentUser->avatar, '/')
    : $baseUrl . '/img/undraw_profile.svg';
$messageModel = new \App\Models\Message();
$unreadMessages = isset($_SESSION['user_id']) ? $messageModel->getUnreadCount($_SESSION['user_id']) : 0;
$isAdmin = isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin';
$pendingCount = $isAdmin ? $userModel->getPendingCount() : 0;
?>
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow glass-navbar">
    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3" style="color: white;"><i class="fa fa-bars"></i></button>
    <form class="d-none d-sm-inline-block form-inline mr-auto ml-md-3 my-2 my-md-0 mw-100 navbar-search" method="GET" action="<?= $baseUrl ?>/members">
        <div class="input-group">
            <input type="text" class="form-control bg-light border-0 small glass-input" placeholder="Search members..." name="search" value="<?= isset($_GET['search']) ? $_GET['search'] : '' ?>">
            <div class="input-group-append">
                <button class="btn btn-primary" type="submit"><i class="fas fa-search fa-sm"></i></button>
            </div>
        </div>
    </form>
    <ul class="navbar-nav ml-auto">
        <li class="nav-item dropdown no-arrow mx-1">
            <a class="nav-link dropdown-toggle" href="#" id="alertsDropdown" role="button" data-toggle="dropdown">
                <i class="fas fa-bell fa-fw" style="color: white;"></i>
                <?php if ($unreadMessages > 0): ?>
                    <span class="badge badge-danger badge-counter"><?= $unreadMessages ?></span>
                <?php endif; ?>
            </a>
            <div class="dropdown-list dropdown-menu dropdown-menu-right shadow animated--grow-in glass-dropdown" aria-labelledby="alertsDropdown">
                <h6 class="dropdown-header">Notifications</h6>
                <a class="dropdown-item d-flex align-items-center" href="<?= $baseUrl ?>/messages/inbox">
                    <div class="mr-3"><div class="icon-circle bg-primary"><i class="fas fa-envelope text-white"></i></div></div>
                    <div><div class="small text-gray-500">Messages</div><span class="font-weight-bold"><?= $unreadMessages ?> unread messages</span></div>
                </a>
                <?php if ($isAdmin): ?>
                <a class="dropdown-item d-flex align-items-center" href="<?= $baseUrl ?>/users/pending">
                    <div class="mr-3"><div class="icon-circle bg-warning"><i class="fas fa-clock text-white"></i></div></div>
                    <div><div class="small text-gray-500">Pending Approvals</div><span class="font-weight-bold"><?= $pendingCount ?> pending registrations</span></div>
                </a>
                <?php endif; ?>
                <a class="dropdown-item text-center small text-gray-500" href="<?= $baseUrl ?>/messages/inbox">Show All Messages</a>
            </div>
        </li>
        <div class="topbar-divider d-none d-sm-block" style="border-color: rgba(255,255,255,0.15);"></div>
        <li class="nav-item dropdown no-arrow">
            <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown">
                <span class="mr-2 d-none d-lg-inline text-gray-600 small" style="color: rgba(255,255,255,0.8) !important;"><?= isset($_SESSION['user_name']) ? $_SESSION['user_name'] : 'User' ?></span>
                <img class="img-profile rounded-circle" src="<?= $avatarSrc ?>" style="width: 32px; height: 32px; object-fit: cover;">
            </a>
            <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in glass-dropdown" aria-labelledby="userDropdown">
                <a class="dropdown-item" href="<?= $baseUrl ?>/member/dashboard"><i class="fas fa-tachometer-alt fa-sm fa-fw mr-2"></i>Member Dashboard</a>
                <a class="dropdown-item" href="<?= $baseUrl ?>/profile"><i class="fas fa-user fa-sm fa-fw mr-2"></i>Profile</a>
                <a class="dropdown-item" href="<?= $baseUrl ?>/member/profile"><i class="fas fa-id-card fa-sm fa-fw mr-2"></i>Membership Profile</a>
                <a class="dropdown-item" href="<?= $baseUrl ?>/settings"><i class="fas fa-cogs fa-sm fa-fw mr-2"></i>Settings</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal"><i class="fas fa-sign-out-alt fa-sm fa-fw mr-2"></i>Logout</a>
            </div>
        </li>
    </ul>
</nav>
