<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Outstanding Fees Report</title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 12px; margin: 20px; }
        .header { text-align: center; margin-bottom: 20px; }
        .info p { margin: 5px 0; }
        table { width: 100%; border-collapse: collapse; margin-top: 16px; }
        th, td { border: 1px solid #000; padding: 6px; text-align: left; }
        th { background: #f2f2f2; }
        .text-right { text-align: right; }
        .total-row { font-weight: bold; background: #e9ecef; }
    </style>
</head>
<body>
    @include('modules.reports.partials.pdf-company-details')
    <div class="header">
        <h2>Outstanding Fees Report</h2>
    </div>

    <div class="info">
        <p><strong>Date:</strong> {{ date('F j, Y') }}</p>
        <p><strong>Period:</strong> {{ $dateFrom }} to {{ $dateTo }}</p>
    </div>

    <p><strong>Total Outstanding:</strong> {{ number_format($metrics['totalOutstanding'], 2) }}</p>
    <p><strong>Overdue Amount:</strong> {{ number_format($metrics['overdueAmount'], 2) }}</p>
    <p><strong>Total Invoices:</strong> {{ $metrics['totalInvoices'] }}</p>
    <p><strong>Average Days Overdue:</strong> {{ $metrics['avgDaysOverdue'] }}</p>

    <table>
        <thead>
            <tr>
                <th>Client Name</th>
                <th>Invoice #</th>
                <th>Invoice Date</th>
                <th>Due Date</th>
                <th class="text-right">Total Amount</th>
                <th class="text-right">Paid Amount</th>
                <th class="text-right">Balance Due</th>
                <th>Status</th>
                <th>Days Overdue</th>
                <th>Currency</th>
            </tr>
        </thead>
        <tbody>
            @forelse($invoices as $invoice)
                <tr>
                    <td>{{ $invoice->customer->full_name ?? 'N/A' }}</td>
                    <td>{{ $invoice->invoice_number }}</td>
                    <td>{{ optional($invoice->invoice_date)->format('Y-m-d') }}</td>
                    <td>{{ optional($invoice->due_date_computed)->format('Y-m-d') }}</td>
                    <td class="text-right">{{ number_format((float) $invoice->total, 2) }}</td>
                    <td class="text-right">{{ number_format((float) ($invoice->paid_amount ?? 0), 2) }}</td>
                    <td class="text-right">{{ number_format((float) ($invoice->balance_due ?? 0), 2) }}</td>
                    <td>{{ ucfirst(str_replace('_', ' ', $invoice->status_label ?? 'unpaid')) }}</td>
                    <td>{{ (int) ($invoice->days_overdue ?? 0) }}</td>
                    <td>{{ $invoice->currency->currency_code ?? 'N/A' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="10" style="text-align:center;">No outstanding fees found.</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</body>
</html>

