<div>
    {{-- Header --}}
    <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('assignments.create')
        <a href="{{ route('assignments.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 active:bg-blue-800 text-sm font-medium transition-colors">
            <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-5 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="px-3 py-2 text-sm 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="scope" class="px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل النطاقات') }}</option>
                @foreach($scopeOptions as $value => $label)
                <option value="{{ $value }}">{{ $label }}</option>
                @endforeach
            </select>
            <select wire:model.live="assignableType" class="px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الأنواع') }}</option>
                <option value="group">{{ __('مجموعة') }}</option>
                <option value="session">{{ __('حصة') }}</option>
            </select>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="hidden md:block bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div wire:loading.class="opacity-50 pointer-events-none">
            <div class="overflow-x-auto">
                <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">{{ __('المستخدم') }}</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>
                            <x-ui.sort-header column="status" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الحالة') }}</x-ui.sort-header>
                            <x-ui.sort-header column="start_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>
                            <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @forelse($assignments as $assignment)
                        <tr class="hover:bg-gray-50">
                            <td class="px-4 py-3">
                                <span class="font-medium text-gray-800">
                                    {{ $assignment->user?->name_ar ?? $assignment->user?->name ?? '—' }}
                                </span>
                                @if($assignment->role_label)
                                <p class="text-xs text-gray-500">{{ $assignment->role_label }}</p>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-gray-600">
                                @if($assignment->assignable)
                                    @if($assignment->assignable_type === \App\Domain\Training\Models\TrainingGroup::class)
                                        <span class="inline-flex items-center gap-1">
                                            <svg class="w-4 h-4 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/></svg>
                                            {{ $assignment->assignable->name_ar ?? '—' }}
                                        </span>
                                    @else
                                        <span class="inline-flex items-center gap-1">
                                            <svg class="w-4 h-4 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                                            {{ __('حصة') }} - {{ $assignment->assignable->session_date?->format('Y-m-d') ?? '—' }}
                                        </span>
                                    @endif
                                @else
                                    <span class="text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $scopeValue = $assignment->scope->value ?? $assignment->scope;
                                    $scopeColors = [
                                        'full' => 'blue',
                                        'partial' => 'amber',
                                        'supervising' => 'purple',
                                        'assisting' => 'green',
                                        'observing' => 'gray',
                                    ];
                                    $sColor = $scopeColors[$scopeValue] ?? 'gray';
                                @endphp
                                <span class="px-2 py-0.5 text-xs bg-{{ $sColor }}-100 text-{{ $sColor }}-700 rounded-full">
                                    {{ $assignment->scope->label() }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $statusValue = $assignment->status->value ?? $assignment->status;
                                    $statusColors = [
                                        'active' => 'green',
                                        'suspended' => 'amber',
                                        'completed' => 'blue',
                                        'cancelled' => 'red',
                                    ];
                                    $stColor = $statusColors[$statusValue] ?? 'gray';
                                @endphp
                                <span class="px-2 py-0.5 text-xs bg-{{ $stColor }}-100 text-{{ $stColor }}-700 rounded-full">
                                    {{ $assignment->status->label() }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                                {{ $assignment->start_date?->format('Y-m-d') ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                                {{ $assignment->end_date?->format('Y-m-d') ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <div class="inline-flex items-center gap-2">
                                    @can('assignments.update')
                                    @if($statusValue === 'active')
                                    <button wire:click="suspend('{{ $assignment->uuid }}')"
                                            wire:loading.attr="disabled"
                                            wire:target="suspend('{{ $assignment->uuid }}')"
                                            class="text-amber-600 hover:text-amber-800 active:text-amber-900 text-sm"
                                            title="{{ __('تعليق') }}">
                                        <span wire:loading.remove wire:target="suspend('{{ $assignment->uuid }}')">{{ __('تعليق') }}</span>
                                        <span wire:loading wire:target="suspend('{{ $assignment->uuid }}')">...</span>
                                    </button>
                                    @endif
                                    @if($statusValue === 'suspended')
                                    <button wire:click="activate('{{ $assignment->uuid }}')"
                                            wire:loading.attr="disabled"
                                            wire:target="activate('{{ $assignment->uuid }}')"
                                            class="text-green-600 hover:text-green-800 active:text-green-900 text-sm"
                                            title="{{ __('تنشيط') }}">
                                        <span wire:loading.remove wire:target="activate('{{ $assignment->uuid }}')">{{ __('تنشيط') }}</span>
                                        <span wire:loading wire:target="activate('{{ $assignment->uuid }}')">...</span>
                                    </button>
                                    @endif
                                    @if(in_array($statusValue, ['active', 'suspended']))
                                    <button wire:click="cancel('{{ $assignment->uuid }}')"
                                            wire:loading.attr="disabled"
                                            wire:target="cancel('{{ $assignment->uuid }}')"
                                            wire:confirm="{{ __('هل أنت متأكد من إلغاء هذا التكليف؟') }}"
                                            class="text-red-600 hover:text-red-800 active:text-red-900 text-sm"
                                            title="{{ __('إلغاء') }}">
                                        <span wire:loading.remove wire:target="cancel('{{ $assignment->uuid }}')">{{ __('إلغاء') }}</span>
                                        <span wire:loading wire:target="cancel('{{ $assignment->uuid }}')">...</span>
                                    </button>
                                    @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 2"/>
                                    </svg>
                                    <p class="text-gray-500 text-sm">{{ __('لا يوجد تكليفات') }}</p>
                                    @can('assignments.create')
                                    <a href="{{ route('assignments.create') }}" wire:navigate
                                       class="mt-2 text-blue-600 hover:text-blue-800 active:text-blue-900 text-sm font-medium">
                                        {{ __('إنشاء أول تكليف') }}
                                    </a>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

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

    {{-- Mobile Cards --}}
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($assignments as $assignment)
            @php
                $scopeValue = $assignment->scope->value ?? $assignment->scope;
                $scopeColors = [
                    'full' => 'blue',
                    'partial' => 'amber',
                    'supervising' => 'purple',
                    'assisting' => 'green',
                    'observing' => 'gray',
                ];
                $sColor = $scopeColors[$scopeValue] ?? 'gray';

                $statusValue = $assignment->status->value ?? $assignment->status;
                $statusColors = [
                    'active' => 'green',
                    'suspended' => 'amber',
                    'completed' => 'blue',
                    'cancelled' => 'red',
                ];
                $stColor = $statusColors[$statusValue] ?? 'gray';
            @endphp
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
                {{-- User & Badges --}}
                <div class="flex items-start justify-between gap-2 mb-3">
                    <div>
                        <span class="font-medium text-gray-800 text-sm">
                            {{ $assignment->user?->name_ar ?? $assignment->user?->name ?? '—' }}
                        </span>
                        @if($assignment->role_label)
                        <p class="text-xs text-gray-500 mt-0.5">{{ $assignment->role_label }}</p>
                        @endif
                    </div>
                    <div class="flex items-center gap-1.5 shrink-0">
                        <span class="px-2 py-0.5 text-xs bg-{{ $sColor }}-100 text-{{ $sColor }}-700 rounded-full">
                            {{ $assignment->scope->label() }}
                        </span>
                        <span class="px-2 py-0.5 text-xs bg-{{ $stColor }}-100 text-{{ $stColor }}-700 rounded-full">
                            {{ $assignment->status->label() }}
                        </span>
                    </div>
                </div>

                {{-- Assignable Info --}}
                <div class="text-sm text-gray-600 mb-3">
                    @if($assignment->assignable)
                        @if($assignment->assignable_type === \App\Domain\Training\Models\TrainingGroup::class)
                            <span class="inline-flex items-center gap-1">
                                <svg class="w-4 h-4 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/></svg>
                                {{ $assignment->assignable->name_ar ?? '—' }}
                            </span>
                        @else
                            <span class="inline-flex items-center gap-1">
                                <svg class="w-4 h-4 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                                {{ __('حصة') }} - {{ $assignment->assignable->session_date?->format('Y-m-d') ?? '—' }}
                            </span>
                        @endif
                    @else
                        <span class="text-gray-400">—</span>
                    @endif
                </div>

                {{-- Dates --}}
                <div class="flex items-center gap-4 text-xs text-gray-500 mb-3">
                    <div>
                        <span class="text-gray-400">{{ __('من') }}:</span>
                        <span dir="ltr">{{ $assignment->start_date?->format('Y-m-d') ?? '—' }}</span>
                    </div>
                    <div>
                        <span class="text-gray-400">{{ __('إلى') }}:</span>
                        <span dir="ltr">{{ $assignment->end_date?->format('Y-m-d') ?? '—' }}</span>
                    </div>
                </div>

                {{-- Actions --}}
                @can('assignments.update')
                <div class="flex items-center gap-3 pt-2 border-t border-gray-100">
                    @if($statusValue === 'active')
                    <button wire:click="suspend('{{ $assignment->uuid }}')"
                            wire:loading.attr="disabled"
                            wire:target="suspend('{{ $assignment->uuid }}')"
                            class="py-2 text-amber-600 hover:text-amber-800 active:text-amber-900 text-sm font-medium"
                            title="{{ __('تعليق') }}">
                        <span wire:loading.remove wire:target="suspend('{{ $assignment->uuid }}')">{{ __('تعليق') }}</span>
                        <span wire:loading wire:target="suspend('{{ $assignment->uuid }}')">...</span>
                    </button>
                    @endif
                    @if($statusValue === 'suspended')
                    <button wire:click="activate('{{ $assignment->uuid }}')"
                            wire:loading.attr="disabled"
                            wire:target="activate('{{ $assignment->uuid }}')"
                            class="py-2 text-green-600 hover:text-green-800 active:text-green-900 text-sm font-medium"
                            title="{{ __('تنشيط') }}">
                        <span wire:loading.remove wire:target="activate('{{ $assignment->uuid }}')">{{ __('تنشيط') }}</span>
                        <span wire:loading wire:target="activate('{{ $assignment->uuid }}')">...</span>
                    </button>
                    @endif
                    @if(in_array($statusValue, ['active', 'suspended']))
                    <button wire:click="cancel('{{ $assignment->uuid }}')"
                            wire:loading.attr="disabled"
                            wire:target="cancel('{{ $assignment->uuid }}')"
                            wire:confirm="{{ __('هل أنت متأكد من إلغاء هذا التكليف؟') }}"
                            class="py-2 text-red-600 hover:text-red-800 active:text-red-900 text-sm font-medium"
                            title="{{ __('إلغاء') }}">
                        <span wire:loading.remove wire:target="cancel('{{ $assignment->uuid }}')">{{ __('إلغاء') }}</span>
                        <span wire:loading wire:target="cancel('{{ $assignment->uuid }}')">...</span>
                    </button>
                    @endif
                </div>
                @endcan
            </div>
        @empty
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 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 2"/>
                    </svg>
                    <p class="text-gray-500 text-sm">{{ __('لا يوجد تكليفات') }}</p>
                    @can('assignments.create')
                    <a href="{{ route('assignments.create') }}" wire:navigate
                       class="mt-2 text-blue-600 hover:text-blue-800 active:text-blue-900 text-sm font-medium">
                        {{ __('إنشاء أول تكليف') }}
                    </a>
                    @endcan
                </div>
            </div>
        @endforelse

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