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

    {{-- Flash Messages --}}
    @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

    {{-- Search --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-4">
        <input type="text" wire:model.live.debounce.300ms="search"
               placeholder="{{ __('بحث بالاسم أو الكود...') }}"
               class="w-full sm:w-96 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
    </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>
                        <x-ui.sort-header column="name_ar" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('المستودع') }}</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-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>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($warehouses as $warehouse)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-mono text-gray-600 text-xs" dir="ltr">
                            {{ $warehouse->code }}
                        </td>
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $warehouse->name_ar }}</span>
                            @if($warehouse->name)
                            <p class="text-xs text-gray-500" dir="ltr">{{ $warehouse->name }}</p>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $typeColors = [
                                    'main' => 'blue',
                                    'sub' => 'gray',
                                    'transit' => 'amber',
                                    'returns' => 'red',
                                ];
                                $typeLabels = [
                                    'main' => 'رئيسي',
                                    'sub' => 'فرعي',
                                    'transit' => 'نقل',
                                    'returns' => 'مرتجعات',
                                ];
                                $typeValue = $warehouse->type->value ?? $warehouse->type;
                                $color = $typeColors[$typeValue] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                                {{ __($typeLabels[$typeValue] ?? $typeValue) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $warehouse->branch?->name_ar ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center font-mono" dir="ltr">
                            {{ $warehouse->inventory_levels_count }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="px-2 py-0.5 text-xs rounded-full {{ $warehouse->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                {{ $warehouse->is_active ? __('نشط') : __('غير نشط') }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('inventory.update')
                            <a href="{{ route('inventory.warehouses.edit', $warehouse) }}" wire:navigate
                               class="text-green-600 hover:text-green-800 text-sm">{{ __('تعديل') }}</a>
                            @endcan
                        </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="M8 14v3m4-3v3m4-3v3M3 21h18M3 10h18M3 7l9-4 9 4M4 10h16v11H4V10z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد مستودعات') }}</p>
                                @can('inventory.create')
                                <a href="{{ route('inventory.warehouses.create') }}" wire:navigate
                                   class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium">
                                    {{ __('إضافة أول مستودع') }}
                                </a>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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