@extends('layouts.app')

@section('title', 'Invoices')

@section('content')
<div class="bg-gray-50 min-h-screen py-8">
    <div class="container mx-auto px-4">
        <div class="flex justify-between items-center mb-8">
            <div>
                <h1 class="text-3xl font-bold text-gray-900">Invoices</h1>
                <p class="text-gray-600">Manage customer invoices</p>
            </div>
        </div>

        <!-- Stats -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Total Invoices</p>
                <p class="text-3xl font-bold text-gray-900">{{ $stats['total'] }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Paid</p>
                <p class="text-3xl font-bold text-green-600">{{ $stats['paid'] }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Pending</p>
                <p class="text-3xl font-bold text-yellow-600">{{ $stats['pending'] }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6">
                <p class="text-sm text-gray-500">Overdue</p>
                <p class="text-3xl font-bold text-red-600">{{ $stats['overdue'] }}</p>
            </div>
        </div>

        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-sm p-6 mb-6">
            <form action="{{ route('admin.finance.invoices') }}" method="GET" class="flex flex-wrap gap-4">
                <div class="flex-1 min-w-48">
                    <input type="text" name="search" value="{{ request('search') }}" 
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
                        placeholder="Search invoice # or customer...">
                </div>
                <div>
                    <select name="status" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">All Status</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>Draft</option>
                        <option value="sent" {{ request('status') == 'sent' ? 'selected' : '' }}>Sent</option>
                        <option value="paid" {{ request('status') == 'paid' ? 'selected' : '' }}>Paid</option>
                        <option value="overdue" {{ request('status') == 'overdue' ? 'selected' : '' }}>Overdue</option>
                    </select>
                </div>
                <button type="submit" class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                    Filter
                </button>
            </form>
        </div>

        @if(session('success'))
            <div class="bg-green-50 text-green-600 px-4 py-3 rounded-lg mb-6">{{ session('success') }}</div>
        @endif

        <!-- Invoices Table -->
        <div class="bg-white rounded-xl shadow-sm overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Invoice #</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Customer</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Issue Date</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Due Date</th>
                            <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Amount</th>
                            <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Balance</th>
                            <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Status</th>
                            <th class="px-6 py-4 text-right text-sm font-semibold text-gray-900">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($invoices as $invoice)
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4">
                                    <a href="{{ route('admin.finance.invoices.show', $invoice->id) }}" class="font-medium text-blue-600 hover:underline">
                                        {{ $invoice->invoice_number }}
                                    </a>
                                </td>
                                <td class="px-6 py-4">
                                    <p class="font-medium">{{ $invoice->user->name }}</p>
                                    <p class="text-sm text-gray-500">{{ $invoice->company?->company_name }}</p>
                                </td>
                                <td class="px-6 py-4 text-gray-600">{{ $invoice->issue_date->format('d M Y') }}</td>
                                <td class="px-6 py-4 text-gray-600">{{ $invoice->due_date->format('d M Y') }}</td>
                                <td class="px-6 py-4 text-right font-semibold">R {{ number_format($invoice->total_amount, 2) }}</td>
                                <td class="px-6 py-4 text-right font-semibold {{ $invoice->balance_due > 0 ? 'text-red-600' : 'text-green-600' }}">
                                    R {{ number_format($invoice->balance_due, 2) }}
                                </td>
                                <td class="px-6 py-4 text-center">
                                    @php
                                        $statusColors = [
                                            'draft' => 'bg-gray-100 text-gray-800',
                                            'sent' => 'bg-blue-100 text-blue-800',
                                            'paid' => 'bg-green-100 text-green-800',
                                            'partially_paid' => 'bg-yellow-100 text-yellow-800',
                                            'overdue' => 'bg-red-100 text-red-800',
                                        ];
                                    @endphp
                                    <span class="inline-flex px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$invoice->status] ?? 'bg-gray-100 text-gray-800' }}">
                                        {{ ucfirst(str_replace('_', ' ', $invoice->status)) }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-right">
                                    <div class="flex justify-end gap-2">
                                        <a href="{{ route('admin.finance.invoices.show', $invoice->id) }}" class="text-blue-600 hover:text-blue-800">View</a>
                                        @if($invoice->status === 'draft')
                                            <form action="{{ route('admin.finance.invoices.send', $invoice->id) }}" method="POST" class="inline">
                                                @csrf
                                                <button type="submit" class="text-green-600 hover:text-green-800">Send</button>
                                            </form>
                                        @endif
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="px-6 py-12 text-center text-gray-500">No invoices found</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="px-6 py-4 border-t">
                {{ $invoices->links() }}
            </div>
        </div>
    </div>
</div>
@endsection
