<div>
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">
            {{ $stockCountId ? __('متابعة الجرد') : __('جرد مخزني جديد') }}
        </h1>
        <a href="{{ route('inventory.stock-counts') }}" wire:navigate
           class="text-sm text-gray-600 hover:text-gray-800 font-medium">
            {{ __('العودة للقائمة') }}
        </a>
    </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

    <form wire:submit="save" class="space-y-4 sm:space-y-6">
        {{-- Warehouse Selection --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h2 class="text-base sm:text-lg font-semibold text-gray-700 mb-4">{{ __('بيانات الجرد') }}</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-3 sm:gap-4">
                <div>
                    <label for="warehouse_id" class="block text-sm font-medium text-gray-700 mb-1">{{ __('المستودع') }} *</label>
                    <select id="warehouse_id" wire:model.live="warehouse_id"
                            @if($stockCountId) disabled @endif
                            class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm @error('warehouse_id') border-red-500 @enderror">
                        <option value="">{{ __('-- اختر المستودع --') }}</option>
                        @foreach($warehouses as $warehouse)
                        <option value="{{ $warehouse->id }}">{{ $warehouse->name_ar }} ({{ $warehouse->code }})</option>
                        @endforeach
                    </select>
                    @error('warehouse_id') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>

                <div>
                    <label for="notes" class="block text-sm font-medium text-gray-700 mb-1">{{ __('ملاحظات') }}</label>
                    <input type="text" id="notes" wire:model="notes"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm @error('notes') border-red-500 @enderror"
                           placeholder="{{ __('ملاحظات اختيارية...') }}">
                    @error('notes') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>
        </div>

        {{-- Products Grid --}}
        @if(count($items) > 0)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-4 sm:px-6 py-4 border-b border-gray-200 flex items-center justify-between">
                <h2 class="text-base sm:text-lg font-semibold text-gray-700">{{ __('المنتجات') }}</h2>
                <span class="text-sm text-gray-500">{{ count($items) }} {{ __('منتج') }}</span>
            </div>

            <div class="overflow-x-auto">
                <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">{{ __('SKU') }}</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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @foreach($items as $index => $item)
                        <tr class="hover:bg-gray-50">
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $index + 1 }}</td>
                            <td class="px-4 py-3">
                                <span class="font-medium text-gray-800">{{ $item['name_ar'] }}</span>
                            </td>
                            <td class="px-4 py-3 font-mono text-gray-600 text-xs" dir="ltr">
                                {{ $item['sku'] }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="px-2 py-0.5 bg-gray-100 text-gray-700 rounded text-xs font-mono" dir="ltr">
                                    {{ $item['system_quantity'] }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center">
                                <input type="number"
                                       wire:model.blur="items.{{ $index }}.counted_quantity"
                                       min="0"
                                       dir="ltr"
                                       class="w-24 mx-auto px-3 py-1.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm text-center"
                                       placeholder="—">
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $counted = $item['counted_quantity'];
                                    $system = $item['system_quantity'];
                                    $variance = $counted !== null && $counted !== '' ? (int)$counted - $system : null;
                                @endphp
                                @if($variance !== null)
                                    @if($variance === 0)
                                        <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full font-mono" dir="ltr">0</span>
                                    @elseif($variance > 0)
                                        <span class="px-2 py-0.5 text-xs bg-amber-100 text-amber-700 rounded-full font-mono" dir="ltr">+{{ $variance }}</span>
                                    @else
                                        <span class="px-2 py-0.5 text-xs bg-red-100 text-red-700 rounded-full font-mono" dir="ltr">{{ $variance }}</span>
                                    @endif
                                @else
                                    <span class="text-xs text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center font-mono text-xs" dir="ltr">
                                @if($variance !== null && $variance !== 0)
                                    @php
                                        $costPrice = $item['cost_price'] ?? 0;
                                        $varianceValue = abs($variance) * $costPrice;
                                    @endphp
                                    <span class="{{ $variance < 0 ? 'text-red-600' : 'text-amber-600' }}">
                                        {{ number_format($varianceValue / 100, 2) }} {{ __('ج.م') }}
                                    </span>
                                @elseif($variance === 0)
                                    <span class="text-green-600">0.00 {{ __('ج.م') }}</span>
                                @else
                                    <span class="text-gray-400">—</span>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            {{-- Summary --}}
            @php
                $totalCounted = collect($items)->filter(fn($i) => $i['counted_quantity'] !== null && $i['counted_quantity'] !== '')->count();
                $totalWithVariance = collect($items)->filter(function($i) {
                    if ($i['counted_quantity'] === null || $i['counted_quantity'] === '') return false;
                    return (int)$i['counted_quantity'] !== $i['system_quantity'];
                })->count();
                $totalVarianceValue = collect($items)->reduce(function($carry, $i) {
                    if ($i['counted_quantity'] === null || $i['counted_quantity'] === '') return $carry;
                    $variance = (int)$i['counted_quantity'] - $i['system_quantity'];
                    $cost = $i['cost_price'] ?? 0;
                    return $carry + (abs($variance) * $cost);
                }, 0);
            @endphp
            <div class="px-4 sm:px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex flex-wrap gap-4 sm:gap-6 text-sm">
                    <div>
                        <span class="text-gray-500">{{ __('تم عدها:') }}</span>
                        <span class="font-medium text-gray-800 ms-1">{{ $totalCounted }} / {{ count($items) }}</span>
                    </div>
                    <div>
                        <span class="text-gray-500">{{ __('بها فروقات:') }}</span>
                        <span class="font-medium {{ $totalWithVariance > 0 ? 'text-red-600' : 'text-green-600' }} ms-1">{{ $totalWithVariance }}</span>
                    </div>
                    <div>
                        <span class="text-gray-500">{{ __('إجمالي قيمة الفروقات:') }}</span>
                        <span class="font-medium {{ $totalVarianceValue > 0 ? 'text-red-600' : 'text-green-600' }} ms-1" dir="ltr">
                            {{ number_format($totalVarianceValue / 100, 2) }} {{ __('ج.م') }}
                        </span>
                    </div>
                </div>
            </div>
        </div>
        @elseif($warehouse_id)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8 text-center">
            <svg class="w-12 h-12 text-gray-300 mb-3 mx-auto" 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>
        </div>
        @endif

        {{-- Actions --}}
        @if(count($items) > 0)
        <div class="flex flex-col-reverse sm:flex-row gap-3">
            <button type="submit" wire:loading.attr="disabled" wire:target="save"
                    class="w-full sm:w-auto px-6 py-2.5 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 disabled:bg-blue-400 transition-colors text-sm">
                <span wire:loading.remove wire:target="save">
                    {{ $stockCountId ? __('حفظ بيانات الجرد') : __('بدء الجرد وحفظ') }}
                </span>
                <span wire:loading wire:target="save">{{ __('جارٍ الحفظ...') }}</span>
            </button>
            <a href="{{ route('inventory.stock-counts') }}" wire:navigate
               class="w-full sm:w-auto px-6 py-2.5 bg-gray-100 text-gray-700 font-medium rounded-lg hover:bg-gray-200 transition-colors text-sm text-center">
                {{ __('إلغاء') }}
            </a>
        </div>
        @endif
    </form>
</div>
