<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        {{ $isLatePayment ? 'Dettes et Retards - Association Sénégalaise de la Valls Icamp' : 'Liste des Cotisations - 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;
            color: #000;
        }

        td {
            font-size: 12px;
            vertical-align: top;
            color: #000;
            font-weight: bold;
        }

        .item-bold {
            font-weight: bold;
        }

        .total-row {
            font-weight: bold;
            text-align: right;
            border-top: 2px solid #000;
            background-color: #e0e0e0;
        }

        .project-details {
            font-size: 12px;
            color: #000;
            margin-top: 3px;
        }
    </style>
</head>

<body>
    <div
        style="position: fixed; bottom: 0px; right: 0px; text-align: right; font-size: 12px; color: #000; font-weight: bold;">
        Date de génération : {{ date('d/m/Y') }}
    </div>

    <div class="title-container">
        <div class="main-title">Association Sénégalaise de la Valls Icamp</div>
        <div class="sub-title">
            {{ $isLatePayment ? 'Dettes et Retards de Cotisations' : 'Liste des Cotisations' }}
            <br>
            <span
                style="text-decoration: none; font-style: normal; font-weight: bold; font-size: 12px; color: #000000;">
                @if($startDate && $endDate)
                    Période du {{ \Carbon\Carbon::parse($startDate)->format('d/m/Y') }} au
                    {{ \Carbon\Carbon::parse($endDate)->format('d/m/Y') }}
                @elseif($startDate)
                    Depuis le {{ \Carbon\Carbon::parse($startDate)->format('d/m/Y') }}
                @elseif($endDate)
                    Jusqu'au {{ \Carbon\Carbon::parse($endDate)->format('d/m/Y') }}
                @else
                    Période globale
                @endif
            </span>

            {{-- Display Filters if present --}}
            @if(isset($filters) && (array_filter($filters)))
                <div style="margin-top: 10px; font-size: 12px; color: #000; font-weight: bold;">
                    @if($filters['project'])
                        <span style="margin-right: 15px; font-size: 12px; font-weight: bold;"><strong>Projet :</strong>
                            {{ $filters['project'] }}</span>
                    @endif

                    @if($filters['member_name'])
                        <span style="margin-right: 15px; font-size: 12px; font-weight: bold;"><strong>Membre :</strong>
                            {{ $filters['member_name'] }}</span>
                    @endif

                    @if($filters['frequency'])
                        <span><strong>Fréquence :</strong> {{ ucfirst($filters['frequency']) }}</span>
                    @endif
                </div>
            @endif
        </div>
    </div>

    <table>
        <thead>
            <tr>
                @if($isLatePayment)
                    <th style="width: 25%;">Membre</th>
                    <th style="width: 35%;">Projet</th>
                    <th style="width: 20%;">Montant Dû</th>
                    <th style="width: 20%;">Échéance</th>
                @else
                    <th style="width: 15%;">Date</th>
                    <th style="width: 25%;">Membre</th>
                    <th style="width: 30%;">Projet</th>
                    <th style="width: 15%;">Montant</th>
                    <th style="width: 15%;">Reste</th>
                @endif
            </tr>
        </thead>
        <tbody>
            @foreach($data as $item)
                <tr>
                    @if($isLatePayment)
                        <td class="item-bold">{{ $item->user->first_name }} {{ $item->user->last_name }}</td>
                        <td>
                            <div class="item-bold">{{ $item->project->name }}</div>
                            <div class="project-details">
                                Début :
                                {{ $item->project->start_date ? \Carbon\Carbon::parse($item->project->start_date)->format('d/m/Y') : '-' }}
                            </div>
                        </td>
                        <td class="item-bold">{{ number_format($item->amount, 0, ',', ' ') }} {{ $currency }}</td>
                        <td>{{ $item->due_date->format('d/m/Y') }}</td>
                    @else
                        <td>{{ $item->payment_date->format('d/m/Y') }}</td>
                        <td class="item-bold">{{ $item->user->first_name }} {{ $item->user->last_name }}</td>
                        <td>
                            <div class="item-bold">{{ $item->project->name }}</div>
                            <div class="project-details">
                                Début :
                                {{ $item->project->start_date ? \Carbon\Carbon::parse($item->project->start_date)->format('d/m/Y') : '-' }}
                            </div>
                        </td>
                        <td class="item-bold">{{ number_format($item->amount, 0, ',', ' ') }} {{ $currency }}</td>
                        {{-- Reste à payer --}}
                        <td>
                            @php
                                $remaining = $item->user->getRemainingBalanceForProject($item->project_id);
                            @endphp
                            @if($remaining > 0)
                                <span style="font-weight: bold; color: #000;">{{ number_format($remaining, 0, ',', ' ') }}
                                    {{ $currency }}</span>
                            @else
                                <span style="font-weight: bold; color: #000;">À jour</span>
                            @endif
                        </td>
                    @endif
                </tr>
            @endforeach

            {{-- Padding for empty rows --}}
            @for($i = 0; $i < 10; $i++)
                <tr>
                    @if($isLatePayment)
                        <td>&nbsp;</td>
                        <td></td>
                        <td></td>
                        <td></td>
                    @else
                        <td>&nbsp;</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    @endif
                </tr>
            @endfor
        </tbody>
    </table>

    {{-- Total after table --}}
    @if($data->count() > 0)
        <div style="margin-top: 15px; text-align: right; font-size: 14px; font-weight: bold; color: #000;">
            <span style="background-color: #e0e0e0; padding: 5px 10px; display: inline-block;">
                @if($isLatePayment)
                    Total des dettes : {{ number_format($total, 0, ',', ' ') }} {{ $currency }}
                @else
                    Total encaissé : {{ number_format($total, 0, ',', ' ') }} {{ $currency }}
                @endif
            </span>
        </div>
    @endif
</body>

</html>