<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <title>Reporte de Cuentas por Cobrar</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        table {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 20px;
        }

        table,
        th,
        td {
            border: 1px solid black;
        }

        th,
        td {
            padding: 8px;
            text-align: center;
        }

        th {
            background-color: #69ABC3;
            color: white;
        }

        .summary th {
            background-color: #69ABC3;
            color: white;
        }

        .summary td {
            font-weight: bold;
        }

        .expired-row {
            background-color: #FFDAB9;
        }
    </style>
</head>

<body>
    <h2 style="text-align: center;">{{ $data['supplierName'] . ' - ' . $data['rut'] }}</h2>

    <table class="summary">
        <tr>
            <th></th>
            <th>Vencidas</th>
            <th>No vencidas</th>
            <th>Total</th>
        </tr>
        <tr>
            <td>N° Facturas</td>
            <td>{{ $data['summary']['accounts_receivable_expired'] ?? 0 }}</td>
            <td>{{ $data['summary']['accounts_receivable_not_expired'] ?? 0 }}</td>
            <td>{{ $data['summary']['accounts_receivable'] ?? 0 }}</td>
        </tr>
        <tr>
            <td>Monto</td>
            <td>${{ number_format($data['summary']['amount_receivable_expired'] ?? 0, 0, ',', '.') }}</td>
            <td>${{ number_format($data['summary']['amount_receivable_not_expired'] ?? 0, 0, ',', '.') }}</td>
            <td>${{ number_format($data['summary']['amount_receivable'] ?? 0, 0, ',', '.') }}</td>
        </tr>
    </table>

    <table>
        <tr>
            <th>Folio</th>
            <th>Forma de Pago</th>
            <th>Emisión</th>
            <th>Vencimiento</th>
            <th>Total Factura</th>
            <th>Monto Pagado</th>
            <th>Saldo Pendiente</th>
        </tr>
        @foreach ($data['accounts'] as $account)
            <tr class="{{ \Carbon\Carbon::parse($account['deadline'])->isPast() ? 'expired-row' : '' }}">
                <td>{{ $account['ticket']['folio'] ?? '-' }}</td>
                <td>{{ $account['ticket']['wayToPay']['name'] ?? '-' }}</td>
                <td>{{ \Carbon\Carbon::parse($account['ticket']['created_at'])->format('d/m/Y H:i') }}</td>
                <td>{{ \Carbon\Carbon::parse($account['deadline'])->format('d/m/Y') ?? 'N/A' }}</td>
                <td>${{ number_format($account['total'], 0, ',', '.') }}</td>
                <td>${{ number_format($account['amount_paid'] ?? 0, 0, ',', '.') }}</td>
                <td>${{ number_format($account['debt'], 0, ',', '.') }}</td>
            </tr>
        @endforeach

        <tr>
            <td colspan="4" style="font-weight: bold; background-color: #69ABC3; color: white;">Total</td>
            <td style="font-weight: bold;">${{ number_format($data['totals']['totalFactura'], 0, ',', '.') }}</td>
            <td style="font-weight: bold;">${{ number_format($data['totals']['montoPagado'], 0, ',', '.') }}</td>
            <td style="font-weight: bold;">${{ number_format($data['totals']['saldoPendiente'], 0, ',', '.') }}</td>
        </tr>
    </table>

</body>

</html>
