<div class="space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-2xl font-semibold text-zinc-900 dark:text-white dark:text-white">Purchase Order #{{ $purchaseOrder->po_number }}</h1>
            <p class="mt-1 text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                Created {{ $purchaseOrder->created_at->format('M d, Y') }}
            </p>
        </div>
        <div class="flex items-center space-x-3">
            @if(!$receivingMode)
                @if(in_array($purchaseOrder->status, ['draft', 'pending']))
                    <flux:button wire:click="markAsOrdered" variant="outline">
                        Mark as Ordered
                    </flux:button>
                @endif
                @if(in_array($purchaseOrder->status, ['ordered', 'partial']))
                    <flux:button wire:click="startReceiving" variant="primary">
                        Receive Items
                    </flux:button>
                @endif
                @if(in_array($purchaseOrder->status, ['draft', 'pending', 'ordered']))
                    <flux:button wire:click="cancelOrder" variant="danger">
                        Cancel Order
                    </flux:button>
                @endif
            @else
                <flux:button wire:click="receiveItems" variant="primary">
                    Save Received Items
                </flux:button>
                <flux:button wire:click="cancelReceiving" variant="ghost">
                    Cancel
                </flux:button>
            @endif
            <flux:button wire:navigate href="{{ route('inventory.purchase-orders.index') }}" variant="ghost">
                <flux:icon.arrow-left class="w-4 h-4" />
                Back to Orders
            </flux:button>
        </div>
    </div>

    <!-- Order Summary -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Order Details -->
        <div class="lg:col-span-2 bg-white dark:bg-zinc-800 rounded-lg border border-zinc-200 dark:border-zinc-700 p-6">
            <h3 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white mb-4">Order Details</h3>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <dl class="space-y-3">
                        <div>
                            <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Supplier</dt>
                            <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->supplier->name }}</dd>
                        </div>
                        <div>
                            <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Order Date</dt>
                            <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->order_date->format('M d, Y') }}</dd>
                        </div>
                        <div>
                            <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Expected Date</dt>
                            <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->expected_date ? $purchaseOrder->expected_date->format('M d, Y') : 'Not set' }}</dd>
                        </div>
                    </dl>
                </div>
                <div>
                    <dl class="space-y-3">
                        <div>
                            <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Status</dt>
                            <dd class="text-sm">
                                @php
                                    $statusColors = [
                                        'draft' => 'gray',
                                        'pending' => 'yellow',
                                        'ordered' => 'blue',
                                        'partial' => 'orange',
                                        'received' => 'green',
                                        'cancelled' => 'red'
                                    ];
                                @endphp
                                <flux:badge color="{{ $statusColors[$purchaseOrder->status] ?? 'gray' }}">
                                    {{ ucfirst($purchaseOrder->status) }}
                                </flux:badge>
                            </dd>
                        </div>
                        @if($purchaseOrder->received_date)
                            <div>
                                <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Received Date</dt>
                                <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->received_date->format('M d, Y') }}</dd>
                            </div>
                        @endif
                        <div>
                            <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Total Items</dt>
                            <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->items->count() }}</dd>
                        </div>
                    </dl>
                </div>
            </div>

            @if($purchaseOrder->notes)
                <div class="mt-6 pt-6 border-t border-zinc-200 dark:border-zinc-700">
                    <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 mb-2">Notes</dt>
                    <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->notes }}</dd>
                </div>
            @endif
        </div>

        <!-- Supplier Information -->
        <div class="bg-white dark:bg-zinc-800 rounded-lg border border-zinc-200 dark:border-zinc-700 p-6">
            <h3 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white mb-4">Supplier Information</h3>
            
            <dl class="space-y-3">
                <div>
                    <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Company</dt>
                    <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->supplier->company_name ?: $purchaseOrder->supplier->name }}</dd>
                </div>
                @if($purchaseOrder->supplier->email)
                    <div>
                        <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Email</dt>
                        <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->supplier->email }}</dd>
                    </div>
                @endif
                @if($purchaseOrder->supplier->phone)
                    <div>
                        <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Phone</dt>
                        <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->supplier->phone }}</dd>
                    </div>
                @endif
                @if($purchaseOrder->supplier->contact_person)
                    <div>
                        <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Contact Person</dt>
                        <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->supplier->contact_person }}</dd>
                    </div>
                @endif
                @if($purchaseOrder->supplier->payment_terms)
                    <div>
                        <dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Payment Terms</dt>
                        <dd class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ $purchaseOrder->supplier->payment_terms }}</dd>
                    </div>
                @endif
            </dl>
        </div>
    </div>

    <!-- Order Items -->
    <div class="bg-white dark:bg-zinc-800 rounded-lg border border-zinc-200 dark:border-zinc-700">
        <div class="p-6 border-b border-zinc-200 dark:border-zinc-700">
            <h3 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white">Order Items</h3>
            @if($receivingMode)
                <p class="mt-1 text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Enter the quantities actually received for each item</p>
            @endif
        </div>
        
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-zinc-200 dark:divide-zinc-700">
                <thead class="bg-zinc-50 dark:bg-zinc-900">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">Part</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">Part Number</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">Ordered Qty</th>
                        @if($receivingMode || $purchaseOrder->status === 'partial' || $purchaseOrder->status === 'received')
                            <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">
                                {{ $receivingMode ? 'Receive Qty' : 'Received Qty' }}
                            </th>
                        @endif
                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">Unit Cost</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">Total Cost</th>
                    </tr>
                </thead>
                <tbody class="bg-white dark:bg-zinc-800 divide-y divide-zinc-200 dark:divide-zinc-700">
                    @php $totalCost = 0; @endphp
                    @foreach($purchaseOrder->items as $item)
                        @php $totalCost += $item->total_cost; @endphp
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-zinc-900 dark:text-white dark:text-white">{{ $item->part->name }}</div>
                                <div class="text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Current Stock: {{ $item->part->quantity_on_hand }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                {{ $item->part->part_number }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                {{ number_format($item->quantity_ordered) }}
                            </td>
                            @if($receivingMode || $purchaseOrder->status === 'partial' || $purchaseOrder->status === 'received')
                                <td class="px-6 py-4 whitespace-nowrap">
                                    @if($receivingMode)
                                        <flux:input 
                                            wire:model="receivedQuantities.{{ $item->id }}" 
                                            type="number" 
                                            min="0" 
                                            max="{{ $item->quantity_ordered }}"
                                            class="w-20"
                                        />
                                    @else
                                        <span class="text-sm text-zinc-900 dark:text-white dark:text-white">{{ number_format($item->quantity_received ?? 0) }}</span>
                                        @if($item->quantity_received < $item->quantity_ordered)
                                            <span class="text-xs text-orange-600 dark:text-orange-400 block">
                                                ({{ $item->quantity_ordered - $item->quantity_received }} pending)
                                            </span>
                                        @endif
                                    @endif
                                </td>
                            @endif
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                ${{ number_format($item->unit_cost, 2) }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-zinc-900 dark:text-white dark:text-white">
                                ${{ number_format($item->total_cost, 2) }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot class="bg-zinc-50 dark:bg-zinc-900">
                    <tr>
                        <td colspan="{{ $receivingMode || $purchaseOrder->status === 'partial' || $purchaseOrder->status === 'received' ? '5' : '4' }}" class="px-6 py-4 text-right text-sm font-medium text-zinc-900 dark:text-white dark:text-white">
                            Total Amount:
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-zinc-900 dark:text-white dark:text-white">
                            ${{ number_format($totalCost, 2) }}
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
