<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">Edit Purchase Order #{{ $purchaseOrder->po_number }}</h1>
            <p class="mt-1 text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">Update purchase order details and items</p>
        </div>
        <flux:button wire:navigate href="{{ route('inventory.purchase-orders.show', $purchaseOrder) }}" variant="ghost">
            <flux:icon.arrow-left class="w-4 h-4" />
            Back to Order
        </flux:button>
    </div>

    <form wire:submit="save" class="space-y-6">
        <!-- Purchase Order Details -->
        <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">Order Details</h3>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Supplier -->
                <div>
                    <flux:field>
                        <flux:label>Supplier*</flux:label>
                        <select wire:model="supplier_id" class="block w-full rounded-md border-0 py-1.5 text-zinc-900 dark:text-white border border-zinc-200 dark:border-zinc-700 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 dark:bg-zinc-800 dark:text-white dark:ring-zinc-600">
                            <option value="">Select a supplier</option>
                            @foreach($suppliers as $supplier)
                                <option value="{{ $supplier->id }}">{{ $supplier->name }}</option>
                            @endforeach
                        </select>
                        <flux:error name="supplier_id" />
                    </flux:field>
                </div>

                <!-- Order Date -->
                <div>
                    <flux:field>
                        <flux:label>Order Date*</flux:label>
                        <flux:input wire:model="order_date" type="date" />
                        <flux:error name="order_date" />
                    </flux:field>
                </div>

                <!-- Expected Date -->
                <div>
                    <flux:field>
                        <flux:label>Expected Delivery Date</flux:label>
                        <flux:input wire:model="expected_date" type="date" />
                        <flux:error name="expected_date" />
                    </flux:field>
                </div>

                <!-- Status -->
                <div>
                    <flux:field>
                        <flux:label>Status*</flux:label>
                        <select wire:model="status" class="block w-full rounded-md border-0 py-1.5 text-zinc-900 dark:text-white border border-zinc-200 dark:border-zinc-700 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 dark:bg-zinc-800 dark:text-white dark:ring-zinc-600">
                            <option value="draft">Draft</option>
                            <option value="pending">Pending</option>
                            <option value="ordered">Ordered</option>
                        </select>
                        <flux:error name="status" />
                    </flux:field>
                </div>

                <!-- Notes -->
                <div class="md:col-span-2">
                    <flux:field>
                        <flux:label>Notes</flux:label>
                        <flux:textarea wire:model="notes" placeholder="Enter any additional notes for this purchase order" rows="3" />
                        <flux:error name="notes" />
                    </flux:field>
                </div>
            </div>
        </div>

        <!-- Add Items Section -->
        <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">Add Items</h3>
            
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-4">
                <!-- Part Selection -->
                <div>
                    <flux:field>
                        <flux:label>Part</flux:label>
                        <select wire:model.live="selectedPart" class="block w-full rounded-md border-0 py-1.5 text-zinc-900 dark:text-white border border-zinc-200 dark:border-zinc-700 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 dark:bg-zinc-800 dark:text-white dark:ring-zinc-600">
                            <option value="">Select a part</option>
                            @foreach($parts as $part)
                                <option value="{{ $part->id }}">{{ $part->name }} ({{ $part->part_number }})</option>
                            @endforeach
                        </select>
                    </flux:field>
                </div>

                <!-- Quantity -->
                <div>
                    <flux:field>
                        <flux:label>Quantity</flux:label>
                        <flux:input wire:model="quantity" type="number" min="1" placeholder="1" />
                    </flux:field>
                </div>

                <!-- Unit Cost -->
                <div>
                    <flux:field>
                        <flux:label>Unit Cost</flux:label>
                        <flux:input wire:model="unitCost" type="number" step="0.01" min="0" placeholder="0.00" />
                    </flux:field>
                </div>

                <!-- Add Button -->
                <div class="flex items-end">
                    <flux:button wire:click="addItem" type="button" variant="outline" class="w-full">
                        Add Item
                    </flux:button>
                </div>
            </div>
        </div>

        <!-- Items List -->
        @if(count($items) > 0)
            <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>
                </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">Quantity</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">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</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-zinc-500 dark:text-zinc-400 dark:text-gray-400 uppercase tracking-wider">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white dark:bg-zinc-800 divide-y divide-zinc-200 dark:divide-zinc-700">
                            @foreach($items as $index => $item)
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-zinc-900 dark:text-white dark:text-white">
                                        {{ $item['part_name'] }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                        {{ $item['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']) }}
                                    </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['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>
                                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                        <flux:button wire:click="removeItem({{ $index }})" size="sm" variant="danger">
                                            Remove
                                        </flux:button>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot class="bg-zinc-50 dark:bg-zinc-900">
                            <tr>
                                <td colspan="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($this->getTotalAmount(), 2) }}
                                </td>
                                <td></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        @endif

        <flux:error name="items" />

        <!-- Form Actions -->
        <div class="flex items-center justify-end space-x-4 pt-6 border-t border-zinc-200 dark:border-zinc-700">
            <flux:button wire:navigate href="{{ route('inventory.purchase-orders.show', $purchaseOrder) }}" variant="ghost">
                Cancel
            </flux:button>
            <flux:button type="submit" variant="primary" :disabled="count($items) === 0">
                Update Purchase Order
            </flux:button>
        </div>
    </form>
</div>
