<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('المنشآت') }}</h1>
        @permission('facilities.create')
        <a href="{{ route('facilities.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>
        @endpermission
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
        <div class="mb-4 p-4 bg-green-50 border border-green-200 text-green-700 rounded-lg text-sm">
            {{ session('success') }}
        </div>
    @endif
    @if(session('error'))
        <div class="mb-4 p-4 bg-red-50 border border-red-200 text-red-700 rounded-lg 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="flex flex-col sm:flex-row gap-4">
            <div class="flex-1">
                <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 $key => $label)
                    <option value="{{ $key }}">{{ $label }}</option>
                @endforeach
            </select>
            <select wire:model.live="type" 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($typeOptions as $key => $label)
                    <option value="{{ $key }}">{{ $label }}</option>
                @endforeach
            </select>
            <select wire:model.live="branch_id" 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($branches as $branch)
                    <option value="{{ $branch->id }}">{{ $branch->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">{{ __('الاسم') }}</th>
                        <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>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('الحالة') }}</th>
                        <th class="px-4 py-3 text-end font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($facilities as $facility)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <div>
                                <p class="font-medium text-gray-800">{{ $facility->name_ar }}</p>
                                @if($facility->code)
                                    <p class="text-xs text-gray-400" dir="ltr">{{ $facility->code }}</p>
                                @endif
                            </div>
                        </td>
                        <td class="px-4 py-3">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-50 text-blue-700">
                                {{ $facility->type->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $facility->branch?->name_ar ?? '-' }}
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600">
                            {{ $facility->capacity ?? '-' }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusColors = [
                                    'active' => 'bg-green-50 text-green-700',
                                    'maintenance' => 'bg-yellow-50 text-yellow-700',
                                    'closed' => 'bg-red-50 text-red-700',
                                    'reserved' => 'bg-purple-50 text-purple-700',
                                    'unavailable' => 'bg-gray-100 text-gray-600',
                                ];
                                $colorClass = $statusColors[$facility->status->value] ?? 'bg-gray-100 text-gray-600';
                            @endphp
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $colorClass }}">
                                {{ $facility->status->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-end">
                            <div class="flex items-center justify-end gap-2">
                                @permission('facilities.manage_layouts')
                                <a href="{{ route('facilities.layouts', $facility) }}" wire:navigate
                                   class="text-indigo-600 hover:text-indigo-800 text-sm font-medium">{{ __('التخطيط') }}</a>
                                @endpermission
                                @permission('facilities.update')
                                <a href="{{ route('facilities.edit', $facility) }}" wire:navigate
                                   class="text-blue-600 hover:text-blue-800 text-sm font-medium">{{ __('تعديل') }}</a>
                                @endpermission
                                @permission('facilities.delete')
                                <button wire:click="delete('{{ $facility->uuid }}')"
                                        wire:confirm="{{ __('هل أنت متأكد من حذف هذه المنشأة؟') }}"
                                        wire:loading.attr="disabled"
                                        class="text-red-600 hover:text-red-800 text-sm font-medium">{{ __('حذف') }}</button>
                                @endpermission
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="px-4 py-12 text-center text-gray-500">
                            <svg class="w-12 h-12 mx-auto mb-3 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/></svg>
                            <p class="text-lg font-medium">{{ __('لا توجد منشآت مسجلة') }}</p>
                            <p class="text-sm mt-1">{{ __('ابدأ بإضافة منشأة جديدة') }}</p>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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