<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('السجل العام') }}</h1>
        @can('participants.create')
        <a href="{{ route('people.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>

    <!-- 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="gender" 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>
                <option value="male">{{ __('ذكر') }}</option>
                <option value="female">{{ __('أنثى') }}</option>
            </select>
            <select wire:model.live="classification" 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>
                <option value="regular">{{ __('عادي') }}</option>
                <option value="vip">VIP</option>
                <option value="scholarship">{{ __('منحة') }}</option>
                <option value="staff_child">{{ __('ابن موظف') }}</option>
                <option value="trial">{{ __('تجريبي') }}</option>
            </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="name_ar" :sortBy="$sortBy" :sortDir="$sortDir" align="start">{{ __('الاسم') }}</x-ui.sort-header>
                        <x-ui.sort-header column="national_id" :sortBy="$sortBy" :sortDir="$sortDir" align="start">{{ __('الرقم القومي') }}</x-ui.sort-header>
                        <x-ui.sort-header column="phone" :sortBy="$sortBy" :sortDir="$sortDir" align="start">{{ __('الهاتف') }}</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>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('العمر') }}</th>
                        <x-ui.sort-header column="created_at" :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($people as $person)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $person->name_ar }}</span>
                            @if($person->name && $person->name !== $person->name_ar)
                            <p class="text-xs text-gray-500" dir="ltr">{{ $person->name }}</p>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600 font-mono" dir="ltr">
                            @if($person->national_id)
                                {{ substr($person->national_id, 0, 3) }}*****{{ substr($person->national_id, -4) }}
                            @else
                                —
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600" dir="ltr">{{ $person->phone ?? '—' }}</td>
                        <td class="px-4 py-3 text-center">
                            @if($person->gender)
                            <span class="text-sm">{{ $person->gender === 'male' ? __('ذكر') : __('أنثى') }}</span>
                            @else
                            —
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($person->classification)
                            @php
                                $classLabels = ['regular' => 'عادي', 'vip' => 'VIP', 'scholarship' => 'منحة', 'staff_child' => 'ابن موظف', 'trial' => 'تجريبي'];
                                $classColors = ['regular' => 'gray', 'vip' => 'purple', 'scholarship' => 'green', 'staff_child' => 'blue', 'trial' => 'amber'];
                                $color = $classColors[$person->classification] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                                {{ __($classLabels[$person->classification] ?? $person->classification) }}
                            </span>
                            @else
                            —
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600">
                            {{ $person->age ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center text-gray-500 text-xs" dir="ltr">
                            {{ $person->created_at?->format('Y-m-d') ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center space-x-2 space-x-reverse">
                            @can('participants.view')
                            <a href="{{ route('people.show', $person) }}" wire:navigate class="text-blue-600 hover:text-blue-800 text-sm">{{ __('عرض') }}</a>
                            @endcan
                            @can('participants.update')
                            <a href="{{ route('people.edit', $person) }}" wire:navigate class="text-green-600 hover:text-green-800 text-sm">{{ __('تعديل') }}</a>
                            @endcan
                            @can('people.delete')
                            <button wire:click="deletePerson({{ $person->id }})"
                                    wire:confirm="{{ __('هل أنت متأكد؟ سيتم حذف الشخص وأي حساب مستخدم مرتبط به.') }}"
                                    class="text-red-600 hover:text-red-800 text-sm">{{ __('حذف') }}</button>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="px-4 py-8 text-center text-gray-500">
                            {{ __('لا توجد سجلات') }}
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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