@extends('layouts.app')

@section('content')
<div class="py-8">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-6">

        <h1 class="text-2xl font-bold text-gray-800">Webhook Events</h1>

        @if(session('success'))<div class="bg-green-50 border-l-4 border-green-500 text-green-700 px-4 py-3 rounded">{{ session('success') }}</div>@endif

        <form method="GET" class="bg-white rounded-xl border border-gray-200 p-4 grid grid-cols-2 md:grid-cols-6 gap-3">
            <input type="text" name="q" value="{{ request('q') }}" placeholder="Search reff_no..." class="border-gray-300 rounded-lg text-sm">
            <select name="status" class="border-gray-300 rounded-lg text-sm">
                <option value="">All status</option>
                @foreach(['pending', 'forwarding', 'success', 'failed', 'unmatched'] as $s)
                    <option value="{{ $s }}" @selected(request('status') === $s)>{{ $s }}</option>
                @endforeach
            </select>
            <select name="provider" class="border-gray-300 rounded-lg text-sm">
                <option value="">All providers</option>
                <option value="singapay" @selected(request('provider') === 'singapay')>SingaPay</option>
                <option value="faspay" @selected(request('provider') === 'faspay')>Faspay</option>
            </select>
            <select name="prefix" class="border-gray-300 rounded-lg text-sm">
                <option value="">All prefixes</option>
                @foreach($mappings as $m)
                    <option value="{{ $m->prefix }}" @selected(request('prefix') === $m->prefix)>{{ $m->prefix }} — {{ $m->name }}</option>
                @endforeach
            </select>
            <input type="date" name="from" value="{{ request('from') }}" class="border-gray-300 rounded-lg text-sm">
            <div class="flex gap-2">
                <button class="px-4 py-1.5 bg-indigo-600 text-white text-sm rounded-lg">Filter</button>
                <a href="{{ route('admin.events.index') }}" class="px-4 py-1.5 text-sm text-gray-600">Reset</a>
            </div>
        </form>

        <div class="bg-white rounded-xl border border-gray-200 overflow-hidden">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 text-xs uppercase tracking-wider text-gray-500">
                    <tr>
                        <th class="px-5 py-3 text-left">ID</th>
                        <th class="px-5 py-3 text-left">Time</th>
                        <th class="px-5 py-3 text-left">Provider</th>
                        <th class="px-5 py-3 text-left">Reff No</th>
                        <th class="px-5 py-3 text-left">Prefix</th>
                        <th class="px-5 py-3 text-left">Mapping</th>
                        <th class="px-5 py-3 text-center">Attempts</th>
                        <th class="px-5 py-3 text-left">Status</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($events as $e)
                        <tr class="hover:bg-gray-50">
                            <td class="px-5 py-3"><a href="{{ route('admin.events.show', $e) }}" class="text-indigo-600 hover:underline">#{{ $e->id }}</a></td>
                            <td class="px-5 py-3 text-gray-500 text-xs">{{ $e->created_at->format('d M Y H:i:s') }}</td>
                            <td class="px-5 py-3"><span class="text-xs px-2 py-0.5 bg-gray-100 rounded uppercase">{{ $e->provider }}</span></td>
                            <td class="px-5 py-3 font-mono text-xs">{{ $e->reff_no ?? '—' }}</td>
                            <td class="px-5 py-3"><code class="text-xs bg-gray-100 px-1.5 py-0.5 rounded">{{ $e->prefix ?? '—' }}</code></td>
                            <td class="px-5 py-3 text-gray-700">{{ $e->mapping->name ?? '—' }}</td>
                            <td class="px-5 py-3 text-center">{{ $e->attempts }}</td>
                            <td class="px-5 py-3"><span class="text-xs px-2 py-0.5 rounded {{ $e->statusBadgeClass() }}">{{ $e->status }}</span></td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="px-5 py-12 text-center text-gray-400">Tidak ada event.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{ $events->links() }}
    </div>
</div>
@endsection
