@extends('layouts.admin')

@section('content')
    <div class="container mx-auto p-4">
        <h1 class="text-3xl font-extrabold text-center mb-6 text-gray-800">Football Federation Ticket Creation</h1>

        <div class="bg-white p-6 rounded-xl shadow-lg max-w-lg mx-auto" x-data="ticketForm()" x-init="updateAmounts()">
            <form action="{{ route('tickets.store') }}" method="POST">
                @csrf

                <input type="hidden" name="user_id" value="{{ Auth::id() }}">

                <div class="mb-4">
                    <label for="match_id" class="block text-gray-700 font-medium mb-2">Select Match</label>
                    {{-- Changed @change="updateAmount()" to @change="updateAmounts()" --}}
                    <select name="match_id" id="match_id" x-model="matchId" @change="updateAmounts()" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition duration-150" required>
                        <option value="">Select a Match</option>
                        @foreach ($matches as $match)
                            <option value="{{ $match->match_id }}" data-ordinary-amount="{{ $match->ordinary_amount }}" data-vip-amount="{{ $match->vip_amount }}">{{ $match->home_team }} vs {{ $match->away_team }} - {{ \Carbon\Carbon::parse($match->match_date)->format('Y-m-d H:i') }}</option>
                        @endforeach
                    </select>
                    @error('match_id')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-4">
                    <label for="category" class="block text-gray-700 font-medium mb-2">Ticket Category</label>
                    {{-- Changed @change="updateAmount()" to @change="updateAmounts()" --}}
                    <select name="category" id="category" x-model="category" @change="updateAmounts()" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition duration-150" required>
                        <option value="ordinary">Ordinary</option>
                        <option value="vip">VIP</option>
                    </select>
                    @error('category')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-4">
                    <label for="amount_display" class="block text-gray-700 font-medium mb-2">Ticket Amount</label>
                    <input type="text" id="amount_display" x-model="amount" readonly class="w-full p-3 border border-gray-300 rounded-lg bg-gray-100" required>
                    <input type="hidden" name="amount" x-model="amount">
                    @error('amount')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-4">
                    <label for="purchase_channel" class="block text-gray-700 font-medium mb-2">Purchase Channel</label>
                    <select name="purchase_channel" id="purchase_channel" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition duration-150" required>
                        <option value="ticket_center" selected>Ticket Center (Staff Issued)</option>
                        <option value="online">Online (User Purchase)</option>
                    </select>
                    @error('purchase_channel')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-6">
                    <label for="status" class="block text-gray-700 font-medium mb-2">Status</label>
                    <select name="status" id="status" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition duration-150" required>
                        <option value="valid">Valid (Default)</option>
                        <option value="used">Used</option>
                        <option value="cancelled">Cancelled</option>
                    </select>
                    @error('status')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <button type="submit" class="w-full bg-indigo-600 text-white p-3 rounded-lg font-semibold hover:bg-indigo-700 transition duration-200 shadow-md">
                    Create Ticket
                </button>
            </form>
        </div>

        @if (session('ticket'))
            <div class="mt-8 max-w-md mx-auto bg-white rounded-xl shadow-2xl overflow-hidden relative border-4 border-indigo-600">
                <div class="absolute inset-0 bg-gray-50 bg-opacity-70"></div>
                <div class="relative p-8">
                    <div class="text-center border-b pb-4 mb-4">
                        <i class="fas fa-check-circle text-4xl text-green-500 mb-2"></i>
                        <h2 class="text-2xl font-bold text-gray-900">Ticket Successfully Issued!</h2>
                    </div>
                    
                    <div class="grid grid-cols-2 gap-y-4 mb-6 text-sm">
                        <div class="col-span-2">
                            <p class="text-gray-600">Match:</p>
                            <p class="font-bold text-lg text-gray-800">{{ session('ticket')->match->home_team }} <span class="text-indigo-600">vs</span> {{ session('ticket')->match->away_team }}</p>
                        </div>
                        <div>
                            <p class="text-gray-600">Date & Time:</p>
                            <p class="font-semibold">{{ \Carbon\Carbon::parse(session('ticket')->match->match_date)->format('M d, Y H:i') }}</p>
                        </div>
                        <div>
                            <p class="text-gray-600">Ticket No:</p>
                            <p class="font-semibold text-indigo-700">{{ session('ticket')->ticket_number }}</p>
                        </div>
                        <div>
                            <p class="text-gray-600">Category:</p>
                            <p class="font-semibold uppercase">{{ session('ticket')->category }}</p>
                        </div>
                        <div>
                            <p class="text-gray-600">Channel:</p>
                            <p class="font-semibold uppercase">{{ session('ticket')->purchase_channel }}</p>
                        </div>
                        <div>
                            <p class="text-gray-600">Status:</p>
                            <span class="px-2 py-1 text-xs font-semibold rounded-full @if(session('ticket')->status == 'valid') bg-green-100 text-green-800 @else bg-gray-100 text-gray-800 @endif">{{ session('ticket')->status }}</span>
                        </div>
                    </div>
                    
                    <div class="flex flex-col items-center">
                        <p class="text-xs text-gray-500 mb-2">Scan for Validation</p>
                        <div class="p-2 border border-gray-300 rounded-lg bg-white">
                            {!! session('ticket')->qr_code !!}
                        </div>
                    </div>
                </div>
            </div>
        @endif
    </div>

    <script>
        function ticketForm() {
            console.log('ticketForm initialized');
            return {
                matchId: '',
                category: 'ordinary',
                ordinaryAmount: 0, // State to hold the ordinary amount for the selected match
                vipAmount: 0,      // State to hold the VIP amount for the selected match
                amount: '',

                updateAmounts() {
                    const select = document.getElementById('match_id');
                    const selectedOption = select.options[select.selectedIndex];
                    
                    // 1. Update Category Amounts (runs when matchId changes)
                    if (selectedOption && this.matchId) {
                        this.ordinaryAmount = parseFloat(selectedOption.getAttribute('data-ordinary-amount')) || 0;
                        this.vipAmount = parseFloat(selectedOption.getAttribute('data-vip-amount')) || 0;
                    } else {
                        // Reset if no match is selected
                        this.ordinaryAmount = 0;
                        this.vipAmount = 0;
                    }

                    // 2. Update the main 'amount' property based on the selected category
                    if (this.category === 'ordinary') {
                        this.amount = this.ordinaryAmount.toFixed(2);
                    } else if (this.category === 'vip') {
                        this.amount = this.vipAmount.toFixed(2);
                    } else {
                        this.amount = ''; // Fallback
                    }
                }
            }
        }
    </script>
@endsection