<x-app-layout>

<x-module-hero
    title="Pending Registrations"
    description="Review and approve training registration requests"
    icon="fa-clock">

    <x-slot name="tabs">
        @can('view training packages')
            <a href="{{ route('training.packages.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.packages.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-box text-sm"></i> Packages
            </a>
        @endcan
        @can('view training sessions')
            <a href="{{ route('training.sessions.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.sessions.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-calendar-alt text-sm"></i> Sessions
            </a>
            <a href="{{ route('training.sessions.calendar') }}"
               class="module-hero-tab {{ request()->routeIs('training.sessions.calendar') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-calendar-week text-sm"></i> Calendar
            </a>
        @endcan
        @can('view training registrations')
            <a href="{{ route('training.registrations.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.registrations.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-users text-sm"></i> Registrations
            </a>
            <a href="{{ route('training.registrations.pending') }}"
               class="module-hero-tab {{ request()->routeIs('training.registrations.pending') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-clock text-sm"></i> Pending ({{ $statistics['pending'] }})
            </a>
        @endcan
        @can('view training certificates')
            <a href="{{ route('training.certificates.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.certificates.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-certificate text-sm"></i> Certificates
            </a>
        @endcan
        @can('view training reports')
            <a href="{{ route('training.reports.index') }}"
               class="module-hero-tab {{ request()->routeIs('training.reports.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-chart-bar text-sm"></i> Reports
            </a>
        @endcan
    </x-slot>

</x-module-hero>

<div class="p-6">

    {{-- Flash Messages --}}
    @if(session('success'))
        <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-check-circle"></i> {{ session('success') }}
        </div>
    @endif
    @if(session('error'))
        <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
        </div>
    @endif

    @if($registrations->count() > 0)
        <div class="space-y-4">
            @foreach($registrations as $registration)
                <div class="bg-white rounded-lg shadow-sm border-l-4 border-yellow-500 border border-gray-200 overflow-hidden">
                    <div class="p-5">
                        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">

                            {{-- Participant Info --}}
                            <div class="flex items-center gap-4">
                                <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center flex-shrink-0">
                                    <i class="fas fa-user text-green-600"></i>
                                </div>
                                <div>
                                    <div class="font-semibold text-gray-900 text-sm">{{ $registration->user->name }}</div>
                                    <div class="text-xs text-gray-500">{{ $registration->user->email }}</div>
                                </div>
                            </div>

                            {{-- Training Info --}}
                            <div>
                                <div class="font-medium text-gray-900 text-sm">{{ $registration->trainingPackage->name }}</div>
                                <div class="text-xs text-gray-500">
                                    <i class="fas fa-calendar mr-1"></i>
                                    {{ $registration->registration_date->format('M d, Y') }}
                                </div>
                            </div>

                            {{-- Payment Info --}}
                            <div>
                                @if($registration->trainingPackage->is_free)
                                    <span class="text-green-600 font-medium text-sm">Free Program</span>
                                @else
                                    <div class="font-medium text-gray-900 text-sm">{{ $registration->trainingPackage->getFormattedFee() }}</div>
                                    <div class="text-xs text-yellow-600">Payment Pending</div>
                                @endif
                            </div>

                            {{-- Actions --}}
                            <div class="flex items-center gap-2">
                                <a href="{{ route('training.registrations.show', $registration) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                @can('approve training registrations')
                                    <form action="{{ route('training.registrations.approve', $registration) }}" method="POST" class="inline">
                                        @csrf
                                        <button type="submit" class="btn-action-approve">
                                            <i class="fas fa-check"></i> Approve
                                        </button>
                                    </form>
                                    <button type="button"
                                            onclick="openRejectModal({{ $registration->id }}, '{{ route('training.registrations.reject', $registration) }}')"
                                            class="btn-action-reject">
                                        <i class="fas fa-times"></i> Reject
                                    </button>
                                @endcan
                            </div>
                        </div>

                        @if($registration->notes)
                            <div class="mt-3 p-3 bg-gray-50 rounded-lg">
                                <div class="text-xs text-gray-500 mb-1">Notes:</div>
                                <div class="text-sm text-gray-700">{{ $registration->notes }}</div>
                            </div>
                        @endif
                    </div>
                </div>
            @endforeach
        </div>

        @if($registrations->hasPages())
            <div class="mt-6">{{ $registrations->links() }}</div>
        @endif
    @else
        <div class="text-center py-16 bg-white rounded-xl shadow-sm border border-gray-200">
            <i class="fas fa-check-circle text-5xl text-green-300 mb-4 block"></i>
            <h3 class="text-lg font-semibold text-gray-700 mb-2">All Caught Up!</h3>
            <p class="text-gray-500 text-sm">There are no pending registrations that require your attention.</p>
        </div>
    @endif
</div>

{{-- Reject Modal --}}
@can('approve training registrations')
<div id="rejectModal" class="fixed inset-0 z-50 hidden overflow-y-auto">
    <div class="fixed inset-0 bg-black bg-opacity-50"
         onclick="document.getElementById('rejectModal').classList.add('hidden')"></div>
    <div class="relative min-h-screen flex items-center justify-center p-4">
        <div class="relative bg-white rounded-xl shadow-xl max-w-md w-full">
            <div class="flex items-center justify-between px-6 py-4 bg-gradient-to-r from-red-600 to-red-700 rounded-t-xl">
                <h3 class="text-base font-semibold text-white flex items-center gap-2">
                    <i class="fas fa-times-circle"></i> Reject Registration
                </h3>
                <button type="button" onclick="document.getElementById('rejectModal').classList.add('hidden')"
                        class="text-white/80 hover:text-white transition"><i class="fas fa-times"></i></button>
            </div>
            <div class="p-6">
                <p class="text-sm text-gray-600 mb-4">
                    Please provide a reason for rejecting this registration. The applicant will be notified.
                </p>
                <form id="rejectForm" method="POST">
                    @csrf
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">
                            Rejection Reason <span class="text-red-500">*</span>
                        </label>
                        <textarea name="rejection_reason" rows="3" required
                                  class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500"
                                  placeholder="Please provide a clear reason for rejection..."></textarea>
                    </div>
                    <div class="flex justify-end gap-3">
                        <button type="button"
                                onclick="document.getElementById('rejectModal').classList.add('hidden')"
                                class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg text-sm hover:bg-gray-50 transition">
                            Cancel
                        </button>
                        <button type="submit"
                                class="px-4 py-2 bg-red-600 text-white rounded-lg text-sm hover:bg-red-700 transition">
                            <i class="fas fa-times-circle mr-1"></i> Reject Registration
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endcan

@push('scripts')
<script>
    function openRejectModal(registrationId, actionUrl) {
        const form = document.getElementById('rejectForm');
        form.action = actionUrl;
        document.getElementById('rejectModal').classList.remove('hidden');
    }
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            document.getElementById('rejectModal')?.classList.add('hidden');
        }
    });
</script>
@endpush

</x-app-layout>
