<div class="space-y-6">
    <!-- Enhanced Header -->
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-3xl font-bold text-zinc-900 dark:text-white dark:text-white">Purchase Orders</h1>
            <p class="mt-2 text-lg text-zinc-600 dark:text-zinc-400 dark:text-gray-300">
                Manage {{ number_format($orders->total()) }} purchase orders and track incoming inventory
            </p>
        </div>
        <div class="flex flex-col sm:flex-row gap-3">
            <flux:button wire:navigate href="{{ route('inventory.suppliers.index') }}" variant="outline" icon="building-office" size="sm">
                Manage Suppliers
            </flux:button>
            <flux:button wire:navigate href="{{ route('inventory.purchase-orders.create') }}" variant="primary" icon="plus" size="sm">
                New Purchase Order
            </flux:button>
        </div>
    </div>

    <!-- Status Overview Cards -->
    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4">
        <div class="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4 border border-blue-200 dark:border-blue-700">
            <div class="text-center">
                <div class="text-2xl font-bold text-blue-600 dark:text-blue-400">{{ $statusCounts['draft'] ?? 0 }}</div>
                <div class="text-xs text-blue-600 dark:text-blue-400 font-medium">Draft</div>
            </div>
        </div>
        <div class="bg-orange-50 dark:bg-orange-900/20 rounded-lg p-4 border border-orange-200 dark:border-orange-700">
            <div class="text-center">
                <div class="text-2xl font-bold text-orange-600 dark:text-orange-400">{{ $statusCounts['pending'] ?? 0 }}</div>
                <div class="text-xs text-orange-600 dark:text-orange-400 font-medium">Pending</div>
            </div>
        </div>
        <div class="bg-purple-50 dark:bg-purple-900/20 rounded-lg p-4 border border-purple-200 dark:border-purple-700">
            <div class="text-center">
                <div class="text-2xl font-bold text-purple-600 dark:text-purple-400">{{ $statusCounts['ordered'] ?? 0 }}</div>
                <div class="text-xs text-purple-600 dark:text-purple-400 font-medium">Ordered</div>
            </div>
        </div>
        <div class="bg-yellow-50 dark:bg-yellow-900/20 rounded-lg p-4 border border-yellow-200 dark:border-yellow-700">
            <div class="text-center">
                <div class="text-2xl font-bold text-yellow-600 dark:text-yellow-400">{{ $statusCounts['partial'] ?? 0 }}</div>
                <div class="text-xs text-yellow-600 dark:text-yellow-400 font-medium">Partial</div>
            </div>
        </div>
        <div class="bg-green-50 dark:bg-green-900/20 rounded-lg p-4 border border-green-200 dark:border-green-700">
            <div class="text-center">
                <div class="text-2xl font-bold text-green-600 dark:text-green-400">{{ $statusCounts['received'] ?? 0 }}</div>
                <div class="text-xs text-green-600 dark:text-green-400 font-medium">Received</div>
            </div>
        </div>
        <div class="bg-red-50 dark:bg-red-900/20 rounded-lg p-4 border border-red-200 dark:border-red-700">
            <div class="text-center">
                <div class="text-2xl font-bold text-red-600 dark:text-red-400">{{ $statusCounts['cancelled'] ?? 0 }}</div>
                <div class="text-xs text-red-600 dark:text-red-400 font-medium">Cancelled</div>
            </div>
        </div>
    </div>

    <!-- Enhanced Filters -->
    <div class="bg-white dark:bg-zinc-800 rounded-lg border border-zinc-200 dark:border-zinc-700 border border-zinc-200 dark:border-zinc-700">
        <div class="p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-zinc-900 dark:text-white dark:text-white">Filter Orders</h3>
                <flux:button wire:click="clearFilters" variant="outline" size="xs">
                    Clear All
                </flux:button>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <!-- Enhanced Search -->
                <div>
                    <flux:field>
                        <flux:label>Search Orders</flux:label>
                        <flux:input
                            wire:model.live.debounce.300ms="search"
                            placeholder="Order number or supplier..."
                            icon="magnifying-glass"
                        />
                    </flux:field>
                </div>

                <!-- Status Filter -->
                <div>
                    <flux:field>
                        <flux:label>Order Status</flux:label>
                        <flux:select wire:model.live="statusFilter">
                            <option value="">All Statuses</option>
                            <option value="draft">📝 Draft</option>
                            <option value="pending">⏳ Pending</option>
                            <option value="ordered">📦 Ordered</option>
                            <option value="partial">📋 Partially Received</option>
                            <option value="received">✅ Received</option>
                            <option value="cancelled">❌ Cancelled</option>
                        </flux:select>
                    </flux:field>
                </div>

                <!-- Supplier Filter -->
                <div>
                    <flux:field>
                        <flux:label>Supplier</flux:label>
                        <flux:select wire:model.live="supplierFilter">
                            <option value="">All Suppliers</option>
                            @foreach($suppliers as $supplier)
                                <option value="{{ $supplier->id }}">{{ $supplier->name }}</option>
                            @endforeach
                        </flux:select>
                    </flux:field>
                </div>

                <!-- Date Range -->
                <div>
                    <flux:field>
                        <flux:label>Order Date</flux:label>
                        <flux:input wire:model.live="dateFilter" type="date" />
                    </flux:field>
                </div>
            </div>

            <!-- Active Filters Display -->
            <div class="mt-4 flex flex-wrap gap-2">
                @if($search)
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200">
                        Search: "{{ $search }}"
                        <button wire:click="$set('search', '')" class="ml-2 text-blue-600 hover:text-blue-800">×</button>
                    </span>
                @endif
                @if($statusFilter)
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200">
                        Status: {{ ucfirst($statusFilter) }}
                        <button wire:click="$set('statusFilter', '')" class="ml-2 text-green-600 hover:text-green-800">×</button>
                    </span>
                @endif
                @if($supplierFilter)
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200">
                        Supplier: {{ $suppliers->firstWhere('id', $supplierFilter)?->name ?? 'Unknown' }}
                        <button wire:click="$set('supplierFilter', '')" class="ml-2 text-purple-600 hover:text-purple-800">×</button>
                    </span>
                @endif
                @if($dateFilter)
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200">
                        Date: {{ $dateFilter }}
                        <button wire:click="$set('dateFilter', '')" class="ml-2 text-orange-600 hover:text-orange-800">×</button>
                    </span>
                @endif
            </div>
        </div>
    </div>

    <!-- Results Summary -->
    <div class="flex items-center justify-between text-sm text-zinc-600 dark:text-zinc-400 dark:text-gray-400">
        <div>
            Showing {{ $orders->firstItem() ?? 0 }} to {{ $orders->lastItem() ?? 0 }} of {{ number_format($orders->total()) }} orders
        </div>
        <div class="flex items-center space-x-2">
            <span>Per page:</span>
            <flux:select wire:model.live="perPage" class="w-20">
                <option value="10">10</option>
                <option value="25">25</option>
                <option value="50">50</option>
            </flux:select>
        </div>
    </div>
                    Clear Filters
                </flux:button>
            </div>
        </div>
    </div>

    <!-- Purchase Orders Table -->
    <div class="bg-white dark:bg-zinc-800 rounded-lg border border-zinc-200 dark:border-zinc-700 overflow-hidden">
        <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 wire:click="sortBy('po_number')" 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 cursor-pointer hover:bg-zinc-100 dark:hover:bg-zinc-800">
                            <div class="flex items-center space-x-1">
                                <span>Order Number</span>
                                @if($sortBy === 'po_number')
                                    @if($sortDirection === 'asc')
                                        <flux:icon.chevron-up class="w-4 h-4" />
                                    @else
                                        <flux:icon.chevron-down class="w-4 h-4" />
                                    @endif
                                @endif
                            </div>
                        </th>
                        <th wire:click="sortBy('order_date')" 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 cursor-pointer hover:bg-zinc-100 dark:hover:bg-zinc-800">
                            <div class="flex items-center space-x-1">
                                <span>Order Date</span>
                                @if($sortBy === 'order_date')
                                    @if($sortDirection === 'asc')
                                        <flux:icon.chevron-up class="w-4 h-4" />
                                    @else
                                        <flux:icon.chevron-down class="w-4 h-4" />
                                    @endif
                                @endif
                            </div>
                        </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">
                            Supplier
                        </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">
                            Items
                        </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 Amount
                        </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">
                            Status
                        </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">
                            Expected Date
                        </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">
                    @forelse($purchaseOrders as $order)
                        <tr class="hover:bg-zinc-50 dark:hover:bg-zinc-700">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="font-medium text-zinc-900 dark:text-white dark:text-white">{{ $order->po_number }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                {{ $order->order_date->format('M d, Y') }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-zinc-900 dark:text-white dark:text-white">{{ $order->supplier->name }}</div>
                                <div class="text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">{{ $order->supplier->email }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                {{ $order->items_count }} {{ Str::plural('item', $order->items_count) }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-zinc-900 dark:text-white dark:text-white">
                                ${{ number_format($order->total_amount ?? 0, 2) }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                @php
                                    $statusColors = [
                                        'draft' => 'gray',
                                        'pending' => 'yellow',
                                        'ordered' => 'blue',
                                        'partial' => 'orange',
                                        'received' => 'green',
                                        'cancelled' => 'red'
                                    ];
                                @endphp
                                <flux:badge size="sm" color="{{ $statusColors[$order->status] ?? 'gray' }}">
                                    {{ ucfirst($order->status) }}
                                </flux:badge>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                {{ $order->expected_date ? $order->expected_date->format('M d, Y') : 'Not set' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium space-x-2">
                                <flux:button wire:navigate href="{{ route('inventory.purchase-orders.show', $order) }}" size="sm" variant="subtle">
                                    View
                                </flux:button>
                                @if(in_array($order->status, ['draft', 'pending']))
                                    <flux:button wire:navigate href="{{ route('inventory.purchase-orders.edit', $order) }}" size="sm" variant="subtle">
                                        Edit
                                    </flux:button>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="px-6 py-12 text-center">
                                <div class="text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                    <flux:icon.shopping-cart class="mx-auto h-12 w-12 mb-4 opacity-40" />
                                    <p class="text-lg font-medium">No purchase orders found</p>
                                    <p class="text-sm">Get started by creating your first purchase order.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        @if($purchaseOrders->hasPages())
            <div class="bg-zinc-50 dark:bg-zinc-900 px-6 py-3 border-t border-zinc-200 dark:border-zinc-700">
                {{ $purchaseOrders->links() }}
            </div>
        @endif
    </div>
</div>
