<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Relevé de compte - {{ $client->nom }}</title>
    <style>
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 11px;
            color: #333;
        }

        .header-right {
            text-align: right;
            margin-bottom: 20px;
        }

        .company-name {
            font-weight: bold;
            font-size: 14px;
        }

        .company-address {
            font-size: 10px;
            color: #555;
        }

        .box-title {
            background: #2563eb;
            color: white;
            padding: 8px 12px;
            font-weight: bold;
            margin-bottom: 4px;
        }

        .box-period {
            font-size: 10px;
            margin-bottom: 20px;
        }

        .client-box {
            background: #e0e7ff;
            padding: 10px 12px;
            margin-bottom: 20px;
            width: 280px;
        }

        .client-label {
            font-weight: bold;
            margin-bottom: 6px;
        }

        .client-name {
            font-weight: bold;
        }

        .client-detail {
            margin: 2px 0;
        }

        .summary {
            margin: 20px 0;
        }

        .summary-row {
            padding: 4px 0;
        }

        .summary-label {
            display: inline-block;
            width: 160px;
        }

        .summary-value {
            font-weight: bold;
        }

        .solde-due {
            font-size: 13px;
            font-weight: bold;
            margin-top: 6px;
        }

        .intro-text {
            font-weight: bold;
            margin: 16px 0 10px;
            font-size: 11px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        th {
            background: #2563eb;
            color: white;
            padding: 8px 6px;
            text-align: left;
            font-size: 9px;
        }

        td {
            padding: 6px;
            border-bottom: 1px solid #e2e8f0;
            font-size: 9px;
        }

        tr:nth-child(even) {
            background: #f8fafc;
        }

        .text-right {
            text-align: right;
        }

        .footer {
            margin-top: 24px;
            font-size: 9px;
            color: #64748b;
        }

    </style>
</head>
<body>

    <div class="header-right">
        <div class="company-name">{{ config('app.name') }}</div>
        <div class="company-address">{{ config('app.company_address', 'Côte d\'Ivoire') }}</div>
    </div>

    <div class="box-title">Relevé de compte</div>
    <div class="box-period">{{ $dateDebut->format('d-m-Y') }} au {{ $dateFin->format('d-m-Y') }}</div>

    <div class="client-box">
        <div class="client-label">À :</div>
        <div class="client-name">{{ $client->nom }}</div>
        @if($client->adresse)
        <div class="client-detail">{{ $client->adresse }}</div>
        @endif
        <div class="client-detail">Tél. {{ $client->telephone }}</div>
        @if(!empty($cautionStatutsResume))
        <div class="client-detail" style="margin-top: 6px;">
            <strong>Statut caution :</strong> {{ $cautionStatutsResume }}
        </div>
        @endif
    </div>

    <div class="summary">
        <div class="summary-row"><span class="summary-label">Solde d'ouverture :</span> <span class="summary-value">{{ number_format($soldeOuverture, 0, ',', ' ') }} FCFA</span></div>
        <div class="summary-row"><span class="summary-label">Total des factures :</span> <span class="summary-value">{{ number_format($totalFactures, 0, ',', ' ') }} FCFA</span></div>
        <div class="summary-row"><span class="summary-label">Total payé :</span> <span class="summary-value">{{ number_format($totalPaye, 0, ',', ' ') }} FCFA</span></div>
        <div class="summary-row"><span class="summary-label">Solde anticipé :</span> <span class="summary-value">{{ number_format($soldeAnticipe ?? 0, 0, ',', ' ') }} FCFA</span></div>
        <div class="summary-row solde-due"><span class="summary-label">Solde dû :</span> <span class="summary-value">{{ number_format($soldeDu, 0, ',', ' ') }} FCFA</span></div>
    </div>

    <p class="intro-text">Détail des factures et des paiements entre le {{ $dateDebut->format('d-m-Y') }} et le {{ $dateFin->format('d-m-Y') }}.</p>

    <p class="intro-note" style="font-size: 9px; color: #64748b; margin-bottom: 8px;">
        <strong>Calcul du solde :</strong> à chaque ligne, le solde = solde de la ligne précédente + débit − crédit.
        Une facture (débit) augmente ce que le client doit ; un paiement (crédit) le diminue.
    </p>

    <table>
        <thead>
            <tr>
                <th>Date</th>
                <th>Référence</th>
                <th>Type</th>
                <th class="text-right">Débit</th>
                <th class="text-right">Crédit</th>
                <th class="text-right">Solde (dû)</th>
                <th>Mode de paiement</th>
                <th>Remarques</th>
            </tr>
        </thead>
        <tbody>
            @foreach($transactions as $t)
            <tr>
                <td>{{ $t['date'] instanceof \Carbon\Carbon ? $t['date']->format('d-m-Y H:i') : \Carbon\Carbon::parse($t['date'])->format('d-m-Y H:i') }}</td>
                <td>{{ $t['reference'] ?? '—' }}</td>
                <td>{{ $t['type'] }}</td>
                <td class="text-right">{{ $t['debit'] > 0 ? number_format($t['debit'], 0, ',', ' ') . ' FCFA' : '—' }}</td>
                <td class="text-right">{{ $t['credit'] > 0 ? number_format($t['credit'], 0, ',', ' ') . ' FCFA' : '—' }}</td>
                <td class="text-right">{{ number_format($t['solde'], 0, ',', ' ') }} {{ $t['solde'] > 0 ? 'DR' : '' }}</td>
                <td>{{ $t['mode_paiement'] ?? '—' }}</td>
                <td>{{ $t['remarques'] ?? '—' }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    @if(empty($transactions))
    <p style="margin-top: 12px;">Aucune facture ni paiement sur la période.</p>
    @endif

    <div class="footer">
        Document généré le {{ now()->format('d/m/Y à H:i') }} — {{ config('app.name') }}
    </div>

</body>
</html>
