@extends('dashboard.app')

@section('page_title', 'Audit Logs')
@section('page_subtitle', 'Track administrative actions and changes.')

@section('dashboard_content')
    <div class="rounded-2xl glass-card p-6">
        <div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
            <div>
                <div class="text-xs uppercase tracking-[0.2em] text-gray-400">Audit trail</div>
                <div class="mt-2 text-lg font-semibold text-white">Recent admin actions</div>
            </div>
            <form method="GET" class="flex w-full flex-wrap gap-3 md:w-auto md:justify-end">
                <input type="search" name="q" value="{{ $filters['q'] ?? '' }}" placeholder="Search email or action"
                       class="w-full md:w-72 rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 placeholder-gray-500 theme-surface">
                <select name="action" class="rounded-xl border border-white/10 px-4 py-2 text-sm text-gray-200 theme-surface">
                    <option value="">All actions</option>
                    @foreach ($actions ?? [] as $action)
                        <option value="{{ $action }}" @selected(($filters['action'] ?? '') === $action)>{{ $action }}</option>
                    @endforeach
                </select>
                <button class="rounded-xl border border-white/10 px-4 py-2 text-sm font-semibold text-gray-200 hover:bg-white/5 transition-colors">Filter</button>
            </form>
        </div>

        <div class="mt-6 overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="text-xs uppercase tracking-[0.15em] text-gray-400">
                    <tr>
                        <th class="py-3 pr-4">Admin</th>
                        <th class="py-3 pr-4">Action</th>
                        <th class="py-3 pr-4">IP</th>
                        <th class="py-3 pr-4">Time</th>
                        <th class="py-3 pr-4">Payload</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/10 text-gray-300">
                    @forelse ($logs ?? [] as $log)
                        <tr>
                            <td class="py-4 pr-4">
                                <div class="font-semibold text-white">{{ $log->admin_email ?? '—' }}</div>
                                <div class="text-xs text-gray-400">#{{ $log->admin_id ?? 'n/a' }}</div>
                            </td>
                            <td class="py-4 pr-4">{{ $log->action }}</td>
                            <td class="py-4 pr-4 text-xs">{{ $log->ip_address ?? '—' }}</td>
                            <td class="py-4 pr-4 text-xs">{{ $log->created_at?->toDateTimeString() ?? '—' }}</td>
                            <td class="py-4 pr-4 text-xs">
                                <pre class="whitespace-pre-wrap rounded-xl border border-slate-900/60 bg-slate-950 px-3 py-2 text-slate-100 dark:border-black/30 dark:bg-black/70">{{ json_encode($log->payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) }}</pre>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="py-6 text-center text-sm text-gray-400">No audit logs yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="mt-6">
            {{ $logs->links('vendor.pagination.dashboard') }}
        </div>
    </div>
@endsection
