<?php
/** Barre de navigation et recherche globale. */
?>
<header class="navbar navbar-expand-md d-print-none">
    <div class="container-xl">
        <div class="topbar-surface d-flex align-items-center justify-content-between w-100">
            <a class="navbar-brand d-flex flex-row align-items-center py-0"
                href="<?= h(PermissionService::can('dashboard') ? '/dashboard' : PermissionService::firstAllowedPath()) ?>">
                <img src="/assets/logo-globinours-64.png" width="52" height="54" alt="" class="flex-shrink-0"
                    style="margin-right:10px">
                <span class="d-flex flex-column align-items-start">
                    <span class="fw-bold">Globinours</span>
                    <span class="text-muted fw-normal" style="font-size:.72rem; line-height:1.2; letter-spacing:0;">
                        <?= h(t('footer.tagline')) ?>
                    </span>
                </span>
            </a>

            <div class="navbar-nav flex-row order-md-last">
                <span class="search-trigger-wrap"><button class="search-trigger" type="button" data-bs-toggle="modal"
                        data-bs-target="#globalSearchModal"
                        title="<?= h(t('search.open')) ?> — <?= h(t('search.shortcut')) ?>"
                        aria-label="<?= h(t('search.open')) ?> (<?= h(t('search.shortcut')) ?>)"><svg
                            xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                            stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                            stroke-linejoin="round" aria-hidden="true">
                            <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                            <circle cx="10" cy="10" r="7" />
                            <path d="M21 21l-6 -6" />
                        </svg></button></span>
                <!-- Dashboard -->
                <?php if (PermissionService::can('dashboard')): ?>
                <a class="btn <?= $dashboardActive ? 'btn-teal' : 'btn-outline-teal' ?> btn-sm me-2" href="/dashboard">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon me-1" width="18" height="18" viewBox="0 0 24 24"
                        stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                        stroke-linejoin="round">
                        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                        <path d="M4 4h6v8h-6z" />
                        <path d="M4 16h6v4h-6z" />
                        <path d="M14 12h6v8h-6z" />
                        <path d="M14 4h6v6h-6z" />
                    </svg>
                    <?= h(t('nav.dashboard')) ?>
                </a>
                <?php endif; ?>

                <!-- Animaux -->
                <?php if (PermissionService::can('animals')): ?>
                <a class="btn <?= $animalsActive ? 'btn-primary' : 'btn-outline-primary' ?> btn-sm me-2"
                    href="/animals">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon me-1" width="18" height="18" viewBox="0 0 24 24"
                        stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                        stroke-linejoin="round">
                        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                        <path d="M4.5 9.5a2.5 2.5 0 1 0 0 -5a2.5 2.5 0 0 0 0 5z" />
                        <path d="M9 5.5a2.5 2.5 0 1 0 5 0a2.5 2.5 0 0 0 -5 0z" />
                        <path d="M17 9.5a2.5 2.5 0 1 0 0 -5a2.5 2.5 0 0 0 0 5z" />
                        <path
                            d="M7 15.5c0 -2.5 2.2 -4.5 5 -4.5s5 2 5 4.5c0 2 -1.3 4 -3.5 4c-.8 0 -1.1 -.5 -1.5 -.5s-.7 .5 -1.5 .5c-2.2 0 -3.5 -2 -3.5 -4z" />
                    </svg>
                    <?= h(t('nav.animals')) ?>
                </a>
                <?php endif; ?>

                <!-- Administratif -->
                <?php if (PermissionService::can('directory')): ?>
                <a class="btn <?= $directoryActive ? 'btn-purple' : 'btn-outline-purple' ?> btn-sm me-2"
                    href="/directory">👥 <?= h(t('nav.directory')) ?></a>
                <?php endif; ?>

                <!-- Tournée de soins -->
                <?php if (PermissionService::can('care')): ?>
                <a class="btn <?= $careRoundActive ? 'btn-orange' : 'btn-outline-orange' ?> btn-sm me-2"
                    href="/care-round">🩺 <?= h(t('nav.care_round')) ?></a>
                <?php endif; ?>

                <!-- Agenda -->
                <?php if (PermissionService::can('agenda')): ?><a
                    class="btn <?= $agendaActive ? 'btn-pink' : 'btn-outline-pink' ?> btn-sm me-2" href="/agenda">📅
                    <?= h(t('nav.agenda')) ?></a><?php endif; ?>

                <!-- Statistiques -->
                <?php if (PermissionService::can('statistics')): ?>
                <a class="btn <?= $statisticsActive ? 'btn-cyan' : 'btn-outline-cyan' ?> btn-sm me-2" href="/statistics"
                    title="<?= h(t('nav.statistics')) ?>">📊 <?= h(t('nav.statistics')) ?></a>
                <?php endif; ?>

                <?php if (PermissionService::can('accounting')): ?><a
                    class="btn <?= $accountingActive ? 'btn-teal' : 'btn-outline-teal' ?> btn-sm me-2"
                    href="/accounting?year=0">🧾 Comptabilité</a><?php endif; ?>

                <div class="dropdown settings-dropdown"><button class="btn btn-dark btn-sm me-2 settings-trigger"
                        data-bs-toggle="dropdown" aria-expanded="false" title="<?= h(t('account.settings')) ?>"
                        aria-label="<?= h(t('account.settings')) ?>"><svg xmlns="http://www.w3.org/2000/svg"
                            class="icon m-0" width="22" height="22" viewBox="0 0 24 24" stroke-width="2"
                            stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                            <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                            <path d="M12 15.5a3.5 3.5 0 1 0 0 -7a3.5 3.5 0 0 0 0 7z" />
                            <path
                                d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06 .06a2 2 0 1 1 -2.83 2.83l-.06 -.06a1.65 1.65 0 0 0 -1.82 -.33a1.65 1.65 0 0 0 -1 1.51v.17a2 2 0 1 1 -4 0v-.09a1.65 1.65 0 0 0 -1.08 -1.51a1.65 1.65 0 0 0 -1.82 .33l-.06 .06a2 2 0 1 1 -2.83 -2.83l.06 -.06a1.65 1.65 0 0 0 .33 -1.82a1.65 1.65 0 0 0 -1.51 -1h-.17a2 2 0 1 1 0 -4h.09a1.65 1.65 0 0 0 1.51 -1.08a1.65 1.65 0 0 0 -.33 -1.82l-.06 -.06a2 2 0 1 1 2.83 -2.83l.06 .06a1.65 1.65 0 0 0 1.82 .33h.08a1.65 1.65 0 0 0 1 -1.51v-.17a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51a1.65 1.65 0 0 0 1.82 -.33l.06 -.06a2 2 0 1 1 2.83 2.83l-.06 .06a1.65 1.65 0 0 0 -.33 1.82v.08a1.65 1.65 0 0 0 1.51 1h.17a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0 -1.51 1z" />
                        </svg></button>
                    <div class="dropdown-menu dropdown-menu-end">
                        <div class="dropdown-item-text">
                            <div class="fw-bold">👤 <?= h($currentUser['display_name'] ?: $currentUser['username']) ?>
                            </div>
                            <div class="small text-muted"><?= h(ucfirst((string)$currentUser['role'])) ?></div>
                        </div>
                        <div class="dropdown-divider"></div><?php if (PermissionService::can('administrative')):?><a
                            class="dropdown-item" href="/admin/register">📁
                            <?= h(t('nav.administrative')) ?></a><?php endif;?><?php if (PermissionService::can('grants')):?><a
                            class="dropdown-item" href="/admin/grants">📊
                            <?= h(t('nav.grants')) ?></a><?php endif;?><?php if (PermissionService::can('administrative') || PermissionService::can('grants')):?>
                        <div class="dropdown-divider"></div>
                        <?php endif;?><?php if (Auth::is('admin')): ?><?php $lastSettingsGroup = null;
                            foreach ($settingsMenuLinks as [$settingsHref,$settingsIcon,$settingsLabel,$settingsGroup]): if ($settingsGroup !== $lastSettingsGroup): if ($lastSettingsGroup !== null): ?>
                        <div class="dropdown-divider"></div><?php endif; ?><div class="dropdown-header">
                            <?= h(settingsGroupLabel($settingsGroup)) ?></div>
                        <?php $lastSettingsGroup = $settingsGroup;endif; ?><a class="dropdown-item"
                            href="<?= h($settingsHref) ?>"><?= $settingsIcon ?>
                            <?= h($settingsLabel) ?></a><?php endforeach; ?><div class="dropdown-divider"></div>
                        <?php endif; ?><form method="post" action="/logout"><input type="hidden" name="csrf"
                                value="<?= h(Auth::csrf()) ?>"><button class="dropdown-item text-danger" type="submit">↪
                                <?= h(t('account.logout')) ?></button></form>
                    </div>
                </div>

            </div>
        </div>
    </div>
</header>
<div class="modal modal-blur fade" id="globalSearchModal" tabindex="-1" aria-hidden="true"
    data-hint="<?= h(t('search.hint')) ?>" data-loading="<?= h(t('search.loading')) ?>"
    data-none="<?= h(t('search.none')) ?>" data-error="<?= h(t('search.error')) ?>"
    data-all-results="<?= h(t('search.all_results')) ?>"
    data-groups="<?= h(json_encode(['animals' => t('search.group.animals'),'directory' => t('search.group.directory'),'agenda' => t('search.group.agenda'),'accounting' => t('search.group.accounting')], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ?>">
    <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header">
                <h2 class="modal-title"><?= h(t('search.title')) ?></h2><button type="button" class="btn-close"
                    data-bs-dismiss="modal" aria-label="<?= h(t('common.close')) ?>"></button>
            </div>
            <div class="modal-body">
                <div class="input-group input-group-lg mb-3"><span class="input-group-text">⌕</span><input
                        class="form-control" id="globalSearchInput" maxlength="120" autocomplete="off"
                        placeholder="<?= h(t('search.placeholder')) ?>"></div>
                <div id="globalSearchResults" class="list-group">
                    <div class="text-muted text-center py-4"><?= h(t('search.hint')) ?></div>
                </div>
            </div>
        </div>
    </div>
</div>
