<div>
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('التسجيلات') }}</h1>
        @can('enrollments.create')
        <a href="{{ route('enrollments.create') }}" wire:navigate
           class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
            <span class="hidden sm:inline">{{ __('تسجيل جديد') }}</span>
        </a>
        @endcan
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="mb-4 p-3 sm:p-4 bg-green-50 border border-green-200 rounded-lg text-green-700 text-sm">
        {{ session('success') }}
    </div>
    @endif

    @if(session('error'))
    <div class="mb-4 p-3 sm:p-4 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">
        {{ session('error') }}
    </div>
    @endif

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4 mb-4">
        <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-3">
            <div class="col-span-2 md:col-span-1 lg:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم أو رقم المشترك...') }}"
                       class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="status" class="text-sm px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الحالات') }}</option>
                @foreach($statusOptions as $value => $label)
                <option value="{{ $value }}">{{ __($label) }}</option>
                @endforeach
            </select>
            <select wire:model.live="programFilter" class="text-sm px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل البرامج') }}</option>
                @foreach($programs as $program)
                <option value="{{ $program->id }}">{{ $program->name_ar }}</option>
                @endforeach
            </select>
            <select wire:model.live="groupFilter" class="text-sm px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل المجموعات') }}</option>
                @foreach($groups as $group)
                <option value="{{ $group->id }}">{{ $group->name_ar }}</option>
                @endforeach
            </select>
            <select wire:model.live="paymentFilter" class="text-sm px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل حالات الدفع') }}</option>
                @foreach($paymentOptions as $value => $label)
                <option value="{{ $value }}">{{ __($label) }}</option>
                @endforeach
            </select>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="hidden md:block bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto" wire:loading.class="opacity-50 pointer-events-none">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <x-ui.sort-header column="name_ar" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('المشترك') }}</x-ui.sort-header>
                        <x-ui.sort-header column="group_name" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('المجموعة') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('البرنامج') }}</th>
                        <x-ui.sort-header column="status" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الحالة') }}</x-ui.sort-header>
                        <x-ui.sort-header column="payment_status" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الدفع') }}</x-ui.sort-header>
                        <x-ui.sort-header column="enrollment_date" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('تاريخ التسجيل') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($enrollments as $enrollment)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            @if($enrollment->participant)
                            <div class="flex items-center gap-2">
                                <a href="{{ route('participants.show', $enrollment->participant) }}" wire:navigate class="text-blue-600 hover:text-blue-800 hover:underline font-medium">
                                    {{ $enrollment->participant->person?->name_ar ?? '—' }}
                                </a>
                                @if($enrollment->participant->is_free) <x-ui.free-player-badge :small="true" /> @endif
                            </div>
                            @if($enrollment->participant->participant_number)
                            <p class="text-xs text-gray-500" dir="ltr">
                                {{ $enrollment->participant->participant_number }}
                            </p>
                            @endif
                            @else
                            <span class="text-gray-400">{{ __('محذوف') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3">
                            <a href="{{ route('groups.edit', $enrollment->group) }}" wire:navigate class="text-gray-700 hover:text-blue-600 hover:underline">
                                {{ $enrollment->group?->name_ar ?? '—' }}
                            </a>
                        </td>
                        <td class="px-4 py-3">
                            <a href="{{ route('programs.edit', $enrollment->program) }}" wire:navigate class="text-gray-500 hover:text-blue-600 hover:underline text-xs">
                                {{ $enrollment->program?->name_ar ?? '—' }}
                            </a>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusValue = $enrollment->status->value ?? $enrollment->status;
                                $statusColors = [
                                    'pending' => 'amber',
                                    'active' => 'green',
                                    'completed' => 'blue',
                                    'cancelled' => 'red',
                                    'expired' => 'gray',
                                    'waitlisted' => 'purple',
                                ];
                                $color = $statusColors[$statusValue] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                                {{ __($statusOptions[$statusValue] ?? $statusValue) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $paymentValue = $enrollment->payment_status->value ?? $enrollment->payment_status;
                                $paymentColors = [
                                    'paid' => 'green',
                                    'pending' => 'amber',
                                    'partial' => 'blue',
                                    'overdue' => 'red',
                                    'waived' => 'gray',
                                ];
                                $pColor = $paymentColors[$paymentValue] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $pColor }}-100 text-{{ $pColor }}-700 rounded-full">
                                {{ __($paymentOptions[$paymentValue] ?? $paymentValue) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                            {{ $enrollment->enrollment_date?->format('Y-m-d') ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div x-data="{ showCancel: false, reason: '' }" class="inline-flex items-center gap-2">
                                @can('enrollments.cancel')
                                @if(in_array($statusValue, ['pending', 'active']))
                                <button @click="showCancel = true"
                                        class="text-red-600 hover:text-red-800 text-sm py-2">
                                    {{ __('إلغاء') }}
                                </button>

                                {{-- Cancel Confirmation Modal --}}
                                <div x-show="showCancel" x-cloak
                                     class="fixed inset-0 z-50 flex items-center justify-center bg-black/50"
                                     @keydown.escape.window="showCancel = false">
                                    <div class="bg-white rounded-xl shadow-xl p-6 w-full max-w-md mx-4"
                                         @click.outside="showCancel = false">
                                        <div class="flex items-center justify-between mb-4">
                                            <h3 class="text-lg font-bold text-gray-800">{{ __('تأكيد إلغاء التسجيل') }}</h3>
                                            <button @click="showCancel = false" class="flex items-center justify-center w-8 h-8 rounded-lg text-gray-400 hover:text-gray-600 hover:bg-gray-100 active:bg-gray-200 transition-colors" aria-label="إغلاق">
                                                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                                            </button>
                                        </div>
                                        <p class="text-sm text-gray-600 mb-4">
                                            {{ __('هل أنت متأكد من إلغاء تسجيل') }}
                                            <strong>{{ $enrollment->participant?->person?->name_ar }}</strong>؟
                                        </p>
                                        <textarea x-model="reason"
                                                  class="w-full px-3 py-2 border border-gray-300 rounded-lg mb-4 focus:ring-2 focus:ring-red-500 focus:border-red-500"
                                                  rows="3"
                                                  placeholder="{{ __('سبب الإلغاء (مطلوب)...') }}"></textarea>
                                        <div class="flex items-center justify-end gap-3">
                                            <button @click="showCancel = false"
                                                    class="px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 active:bg-gray-200 rounded-lg">
                                                {{ __('تراجع') }}
                                            </button>
                                            <button @click="if(reason.trim()) { $wire.cancelEnrollment({{ $enrollment->id }}, reason); showCancel = false; }"
                                                    class="px-4 py-2 text-sm bg-red-600 text-white rounded-lg hover:bg-red-700 active:bg-red-800 disabled:opacity-50"
                                                    :disabled="!reason.trim()">
                                                <span wire:loading.remove wire:target="cancelEnrollment">{{ __('تأكيد الإلغاء') }}</span>
                                                <span wire:loading wire:target="cancelEnrollment">{{ __('جارٍ الإلغاء...') }}</span>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                @endif
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-4 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا يوجد تسجيلات') }}</p>
                                @can('enrollments.create')
                                <a href="{{ route('enrollments.create') }}" wire:navigate
                                   class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium py-2">
                                    {{ __('إضافة أول تسجيل') }}
                                </a>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($enrollments->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $enrollments->links() }}
        </div>
        @endif
    </div>

    {{-- Mobile Cards --}}
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($enrollments as $enrollment)
        @php
            $statusValue = $enrollment->status->value ?? $enrollment->status;
            $statusColors = [
                'pending' => 'amber',
                'active' => 'green',
                'completed' => 'blue',
                'cancelled' => 'red',
                'expired' => 'gray',
                'waitlisted' => 'purple',
            ];
            $color = $statusColors[$statusValue] ?? 'gray';

            $paymentValue = $enrollment->payment_status->value ?? $enrollment->payment_status;
            $paymentColors = [
                'paid' => 'green',
                'pending' => 'amber',
                'partial' => 'blue',
                'overdue' => 'red',
                'waived' => 'gray',
            ];
            $pColor = $paymentColors[$paymentValue] ?? 'gray';
        @endphp
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <div class="flex items-start justify-between gap-3 mb-3">
                <div class="min-w-0 flex-1">
                    <h3 class="truncate flex items-center gap-2">
                        @if($enrollment->participant)
                        <a href="{{ route('participants.show', $enrollment->participant) }}" wire:navigate class="text-blue-600 hover:text-blue-800 hover:underline font-medium">
                            {{ $enrollment->participant->person?->name_ar ?? '—' }}
                        </a>
                        @if($enrollment->participant->is_free) <x-ui.free-player-badge :small="true" /> @endif
                        @else
                        <span class="text-gray-400">{{ __('محذوف') }}</span>
                        @endif
                    </h3>
                    @if($enrollment->participant?->participant_number)
                    <p class="text-xs text-gray-500 mt-0.5" dir="ltr">
                        {{ $enrollment->participant->participant_number }}
                    </p>
                    @endif
                </div>
                <div class="flex items-center gap-2 flex-shrink-0">
                    <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                        {{ __($statusOptions[$statusValue] ?? $statusValue) }}
                    </span>
                    <span class="px-2 py-0.5 text-xs bg-{{ $pColor }}-100 text-{{ $pColor }}-700 rounded-full">
                        {{ __($paymentOptions[$paymentValue] ?? $paymentValue) }}
                    </span>
                </div>
            </div>

            <div class="flex items-center gap-4 text-sm mb-3">
                @if($enrollment->group?->name_ar)
                <a href="{{ route('groups.edit', $enrollment->group) }}" wire:navigate class="text-gray-700 hover:text-blue-600 hover:underline truncate">
                    {{ $enrollment->group->name_ar }}
                </a>
                @endif
                @if($enrollment->program?->name_ar)
                <a href="{{ route('programs.edit', $enrollment->program) }}" wire:navigate class="text-gray-500 hover:text-blue-600 hover:underline text-xs truncate">
                    {{ $enrollment->program->name_ar }}
                </a>
                @endif
                @if($enrollment->enrollment_date)
                <span class="flex-shrink-0 text-gray-600" dir="ltr">{{ $enrollment->enrollment_date->format('Y-m-d') }}</span>
                @endif
            </div>

            {{-- Mobile Cancel Action --}}
            <div x-data="{ showCancel: false, reason: '' }">
                @can('enrollments.cancel')
                @if(in_array($statusValue, ['pending', 'active']))
                <button @click="showCancel = true"
                        class="text-red-600 hover:text-red-800 text-sm font-medium py-2">
                    {{ __('إلغاء التسجيل') }}
                </button>

                {{-- Cancel Confirmation Modal --}}
                <div x-show="showCancel" x-cloak
                     class="fixed inset-0 z-50 flex items-center justify-center bg-black/50"
                     @keydown.escape.window="showCancel = false">
                    <div class="bg-white rounded-xl shadow-xl p-6 w-full max-w-md mx-4"
                         @click.outside="showCancel = false">
                        <div class="flex items-center justify-between mb-4">
                            <h3 class="text-lg font-bold text-gray-800">{{ __('تأكيد إلغاء التسجيل') }}</h3>
                            <button @click="showCancel = false" class="flex items-center justify-center w-8 h-8 rounded-lg text-gray-400 hover:text-gray-600 hover:bg-gray-100 active:bg-gray-200 transition-colors" aria-label="إغلاق">
                                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                            </button>
                        </div>
                        <p class="text-sm text-gray-600 mb-4">
                            {{ __('هل أنت متأكد من إلغاء تسجيل') }}
                            <strong>{{ $enrollment->participant?->person?->name_ar }}</strong>؟
                        </p>
                        <textarea x-model="reason"
                                  class="w-full px-3 py-2 border border-gray-300 rounded-lg mb-4 focus:ring-2 focus:ring-red-500 focus:border-red-500"
                                  rows="3"
                                  placeholder="{{ __('سبب الإلغاء (مطلوب)...') }}"></textarea>
                        <div class="flex items-center justify-end gap-3">
                            <button @click="showCancel = false"
                                    class="px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 active:bg-gray-200 rounded-lg">
                                {{ __('تراجع') }}
                            </button>
                            <button @click="if(reason.trim()) { $wire.cancelEnrollment({{ $enrollment->id }}, reason); showCancel = false; }"
                                    class="px-4 py-2 text-sm bg-red-600 text-white rounded-lg hover:bg-red-700 active:bg-red-800 disabled:opacity-50"
                                    :disabled="!reason.trim()">
                                <span wire:loading.remove wire:target="cancelEnrollment">{{ __('تأكيد الإلغاء') }}</span>
                                <span wire:loading wire:target="cancelEnrollment">{{ __('جارٍ الإلغاء...') }}</span>
                            </button>
                        </div>
                    </div>
                </div>
                @endif
                @endcan
            </div>
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8 text-center">
            <svg class="w-12 h-12 text-gray-300 mb-3 mx-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
            </svg>
            <p class="text-gray-500 text-sm">{{ __('لا يوجد تسجيلات') }}</p>
            @can('enrollments.create')
            <a href="{{ route('enrollments.create') }}" wire:navigate
               class="mt-2 inline-block text-blue-600 hover:text-blue-800 text-sm font-medium py-2">
                {{ __('إضافة أول تسجيل') }}
            </a>
            @endcan
        </div>
        @endforelse

        @if($enrollments->hasPages())
        <div class="mt-4">
            {{ $enrollments->links() }}
        </div>
        @endif
    </div>
</div>
