<x-app-layout>
    <x-slot:heading>Invoices</x-slot:heading>

    <x-page-header title="Invoices" description="Fish sales invoices and payment tracking.">
        <x-slot:actions>
            <a href="{{ route('sales.create') }}"
               class="inline-flex items-center gap-2 rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700">
                + Create Invoice
            </a>
        </x-slot:actions>
    </x-page-header>

    @if($outstandingTotal > 0)
    <div class="mb-5 flex items-center justify-between rounded-xl border border-orange-200 bg-orange-50 px-5 py-4">
        <div>
            <p class="font-semibold text-orange-800">Outstanding Receivables</p>
            <p class="text-sm text-orange-700">Unpaid / partial invoices</p>
        </div>
        <p class="text-xl font-bold text-orange-800">₦{{ number_format($outstandingTotal) }}</p>
    </div>
    @endif

    {{-- Filters --}}
    <form method="GET" class="mb-4 flex flex-wrap gap-3">
        <select name="status" class="rounded-lg border-gray-200 text-sm shadow-sm focus:border-primary-500 focus:ring-primary-500">
            <option value="">All statuses</option>
            @foreach(['paid' => 'Paid', 'partial' => 'Partial', 'pending' => 'Pending'] as $val => $label)
                <option value="{{ $val }}" @selected(request('status') === $val)>{{ $label }}</option>
            @endforeach
        </select>
        <button type="submit" class="rounded-lg bg-gray-100 px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-200">
            Filter
        </button>
    </form>

    @if($sales->isEmpty())
        <x-empty-state title="No invoices yet">
            <x-slot:icon>
                <svg class="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z"/>
                </svg>
            </x-slot:icon>
        </x-empty-state>
    @else
    <div class="rounded-xl border border-gray-100 bg-white shadow-sm overflow-x-auto">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 text-xs uppercase tracking-wider text-gray-500">
                <tr>
                    <th class="px-5 py-3 text-left">Invoice #</th>
                    <th class="px-5 py-3 text-left">Date</th>
                    <th class="px-5 py-3 text-left">Customer</th>
                    <th class="px-5 py-3 text-left">Pond</th>
                    <th class="px-5 py-3 text-right">Qty (kg)</th>
                    <th class="px-5 py-3 text-right">Amount</th>
                    <th class="px-5 py-3 text-center">Status</th>
                    <th class="px-5 py-3 text-right">Balance</th>
                    <th class="px-5 py-3"></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-50">
                @foreach($sales as $sale)
                <tr class="hover:bg-gray-50 @if($sale->is_overdue) bg-red-50 @endif">
                    <td class="px-5 py-3">
                        <a href="{{ route('sales.show', $sale) }}"
                           class="font-mono text-xs font-semibold text-primary-700 hover:text-primary-900 hover:underline">
                            {{ $sale->invoice_number ?? "INV-{$sale->id}" }}
                        </a>
                    </td>
                    <td class="px-5 py-3 text-gray-600">{{ $sale->sale_date->format('d M Y') }}</td>
                    <td class="px-5 py-3">
                        @if($sale->customer_id)
                            <a href="{{ route('customers.show', $sale->customer_id) }}"
                               class="font-medium text-gray-900 hover:text-primary-700 hover:underline">
                                {{ $sale->buyer_name }}
                            </a>
                        @else
                            <span class="text-gray-900">{{ $sale->buyer_name }}</span>
                        @endif
                    </td>
                    <td class="px-5 py-3 text-gray-600">{{ $sale->harvest?->pond?->name ?? '—' }}</td>
                    <td class="px-5 py-3 text-right text-gray-600">{{ number_format((float) $sale->quantity_kg, 1) }}</td>
                    <td class="px-5 py-3 text-right font-semibold text-gray-900">₦{{ number_format((float) $sale->total_amount_naira) }}</td>
                    <td class="px-5 py-3 text-center">
                        <x-status-badge :status="$sale->payment_status" size="xs" />
                        @if($sale->is_overdue)
                            <span class="block text-xs text-red-600 mt-0.5">Overdue</span>
                        @endif
                    </td>
                    <td class="px-5 py-3 text-right @if($sale->balance_owed > 0) text-red-600 font-semibold @else text-gray-400 @endif">
                        {{ $sale->balance_owed > 0 ? '₦' . number_format((float) $sale->balance_owed) : '—' }}
                    </td>
                    <td class="px-5 py-3 text-right whitespace-nowrap">
                        <a href="{{ route('sales.show', $sale) }}"
                           class="text-xs font-medium text-gray-500 hover:text-primary-700">View</a>
                        @if(auth()->user()->isAdmin())
                            <span class="mx-1 text-gray-300">·</span>
                            <a href="{{ route('sales.edit', $sale) }}"
                               class="text-xs font-medium text-gray-500 hover:text-primary-700">Edit</a>
                        @endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    <div class="mt-4">{{ $sales->links() }}</div>
    @endif

</x-app-layout>
