<?php
$currentUrl = $_SERVER['REQUEST_URI'];
$settingModel = new \App\Models\Setting();
$companyName = $settingModel->getValue('company_name') ?: 'GAS';
$companyLogo = $settingModel->getValue('company_logo');
$userModel = new \App\Models\User();
$pendingCount = $userModel->getPendingCount();
$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';
$birthdayModel = new \App\Models\BirthdayWish();
$birthdayCount = $birthdayModel->countBirthdaysThisMonth();
$birthdayToday = $birthdayModel->countBirthdaysToday();
?>
<ul class="navbar-nav sidebar sidebar-dark accordion" id="accordionSidebar" style="background: rgba(33, 65, 122, 0.85); backdrop-filter: blur(12px);">
    <a class="sidebar-brand d-flex align-items-center justify-content-center" href="<?= $baseUrl ?>/dashboard">
        <div class="sidebar-brand-icon rotate-n-15">
            <?php if ($companyLogo): ?>
                <img src="<?= $baseUrl . '/' . ltrim($companyLogo, '/') ?>" alt="<?= htmlspecialchars($companyName) ?>" style="height: 40px; width: auto;">
            <?php else: ?>
                <i class="fas fa-stethoscope"></i>
            <?php endif; ?>
        </div>
        <div class="sidebar-brand-text mx-3"><?= htmlspecialchars($companyName) ?></div>
    </a>
    <hr class="sidebar-divider my-0">
    <li class="nav-item <?= strpos($currentUrl, 'dashboard') !== false ? 'active' : '' ?>">
        <a class="nav-link" href="<?= $baseUrl ?>/dashboard"><i class="fas fa-fw fa-tachometer-alt"></i><span>Dashboard</span></a>
    </li>
    <hr class="sidebar-divider">
    <?php $membershipOpen = (strpos($currentUrl, 'member') !== false) || (strpos($currentUrl, '/members') !== false) || (strpos($currentUrl, 'loans') !== false); ?>
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseMembership" aria-expanded="<?= $membershipOpen ? 'true' : 'false' ?>" aria-controls="collapseMembership">
            <i class="fas fa-fw fa-users"></i>
            <span>Membership</span>
        </a>
        <div id="collapseMembership" class="collapse <?= $membershipOpen ? 'show' : '' ?>" aria-labelledby="headingMembership" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item <?= strpos($currentUrl, 'member/dashboard') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/member/dashboard">Member Area</a>
                <a class="collapse-item <?= strpos($currentUrl, 'member/profile') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/member/profile">My Profile</a>
                <a class="collapse-item <?= strpos($currentUrl, '/members') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/members">Member Directory</a>
                <a class="collapse-item <?= strpos($currentUrl, 'loans/my') !== false || strpos($currentUrl, 'loans/apply') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/loans/my">My Loans</a>
            </div>
        </div>
    </li>
    <?php if ($isAdmin): ?>
    <!-- Pending Approvals moved into Administration group -->
    <?php endif; ?>
    <hr class="sidebar-divider">
    <?php $commOpen = strpos($currentUrl, 'announcements') !== false || strpos($currentUrl, 'messages') !== false || strpos($currentUrl, 'forum') !== false; ?>
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseCommunication" aria-expanded="<?= $commOpen ? 'true' : 'false' ?>" aria-controls="collapseCommunication">
            <i class="fas fa-fw fa-bullhorn"></i>
            <span>Communication</span>
        </a>
        <div id="collapseCommunication" class="collapse <?= $commOpen ? 'show' : '' ?>" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item <?= strpos($currentUrl, 'announcements') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/announcements">Announcements</a>
                <a class="collapse-item <?= strpos($currentUrl, 'messages') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/messages/inbox">Messages <?php if ($unreadMessages > 0): ?><span class="badge badge-danger ml-2"><?= $unreadMessages ?></span><?php endif; ?></a>
                <a class="collapse-item <?= strpos($currentUrl, 'forum') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/forum">Forum</a>
            </div>
        </div>
    </li>
    <hr class="sidebar-divider">
    <?php $eventsOpen = strpos($currentUrl, 'events') !== false || strpos($currentUrl, 'attendance') !== false || strpos($currentUrl, 'calendar') !== false || strpos($currentUrl, 'birthdays') !== false; ?>
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseEvents" aria-expanded="<?= $eventsOpen ? 'true' : 'false' ?>" aria-controls="collapseEvents">
            <i class="fas fa-fw fa-calendar-alt"></i>
            <span>Events & Activities</span>
        </a>
        <div id="collapseEvents" class="collapse <?= $eventsOpen ? 'show' : '' ?>" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item <?= strpos($currentUrl, 'events') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/events">Events</a>
                <a class="collapse-item <?= strpos($currentUrl, 'attendance') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/attendance">Attendance</a>
                <a class="collapse-item <?= strpos($currentUrl, 'calendar') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/calendar">Calendar</a>
                <a class="collapse-item <?= strpos($currentUrl, 'birthdays') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/birthdays">Birthdays <?php if ($birthdayCount > 0): ?><span class="badge badge-<?= $birthdayToday > 0 ? 'warning' : 'info' ?> ml-2"><?= $birthdayCount ?></span><?php endif; ?></a>
            </div>
        </div>
    </li>
    <hr class="sidebar-divider">
    <?php $resourcesOpen = strpos($currentUrl, 'documents') !== false || strpos($currentUrl, 'photos') !== false || strpos($currentUrl, 'links') !== false; ?>
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseResources" aria-expanded="<?= $resourcesOpen ? 'true' : 'false' ?>" aria-controls="collapseResources">
            <i class="fas fa-fw fa-folder-open"></i>
            <span>Resources</span>
        </a>
        <div id="collapseResources" class="collapse <?= $resourcesOpen ? 'show' : '' ?>" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item <?= strpos($currentUrl, 'documents') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/documents">Documents</a>
                <a class="collapse-item <?= strpos($currentUrl, 'photos') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/photos">Photo Gallery</a>
                <a class="collapse-item <?= strpos($currentUrl, 'links') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/links">Quick Links</a>
            </div>
        </div>
    </li>
    <hr class="sidebar-divider">
    <?php $financeOpen = strpos($currentUrl, 'contributions') !== false || strpos($currentUrl, 'receipts') !== false || strpos($currentUrl, 'payment') !== false; ?>
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseFinance" aria-expanded="<?= $financeOpen ? 'true' : 'false' ?>" aria-controls="collapseFinance">
            <i class="fas fa-fw fa-hand-holding-usd"></i>
            <span>Finance</span>
        </a>
        <div id="collapseFinance" class="collapse <?= $financeOpen ? 'show' : '' ?>" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item <?= strpos($currentUrl, 'contributions/arrears') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/contributions/arrears">Arrears</a>
                <a class="collapse-item <?= strpos($currentUrl, 'contributions') !== false && strpos($currentUrl, 'contributions/arrears') === false ? 'active' : '' ?>" href="<?= $baseUrl ?>/contributions">Contributions</a>
                <a class="collapse-item <?= strpos($currentUrl, 'receipts') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/receipts">Receipts</a>
                <a class="collapse-item <?= strpos($currentUrl, 'payment') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/payment/make">Make Payment</a>
            </div>
        </div>
    </li>
    <?php if ($isAdmin): ?>
    <hr class="sidebar-divider">
    <?php $adminOpen = strpos($currentUrl, 'reports') !== false || strpos($currentUrl, 'profile-fields') !== false || strpos($currentUrl, 'forms') !== false || strpos($currentUrl, 'users') !== false || strpos($currentUrl, 'cms') !== false || strpos($currentUrl, 'post-categories') !== false || strpos($currentUrl, 'settings') !== false || strpos($currentUrl, 'contribution-types') !== false || strpos($currentUrl, 'loans') !== false || strpos($currentUrl, 'audit') !== false || strpos($currentUrl, 'users/pending') !== false; ?>
    <li class="nav-item">
        <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseAdmin" aria-expanded="<?= $adminOpen ? 'true' : 'false' ?>" aria-controls="collapseAdmin">
            <i class="fas fa-fw fa-cogs"></i>
            <span>Administration</span>
        </a>
        <div id="collapseAdmin" class="collapse <?= $adminOpen ? 'show' : '' ?>" data-parent="#accordionSidebar">
            <div class="bg-white py-2 collapse-inner rounded">
                <a class="collapse-item <?= strpos($currentUrl, 'users/pending') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/users/pending">Pending Approvals <?php if ($pendingCount > 0): ?><span class="badge badge-danger ml-2"><?= $pendingCount ?></span><?php endif; ?></a>
                <a class="collapse-item <?= strpos($currentUrl, 'reports') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/reports/members">Member Reports</a>
                <a class="collapse-item <?= strpos($currentUrl, 'profile-fields') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/profile-fields">Profile Fields</a>
                <a class="collapse-item <?= strpos($currentUrl, 'forms') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/forms">Dynamic Forms</a>
                <a class="collapse-item <?= strpos($currentUrl, 'users') !== false && strpos($currentUrl, 'users/pending') === false ? 'active' : '' ?>" href="<?= $baseUrl ?>/users">Manage Users</a>
                <a class="collapse-item <?= strpos($currentUrl, 'cms') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/cms">Content Manager</a>
                <a class="collapse-item <?= strpos($currentUrl, 'post-categories') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/post-categories">Post Categories</a>
                <a class="collapse-item <?= strpos($currentUrl, 'settings') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/settings">Settings</a>
                <a class="collapse-item <?= strpos($currentUrl, 'contribution-types') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/contribution-types">Contribution Types</a>
                <a class="collapse-item <?= strpos($currentUrl, 'loans') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/loans">Loan Management</a>
                <a class="collapse-item <?= strpos($currentUrl, 'audit') !== false ? 'active' : '' ?>" href="<?= $baseUrl ?>/audit">Audit Log</a>
            </div>
        </div>
    </li>
    <?php endif; ?>
    <hr class="sidebar-divider d-none d-md-block">
    <div class="text-center d-none d-md-inline">
        <button class="rounded-circle border-0" id="sidebarToggle" style="background: rgba(255,255,255,0.1); color: white; width: 30px; height: 30px;"></button>
    </div>
</ul>
