{{--
    A clickable column header that sorts the current listing, preserving
    every other filter already in the URL.

        <x-table.sort-link field="asset_tag" label="Asset Tag" :sort="$filters['sort'] ?? null" :direction="$filters['direction'] ?? 'asc'" />

    Clicking an inactive column sorts it ascending; clicking the active
    column flips direction. `page` is dropped from the resulting URL — a
    re-sort always starts back at page one, since "row 40 of the old order"
    means nothing in the new one.
--}}
@props([
    'field',
    'label',
    'sort' => null,
    'direction' => 'asc',
])

@php
    $isActive = $sort === $field;
    $nextDirection = $isActive && $direction === 'asc' ? 'desc' : 'asc';
    $href = request()->fullUrlWithQuery(['sort' => $field, 'direction' => $nextDirection, 'page' => null]);
    $icon = ! $isActive ? 'arrow-down-up' : ($direction === 'asc' ? 'arrow-up' : 'arrow-down');
@endphp

<a href="{{ $href }}" {{ $attributes->merge(['class' => 'ams-sort-link'.($isActive ? ' is-active' : '')]) }}>
    {{ $label }}
    <i class="bi bi-{{ $icon }}" aria-hidden="true"></i>
</a>
