@extends('customer.layouts.app')

@section('title', 'My Invoices')

@section('page-title', 'My Invoices')

@section('content')
<div class="max-w-7xl mx-auto">
    <!-- Filters -->
    <div class="bg-white dark:bg-[#1e2630] rounded-xl shadow-sm border border-[#e5e7eb] dark:border-gray-700 p-6 mb-6">
        <form method="GET" action="{{ route('customer.invoices.index') }}" class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label class="block text-sm font-medium text-[#111418] dark:text-white mb-2">Status</label>
                <select name="status" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white dark:bg-[#111418] px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary dark:text-white">
                    <option value="">All Statuses</option>
                    <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>Draft</option>
                    <option value="sent" {{ request('status') == 'sent' ? 'selected' : '' }}>Sent</option>
                    <option value="viewed" {{ request('status') == 'viewed' ? 'selected' : '' }}>Viewed</option>
                    <option value="partial" {{ request('status') == 'partial' ? 'selected' : '' }}>Partial</option>
                    <option value="paid" {{ request('status') == 'paid' ? 'selected' : '' }}>Paid</option>
                    <option value="overdue" {{ request('status') == 'overdue' ? 'selected' : '' }}>Overdue</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] dark:text-white mb-2">From Date</label>
                <input type="date" name="date_from" value="{{ request('date_from') }}" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white dark:bg-[#111418] px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary dark:text-white">
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] dark:text-white mb-2">To Date</label>
                <input type="date" name="date_to" value="{{ request('date_to') }}" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white dark:bg-[#111418] px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary dark:text-white">
            </div>
            <div class="md:col-span-3 flex gap-2">
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors">Filter</button>
                <a href="{{ route('customer.invoices.index') }}" class="px-4 py-2 border border-[#dbe0e6] text-[#617589] rounded-lg hover:bg-background-light transition-colors">Clear</a>
            </div>
        </form>
    </div>

    <!-- Invoices Table -->
    <div class="bg-white dark:bg-[#1e2630] rounded-xl shadow-sm border border-[#e5e7eb] dark:border-gray-700 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead class="bg-background-light dark:bg-[#111418] border-b border-[#e5e7eb]">
                    <tr>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Invoice #</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Issue Date</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Due Date</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Total</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Balance</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Status</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#e5e7eb]">
                    @forelse($invoices as $invoice)
                        <tr class="hover:bg-background-light/50 transition-colors">
                            <td class="py-4 px-4">
                                <a href="{{ route('customer.invoices.show', $invoice->id) }}" class="text-primary hover:underline font-medium">
                                    {{ $invoice->invoice_number }}
                                </a>
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $invoice->issue_date->format('M d, Y') }}
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $invoice->due_date->format('M d, Y') }}
                            </td>
                            <td class="py-4 px-4 text-sm font-medium text-[#111418] dark:text-white text-right">
                                {{ $invoice->currency }} {{ number_format($invoice->total, 2) }}
                            </td>
                            <td class="py-4 px-4 text-sm font-medium text-right">
                                <span class="{{ $invoice->balance > 0 ? 'text-red-600' : 'text-green-600' }}">
                                    {{ $invoice->currency }} {{ number_format($invoice->balance, 2) }}
                                </span>
                            </td>
                            <td class="py-4 px-4">
                                @php
                                    $statusColors = [
                                        'draft' => 'bg-gray-100 text-gray-700',
                                        'sent' => 'bg-blue-100 text-blue-700',
                                        'viewed' => 'bg-purple-100 text-purple-700',
                                        'partial' => 'bg-yellow-100 text-yellow-700',
                                        'paid' => 'bg-green-100 text-green-700',
                                        'overdue' => 'bg-red-100 text-red-700',
                                    ];
                                    $status = $invoice->isOverdue() ? 'overdue' : $invoice->status;
                                @endphp
                                <span class="px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$status] ?? 'bg-gray-100 text-gray-700' }}">
                                    {{ ucfirst($status) }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('customer.invoices.show', $invoice->id) }}" class="text-primary hover:underline text-sm">View</a>
                                    <a href="{{ route('customer.invoices.download', $invoice->id) }}" target="_blank" rel="noopener noreferrer" class="text-primary hover:underline text-sm">Download</a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="py-8 px-4 text-center text-[#617589]">No invoices found</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($invoices->hasPages())
            <div class="p-4 border-t border-[#e5e7eb]">
                {{ $invoices->links() }}
            </div>
        @endif
    </div>
</div>
@endsection



