<x-app-layout>
    <x-slot name="header">
        <div>
            <p class="text-sm font-semibold uppercase tracking-wide text-blue-600 dark:text-blue-400">Admin</p>
            <h2 class="mt-1 text-2xl font-bold text-slate-950 dark:text-white">Support Tickets</h2>
        </div>
    </x-slot>

    <div class="space-y-6">
        <section class="grid gap-4 md:grid-cols-3">
            <div class="rounded-lg border border-blue-100 bg-white p-5 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
                <p class="text-sm text-slate-500 dark:text-slate-400">Total Tickets</p>
                <p class="mt-2 text-3xl font-black text-slate-950 dark:text-white">{{ $totalTickets }}</p>
            </div>
            <div class="rounded-lg border border-blue-100 bg-white p-5 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
                <p class="text-sm text-slate-500 dark:text-slate-400">Open Tickets</p>
                <p class="mt-2 text-3xl font-black text-blue-600 dark:text-blue-400">{{ $openTickets }}</p>
            </div>
            <div class="rounded-lg border border-blue-100 bg-white p-5 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
                <p class="text-sm text-slate-500 dark:text-slate-400">Urgent Tickets</p>
                <p class="mt-2 text-3xl font-black text-red-600 dark:text-red-400">{{ $urgentTickets }}</p>
            </div>
        </section>

        <form method="GET" action="{{ route('admin.support.index') }}" class="grid gap-3 rounded-lg border border-blue-100 bg-white p-4 shadow-sm md:grid-cols-6 dark:border-blue-900/50 dark:bg-slate-900">
            <input name="search" value="{{ request('search') }}" placeholder="Search tickets..." class="rounded-lg border-blue-100 bg-white px-3 py-2 text-sm dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100 md:col-span-2">
            <select name="status" class="rounded-lg border-blue-100 bg-white px-3 py-2 text-sm dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100">
                <option value="">All Statuses</option>
                @foreach ($statuses as $value => $label)
                    <option value="{{ $value }}" @selected(request('status') === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <select name="category" class="rounded-lg border-blue-100 bg-white px-3 py-2 text-sm dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100">
                <option value="">All Categories</option>
                @foreach ($categories as $value => $label)
                    <option value="{{ $value }}" @selected(request('category') === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <select name="priority" class="rounded-lg border-blue-100 bg-white px-3 py-2 text-sm dark:border-blue-900 dark:bg-slate-950 dark:text-slate-100">
                <option value="">All Priorities</option>
                @foreach ($priorities as $value => $label)
                    <option value="{{ $value }}" @selected(request('priority') === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <button class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-bold text-white transition hover:bg-blue-700 dark:bg-blue-500">Filter</button>
        </form>

        <section class="overflow-hidden rounded-lg border border-blue-100 bg-white shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-blue-100 dark:divide-blue-900/50">
                    <thead class="bg-blue-50 dark:bg-blue-950/40">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Ticket</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Customer</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Subject</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Status</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Priority</th>
                            <th class="px-4 py-3 text-right text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-blue-50 bg-white dark:divide-blue-900/40 dark:bg-slate-900">
                        @forelse ($tickets as $ticket)
                            <tr>
                                <td class="px-4 py-4 text-sm font-black text-slate-950 dark:text-white">{{ $ticket->ticketNumber() }}</td>
                                <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $ticket->name }}<br><span class="text-xs">{{ $ticket->email }}</span></td>
                                <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $ticket->subject }}</td>
                                <td class="px-4 py-4 text-sm font-bold capitalize text-blue-600 dark:text-blue-300">{{ $ticket->status }}</td>
                                <td class="px-4 py-4 text-sm font-bold capitalize {{ $ticket->priority === 'urgent' ? 'text-red-600 dark:text-red-400' : 'text-slate-600 dark:text-slate-300' }}">{{ $ticket->priority }}</td>
                                <td class="px-4 py-4 text-right text-sm"><a href="{{ route('admin.support.show', $ticket) }}" class="font-semibold text-blue-700 dark:text-blue-300">View</a></td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="px-4 py-10 text-center text-sm text-slate-500 dark:text-slate-400">No tickets found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="border-t border-blue-100 px-4 py-3 dark:border-blue-900/50">{{ $tickets->links() }}</div>
        </section>
    </div>
</x-app-layout>
