<x-app-layout>
    <!-- Loading Spinner -->
    <div id="loading-spinner" class="fixed inset-0 bg-gray-900 bg-opacity-50 flex items-center justify-center z-50">
        <div class="animate-spin rounded-full h-16 w-16 border-t-4 border-indigo-500"></div>
    </div>

    @if (session('success'))
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline"><i class="fas fa-check-circle mr-2"></i> {{ session('success') }}</span>
        </div>
    @endif

    <x-slot name="header">
        <h2 class="font-semibold text-2xl text-gray-800 leading-tight">
            <i class="fas fa-ticket-alt mr-2"></i> {{ __('Admin Ticket') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white shadow-md rounded-lg overflow-hidden">
                <div class="overflow-x-auto">
                    <!-- The table is forced to be at least 1200px wide -->
                    <table class="min-w-[1200px] w-full divide-y divide-gray-200">
                        <thead class="bg-gray-100">
                            <tr>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    ID</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Team 1</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Team 2</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Date</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Time</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Stadium</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Category</th>
                                <!-- New Column for Seat Types Breakdown -->
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Seat Types</th>
                                <!-- Overall Inventory Column -->
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Inventory</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Status</th>
                                <th scope="col"
                                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                    Action</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @foreach ($tickets as $ticket)
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-gray-900">
                                        {{ $ticket->id }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <img src="{{ asset($ticket->image1) }}" alt="Team1"
                                            class="w-12 h-12 rounded-full object-cover">
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <img src="{{ asset($ticket->image2) }}" alt="Team2"
                                            class="w-12 h-12 rounded-full object-cover">
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $ticket->date }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $ticket->time }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $ticket->stadium }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        @if ($ticket->category === 'league')
                                            <span
                                                class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800">
                                                <i class="fas fa-trophy mr-1"></i> League
                                            </span>
                                        @elseif ($ticket->category === 'cup')
                                            <span
                                                class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800">
                                                <i class="fas fa-medal mr-1"></i> Cup
                                            </span>
                                        @else
                                            <span
                                                class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-800">
                                                <i class="fas fa-question-circle mr-1"></i> Unknown
                                            </span>
                                        @endif
                                    </td>
                                    <!-- Seat Types Breakdown -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        @foreach ($ticket->seatTypes as $seatType)
                                            <div class="mb-1">
                                                <span class="font-semibold">{{ $seatType->name }}</span>:
                                                <span>{{ $seatType->available_tickets }} /
                                                    {{ $seatType->max_tickets }}</span>
                                                <span
                                                    class="text-xs text-gray-600">(${{ number_format($seatType->price, 2) }})</span>
                                            </div>
                                        @endforeach
                                    </td>
                                    <!-- Overall Inventory -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                        {{ $ticket->available_tickets }} / {{ $ticket->max_tickets }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        @if ($ticket->is_available)
                                            <span class="text-green-600 inline-flex items-center"><i
                                                    class="fas fa-check-circle mr-1"></i> Available</span>
                                        @else
                                            <span class="text-red-600 inline-flex items-center"><i
                                                    class="fas fa-times-circle mr-1"></i> Unavailable</span>
                                        @endif
                                    </td>
                                    <!-- Action Buttons -->
                                    <td class="px-6 py-4 whitespace-nowrap text-sm ">
                                        <div class="flex space-x-4 mr-3">
                                            <a href="{{ route('admin.edit', $ticket->id) }}"
                                                class="text-blue-600 hover:text-blue-900" style="margin-right: 10px;">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <form action="{{ route('admin.destroy', $ticket->id) }}" method="POST"
                                                onsubmit="return confirm('Are you sure?');">
                                                @csrf
                                                @method('delete')
                                                <button type="submit" class="text-red-600 hover:text-red-900"
                                                    style="margin-right: 10px;">
                                                    <i class="fas fa-trash-alt"></i>
                                                </button>
                                            </form>
                                            <form action="{{ route('admin.toggleAvailability', $ticket->id) }}"
                                                method="POST">
                                                @csrf
                                                @method('PUT')
                                                <button type="submit" class="text-green-600 hover:text-green-900"
                                                    style="margin-right: 10px;">
                                                    @if ($ticket->is_available)
                                                        <i class="fas fa-toggle-on"></i>
                                                    @else
                                                        <i class="fas fa-toggle-off"></i>
                                                    @endif
                                                </button>
                                            </form>
                                        </div>
                                    </td>

                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
<script>
    // Show the loading spinner when the page starts loading
    window.addEventListener('load', function() {
        document.getElementById('loading-spinner').style.display = 'none';
    });

    // Show the loading spinner when submitting the form
    document.querySelector('form').addEventListener('submit', function() {
        document.getElementById('loading-spinner').style.display = 'flex';
    });
</script>
