<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('المجموعات التدريبية') }}</h1>
        @can('groups.create')
        <a href="{{ route('groups.wizard') }}" 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>
            {{ __('إنشاء مجموعة') }}
        </a>
        @endcan
    </div>

    @if(session('success'))
    <div class="mb-4 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-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-4 mb-4">
        <div class="grid grid-cols-1 sm:grid-cols-5 gap-4">
            <div class="sm:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم أو الكود...') }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="activityFilter" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الأنشطة') }}</option>
                @foreach($activities as $activity)
                <option value="{{ $activity->id }}">{{ $activity->name_ar }}</option>
                @endforeach
            </select>
            <select wire:model.live="programFilter" class="px-4 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="status" class="px-4 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>
        </div>
    </div>

    {{-- Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div 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="code" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('الكود') }}</x-ui.sort-header>
                        <x-ui.sort-header column="name_ar" :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="max_capacity" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('السعة') }}</x-ui.sort-header>
                        <x-ui.sort-header column="status" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الحالة') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('المدرب') }}</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($groups as $group)
                    <tr class="hover:bg-gray-50 cursor-pointer" onclick="window.location='{{ route('groups.show', $group) }}'">
                        <td class="px-4 py-3 font-mono text-gray-600" dir="ltr">{{ $group->code }}</td>
                        <td class="px-4 py-3">
                            <a href="{{ route('groups.show', $group) }}" wire:navigate class="font-medium text-gray-800 hover:text-blue-600">{{ $group->name_ar }}</a>
                            @if($group->name)
                            <p class="text-xs text-gray-500" dir="ltr">{{ $group->name }}</p>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600">{{ $group->program?->name_ar ?? '—' }}</td>
                        <td class="px-4 py-3 text-center">
                            <span class="font-medium {{ $group->current_count >= $group->max_capacity ? 'text-red-600' : 'text-gray-700' }}">
                                {{ $group->current_count }}/{{ $group->max_capacity }}
                            </span>
                            @if($group->waitlist_count > 0)
                            <span class="text-xs text-amber-600 block">({{ $group->waitlist_count }} {{ __('انتظار') }})</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $sv = $group->status->value ?? $group->status;
                                $statusClass = match($sv) {
                                    'forming' => 'bg-blue-100 text-blue-700',
                                    'active' => 'bg-green-100 text-green-700',
                                    'full' => 'bg-amber-100 text-amber-700',
                                    'on_hold' => 'bg-gray-100 text-gray-700',
                                    'completed' => 'bg-purple-100 text-purple-700',
                                    'cancelled' => 'bg-red-100 text-red-700',
                                    default => 'bg-gray-100 text-gray-700',
                                };
                            @endphp
                            <span class="px-2 py-0.5 text-xs {{ $statusClass }} rounded-full">
                                {{ __($statusOptions[$sv] ?? $sv) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-gray-600">{{ $group->headTrainer?->name_ar ?? '—' }}</td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex items-center justify-center gap-3">
                                @can('groups.update')
                                @if($sv === 'forming')
                                <button wire:click="changeStatus('{{ $group->uuid }}', 'active')"
                                        wire:confirm="{{ __('تفعيل هذه المجموعة؟') }}"
                                        class="text-emerald-600 hover:text-emerald-800 text-sm font-medium">{{ __('تفعيل') }}</button>
                                @elseif($sv === 'on_hold')
                                <button wire:click="changeStatus('{{ $group->uuid }}', 'active')"
                                        wire:confirm="{{ __('إعادة تفعيل هذه المجموعة؟') }}"
                                        class="text-emerald-600 hover:text-emerald-800 text-sm font-medium">{{ __('تفعيل') }}</button>
                                @elseif($sv === 'active')
                                <button wire:click="changeStatus('{{ $group->uuid }}', 'on_hold')"
                                        wire:confirm="{{ __('إيقاف هذه المجموعة مؤقتاً؟') }}"
                                        class="text-amber-600 hover:text-amber-800 text-sm font-medium">{{ __('إيقاف') }}</button>
                                @endif
                                <a href="{{ route('groups.edit', $group) }}" wire:navigate
                                   class="text-blue-600 hover:text-blue-800 text-sm">{{ __('تعديل') }}</a>
                                @endcan
                                <a href="{{ route('groups.print', $group) }}" target="_blank" class="text-gray-500 hover:text-gray-700 text-sm transition-colors">{{ __('طباعة') }}</a>
                                @can('groups.delete')
                                <button wire:click="deleteGroup('{{ $group->uuid }}')"
                                        wire:confirm="{{ __('هل أنت متأكد من حذف هذه المجموعة؟ سيتم حذف جميع الجداول والحصص والتسجيلات المرتبطة بها نهائيًا.') }}"
                                        class="text-red-600 hover:text-red-800 text-sm">{{ __('حذف') }}</button>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-4 py-12 text-center">
                            <p class="text-gray-500 text-sm">{{ __('لا توجد مجموعات') }}</p>
                            @can('groups.create')
                            <a href="{{ route('groups.wizard') }}" wire:navigate class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium block">{{ __('إنشاء أول مجموعة') }}</a>
                            @endcan
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($groups->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">{{ $groups->links() }}</div>
        @endif
    </div>
</div>
