<div>
    <!-- Header -->
    <div class="mb-6 flex items-center justify-between">
        <div>
            <h1 class="text-xl sm:text-2xl font-bold text-gray-900">{{ __('معايير التقييم') }}</h1>
            <p class="mt-1 text-sm text-gray-500">{{ __('إدارة معايير تقييم المشتركين') }}</p>
        </div>
        <button
            type="button"
            wire:click="openForm"
            class="inline-flex items-center gap-2 px-4 py-2.5 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 active:bg-blue-800 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>
            {{ __('معيار جديد') }}
        </button>
    </div>

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

    <!-- Search -->
    <div class="mb-4">
        <input
            type="text"
            wire:model.live.debounce.300ms="search"
            placeholder="{{ __('بحث بالاسم...') }}"
            class="w-full max-w-sm rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
    </div>

    <!-- Form Modal -->
    @if($showForm)
        <div class="mb-6 bg-white border border-gray-200 rounded-lg p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-bold text-gray-800">
                    {{ $editingId ? __('تعديل المعيار') : __('إضافة معيار جديد') }}
                </h3>
                <button wire:click="closeForm" 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>

            <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
                <div class="sm:col-span-2 lg:col-span-1">
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الاسم بالعربية') }} *</label>
                    <input type="text" wire:model="name_ar"
                           class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm @error('name_ar') border-red-500 @enderror">
                    @error('name_ar') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الاسم بالإنجليزية') }}</label>
                    <input type="text" wire:model="name" dir="ltr"
                           class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('التصنيف') }}</label>
                    <input type="text" wire:model="category"
                           placeholder="{{ __('مثال: بدني، فني، سلوكي') }}"
                           class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الدرجة القصوى') }} *</label>
                    <input type="number" wire:model="max_score" min="1" max="100" dir="ltr"
                           class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm @error('max_score') border-red-500 @enderror">
                    @error('max_score') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الترتيب') }}</label>
                    <input type="number" wire:model="sort_order" min="0" dir="ltr"
                           class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                </div>

                <div class="sm:col-span-2 lg:col-span-3">
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الوصف') }}</label>
                    <textarea wire:model="description_ar" rows="2"
                              class="w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm"></textarea>
                </div>

                <div class="flex items-center gap-2">
                    <input type="checkbox" wire:model="is_active" id="is_active"
                           class="rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                    <label for="is_active" class="text-sm text-gray-700">{{ __('نشط') }}</label>
                </div>
            </div>

            <div class="mt-4 flex items-center gap-3">
                <button type="button" wire:click="save" wire:loading.attr="disabled" wire:target="save"
                        class="px-5 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 active:bg-blue-800 disabled:bg-blue-400 transition-colors">
                    <span wire:loading.remove wire:target="save">{{ __('حفظ') }}</span>
                    <span wire:loading wire:target="save">{{ __('جارٍ الحفظ...') }}</span>
                </button>
                <button type="button" wire:click="closeForm"
                        class="px-5 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 active:bg-gray-200 transition-colors">
                    {{ __('إلغاء') }}
                </button>
            </div>
        </div>
    @endif

    <!-- Table -->
    <div class="bg-white border border-gray-200 rounded-lg overflow-hidden" wire:loading.class="opacity-50 pointer-events-none" wire:target="toggleActive, delete">
        @if($criteria->isEmpty())
            <div class="p-12 text-center">
                <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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="mt-4 text-sm text-gray-500">{{ __('لا توجد معايير تقييم بعد') }}</p>
            </div>
        @else
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 uppercase">{{ __('المعيار') }}</th>
                            <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 uppercase">{{ __('التصنيف') }}</th>
                            <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 uppercase">{{ __('الدرجة القصوى') }}</th>
                            <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 uppercase">{{ __('الترتيب') }}</th>
                            <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 uppercase">{{ __('الحالة') }}</th>
                            <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 uppercase">{{ __('إجراءات') }}</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @foreach($criteria as $criterion)
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-3 text-sm font-medium text-gray-900">
                                    {{ $criterion->name_ar }}
                                    @if($criterion->description_ar)
                                        <p class="text-xs text-gray-500 mt-0.5">{{ Str::limit($criterion->description_ar, 50) }}</p>
                                    @endif
                                </td>
                                <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-700">
                                    {{ $criterion->category ?? '-' }}
                                </td>
                                <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-700" dir="ltr">
                                    {{ $criterion->max_score }}
                                </td>
                                <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-500" dir="ltr">
                                    {{ $criterion->sort_order }}
                                </td>
                                <td class="px-4 py-3 whitespace-nowrap">
                                    <button type="button" wire:click="toggleActive({{ $criterion->id }})"
                                            class="relative inline-flex h-5 w-9 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 {{ $criterion->is_active ? 'bg-blue-600' : 'bg-gray-200' }}">
                                        <span class="pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out {{ $criterion->is_active ? 'translate-x-4 rtl:-translate-x-4' : 'translate-x-0' }}"></span>
                                    </button>
                                </td>
                                <td class="px-4 py-3 whitespace-nowrap">
                                    <div class="flex items-center gap-2">
                                        <button type="button" wire:click="openForm({{ $criterion->id }})"
                                                class="text-blue-600 hover:text-blue-800 active:text-blue-900 text-xs font-medium">
                                            {{ __('تعديل') }}
                                        </button>
                                        <button type="button"
                                                wire:click="delete({{ $criterion->id }})"
                                                wire:confirm="{{ __('هل أنت متأكد من حذف هذا المعيار؟') }}"
                                                class="text-red-600 hover:text-red-800 active:text-red-900 text-xs font-medium">
                                            {{ __('حذف') }}
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

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