<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Journal des Opérations - Association Sénégalaise de la Valls Icamp</title>
    <style>
        @page {
            margin: 30px;
        }

        body {
            font-family: 'Helvetica', sans-serif;
            color: #000;
            margin: 0;
            padding: 0;
        }

        .title-container {
            text-align: center;
            margin-bottom: 30px;
        }

        .main-title {
            font-size: 18px;
            font-weight: bold;
            text-transform: uppercase;
            text-decoration: underline;
        }

        .sub-title {
            font-size: 14px;
            margin-top: 5px;
            font-weight: bold;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            border: 1px solid #000;
            padding: 8px 10px;
            text-align: left;
        }

        th {
            background-color: #fff;
            font-weight: bold;
            text-transform: uppercase;
            font-size: 12px;
        }

        td {
            font-size: 11px;
        }

        .amount-cell {
            text-align: right;
            font-weight: bold;
        }

        .credit {
            color: #059669;
        }

        .debit {
            color: #dc2626;
        }

        .total-box {
            margin-top: 20px;
            float: right;
            width: 300px;
        }
    </style>
</head>

<body>
    <div class="title-container">
        <div class="main-title">Association Sénégalaise de la Valls Icamp</div>
        <div class="sub-title">Journal des Opérations Financières - {{ date('d/m/Y') }}</div>
    </div>

    <table>
        <thead>
            <tr>
                <th style="width: 15%;">Date</th>
                <th style="width: 10%;">Type</th>
                <th style="width: 25%;">Compte</th>
                <th style="width: 35%;">Description / Référence</th>
                <th style="width: 15%;">Montant</th>
            </tr>
        </thead>
        <tbody>
            @php 
                $totalCredit = 0;
                $totalDebit = 0;
            @endphp
            @foreach($transactions as $transaction)
                @php
                    if($transaction->type === 'credit') $totalCredit += $transaction->amount;
                    else $totalDebit += $transaction->amount;
                @endphp
                <tr>
                    <td>{{ $transaction->date->format('d/m/Y') }}</td>
                    <td>{{ $transaction->type === 'credit' ? 'Crédit' : 'Débit' }}</td>
                    <td>{{ $transaction->account->name }}</td>
                    <td>
                        {{ $transaction->description }}
                        @if($transaction->reference)
                            <br><small>Réf: {{ $transaction->reference }}</small>
                        @endif
                    </td>
                    <td class="amount-cell {{ $transaction->type === 'credit' ? 'credit' : 'debit' }}">
                        {{ $transaction->type === 'credit' ? '+' : '-' }} {{ \App\Models\Setting::format($transaction->amount) }}
                    </td>
                </tr>
            @endforeach
            
            {{-- Padding for empty rows --}}
            @if($transactions->count() < 15)
                @for($i = 0; $i < (15 - $transactions->count()); $i++)
                    <tr>
                        <td>&nbsp;</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                @endfor
            @endif
        </tbody>
    </table>

    <div class="total-box">
        <table>
            <tr>
                <th style="text-align: left;">Total Crédits (+)</th>
                <td class="amount-cell credit">{{ \App\Models\Setting::format($totalCredit) }}</td>
            </tr>
            <tr>
                <th style="text-align: left;">Total Débits (-)</th>
                <td class="amount-cell debit">{{ \App\Models\Setting::format($totalDebit) }}</td>
            </tr>
            <tr>
                <th style="text-align: left;">Solde Période</th>
                <td class="amount-cell {{ ($totalCredit - $totalDebit) >= 0 ? 'credit' : 'debit' }}">
                    {{ \App\Models\Setting::format($totalCredit - $totalDebit) }}
                </td>
            </tr>
        </table>
    </div>
</body>

</html>
