@extends('layouts.app')

@section('title', 'Payroll Audit History')
@section('heading', 'Audit History')

@section('content')
<div class="max-w-6xl space-y-6">
    <div class="flex flex-wrap items-center justify-between gap-4">
        <p class="text-sm text-slate-500">Payroll run and cycle actions.</p>
        <form method="GET" class="flex gap-2">
            <input type="search" name="search" value="{{ request('search') }}" placeholder="Search..." class="rounded-lg border border-slate-300 px-3 py-2 text-sm">
            <button type="submit" class="rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-200">Search</button>
        </form>
    </div>

    <div class="overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-200">
                <thead class="bg-slate-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Schedule On</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Request type</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Outcome</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Actioned By</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200 bg-white">
                    @forelse($logs as $log)
                        @php
                            $outcome = $log->payload['processed'] ?? null;
                            $outcomeStr = $outcome !== null ? 'Pass: ' . ($log->payload['processed'] ?? 0) . ' Fail: ' . ($log->payload['skipped'] ?? 0) : '—';
                        @endphp
                        <tr class="hover:bg-slate-50/50">
                            <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $log->created_at?->format('M j, Y, g:i a') }}</td>
                            <td class="whitespace-nowrap px-4 py-3 text-sm font-medium text-slate-900">{{ str_replace('_', ' ', ucfirst($log->action)) }}</td>
                            <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">Completed</td>
                            <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $outcomeStr }}</td>
                            <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $log->actor?->name ?? '—' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="px-4 py-12 text-center text-sm text-slate-500">No audit entries yet. Run a payroll cycle to see history.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($logs->hasPages())
            <div class="border-t border-slate-200 bg-slate-50 px-4 py-3">{{ $logs->links() }}</div>
        @endif
    </div>
</div>
@endsection
