<div class="latest-actions-report">
    <x-title>
        Latest Actions
        <x-slot:buttons>
            <button wire:click="$refresh" class="btn btn-primary btn-sm" title="Refresh">
                <i class="mdi mdi-sync"></i>
            </button>
        </x-slot:buttons>
    </x-title>

    <div class="row">
        {{-- MAIN --}}
        <div class="col-12 col-md-9">
            <div class="card">
                <div class="card-body">

                    {{-- CONTROLS --}}
                    <div class="row align-items-end mb-3">
                        <div class="col-4">
                            <label class="form-label">Per Page</label>
                            <select class="form-select" wire:model.live="perPage">
                                <option value="25">25</option>
                                <option value="50">50</option>
                                <option value="100">100</option>
                                <option value="200">200</option>
                            </select>
                        </div>

                        <div class="col-4">
                            <label class="form-label">User</label>
                            <select class="form-select" wire:model.live="userId">
                                <option value="">Any user</option>
                                @foreach($users as $user)
                                    <option value="{{ $user->id }}">
                                        {{ $user->name_first }} {{ $user->name_last }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="col-4">
                            <label class="form-label">Search</label>
                            <input class="form-control"
                                   wire:model.live.debounce.300ms="search"
                                   placeholder="Search actions…">
                        </div>
                    </div>

                    {{-- TABLE --}}
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                            <tr>
                                <th style="width: 170px">When</th>
                                <th style="width: 220px">Client</th>
                                <th>Action</th>
                                <th style="width: 170px">By</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($entries as $entry)
                                @php
                                    $isClaim = $entry->owner instanceof \App\Models\Claim\Claim;
                                    $client  = $isClaim ? ($entry->owner?->client) : $entry->owner;
                                @endphp
                                <tr>
                                    <td class="text-muted small">
                                        <span title="{{ $entry->created_at }}">
                                            {{ $entry->created_at->diffForHumans() }}
                                        </span>
                                        <div class="text-muted small">
                                            {{ $entry->created_at->format('d M Y H:i') }}
                                        </div>
                                    </td>
                                    <td>
                                        @if($client)
                                            <a wire:navigate href="{{ route('clients.show', $client) }}"
                                               class="fw-semibold">
                                                {{ $client->fullName }}
                                            </a>
                                            @if($client->displayId ?? null)
                                                <div class="text-muted small">
                                                    {{ $client->displayId }}
                                                </div>
                                            @endif
                                        @else
                                            <span class="text-muted fst-italic">Deleted client</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($isClaim)
                                            <span class="badge bg-soft-info text-info me-1">Claim</span>
                                        @else
                                            <span class="badge bg-soft-secondary text-secondary me-1">Client</span>
                                        @endif
                                        {{ $entry->action }}
                                    </td>
                                    <td class="text-muted small">
                                        @if($entry->user)
                                            {{ $entry->user->name_first }} {{ $entry->user->name_last }}
                                        @else
                                            <span class="fst-italic">System</span>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-5">
                                        <i class="mdi mdi-history d-block mb-2 fs-2"></i>
                                        No actions recorded yet.
                                    </td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3">
                        {{ $entries->links() }}
                    </div>
                </div>
            </div>
        </div>

        {{-- SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">About</h4>
                    <hr>
                    <p class="text-muted small mb-0">
                        A live feed of the most recent actions recorded
                        against clients in this workspace. Use the filters
                        to narrow by user or search the action text.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
