<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Edit 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 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">
                    {{-- Display validation errors --}}
                    @if ($errors->any())
                        <div class="mb-4">
                            <ul class="text-red-500">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form method="post" action="{{ route('admin.update', $ticket->id) }}" enctype="multipart/form-data">
                        @csrf
                        @method('put')

                        <input type="hidden" name="t_id" value="{{ auth()->user()->id }}">

                        {{-- Ticket Category --}}
                        <div class="mb-4">
                            <label for="category" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-tags mr-2"></i> {{ __('Ticket Category') }}
                            </label>
                            <select name="category" id="category" class="form-control" required>
                                <option value="league" {{ $ticket->category === 'league' ? 'selected' : '' }}>Cambodia
                                    League</option>
                                <option value="cup" {{ $ticket->category === 'cup' ? 'selected' : '' }}>Hun Sen Cup
                                </option>
                            </select>
                        </div>

                        {{-- Image Team 1 --}}
                        <div class="mb-4">
                            <label for="image1" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-upload mr-2"></i> {{ __('Upload Image Team 1') }}
                            </label>
                            <input type="file" name="image1" id="image1" accept=".png, .jpg, .jpeg"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                        @if ($ticket->image1)
                            <div class="mb-3">
                                <label class="form-label">Current Image Team 1</label>
                                <div>
                                    <img src="{{ asset($ticket->image1) }}" style="width: 50px;">
                                </div>
                            </div>
                        @endif

                        {{-- Name Team 1 --}}
                        <div class="mb-4">
                            <label for="nameteam1" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-shield-alt mr-2"></i> {{ __('Name Team 1') }}
                            </label>
                            <input type="text" name="nameteam1" id="nameteam1"
                                value="{{ old('nameteam1', $ticket->nameteam1) }}"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required>
                        </div>

                        {{-- Image Team 2 --}}
                        <div class="mb-4">
                            <label for="image2" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-upload mr-2"></i> {{ __('Upload Image Team 2') }}
                            </label>
                            <input type="file" name="image2" id="image2" accept=".png, .jpg, .jpeg"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                        @if ($ticket->image2)
                            <div class="mb-3">
                                <label class="form-label">Current Image Team 2</label>
                                <div>
                                    <img src="{{ asset($ticket->image2) }}" style="width: 50px;">
                                </div>
                            </div>
                        @endif

                        {{-- Name Team 2 --}}
                        <div class="mb-4">
                            <label for="nameteam2" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-shield-alt mr-2"></i> {{ __('Name Team 2') }}
                            </label>
                            <input type="text" name="nameteam2" id="nameteam2"
                                value="{{ old('nameteam2', $ticket->nameteam2) }}"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required>
                        </div>

                        {{-- Date --}}
                        <div class="mb-4">
                            <label for="date" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-calendar-alt mr-2"></i> {{ __('Date') }}
                            </label>
                            <input type="date" name="date" id="date"
                                value="{{ old('date', $ticket->date) }}"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required>
                        </div>

                        {{-- Time --}}
                        <div class="mb-4">
                            <label for="time" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-clock mr-2"></i> {{ __('Time') }}
                            </label>
                            <input type="time" name="time"
                                value="{{ old('time', \Carbon\Carbon::parse($ticket->time)->format('H:i')) }}"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required>
                        </div>

                        {{-- Stadium --}}
                        <div class="mb-4">
                            <label for="stadium" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-landmark mr-2"></i> {{ __('Stadium') }}
                            </label>
                            <input type="text" name="stadium" id="stadium"
                                value="{{ old('stadium', $ticket->stadium) }}"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required>
                        </div>

                        {{-- Seat Type Inventory Section --}}
                        <h3 class="text-lg font-semibold mt-6 mb-4">
                            <i class="fas fa-layer-group mr-2"></i> Seat Type Inventory
                        </h3>

                        {{-- Red Ticket A_B --}}
                        @php
                            $redSeat = $ticket->seatTypes()->where('name', 'red_A_B')->first();
                            $redSold = $redSeat ? $redSeat->max_tickets - $redSeat->available_tickets : 0;
                        @endphp
                        <div>
                            <label class="block text-sm font-medium text-gray-700">Red Ticket A_B - Total
                                Tickets</label>
                            <input type="number" name="seat_types[red_A_B][available]"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required
                                min="{{ $redSold }}"
                                value="{{ old('seat_types.red_A_B.available', $redSeat ? $redSeat->max_tickets : '') }}">
                            <small class="text-gray-600">Tickets sold: {{ $redSold }}</small>
                        </div>
                        <div class="mt-4">
                            <label class="block text-sm font-medium text-gray-700">Red Ticket A_B - Price</label>
                            <input type="number" step="0.01" name="seat_types[red_A_B][price]"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required
                                value="{{ old('seat_types.red_A_B.price', $redSeat ? $redSeat->price : '') }}">
                        </div>

                        {{-- Blue Ticket C_D --}}
                        @php
                            $blueSeat = $ticket->seatTypes()->where('name', 'blue_C_D')->first();
                            $blueSold = $blueSeat ? $blueSeat->max_tickets - $blueSeat->available_tickets : 0;
                        @endphp
                        <div class="mt-6">
                            <label class="block text-sm font-medium text-gray-700">Blue Ticket C_D - Total
                                Tickets</label>
                            <input type="number" name="seat_types[blue_C_D][available]"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required
                                min="{{ $blueSold }}"
                                value="{{ old('seat_types.blue_C_D.available', $blueSeat ? $blueSeat->max_tickets : '') }}">
                            <small class="text-gray-600">Tickets sold: {{ $blueSold }}</small>
                        </div>
                        <div class="mt-4">
                            <label class="block text-sm font-medium text-gray-700">Blue Ticket C_D - Price</label>
                            <input type="number" step="0.01" name="seat_types[blue_C_D][price]"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" required
                                value="{{ old('seat_types.blue_C_D.price', $blueSeat ? $blueSeat->price : '') }}">
                        </div>

                        {{-- Overall Inventory (Read-only, computed automatically) --}}
                        @php
                            $overallMax =
                                ($redSeat ? $redSeat->max_tickets : 0) + ($blueSeat ? $blueSeat->max_tickets : 0);
                        @endphp
                        <div class="mb-4 mt-6">
                            <label for="max_tickets" class="block text-sm font-medium text-gray-700">
                                <i class="fas fa-layer-group mr-2"></i> {{ __('Overall Tickets Inventory') }}
                            </label>
                            <input type="number" name="max_tickets" id="max_tickets"
                                value="{{ old('max_tickets', $overallMax) }}"
                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" readonly>
                        </div>

                        <button type="submit"
                            class="w-full px-4 py-2 border border-black text-black font-semibold rounded-md hover:bg-black hover:text-white transition duration-300">
                            <i class="fas fa-paper-plane mr-2"></i> Submit
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/js/all.min.js"></script>
