<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('المنتجات') }}</h1>
        <div class="flex items-center gap-2">
            @can('inventory.list')
            <a href="{{ route('inventory.essential-deliveries') }}" wire:navigate
               class="inline-flex items-center gap-2 px-4 py-2 bg-amber-50 text-amber-700 border border-amber-300 rounded-lg hover:bg-amber-100 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="M7 11.5V14m0-2.5v-6a1.5 1.5 0 113 0m-3 6a1.5 1.5 0 00-3 0v2a7.5 7.5 0 0015 0v-5a1.5 1.5 0 00-3 0m-6-3V11m0-5.5v-1a1.5 1.5 0 013 0v1m0 0V11m0-5.5a1.5 1.5 0 013 0v3m0 0V11"/></svg>
                {{ __('تسليم الأساسيات') }}
            </a>
            @endcan
            @can('inventory.create')
            <a href="{{ route('inventory.products.wizard') }}" 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>
    </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

    {{-- Active filter badge --}}
    @if($stockFilter === 'low')
    <div class="mb-4 flex items-center gap-2">
        <span class="inline-flex items-center gap-1.5 px-3 py-1.5 bg-amber-100 text-amber-800 rounded-full 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 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"/></svg>
            {{ __('عرض المنتجات تحت الحد الأدنى فقط') }}
        </span>
        <button wire:click="$set('stockFilter', '')" class="text-xs text-gray-500 hover:text-gray-700 underline">{{ __('عرض الكل') }}</button>
    </div>
    @endif

    {{-- 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="{{ __('بحث بالاسم، SKU، أو الباركود...') }}"
                       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="categoryFilter" 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($categories as $category)
                <option value="{{ $category->id }}">{{ $category->name_ar }}</option>
                @endforeach
            </select>
            <select wire:model.live="activeFilter" 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="1">{{ __('نشط') }}</option>
                <option value="0">{{ __('غير نشط') }}</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="sku" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('SKU') }}</x-ui.sort-header>
                        <x-ui.sort-header column="name_ar" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('المنتج') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('التصنيف') }}</th>
                        <x-ui.sort-header column="selling_price" :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>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($products as $product)
                    <tr class="hover:bg-gray-50 cursor-pointer" onclick="window.location='{{ route('inventory.products.show', $product) }}'">
                        <td class="px-4 py-3 font-mono text-gray-600 text-xs" dir="ltr">
                            {{ $product->sku }}
                        </td>
                        <td class="px-4 py-3">
                            <div class="flex items-center gap-2">
                                <a href="{{ route('inventory.products.show', $product) }}" wire:navigate class="font-medium text-gray-800 hover:text-blue-600">{{ $product->name_ar }}</a>
                                @if($product->is_essential)
                                <span class="px-1.5 py-0.5 text-xs rounded bg-amber-100 text-amber-700 font-medium">{{ __('أساسي') }}</span>
                                @endif
                            </div>
                            @if($product->name)
                            <p class="text-xs text-gray-500" dir="ltr">{{ $product->name }}</p>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $product->category?->name_ar ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center font-mono" dir="ltr">
                            {{ number_format($product->selling_price / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($product->track_inventory)
                                @php
                                    $totalStock = $product->inventoryLevels->sum('quantity_on_hand');
                                    $stockClass = $totalStock <= 0 ? 'bg-red-100 text-red-700' : ($totalStock <= ($product->min_stock_level ?? 5) ? 'bg-amber-100 text-amber-700' : 'bg-green-100 text-green-700');
                                @endphp
                                <span class="px-2 py-0.5 text-xs {{ $stockClass }} rounded-full" dir="ltr">
                                    {{ $totalStock }}
                                </span>
                            @else
                                <span class="text-xs text-gray-400">{{ __('غير متتبع') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <button wire:click="toggleActive({{ $product->id }})" wire:loading.attr="disabled"
                                    class="px-2 py-0.5 text-xs rounded-full {{ $product->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                                {{ $product->is_active ? __('نشط') : __('غير نشط') }}
                            </button>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('inventory.update')
                            <a href="{{ route('inventory.products.edit', $product) }}" 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="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد منتجات') }}</p>
                                @can('inventory.create')
                                <a href="{{ route('inventory.products.wizard') }}" 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($products->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $products->links() }}
        </div>
        @endif
    </div>
</div>
