<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{ $meta['title'] ?? 'Aging Report' }}</title>
    <style>
        body { font-family: 'Helvetica', Arial, sans-serif; font-size: 12px; color: #333; margin: 40px; }
        table.data { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        table.data th { background-color: #1d4ed8; color: white; padding: 8px 12px; text-align: left; font-size: 11px; text-transform: uppercase; }
        table.data th.text-right { text-align: right; }
        table.data th.text-center { text-align: center; }
        table.data td { padding: 8px 12px; border-bottom: 1px solid #eee; }
        table.data tr:nth-child(even) { background-color: #f8fafc; }
        .section-title { font-size: 14px; font-weight: bold; color: #1d4ed8; margin: 20px 0 10px; border-bottom: 1px solid #e2e8f0; padding-bottom: 5px; }
        .summary-grid { width: 100%; margin-bottom: 20px; }
        .summary-grid td { width: 25%; text-align: center; padding: 12px; }
        .summary-box { background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 4px; padding: 12px; }
        .summary-value { font-size: 18px; font-weight: bold; }
        .summary-label { font-size: 10px; color: #64748b; text-transform: uppercase; margin-top: 4px; }
        .color-green { color: #16a34a; }
        .color-yellow { color: #ca8a04; }
        .color-orange { color: #ea580c; }
        .color-red { color: #dc2626; }
        .total-row td { font-weight: bold; border-top: 2px solid #1d4ed8; }
        .footer { margin-top: 40px; text-align: center; font-size: 10px; color: #999; border-top: 1px solid #eee; padding-top: 15px; }
    </style>
</head>
<body>
    @include('reports._header')

    <div class="section-title">Aging Summary</div>
    <table class="data">
        <thead>
            <tr>
                <th>Age Bracket</th>
                <th class="text-center" style="text-align: center;">Count</th>
                <th class="text-right" style="text-align: right;">Amount</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="color-green">0 - 30 Days</td>
                <td style="text-align: center;">{{ $data['current']['count'] ?? 0 }}</td>
                <td style="text-align: right;">${{ number_format($data['current']['amount'] ?? 0, 2) }}</td>
            </tr>
            <tr>
                <td class="color-yellow">31 - 60 Days</td>
                <td style="text-align: center;">{{ $data['days_31_60']['count'] ?? 0 }}</td>
                <td style="text-align: right;">${{ number_format($data['days_31_60']['amount'] ?? 0, 2) }}</td>
            </tr>
            <tr>
                <td class="color-orange">61 - 90 Days</td>
                <td style="text-align: center;">{{ $data['days_61_90']['count'] ?? 0 }}</td>
                <td style="text-align: right;">${{ number_format($data['days_61_90']['amount'] ?? 0, 2) }}</td>
            </tr>
            <tr>
                <td class="color-red">90+ Days</td>
                <td style="text-align: center;">{{ $data['days_90_plus']['count'] ?? 0 }}</td>
                <td style="text-align: right;">${{ number_format($data['days_90_plus']['amount'] ?? 0, 2) }}</td>
            </tr>
            <tr class="total-row">
                <td>Total</td>
                <td style="text-align: center;">{{ $data['total']['count'] ?? 0 }}</td>
                <td style="text-align: right;">${{ number_format($data['total']['amount'] ?? 0, 2) }}</td>
            </tr>
        </tbody>
    </table>

    @if(!empty($data['invoices']))
        <div class="section-title">Outstanding Invoices</div>
        <table class="data">
            <thead>
                <tr>
                    <th>Invoice #</th>
                    <th>Customer</th>
                    <th class="text-right" style="text-align: right;">Amount</th>
                    <th>Due Date</th>
                    <th class="text-center" style="text-align: center;">Days Overdue</th>
                </tr>
            </thead>
            <tbody>
                @foreach($data['invoices'] as $inv)
                    <tr>
                        <td>{{ $inv['invoice_number'] }}</td>
                        <td>{{ $inv['customer'] }}</td>
                        <td style="text-align: right;">${{ number_format($inv['amount'], 2) }}</td>
                        <td>{{ $inv['due_date'] }}</td>
                        <td style="text-align: center;">
                            <span style="color: {{ $inv['days_overdue'] > 90 ? '#dc2626' : ($inv['days_overdue'] > 60 ? '#ea580c' : ($inv['days_overdue'] > 30 ? '#ca8a04' : '#16a34a')) }}">
                                {{ $inv['days_overdue'] }}
                            </span>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    <div class="footer">
        <p>EZSCALE &mdash; Cloud Infrastructure Services &mdash; support@ezscale.cloud</p>
    </div>
</body>
</html>
