<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.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>
            {{ __('إنشاء مجموعة') }}
        </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-4 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="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>
            <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>
        </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>
                        <th class="px-4 py-3 text-start font-medium text-gray-600 cursor-pointer" wire:click="sortColumn('code')">{{ __('الكود') }}</th>
                        <th class="px-4 py-3 text-start font-medium text-gray-600 cursor-pointer" wire:click="sortColumn('name_ar')">{{ __('اسم المجموعة') }}</th>
                        <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>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('الحالة') }}</th>
                        <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">
                        <td class="px-4 py-3 font-mono text-gray-600" dir="ltr">{{ $group->code }}</td>
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $group->name_ar }}</span>
                            @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;
                                $sc = ['forming'=>'blue','active'=>'green','full'=>'amber','on_hold'=>'gray','completed'=>'purple','cancelled'=>'red'];
                                $c = $sc[$sv] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $c }}-100 text-{{ $c }}-700 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')
                                <a href="{{ route('groups.edit', $group) }}" wire:navigate
                                   class="text-green-600 hover:text-green-800 text-sm">{{ __('تعديل') }}</a>
                                @endcan
                                <a href="{{ route('groups.print', $group) }}" target="_blank" class="text-green-600 hover:text-green-700 active:text-green-800 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.create') }}" 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>
