<?php
/**
 * Admin layout header and sidebar
 * Requires: requireAuth() called, $pageTitle set
 */
if (!isset($pageTitle)) {
    $pageTitle = 'Dashboard';
}
$currentPage = $currentPage ?? '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($pageTitle) ?> - <?= htmlspecialchars(SITE_NAME) ?></title>
    <link rel="icon" href="<?= nazareth_asset('img/logo.png') ?>" type="image/png">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= nazareth_asset('css/style.css') ?>?v=<?= filemtime(__DIR__ . '/../public/css/style.css') ?>">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer">
</head>
<body class="<?= currentUserRole() === 'clerk' ? 'theme-clerk' : (currentUserRole() === 'finance' ? 'theme-finance' : (currentUserRole() === 'group_manager' ? 'theme-ypg' : '')) ?><?= ($currentPage === 'registration-progress') ? ' reg-progress-fullwidth' : '' ?>">
    <div class="layout<?= ($currentPage === 'registration-progress') ? ' layout--reg-progress' : '' ?>">
        <aside class="sidebar">
            <div class="sidebar-brand">
                <img src="<?= nazareth_asset('img/logo.png') ?>" alt="" class="sidebar-logo">
                <p class="sidebar-name"><?= htmlspecialchars(SITE_FULL_NAME) ?></p>
            </div>
            <nav>
                <?php if (isFinance()): ?>
                <div class="sidebar-parent">
                    <a href="<?= nazareth_url('members') ?>" class="<?= ($currentPage === 'members' || $currentPage === 'search') ? 'active' : '' ?>">Members</a>
                    <?php if ($currentPage === 'members' || $currentPage === 'search'): ?>
                    <div class="sidebar-sub">
                        <a href="<?= nazareth_url('search') ?>" class="<?= $currentPage === 'search' ? 'active' : '' ?>">Search</a>
                    </div>
                    <?php endif; ?>
                </div>
                <div class="sidebar-parent">
                    <a href="<?= nazareth_url('finance') ?>" class="<?= $currentPage === 'finance' && ($route_action ?? '') !== 'reports' && ($route_action ?? '') !== 'receipts' && ($route_action ?? '') !== 'budgets' ? 'active' : '' ?>">Finance</a>
                    <?php if ($currentPage === 'finance'): ?>
                    <div class="sidebar-sub">
                        <a href="<?= nazareth_url('finance/receipts') ?>" class="<?= ($route_action ?? '') === 'receipts' ? 'active' : '' ?>">Receipts</a>
                        <a href="<?= nazareth_url('finance/budgets') ?>" class="<?= ($route_action ?? '') === 'budgets' ? 'active' : '' ?>">Budgets</a>
                        <a href="<?= nazareth_url('finance/reports') ?>" class="<?= ($route_action ?? '') === 'reports' ? 'active' : '' ?>">Reports</a>
                    </div>
                    <?php endif; ?>
                </div>
                <a href="<?= nazareth_url('finance/settings') ?>" class="sidebar-bottom <?= ($currentPage === 'finance' && ($route_action ?? '') === 'settings') ? 'active' : '' ?>">Settings</a>
                <?php elseif (isVoting()): ?>
                <div class="sidebar-parent">
                    <a href="<?= nazareth_url('members') ?>" class="<?= ($currentPage === 'members' || $currentPage === 'search') ? 'active' : '' ?>">Members</a>
                    <?php if ($currentPage === 'members' || $currentPage === 'search'): ?>
                    <div class="sidebar-sub">
                        <a href="<?= nazareth_url('search') ?>" class="<?= $currentPage === 'search' ? 'active' : '' ?>">Search</a>
                    </div>
                    <?php endif; ?>
                </div>
                <a href="<?= nazareth_url('voting') ?>" class="<?= $currentPage === 'voting' ? 'active' : '' ?>">Voting</a>
                <?php elseif (isDataCollector()): ?>
                <a href="<?= nazareth_url(isMobileOrTablet() ? 'mobile' : 'collect') ?>" class="<?= in_array($currentPage, ['mobile', 'collect'], true) ? 'active' : '' ?>">Data collection</a>
                <a href="<?= nazareth_url('households') ?>" class="<?= ($currentPage === 'households') ? 'active' : '' ?>">Households</a>
                <?php elseif (isRegCoordinator()): ?>
                <a href="<?= nazareth_url('registration-progress') ?>" class="<?= ($currentPage === 'registration-progress') ? 'active' : '' ?>">Registration progress</a>
                <?php elseif (isGroupManager()): ?>
                <div class="sidebar-parent">
                    <a href="<?= nazareth_url('fellowships') ?>" class="<?= $currentPage === 'fellowships' ? 'active' : '' ?>">Groups</a>
                    <?php if ($currentPage === 'fellowships' && !empty($fellowshipGroupsForSidebar ?? [])): ?>
                    <div class="sidebar-sub">
                        <?php foreach ($fellowshipGroupsForSidebar as $fg): ?>
                        <a href="<?= nazareth_url('fellowships') ?>#tab-<?= (int)$fg['tab_index'] ?>" class="sidebar-fellowship-link <?= ($fg['tab_index'] ?? 0) === 0 ? 'active' : '' ?>" data-tab-index="<?= (int)$fg['tab_index'] ?>"><?= htmlspecialchars($fg['name']) ?> (<?= (int)($fg['count'] ?? 0) ?>)</a>
                        <?php if (!empty($fg['subgroups'] ?? [])): ?>
                        <div class="sidebar-sub sidebar-sub-sub">
                            <a href="<?= nazareth_url('fellowships') ?>#tab-<?= (int)$fg['tab_index'] ?>" class="sidebar-show-all-link" data-tab-index="<?= (int)$fg['tab_index'] ?>">All</a>
                            <?php foreach ($fg['subgroups'] as $sg): ?>
                            <a href="<?= nazareth_url('fellowships') ?>#tab-<?= (int)$fg['tab_index'] ?>-subgroup-<?= urlencode($sg['name']) ?>" class="sidebar-subgroup-link" data-tab-index="<?= (int)$fg['tab_index'] ?>" data-subgroup="<?= htmlspecialchars($sg['name']) ?>"><?= htmlspecialchars($sg['name']) ?> (<?= (int)($sg['count'] ?? 0) ?>)</a>
                            <?php endforeach; ?>
                        </div>
                        <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                </div>
                <div class="sidebar-parent">
                    <a href="<?= nazareth_url('members') ?>" class="<?= ($currentPage === 'members' || $currentPage === 'search') ? 'active' : '' ?>">Members</a>
                    <?php if ($currentPage === 'members' || $currentPage === 'search'): ?>
                    <div class="sidebar-sub">
                        <a href="<?= nazareth_url('search') ?>" class="<?= $currentPage === 'search' ? 'active' : '' ?>">Search</a>
                    </div>
                    <?php endif; ?>
                </div>
                <?php else: ?>
                <?php if (isClerk()): ?>
                    <div class="sidebar-parent">
                        <a href="<?= nazareth_url('members') ?>" class="<?= ($currentPage === 'members' || $currentPage === 'search' || $currentPage === 'households') ? 'active' : '' ?>">Members</a>
                        <?php if ($currentPage === 'members' || $currentPage === 'search' || $currentPage === 'households'): ?>
                        <div class="sidebar-sub">
                            <a href="<?= nazareth_url('search') ?>" class="<?= $currentPage === 'search' ? 'active' : '' ?>">Search</a>
                            <a href="<?= nazareth_url('households') ?>" class="<?= $currentPage === 'households' ? 'active' : '' ?>">Households</a>
                        </div>
                        <?php endif; ?>
                    </div>
                    <a href="<?= nazareth_url('attendance') ?>" class="<?= $currentPage === 'attendance' ? 'active' : '' ?>">Attendance</a>
                <?php else: ?>
                    <?php if (isAdmin()): ?>
                    <a href="<?= nazareth_url('dashboard') ?>" class="<?= $currentPage === 'dashboard' ? 'active' : '' ?>">Dashboard</a>
                    <a href="<?= nazareth_url('registration-progress') ?>" class="<?= $currentPage === 'registration-progress' ? 'active' : '' ?>">Registration progress</a>
                    <?php endif; ?>
                    <div class="sidebar-parent">
                        <a href="<?= nazareth_url('members') ?>" class="<?= ($currentPage === 'members' || $currentPage === 'search' || $currentPage === 'households') ? 'active' : '' ?>">Members</a>
                        <?php if ($currentPage === 'members' || $currentPage === 'search' || $currentPage === 'households'): ?>
                        <div class="sidebar-sub">
                            <a href="<?= nazareth_url('search') ?>" class="<?= $currentPage === 'search' ? 'active' : '' ?>">Search</a>
                            <a href="<?= nazareth_url('households') ?>" class="<?= $currentPage === 'households' ? 'active' : '' ?>">Households</a>
                            <?php if (isAdmin()): ?>
                            <a href="<?= nazareth_url('members/remove') ?>" class="<?= ($route_action ?? '') === 'remove' ? 'active' : '' ?>">Remove member</a>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                    </div>
                    <div class="sidebar-parent">
                        <a href="<?= nazareth_url('groups') ?>" class="<?= ($currentPage === 'groups' || $currentPage === 'fellowships' || $currentPage === 'ministries' || $currentPage === 'aged-invalids') ? 'active' : '' ?>">Groups</a>
                        <?php if ($currentPage === 'groups' || $currentPage === 'fellowships' || $currentPage === 'ministries' || $currentPage === 'aged-invalids'): ?>
                        <div class="sidebar-sub">
                            <a href="<?= nazareth_url('fellowships') ?>" class="<?= $currentPage === 'fellowships' ? 'active' : '' ?>">Gen Grps</a>
                            <a href="<?= nazareth_url('ministries') ?>" class="<?= $currentPage === 'ministries' ? 'active' : '' ?>">Service Groups</a>
                            <a href="<?= nazareth_url('aged-invalids') ?>" class="<?= $currentPage === 'aged-invalids' ? 'active' : '' ?>">Aged & Invalids</a>
                        </div>
                        <?php endif; ?>
                    </div>
                    <?php if (isAdmin()): ?>
                    <a href="<?= nazareth_url('birthdays') ?>" class="<?= $currentPage === 'birthdays' ? 'active' : '' ?>">Birthdays</a>
                    <a href="<?= nazareth_url('dayborns') ?>" class="<?= $currentPage === 'dayborns' ? 'active' : '' ?>">Day-born</a>
                    <div class="sidebar-parent">
                        <a href="<?= nazareth_url('finance') ?>" class="<?= $currentPage === 'finance' && ($route_action ?? '') !== 'reports' && ($route_action ?? '') !== 'receipts' && ($route_action ?? '') !== 'budgets' ? 'active' : '' ?>">Finance</a>
                        <?php if ($currentPage === 'finance'): ?>
                        <div class="sidebar-sub">
                            <a href="<?= nazareth_url('finance/receipts') ?>" class="<?= ($route_action ?? '') === 'receipts' ? 'active' : '' ?>">Receipts</a>
                            <a href="<?= nazareth_url('finance/reports') ?>" class="<?= ($route_action ?? '') === 'reports' ? 'active' : '' ?>">Reports</a>
                        </div>
                        <?php endif; ?>
                    </div>
                    <a href="<?= nazareth_url('events') ?>" class="<?= $currentPage === 'events' ? 'active' : '' ?>">Events</a>
                    <a href="<?= nazareth_url('attendance') ?>" class="<?= $currentPage === 'attendance' ? 'active' : '' ?>">Attendance</a>
                    <a href="<?= nazareth_url('messaging') ?>" class="<?= $currentPage === 'messaging' ? 'active' : '' ?>">Messaging</a>
                    <div class="sidebar-parent">
                        <a href="<?= nazareth_url('executives') ?>" class="<?= ($currentPage === 'executives' || $currentPage === 'committees') ? 'active' : '' ?>">Leadership</a>
                        <?php if ($currentPage === 'executives' || $currentPage === 'committees'): ?>
                        <div class="sidebar-sub">
                            <a href="<?= nazareth_url('executives') ?>" class="<?= $currentPage === 'executives' ? 'active' : '' ?>">Executives</a>
                            <a href="<?= nazareth_url('committees') ?>" class="<?= $currentPage === 'committees' ? 'active' : '' ?>">Committees</a>
                        </div>
                        <?php endif; ?>
                    </div>
                    <a href="<?= nazareth_url('voting') ?>" class="<?= $currentPage === 'voting' ? 'active' : '' ?>">Voting</a>
                    <a href="<?= nazareth_url('users') ?>" class="<?= $currentPage === 'users' ? 'active' : '' ?>">Users</a>
                    <a href="<?= nazareth_url('settings') ?>" class="sidebar-bottom <?= $currentPage === 'settings' ? 'active' : '' ?>">Settings</a>
                    <?php endif; ?>
                <?php endif; ?>
                <?php endif; ?>
            <a href="<?= nazareth_url('mobile-help') ?>" class="sidebar-mobile-help<?= ($currentPage === 'mobile-help') ? ' active' : '' ?>">Mobile &amp; PWA tips</a>
            </nav>
        </aside>
        <main class="main">
