<x-app-layout title="Audit log">
    <div class="flex items-center justify-between">
        <h1 class="text-xl font-semibold text-slate-900">Audit log</h1>
        @if ($canExport)
            <a href="{{ route('care.audit.export', request()->query()) }}" class="btn-primary">Export CSV</a>
        @endif
    </div>

    <form method="GET" class="mt-4 flex flex-wrap gap-3">
        <input type="text" name="q" value="{{ request('q') }}" placeholder="Search…" class="rounded-lg border-slate-300 text-sm">
        <select name="action" class="rounded-lg border-slate-300 text-sm">
            <option value="">All actions</option>
            @foreach ($actions as $key => $label)
                <option value="{{ $key }}" @selected(request('action') === $key)>{{ $label }}</option>
            @endforeach
        </select>
        <button type="submit" class="btn-primary">Filter</button>
    </form>

    <div class="mt-4 overflow-hidden rounded-2xl border border-slate-200 bg-white">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                <tr><th class="px-4 py-3">Time</th><th class="px-4 py-3">Action</th><th class="px-4 py-3">Actor</th><th class="px-4 py-3">Subject</th></tr>
            </thead>
            <tbody class="divide-y divide-slate-50">
                @forelse ($logs as $log)
                    <tr>
                        <td class="px-4 py-3 whitespace-nowrap">{{ $log->created_at?->format('Y-m-d H:i') }}</td>
                        <td class="px-4 py-3">{{ $actions[$log->action] ?? $log->action }}</td>
                        <td class="px-4 py-3 font-mono text-xs">{{ $log->actor_ref ?? '—' }}</td>
                        <td class="px-4 py-3">{{ $log->subject_type ? class_basename($log->subject_type).' #'.$log->subject_id : '—' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="4" class="px-4 py-6 text-center text-slate-500">No audit entries yet.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4">{{ $logs->links() }}</div>
</x-app-layout>
