<div>
    <div class="flex items-center justify-between mb-6">
        <div>
            <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('أمر الشراء') }}: <span class="font-mono" dir="ltr">{{ $purchaseOrder->order_number }}</span></h1>
            <p class="text-sm text-gray-500 mt-1">{{ __('تاريخ الطلب') }}: {{ $purchaseOrder->order_date->format('Y-m-d') }}</p>
        </div>
        <a href="{{ route('inventory.purchase-orders') }}" wire:navigate
           class="text-sm text-gray-500 hover:text-gray-700">
            &larr; {{ __('العودة للقائمة') }}
        </a>
    </div>

    @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

    {{-- Status & Actions --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
        <div class="flex items-center justify-between">
            <div class="flex items-center gap-3">
                <span class="text-sm font-medium text-gray-600">{{ __('الحالة') }}:</span>
                @php
                    $badgeClass = match($purchaseOrder->status) {
                        \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-3 py-1 text-sm rounded-full {{ $badgeClass }}">{{ $purchaseOrder->status->label() }}</span>
            </div>

            <div class="flex items-center gap-2">
                @can('inventory.manage')
                    @if($purchaseOrder->status === \App\Domain\Inventory\Enums\PurchaseOrderStatus::Draft)
                        <button wire:click="submit" wire:confirm="{{ __('هل تريد إرسال أمر الشراء؟') }}"
                                class="px-3 py-1.5 bg-blue-600 text-white rounded-lg text-sm hover:bg-blue-700">
                            {{ __('إرسال') }}
                        </button>
                    @endif
                    @if($purchaseOrder->status === \App\Domain\Inventory\Enums\PurchaseOrderStatus::Submitted)
                        <button wire:click="confirm" wire:confirm="{{ __('هل تريد تأكيد أمر الشراء؟') }}"
                                class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
                            {{ __('تأكيد') }}
                        </button>
                    @endif
                @endcan
            </div>
        </div>
    </div>

    {{-- Supplier & Warehouse --}}
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h2 class="text-sm font-semibold text-gray-500 mb-3">{{ __('المورد') }}</h2>
            <p class="text-gray-800 font-medium">{{ $purchaseOrder->supplier_name }}</p>
            @if($purchaseOrder->supplier_contact)
                <p class="text-sm text-gray-500 mt-1">{{ $purchaseOrder->supplier_contact }}</p>
            @endif
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h2 class="text-sm font-semibold text-gray-500 mb-3">{{ __('المستودع') }}</h2>
            <p class="text-gray-800 font-medium">{{ $purchaseOrder->warehouse?->name_ar ?? '-' }}</p>
            @if($purchaseOrder->expected_delivery_date)
                <p class="text-sm text-gray-500 mt-1">{{ __('التسليم المتوقع') }}: {{ $purchaseOrder->expected_delivery_date->format('Y-m-d') }}</p>
            @endif
        </div>
    </div>

    {{-- Items Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden mb-6">
        <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-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($purchaseOrder->items as $item)
                <tr>
                    <td class="px-4 py-3">
                        <span class="font-medium text-gray-800">{{ $item->product?->name_ar ?? __('منتج محذوف') }}</span>
                        @if($item->product?->sku)
                            <span class="text-xs text-gray-400 ms-1 font-mono" dir="ltr">{{ $item->product->sku }}</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 text-center text-gray-600">{{ $item->quantity_ordered }}</td>
                    <td class="px-4 py-3 text-center">
                        <span class="{{ $item->quantity_received >= $item->quantity_ordered ? 'text-green-600' : 'text-amber-600' }}">
                            {{ $item->quantity_received }}
                        </span>
                    </td>
                    <td class="px-4 py-3 text-center font-mono text-gray-600" dir="ltr">{{ number_format($item->unit_cost / 100, 2) }}</td>
                    <td class="px-4 py-3 text-center font-mono text-gray-700" dir="ltr">{{ number_format($item->line_total / 100, 2) }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    {{-- Totals --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="max-w-xs ms-auto space-y-2 text-sm">
            <div class="flex justify-between text-gray-600">
                <span>{{ __('المجموع الفرعي') }}</span>
                <span class="font-mono" dir="ltr">{{ number_format($purchaseOrder->subtotal / 100, 2) }} {{ __('ج.م') }}</span>
            </div>
            @if($purchaseOrder->tax_amount)
            <div class="flex justify-between text-gray-600">
                <span>{{ __('الضريبة') }}</span>
                <span class="font-mono" dir="ltr">{{ number_format($purchaseOrder->tax_amount / 100, 2) }} {{ __('ج.م') }}</span>
            </div>
            @endif
            @if($purchaseOrder->shipping_cost)
            <div class="flex justify-between text-gray-600">
                <span>{{ __('الشحن') }}</span>
                <span class="font-mono" dir="ltr">{{ number_format($purchaseOrder->shipping_cost / 100, 2) }} {{ __('ج.م') }}</span>
            </div>
            @endif
            <div class="flex justify-between font-bold text-gray-800 pt-2 border-t">
                <span>{{ __('الإجمالي') }}</span>
                <span class="font-mono" dir="ltr">{{ number_format($purchaseOrder->total_amount / 100, 2) }} {{ __('ج.م') }}</span>
            </div>
        </div>
    </div>

    @if($purchaseOrder->notes)
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mt-6">
        <h2 class="text-sm font-semibold text-gray-500 mb-2">{{ __('ملاحظات') }}</h2>
        <p class="text-gray-700 text-sm">{{ $purchaseOrder->notes }}</p>
    </div>
    @endif
</div>
