<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('حركة المخزون') }}</h1>
    </div>

    {{-- 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-2 lg:grid-cols-6 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>
            <div>
                <input type="date" wire:model.live="dateFrom" dir="ltr"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                       placeholder="{{ __('من تاريخ') }}">
            </div>
            <div>
                <input type="date" wire:model.live="dateTo" dir="ltr"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                       placeholder="{{ __('إلى تاريخ') }}">
            </div>
            <select wire:model.live="movementType" 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($movementTypeOptions as $value => $label)
                <option value="{{ $value }}">{{ __($label) }}</option>
                @endforeach
            </select>
            <select wire:model.live="direction" 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="in">{{ __('وارد') }}</option>
                <option value="out">{{ __('صادر') }}</option>
            </select>
        </div>
        <div class="mt-3">
            <select wire:model.live="warehouseFilter" 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($warehouses as $wh)
                <option value="{{ $wh->id }}">{{ $wh->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-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-start font-medium text-gray-600">{{ __('بواسطة') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($movements as $movement)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 text-gray-600 text-xs" dir="ltr">
                            {{ \Carbon\Carbon::parse($movement->created_at)->format('Y-m-d H:i') }}
                        </td>
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $movement->product_name_ar }}</span>
                            <p class="text-xs text-gray-500 font-mono" dir="ltr">{{ $movement->product_sku }}</p>
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $movement->warehouse_name_ar }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $typeLabels = [
                                    'purchase_received' => 'استلام مشتريات',
                                    'sale' => 'بيع',
                                    'transfer_out' => 'تحويل صادر',
                                    'transfer_in' => 'تحويل وارد',
                                    'return_to_stock' => 'مرتجع للمخزون',
                                    'return_to_supplier' => 'مرتجع للمورد',
                                    'adjustment_up' => 'تسوية ↑',
                                    'adjustment_down' => 'تسوية ↓',
                                    'damage' => 'تلف',
                                    'loss' => 'فقد',
                                    'consumption' => 'استهلاك',
                                    'kit_assembly' => 'تجميع',
                                    'kit_disassembly' => 'تفكيك',
                                    'initial_stock' => 'رصيد افتتاحي',
                                    'count_adjustment_up' => 'جرد ↑',
                                    'count_adjustment_down' => 'جرد ↓',
                                ];
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-gray-100 text-gray-700 rounded-full">
                                {{ __($typeLabels[$movement->movement_type] ?? $movement->movement_type) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($movement->direction === 'in')
                            <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">{{ __('وارد') }}</span>
                            @else
                            <span class="px-2 py-0.5 text-xs bg-red-100 text-red-700 rounded-full">{{ __('صادر') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center font-mono font-medium" dir="ltr">
                            {{ $movement->quantity }}
                        </td>
                        <td class="px-4 py-3 text-center font-mono text-xs" dir="ltr">
                            <span class="text-gray-500">{{ $movement->quantity_before }}</span>
                            <span class="text-gray-400 mx-1">→</span>
                            <span class="font-medium text-gray-800">{{ $movement->quantity_after }}</span>
                        </td>
                        <td class="px-4 py-3 text-gray-600 text-xs">
                            {{ $movement->creator_name_ar ?? $movement->creator_name ?? '—' }}
                        </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="M7 16V4m0 0L3 8m4-4l4 4m6 0v12m0 0l4-4m-4 4l-4-4"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد حركات مخزون') }}</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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