{{--
    Application topbar.

    Light, sticky, and sits over the content column only — the rail already owns
    the brand, so this bar carries three things: where you are, search, and who
    you are.

    The location label is not decoration. The bar is sticky and the page header
    is not, so once a long listing is scrolled the label is the only thing still
    saying which screen this is. It also stops the bar being an empty strip —
    the global search does not exist until Phase 4, and 48px of nothing across
    the top of every page is the most template-looking thing an interface can
    do.

    The global search box is the single most common action in an asset system
    ("who has AUM-LAP-0042?"), so it lives here rather than inside the assets
    module. It is rendered only once the assets index exists.
--}}
<header class="ams-topbar ams-no-print">

    <button class="ams-icon-btn d-lg-none"
            type="button"
            data-bs-toggle="offcanvas"
            data-bs-target="#amsSidebar"
            aria-controls="amsSidebar"
            aria-label="Open navigation">
        <i class="bi bi-list fs-5" aria-hidden="true"></i>
    </button>

    {{-- Below lg the rail is hidden, so the bar has to name the application. --}}
    <span class="fw-semibold d-lg-none ams-truncate">
        {{ setting('application_name', config('app.name')) }}
    </span>

    <span class="ams-topbar__context d-none d-lg-block ams-truncate">
        @hasSection('title')@yield('title')@else{{ setting('application_name', config('app.name')) }}@endif
    </span>

    {{-- Holds the search and identity against the right edge at any width. --}}
    <span class="flex-grow-1"></span>

    @if (Route::has('assets.index'))
        <form class="ams-search d-none d-md-block"
              style="max-width: 20rem;"
              method="GET"
              action="{{ route('assets.index') }}"
              role="search">
            <input type="search"
                   class="form-control form-control-sm"
                   name="search"
                   value="{{ request('search') }}"
                   placeholder="Search asset tag, serial, MAC or name"
                   aria-label="Search assets">
            <i class="bi bi-search" aria-hidden="true"></i>
        </form>

        <span class="ams-topbar__divider d-none d-md-block"></span>
    @endif

    <div class="d-flex align-items-center gap-1">
        @auth
            <div class="dropdown">
                <button class="ams-user-btn dropdown-toggle"
                        type="button"
                        data-bs-toggle="dropdown"
                        aria-expanded="false">
                    <span class="ams-avatar" aria-hidden="true">{{ auth()->user()->initials() }}</span>
                    <span class="d-none d-sm-inline ams-truncate" style="max-width: 10rem;">
                        {{ auth()->user()->name }}
                    </span>
                </button>

                <ul class="dropdown-menu dropdown-menu-end">
                    <li>
                        <div class="dropdown-header">
                            <div class="d-flex align-items-center gap-2">
                                <span class="ams-avatar ams-avatar--lg" aria-hidden="true">
                                    {{ auth()->user()->initials() }}
                                </span>
                                <div class="min-width-0">
                                    <div class="fw-semibold text-body ams-truncate">{{ auth()->user()->name }}</div>
                                    <div class="ams-text-xs text-secondary ams-truncate">{{ auth()->user()->email }}</div>
                                </div>
                            </div>

                            @if (auth()->user()->is_master)
                                <x-badge color="primary" icon="shield-lock" class="mt-2">Master User</x-badge>
                            @endif
                        </div>
                    </li>
                    <li><hr class="dropdown-divider"></li>

                    @if (Route::has('profile.edit'))
                        <li>
                            <a class="dropdown-item" href="{{ route('profile.edit') }}">
                                <i class="bi bi-person" aria-hidden="true"></i>My Profile
                            </a>
                        </li>
                    @endif

                    @if (Route::has('password.change'))
                        <li>
                            <a class="dropdown-item" href="{{ route('password.change') }}">
                                <i class="bi bi-key" aria-hidden="true"></i>Change Password
                            </a>
                        </li>
                    @endif

                    @if (Route::has('logout'))
                        <li><hr class="dropdown-divider"></li>
                        <li>
                            <form method="POST" action="{{ route('logout') }}">
                                @csrf
                                <button type="submit" class="dropdown-item text-danger">
                                    <i class="bi bi-box-arrow-right" aria-hidden="true"></i>Sign Out
                                </button>
                            </form>
                        </li>
                    @endif
                </ul>
            </div>
        @endauth
    </div>

</header>
