<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.purchase-orders.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="status" 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 $statusOption)
                <option value="{{ $statusOption->value }}">{{ $statusOption->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>
                        <x-ui.sort-header column="order_number" :sortBy="$sortBy" :sortDir="$sortDir" align="start">{{ __('رقم الأمر') }}</x-ui.sort-header>
                        <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="total_amount" :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>
                        <x-ui.sort-header column="created_at" :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($purchaseOrders as $po)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-mono text-gray-700 text-xs" dir="ltr">
                            {{ $po->order_number }}
                        </td>
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">{{ $po->supplier_name }}</span>
                            @if($po->supplier_contact)
                            <p class="text-xs text-gray-500">{{ $po->supplier_contact }}</p>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600">
                            {{ $po->items_count }}
                        </td>
                        <td class="px-4 py-3 text-center font-mono" dir="ltr">
                            {{ number_format($po->total_amount / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusEnum = $po->status instanceof \App\Domain\Inventory\Enums\PurchaseOrderStatus
                                    ? $po->status
                                    : \App\Domain\Inventory\Enums\PurchaseOrderStatus::from($po->status);

                                $badgeClass = match($statusEnum) {
                                    \App\Domain\Inventory\Enums\PurchaseOrderStatus::Draft => 'bg-gray-100 text-gray-700',
                                    \App\Domain\Inventory\Enums\PurchaseOrderStatus::Submitted => 'bg-blue-100 text-blue-700',
                                    \App\Domain\Inventory\Enums\PurchaseOrderStatus::Confirmed => 'bg-indigo-100 text-indigo-700',
                                    \App\Domain\Inventory\Enums\PurchaseOrderStatus::PartiallyReceived => 'bg-amber-100 text-amber-700',
                                    \App\Domain\Inventory\Enums\PurchaseOrderStatus::Received => 'bg-green-100 text-green-700',
                                    \App\Domain\Inventory\Enums\PurchaseOrderStatus::Cancelled => 'bg-red-100 text-red-700',
                                };
                            @endphp
                            <span class="px-2 py-0.5 text-xs rounded-full {{ $badgeClass }}">
                                {{ $statusEnum->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600 text-xs" dir="ltr">
                            {{ $po->order_date->format('Y-m-d') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex items-center justify-center gap-2">
                                @can('inventory.list')
                                <a href="{{ route('inventory.purchase-orders.show', $po) }}" wire:navigate
                                   class="text-blue-600 hover:text-blue-800 text-xs font-medium">
                                    {{ __('عرض') }}
                                </a>
                                @endcan

                                @can('inventory.update')
                                @if(in_array($statusEnum, [\App\Domain\Inventory\Enums\PurchaseOrderStatus::Confirmed, \App\Domain\Inventory\Enums\PurchaseOrderStatus::PartiallyReceived]))
                                <button wire:click="receive({{ $po->id }})"
                                        wire:loading.attr="disabled"
                                        wire:target="receive({{ $po->id }})"
                                        wire:confirm="{{ __('هل تريد استلام جميع الأصناف المتبقية؟') }}"
                                        class="text-green-600 hover:text-green-800 text-xs font-medium">
                                    <span wire:loading.remove wire:target="receive({{ $po->id }})">{{ __('استلام') }}</span>
                                    <span wire:loading wire:target="receive({{ $po->id }})">{{ __('جارٍ...') }}</span>
                                </button>
                                @endif
                                @endcan

                                @can('inventory.update')
                                @if(in_array($statusEnum, [\App\Domain\Inventory\Enums\PurchaseOrderStatus::Draft, \App\Domain\Inventory\Enums\PurchaseOrderStatus::Submitted, \App\Domain\Inventory\Enums\PurchaseOrderStatus::Confirmed]))
                                <button wire:click="cancel({{ $po->id }})"
                                        wire:loading.attr="disabled"
                                        wire:target="cancel({{ $po->id }})"
                                        wire:confirm="{{ __('هل أنت متأكد من إلغاء أمر الشراء؟') }}"
                                        class="text-red-600 hover:text-red-800 text-xs font-medium">
                                    <span wire:loading.remove wire:target="cancel({{ $po->id }})">{{ __('إلغاء') }}</span>
                                    <span wire:loading wire:target="cancel({{ $po->id }})">{{ __('جارٍ...') }}</span>
                                </button>
                                @endif
                                @endcan
                            </div>
                        </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="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد أوامر شراء') }}</p>
                                @can('inventory.create')
                                <a href="{{ route('inventory.purchase-orders.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($purchaseOrders->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $purchaseOrders->links() }}
        </div>
        @endif
    </div>
</div>
