<?php

use MintyPHP\Service\Docs\DocsCatalogService;

$layoutAuth = is_array($viewAuth['layout'] ?? null) ? $viewAuth['layout'] : [];
$canViewTenants = (bool) ($layoutAuth['can_view_tenants'] ?? false);
$canViewDepartments = (bool) ($layoutAuth['can_view_departments'] ?? false);
$canViewUsers = (bool) ($layoutAuth['can_view_users'] ?? false);
$canViewRoles = (bool) ($layoutAuth['can_view_roles'] ?? false);
$canViewPermissions = (bool) ($layoutAuth['can_view_permissions'] ?? false);
$canViewSettings = (bool) ($layoutAuth['can_view_settings'] ?? false);
$canViewImports = (bool) ($layoutAuth['can_view_imports'] ?? false);
$canViewJobs = (bool) ($layoutAuth['can_view_jobs'] ?? false);
$canViewApiDocs = (bool) ($layoutAuth['can_view_api_docs'] ?? false);
$canViewDocs = (bool) ($layoutAuth['can_view_docs'] ?? false);
$canViewMailLog = (bool) ($layoutAuth['can_view_mail_log'] ?? false);
$canViewApiAudit = (bool) ($layoutAuth['can_view_api_audit'] ?? false);
$canViewSystemAudit = (bool) ($layoutAuth['can_view_system_audit'] ?? false);
$canViewImportsAudit = (bool) ($layoutAuth['can_view_imports_audit'] ?? false);
$canViewUserLifecycleAudit = (bool) ($layoutAuth['can_view_user_lifecycle_audit'] ?? false);
$canViewStats = (bool) ($layoutAuth['can_view_stats'] ?? false);
$canViewAddressBook = (bool) ($layoutAuth['can_view_address_book'] ?? false);
$docsDefaultSlug = DocsCatalogService::defaultSlug();
$docsDefaultPath = 'admin/docs/' . $docsDefaultSlug;
$hasOrganization = $canViewTenants || $canViewDepartments || $canViewUsers;
$hasUsersSection = $canViewRoles || $canViewPermissions;
$hasAutomationSection = $canViewImports || $canViewJobs || $canViewApiDocs;
$hasMonitoringSection = $canViewStats;
$hasLogsSection = $canViewMailLog || $canViewApiAudit || $canViewSystemAudit || $canViewImportsAudit || $canViewUserLifecycleAudit;
$hasSystemSection = $canViewSettings || $canViewDocs;
$hasAdminPanel = layoutHasAdminPanel($layoutAuth);

// Declarative nav config for admin panel groups
$adminNavGroups = [
    [
        'key' => 'admin-organization',
        'label' => t('Organization'),
        'icon' => 'bi-building',
        'visible' => $hasOrganization,
        'items' => [
            [
                'label' => t('Tenants'),
                'path' => 'admin/tenants',
                'active' => navActive('admin/tenants', true),
                'visible' => $canViewTenants,
                'withTenant' => false,
            ],
            [
                'label' => t('Departments'),
                'path' => 'admin/departments',
                'active' => navActive('admin/departments', true),
                'visible' => $canViewDepartments,
                'withTenant' => true,
            ],
            [
                'label' => t('Users'),
                'path' => 'admin/users',
                'active' => navActive('admin/users', true),
                'visible' => $canViewUsers,
                'withTenant' => true,
            ],
        ],
    ],
    [
        'key' => 'admin-roles-permissions',
        'label' => t('Roles & permissions'),
        'icon' => 'bi-shield-lock',
        'visible' => $hasUsersSection,
        'items' => [
            [
                'label' => t('Roles'),
                'path' => 'admin/roles',
                'active' => navActive('admin/roles', true),
                'visible' => $canViewRoles,
                'withTenant' => false,
            ],
            [
                'label' => t('Permissions'),
                'path' => 'admin/permissions',
                'active' => navActive('admin/permissions', true),
                'visible' => $canViewPermissions,
                'withTenant' => false,
            ],
        ],
    ],
    [
        'key' => 'admin-automation',
        'label' => t('Automation & integrations'),
        'icon' => 'bi-plug',
        'visible' => $hasAutomationSection,
        'items' => [
            [
                'label' => t('Imports'),
                'path' => 'admin/imports',
                'active' => navActive('admin/imports', true),
                'visible' => $canViewImports,
                'withTenant' => false,
            ],
            [
                'label' => t('Scheduled jobs'),
                'path' => 'admin/scheduled-jobs',
                'active' => navActive('admin/scheduled-jobs', true),
                'visible' => $canViewJobs,
                'withTenant' => false,
            ],
            [
                'label' => t('API docs'),
                'path' => 'admin/api-docs',
                'active' => navActive('admin/api-docs', true),
                'visible' => $canViewApiDocs,
                'withTenant' => false,
            ],
        ],
    ],
    [
        'key' => 'admin-monitoring',
        'label' => t('Monitoring'),
        'icon' => 'bi-graph-up',
        'visible' => $hasMonitoringSection,
        'items' => [
            [
                'label' => t('Statistics'),
                'path' => 'admin/stats',
                'active' => navActive('admin/stats', true),
                'visible' => $canViewStats,
                'withTenant' => false,
            ],
        ],
    ],
    [
        'key' => 'admin-logs',
        'label' => t('Logs'),
        'icon' => 'bi-journal-text',
        'visible' => $hasLogsSection,
        'items' => [
            [
                'label' => t('Mail logs'),
                'path' => 'admin/mail-log',
                'active' => navActive('admin/mail-log', true),
                'visible' => $canViewMailLog,
                'withTenant' => false,
            ],
            [
                'label' => t('API audit'),
                'path' => 'admin/api-audit',
                'active' => navActive('admin/api-audit', true),
                'visible' => $canViewApiAudit,
                'withTenant' => false,
            ],
            [
                'label' => t('System audit logs'),
                'path' => 'admin/system-audit',
                'active' => navActive('admin/system-audit', true),
                'visible' => $canViewSystemAudit,
                'withTenant' => false,
            ],
            [
                'label' => t('Import logs'),
                'path' => 'admin/import-audit',
                'active' => navActive('admin/import-audit', true),
                'visible' => $canViewImportsAudit,
                'withTenant' => false,
            ],
            [
                'label' => t('User lifecycle logs'),
                'path' => 'admin/user-lifecycle-audit',
                'active' => navActive('admin/user-lifecycle-audit', true),
                'visible' => $canViewUserLifecycleAudit,
                'withTenant' => false,
            ],
        ],
    ],
    [
        'key' => 'admin-system',
        'label' => t('System'),
        'icon' => 'bi-sliders',
        'visible' => $hasSystemSection,
        'items' => [
            [
                'label' => t('Settings'),
                'path' => 'admin/settings',
                'active' => navActive('admin/settings', true),
                'visible' => $canViewSettings,
                'withTenant' => false,
            ],
            [
                'label' => t('Documentation'),
                'path' => $docsDefaultPath,
                'active' => navActive('admin/docs', true),
                'visible' => $canViewDocs,
                'withTenant' => false,
            ],
        ],
    ],
];

// Render helper for grouped admin aside navigation
$renderAdminNavGroup = static function (array $group, string $tenantQueryParam): void {
    if (empty($group['visible'])) {
        return;
    }
    $groupKey = trim((string) ($group['key'] ?? ''));
    if ($groupKey === '') {
        return;
    }
    $items = $group['items'] ?? [];
    $items = array_values(array_filter($items, static fn ($item) => !empty($item['visible'])));
    if (!$items) {
        return;
    }
    $groupIsActive = false;
    foreach ($items as $item) {
        $activeState = $item['active'] ?? [];
        if (!empty($activeState['isActive'])) {
            $groupIsActive = true;
            break;
        }
    }
    ?>
    <li class="app-sidebar-group app-sidebar-admin-group">
        <details data-details-key="<?php e($groupKey); ?>"<?php if ($groupIsActive): ?> open<?php endif; ?>>
            <summary>
                <?php if (!empty($group['icon'])): ?><i class="bi <?php e($group['icon']); ?>"></i> <?php endif; ?>
                <span><?php e($group['label'] ?? ''); ?></span>
            </summary>
            <ul>
                <?php foreach ($items as $item): ?>
                    <?php
                    $href = $item['path'] ?? '';
                    if ($href !== '' && !empty($item['withTenant'])) {
                        $href .= $tenantQueryParam;
                    }
                    $active = $item['active'] ?? ['class' => '', 'aria' => ''];
                    ?>
                    <li>
                        <a href="<?php e($href); ?>" class="<?php e($active['class'] ?? ''); ?>" <?php echo $active['aria'] ?? ''; ?>>
                            <?php e($item['label'] ?? ''); ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        </details>
    </li>
    <?php
};

// Read precomputed layout context from web/index.php (before Session::end()).
$layoutNav = is_array($layoutNav ?? null) ? $layoutNav : [];
$currentTenant = is_array($layoutNav['currentTenant'] ?? null) ? $layoutNav['currentTenant'] : null;
$availableTenants = is_array($layoutNav['availableTenants'] ?? null) ? $layoutNav['availableTenants'] : [];
$tenantQueryParam = trim((string) ($layoutNav['tenantQueryParam'] ?? ''));
$tenantAvatar = is_array($layoutNav['tenantAvatar'] ?? null) ? $layoutNav['tenantAvatar'] : [];
$tenantUuid = trim((string) ($tenantAvatar['uuid'] ?? ''));
$tenantName = trim((string) ($tenantAvatar['name'] ?? ''));
$hasTenantAvatar = !empty($tenantAvatar['hasAvatar']);
$addressBook = is_array($layoutNav['addressBook'] ?? null) ? $layoutNav['addressBook'] : [];
$addressBookUrl = trim((string) ($addressBook['url'] ?? lurl('address-book')));
$activeAddressSearch = trim((string) ($addressBook['activeSearch'] ?? ''));
$activeAddressTenants = is_array($addressBook['activeTenants'] ?? null) ? $addressBook['activeTenants'] : [];
$activeAddressDepartments = is_array($addressBook['activeDepartments'] ?? null) ? $addressBook['activeDepartments'] : [];
$activeAddressRoles = is_array($addressBook['activeRoles'] ?? null) ? $addressBook['activeRoles'] : [];
$activeAddressCustomFilters = is_array($addressBook['activeCustomFilters'] ?? null) ? $addressBook['activeCustomFilters'] : [];
$peopleGroups = is_array($addressBook['peopleGroups'] ?? null) ? $addressBook['peopleGroups'] : [];
$savedAddressFilters = is_array($addressBook['savedFilters'] ?? null) ? $addressBook['savedFilters'] : [];
$csrfKey = trim((string) ($layoutNav['csrfKey'] ?? \MintyPHP\Session::$csrfSessionKey));
$csrfToken = (string) ($layoutNav['csrfToken'] ?? '');
?>
<aside class="app-sidebar" id="app-sidebar">
    <?php if ($currentTenant): ?>
        <div class="app-sidebar-tenant-logo">
            <a href="<?php e(lurl('')); ?>">
                <?php if ($hasTenantAvatar): ?>
                    <img src="auth/tenant-avatar-file?uuid=<?php e($tenantUuid); ?>&size=128" alt="<?php e($tenantName); ?>"
                        title="<?php e($tenantName); ?>">
                <?php else: ?>
                    <div class="app-sidebar-tenant-name">
                        <?php e($tenantName); ?>
                    </div>
                <?php endif; ?>
            </a>
            <button type="button" class="app-sidebar-tenant-toggle" data-sidebar-toggle
                aria-label="<?php e(t('Toggle Sidebar')); ?>">
                <i class="bi bi-list"></i>
            </button>
        </div>
    <?php endif; ?>
    <div id="app-sidebar-panels" class="app-sidebar-panels">
        <nav id="aside-panel-explorer" class="app-sidebar-panel" data-aside-panel="explorer"
            data-aside-title="<?php e(t('Explorer')); ?>" role="tabpanel" aria-labelledby="aside-tab-explorer"
            aria-label="<?php e(t('Primary navigation')); ?>">
            <ul>
                <li>
                    <?php $home = navActive(['', 'admin'], false); ?>
                    <a href="<?php e(lurl('')); ?>" class="<?php e($home['class']); ?>" <?php echo $home['aria']; ?>>
                        <?php e(t('Home')); ?>
                    </a>
                </li>
            </ul>
        </nav>
        <?php if ($hasAdminPanel): ?>
            <nav id="aside-panel-admin" class="app-sidebar-panel" data-aside-panel="admin"
                data-aside-title="<?php e(t('Admin')); ?>" data-aside-details-storage="aside-admin-sections-v1"
                data-aside-details-open-active="1" role="tabpanel" aria-labelledby="aside-tab-admin" hidden>
                <ul>
                    <?php foreach ($adminNavGroups as $group): ?>
                        <?php $renderAdminNavGroup($group, $tenantQueryParam); ?>
                    <?php endforeach; ?>
                </ul>
            </nav>
        <?php endif; ?>

        <?php if ($canViewAddressBook): ?>
            <?php $addressBookActive = navActive('address-book', true); ?>
            <nav id="aside-panel-people" class="app-sidebar-panel" data-aside-panel="people"
                data-aside-details-storage="aside-people-tenant"
                data-aside-title="<?php e(t('Address book')); ?>" role="tabpanel" aria-labelledby="aside-tab-people" hidden>
                <ul>
                    <?php if ($savedAddressFilters): ?>
                        <li class="app-sidebar-group app-sidebar-saved-filters">
                            <small class="muted"><?php e(t('Saved filters')); ?></small>
                            <ul class="app-sidebar-saved-filter-list">
                                <?php foreach ($savedAddressFilters as $savedFilter): ?>
                                    <?php
                                    $savedUuid = trim((string) ($savedFilter['uuid'] ?? ''));
                                    $savedName = trim((string) ($savedFilter['name'] ?? ''));
                                    $savedQuery = $savedFilter['query'] ?? [];
                                    $savedQuery = is_array($savedQuery) ? $savedQuery : [];
                                    if ($savedUuid === '' || $savedName === '') {
                                        continue;
                                    }

                                    $savedSearch = trim((string) ($savedQuery['search'] ?? ''));
                                    $savedTenants = appNormalizeStringList($savedQuery['tenants'] ?? []);
                                    $savedDepartments = appNormalizePositiveIntList($savedQuery['departments'] ?? []);
                                    $savedRoles = appNormalizePositiveIntList($savedQuery['roles'] ?? []);
                                    $savedCustomFilters = appNormalizeAddressBookCustomFilterQuery($savedQuery);

                                    $savedQueryParams = [];
                                    if ($savedSearch !== '') {
                                        $savedQueryParams['search'] = $savedSearch;
                                    }
                                    if ($savedTenants) {
                                        $savedQueryParams['tenants'] = implode(',', $savedTenants);
                                    }
                                    if ($savedDepartments) {
                                        $savedQueryParams['departments'] = implode(',', $savedDepartments);
                                    }
                                    if ($savedRoles) {
                                        $savedQueryParams['roles'] = implode(',', $savedRoles);
                                    }
                                    foreach ($savedCustomFilters as $customKey => $customValue) {
                                        if (is_array($customValue)) {
                                            $savedQueryParams[$customKey] = implode(',', $customValue);
                                        } else {
                                            $savedQueryParams[$customKey] = (string) $customValue;
                                        }
                                    }

                                    $savedHref = $addressBookUrl;
                                    if ($savedQueryParams) {
                                        $savedHref .= '?' . http_build_query($savedQueryParams);
                                    }

                                    $isSavedFilterActive = $addressBookActive['isActive']
                                        && $savedSearch === $activeAddressSearch
                                        && $savedTenants === $activeAddressTenants
                                        && $savedDepartments === $activeAddressDepartments
                                        && $savedRoles === $activeAddressRoles
                                        && $savedCustomFilters === $activeAddressCustomFilters;
                                    ?>
                                    <li class="app-sidebar-saved-filter-item">
                                        <a href="<?php e($savedHref); ?>" class="<?php e($isSavedFilterActive ? 'active' : ''); ?>"
                                            <?php echo $isSavedFilterActive ? 'aria-current="page"' : ''; ?>>
                                            <?php e($savedName); ?>
                                        </a>
                                        <form method="post" action="<?php e(lurl('address-book/saved-filter-delete')); ?>"
                                            class="app-sidebar-saved-filter-delete"
                                            data-confirm-message="<?php e(t('Delete saved filter')); ?>?">
                                            <input type="hidden" name="<?php e($csrfKey); ?>" value="<?php e($csrfToken); ?>">
                                            <input type="hidden" name="uuid" value="<?php e($savedUuid); ?>">
                                            <input type="hidden" name="search" value="<?php e($activeAddressSearch); ?>">
                                            <input type="hidden" name="tenants" value="<?php e(implode(',', $activeAddressTenants)); ?>">
                                            <input type="hidden" name="departments" value="<?php e(implode(',', $activeAddressDepartments)); ?>">
                                            <input type="hidden" name="roles" value="<?php e(implode(',', $activeAddressRoles)); ?>">
                                            <?php foreach ($activeAddressCustomFilters as $customKey => $customValue): ?>
                                                <input type="hidden" name="<?php e($customKey); ?>" value="<?php e(is_array($customValue) ? implode(',', $customValue) : (string) $customValue); ?>">
                                            <?php endforeach; ?>
                                            <button type="submit" class="transparent"
                                                title="<?php e(t('Delete saved filter')); ?>"
                                                aria-label="<?php e(t('Delete saved filter')); ?>">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </li>
                    <?php endif; ?>
                    <?php if (!$peopleGroups): ?>
                        <li>
                            <a href="<?php e($addressBookUrl); ?>" class="<?php e($addressBookActive['class'] ?? ''); ?>"
                                <?php echo $addressBookActive['aria'] ?? ''; ?>>
                                <?php e(t('Address book')); ?>
                            </a>
                        </li>
                    <?php else: ?>
                        <?php foreach ($peopleGroups as $index => $group): ?>
                            <?php
                            $tenant = $group['tenant'] ?? [];
                            $departments = $group['departments'] ?? [];
                            $tenantUuid = (string) ($tenant['uuid'] ?? '');
                            $tenantName = (string) ($tenant['description'] ?? '');
                            if ($tenantUuid === '') {
                                continue;
                            }
                            $baseHref = $addressBookUrl . '?tenants=' . urlencode($tenantUuid);
                            $isActiveTenant = $addressBookActive['isActive']
                                && !$activeAddressDepartments
                                && count($activeAddressTenants) === 1
                                && $activeAddressTenants[0] === $tenantUuid;
                            ?>
                            <li class="app-sidebar-group">
                                    <small>
                                        <a href="<?php e($baseHref); ?>" class="<?php e($isActiveTenant ? 'active' : 'muted'); ?>"
                                            <?php echo $isActiveTenant ? 'aria-current="page"' : ''; ?>>
                                            <?php e($tenantName); ?>
                                        </a>
                                    </small>
                                    <ul>
                                        <?php if (!$departments): ?>
                                            <li>
                                                <?php
                                                $emptyState = [
                                                    'message' => t('No departments'),
                                                    'size' => 'small',
                                                    'align' => 'center',
                                                ];
                                                require templatePath('partials/app-empty-state.phtml');
                                                ?>
                                            </li>
                                        <?php else: ?>
                                            <?php foreach ($departments as $department): ?>
                                                <?php
                                                $departmentId = (int) ($department['id'] ?? 0);
                                                if ($departmentId <= 0) {
                                                    continue;
                                                }
                                                $departmentName = (string) ($department['description'] ?? '');
                                                $href = $baseHref . '&departments=' . $departmentId;
                                                $isActive = $addressBookActive['isActive']
                                                    && in_array($tenantUuid, $activeAddressTenants, true)
                                                    && in_array($departmentId, $activeAddressDepartments, true);
                                                ?>
                                                <li>
                                                    <a href="<?php e($href); ?>" class="<?php e($isActive ? 'active' : ''); ?>"
                                                        <?php echo $isActive ? 'aria-current="page"' : ''; ?>>
                                                        <?php e($departmentName); ?>
                                                    </a>
                                                </li>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </ul>
                            </li>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </nav>
        <?php endif; ?>

        <div id="aside-panel-search" class="app-sidebar-panel" data-aside-panel="search"
            data-aside-title="<?php e(t('Search')); ?>" role="tabpanel" aria-labelledby="aside-tab-search" hidden>
            <form class="app-search">
                <input type="search" name="side-search" id="side-search" placeholder="<?php e(t('Search...')); ?>"
                    aria-label="<?php e(t('Search')); ?>">
                <span class="app-search-shortcut" data-search-shortcut aria-hidden="true"></span>
            </form>
            <div class="app-search-empty-state" data-global-search-empty>
                <i class="bi bi-search" aria-hidden="true"></i>
                <p><?php e(t('To start searching, type in the search field.')); ?></p>
                <small class="muted" data-search-empty-hint-template="<?php e(t('Tip: Use {shortcut} to focus search quickly.')); ?>"></small>
            </div>
            <nav id="global-search">
                <ul class="app-search-results" data-global-search-results>
                    <?php if ($canViewUsers): ?>
                        <?php $usersSearch = navActive('admin/users', true); ?>
                        <li data-search-key="users" data-search-base="<?php e(lurl('admin/users')); ?>">
                            <a href="<?php e(lurl('admin/users')); ?>" class="<?php e($usersSearch['class']); ?>" <?php echo $usersSearch['aria']; ?>>
                                <span><?php e(t('Users')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewAddressBook): ?>
                        <?php $addressBookSearch = navActive('address-book', true); ?>
                        <li data-search-key="address-book" data-search-base="<?php e(lurl('address-book')); ?>">
                            <a href="<?php e(lurl('address-book')); ?>" class="<?php e($addressBookSearch['class'] ?? ''); ?>"
                                <?php echo $addressBookSearch['aria'] ?? ''; ?>>
                                <span><?php e(t('Address book')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewTenants): ?>
                        <?php $tenantsSearch = navActive('admin/tenants', true); ?>
                        <li data-search-key="tenants" data-search-base="<?php e(lurl('admin/tenants')); ?>">
                            <a href="<?php e(lurl('admin/tenants')); ?>" class="<?php e($tenantsSearch['class']); ?>" <?php echo $tenantsSearch['aria']; ?>>
                                <span><?php e(t('Tenants')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewDepartments): ?>
                        <?php $departmentsSearch = navActive('admin/departments', true); ?>
                        <li data-search-key="departments" data-search-base="<?php e(lurl('admin/departments')); ?>">
                            <a href="<?php e(lurl('admin/departments')); ?>"
                                class="<?php e($departmentsSearch['class']); ?>" <?php echo $departmentsSearch['aria']; ?>>
                                <span><?php e(t('Departments')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewRoles): ?>
                        <?php $rolesSearch = navActive('admin/roles', true); ?>
                        <li data-search-key="roles" data-search-base="<?php e(lurl('admin/roles')); ?>">
                            <a href="<?php e(lurl('admin/roles')); ?>" class="<?php e($rolesSearch['class']); ?>" <?php echo $rolesSearch['aria']; ?>>
                                <span><?php e(t('Roles')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewPermissions): ?>
                        <?php $permissionsSearch = navActive('admin/permissions', true); ?>
                        <li data-search-key="permissions" data-search-base="<?php e(lurl('admin/permissions')); ?>">
                            <a href="<?php e(lurl('admin/permissions')); ?>"
                                class="<?php e($permissionsSearch['class']); ?>" <?php echo $permissionsSearch['aria']; ?>>
                                <span><?php e(t('Permissions')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewJobs): ?>
                        <?php $scheduledJobsSearch = navActive('admin/scheduled-jobs', true); ?>
                        <li data-search-key="scheduled-jobs" data-search-base="<?php e(lurl('admin/scheduled-jobs')); ?>">
                            <a href="<?php e(lurl('admin/scheduled-jobs')); ?>"
                                class="<?php e($scheduledJobsSearch['class']); ?>" <?php echo $scheduledJobsSearch['aria']; ?>>
                                <span><?php e(t('Scheduled jobs')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php
                    $pagesSearch = navActivePublicPages();
                    ?>
                    <li data-search-key="pages" data-search-base="<?php e(lurl('')); ?>">
                        <a href="<?php e(lurl('')); ?>" class="<?php e($pagesSearch['class']); ?>" <?php echo $pagesSearch['aria']; ?>>
                            <span><?php e(t('Pages')); ?></span>
                            <span class="badge" data-search-count>0</span>
                        </a>
                        <ul class="app-search-preview" data-search-preview></ul>
                    </li>
                    <?php if ($canViewDocs): ?>
                        <?php $docsSearch = navActive('admin/docs', true); ?>
                        <li data-search-key="docs" data-search-base="<?php e(lurl($docsDefaultPath)); ?>">
                            <a href="<?php e(lurl($docsDefaultPath)); ?>" class="<?php e($docsSearch['class']); ?>" <?php echo $docsSearch['aria']; ?>>
                                <span><?php e(t('Documentation')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php $hotkeysSearch = navActive('help/hotkeys', true); ?>
                    <li data-search-key="hotkeys" data-search-base="<?php e(lurl('help/hotkeys')); ?>">
                        <a href="<?php e(lurl('help/hotkeys')); ?>" class="<?php e($hotkeysSearch['class']); ?>" <?php echo $hotkeysSearch['aria']; ?>>
                            <span><?php e(t('Keyboard shortcuts')); ?></span>
                            <span class="badge" data-search-count>0</span>
                        </a>
                        <ul class="app-search-preview" data-search-preview></ul>
                    </li>
                    <?php if ($canViewMailLog): ?>
                        <?php $mailLogSearch = navActive('admin/mail-log', true); ?>
                        <li data-search-key="mail-log" data-search-base="<?php e(lurl('admin/mail-log')); ?>">
                            <a href="<?php e(lurl('admin/mail-log')); ?>" class="<?php e($mailLogSearch['class']); ?>"
                                <?php echo $mailLogSearch['aria']; ?>>
                                <span><?php e(t('Mail logs')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                    <?php if ($canViewApiAudit): ?>
                        <?php $apiAuditSearch = navActive('admin/api-audit', true); ?>
                        <li data-search-key="api-audit" data-search-base="<?php e(lurl('admin/api-audit')); ?>">
                            <a href="<?php e(lurl('admin/api-audit')); ?>" class="<?php e($apiAuditSearch['class']); ?>"
                                <?php echo $apiAuditSearch['aria']; ?>>
                                <span><?php e(t('API audit')); ?></span>
                                <span class="badge" data-search-count>0</span>
                            </a>
                            <ul class="app-search-preview" data-search-preview></ul>
                        </li>
                    <?php endif; ?>
                </ul>
            </nav>
        </div>

        <nav id="aside-panel-files" class="app-sidebar-panel" data-aside-panel="files"
            data-aside-title="<?php e(t('Files')); ?>" role="tabpanel" aria-labelledby="aside-tab-files" hidden>
            <ul>
                <li>
                    <a href="#"><?php e(t('All files')); ?></a>
                </li>
                <li>
                    <a href="#"><?php e(t('My files')); ?></a>
                </li>
                <li>
                    <a href="#"><?php e(t('Shared with me')); ?></a>
                </li>
            </ul>
            <div class="app-sidebar-panel-tools" data-aside-panel-tools hidden>
                <button type="button" class="icon-button" aria-label="<?php e(t('New folder')); ?>">
                    <i class="bi bi-folder-plus"></i>
                </button>
            </div>
        </nav>
    </div>
</aside>
