<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <title>Collection Report</title>

    <style>
        @page {
            margin: 25px;
        }

        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 10px;
            color: #1f2937;
        }

        .header {
            border-bottom: 2px solid #222;
            padding-bottom: 12px;
            margin-bottom: 18px;
        }

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

        .title {
            font-size: 20px;
            font-weight: bold;
        }

        .subtitle {
            font-size: 9px;
            color: #666;
            margin-top: 4px;
        }

        .date {
            font-size: 9px;
            color: #666;
            margin-top: 6px;
        }

        .summary {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 18px;
        }

        .summary td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }

        .summary-label {
            font-size: 8px;
            color: #666;
            text-transform: uppercase;
        }

        .summary-value {
            font-size: 16px;
            font-weight: bold;
            margin-top: 4px;
        }

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

        .report th {
            background: #f3f4f6;
            border: 1px solid #d1d5db;
            padding: 8px 6px;
            font-size: 9px;
            text-align: left;
        }

        .report td {
            border: 1px solid #e5e7eb;
            padding: 7px 6px;
            font-size: 10px;
        }

        .amount {
            text-align: right;
            white-space: nowrap;
        }

        .total-row td {
            font-weight: bold;
            background: #f9fafb;
        }

        .empty {
            text-align: center;
            padding: 20px !important;
            color: #666;
        }

        .footer {
            margin-top: 20px;
            padding-top: 8px;
            border-top: 1px solid #ddd;
            text-align: center;
            font-size: 8px;
            color: #777;
        }
    </style>
</head>

<body>

<div class="header">

    <table class="header-table">

        <tr>

            <td>

                <div class="title">
                    Collection Report
                </div>

                <div class="subtitle">
                    EMI and other payments received
                </div>

                <div class="date">

                    @if($fromDate && $toDate)

                        Date:
                        {{ \Carbon\Carbon::parse($fromDate)->format('d-m-Y') }}
                        -
                        {{ \Carbon\Carbon::parse($toDate)->format('d-m-Y') }}

                    @elseif($fromDate)

                        From:
                        {{ \Carbon\Carbon::parse($fromDate)->format('d-m-Y') }}

                    @elseif($toDate)

                        Up to:
                        {{ \Carbon\Carbon::parse($toDate)->format('d-m-Y') }}

                    @else

                        All Dates

                    @endif

                </div>

            </td>

            <td style="text-align:right; vertical-align:top;">

                <div style="font-size:8px;color:#777;">
                    Generated
                </div>

                <div style="font-size:8px;color:#777;">
                     {{ now()->format('d-m-Y h:i A') }}
                </div>

            </td>

        </tr>

    </table>

</div>


<table class="summary">

    <tr>

        <td>

            <div class="summary-label">
                Total Payments
            </div>

            <div class="summary-value">
                {{ $payments->count() }}
            </div>

        </td>

        <td>

            <div class="summary-label">
                Total Collected
            </div>

            <div class="summary-value">
                {{ $currencySymbol }}{{ number_format($totalCollected, $decimalPlaces) }}
            </div>

        </td>

    </tr>

</table>


<table class="report">

    <thead>

        <tr>

            <th style="width:14%;">
                Date
            </th>

            <th style="width:18%;">
                Client
            </th>

            <th style="width:15%;">
                Loan
            </th>

            <th style="width:14%;">
                Type
            </th>

            <th style="width:13%;">
                Method
            </th>

            <th style="width:13%;">
                Amount
            </th>

            <th style="width:13%;">
                Reference
            </th>

        </tr>

    </thead>

    <tbody>

        @forelse($payments as $payment)

            <tr>

                <td>
                    {{ $payment->paid_at?->format('d-m-Y') ?? '-' }}
                </td>

                <td>
                    {{ $payment->user?->name ?? '-' }}
                </td>

                <td>
                    {{ $payment->loan?->loan_number ?? '-' }}
                </td>

                <td>
                    {{ ucfirst(
                        str_replace(
                            '_',
                            ' ',
                            $payment->payment_type ?? 'general'
                        )
                    ) }}
                </td>

                <td>
                    {{ ucfirst(
                        str_replace(
                            '_',
                            ' ',
                            $payment->payment_method ?? '-'
                        )
                    ) }}
                </td>

                <td class="amount">
                    {{ $currencySymbol }}{{ number_format((float) $payment->amount, $decimalPlaces) }}
                </td>

                <td>
                    {{ $payment->reference_number ?? '-' }}
                </td>

            </tr>

        @empty

            <tr>

                <td colspan="7" class="empty">
                    No payments found.
                </td>

            </tr>

        @endforelse


        @if($payments->count() > 0)

            <tr class="total-row">

                <td colspan="5" class="amount">
                    Total Collection
                </td>

                <td class="amount">
                    {{ $currencySymbol }}{{ number_format((float) $totalCollected, $decimalPlaces) }}
                </td>

                <td></td>

            </tr>

        @endif

    </tbody>

</table>


<div class="footer">
    EMI Management System
</div>

</body>
</html>