<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.manage')
        <a href="{{ route('inventory.stock-counts.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
    @if(session('error'))
    <div class="mb-4 p-4 bg-red-50 border border-red-200 rounded-lg text-red-700 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="grid grid-cols-1 sm:grid-cols-3 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="statusFilter" 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($statuses as $status)
                <option value="{{ $status->value }}">{{ $status->label() }}</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>
                        <x-ui.sort-header column="count_date" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('التاريخ') }}</x-ui.sort-header>
                        <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>
                        <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($stockCounts as $count)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-mono text-gray-700 text-xs" dir="ltr">
                            {{ $count->count_number }}
                        </td>
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $count->warehouse->name_ar ?? '—' }}</span>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                            {{ $count->started_at?->format('Y-m-d') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusValue = $count->status instanceof \App\Domain\Inventory\Enums\StockCountStatus
                                    ? $count->status->value
                                    : $count->status;
                                $statusColors = [
                                    'open' => 'bg-blue-100 text-blue-700',
                                    'in_progress' => 'bg-amber-100 text-amber-700',
                                    'completed' => 'bg-green-100 text-green-700',
                                    'cancelled' => 'bg-gray-100 text-gray-600',
                                ];
                                $statusLabel = $count->status instanceof \App\Domain\Inventory\Enums\StockCountStatus
                                    ? $count->status->label()
                                    : $statusValue;
                            @endphp
                            <span class="px-2 py-0.5 text-xs rounded-full {{ $statusColors[$statusValue] ?? 'bg-gray-100 text-gray-600' }}">
                                {{ $statusLabel }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600">
                            {{ $count->items_count ?? $count->total_items }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($count->total_discrepancies > 0)
                                <span class="px-2 py-0.5 text-xs bg-red-100 text-red-700 rounded-full">
                                    {{ $count->total_discrepancies }}
                                </span>
                            @else
                                <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">
                                    {{ __('لا فروقات') }}
                                </span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center font-mono text-xs" dir="ltr">
                            @if($count->total_variance_value > 0)
                                <span class="text-red-600">
                                    {{ number_format($count->total_variance_value / 100, 2) }} {{ __('ج.م') }}
                                </span>
                            @else
                                <span class="text-green-600">0.00 {{ __('ج.م') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex items-center justify-center gap-2">
                                @if(in_array($statusValue, ['open', 'in_progress']))
                                    @can('inventory.manage')
                                    <a href="{{ route('inventory.stock-counts.edit', $count) }}" wire:navigate
                                       class="text-blue-600 hover:text-blue-800 text-xs font-medium">
                                        {{ __('متابعة') }}
                                    </a>
                                    <button wire:click="finalize({{ $count->id }})"
                                            wire:loading.attr="disabled"
                                            wire:confirm="{{ __('هل تريد إكمال الجرد؟ يجب أن تكون كل المنتجات قد تم عدها.') }}"
                                            class="text-green-600 hover:text-green-800 text-xs font-medium">
                                        {{ __('إكمال') }}
                                    </button>
                                    <button wire:click="cancel({{ $count->id }})"
                                            wire:loading.attr="disabled"
                                            wire:confirm="{{ __('هل أنت متأكد من إلغاء هذا الجرد؟') }}"
                                            class="text-red-600 hover:text-red-800 text-xs font-medium">
                                        {{ __('إلغاء') }}
                                    </button>
                                    @endcan
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" 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="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 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد عمليات جرد') }}</p>
                                @can('inventory.manage')
                                <a href="{{ route('inventory.stock-counts.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($stockCounts->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $stockCounts->links() }}
        </div>
        @endif
    </div>
</div>
