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

    <title>EMI Report</title>

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

        * {
            box-sizing: border-box;
        }

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

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

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

        .header-left {
            width: 70%;
            vertical-align: top;
        }

        .header-right {
            width: 30%;
            text-align: right;
            vertical-align: top;
        }

        .title {
            font-size: 22px;
            font-weight: bold;
            margin: 0 0 5px 0;
        }

        .subtitle {
            font-size: 10px;
            color: #6b7280;
            margin: 0;
        }

        .generated {
            font-size: 8px;
            color: #6b7280;
        }

        .date-range {
            margin-top: 8px;
            font-size: 9px;
            color: #4b5563;
        }


        /* Summary */

        .summary-table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 6px;
            margin-left: -6px;
            margin-right: -6px;
            margin-bottom: 18px;
        }

        .summary-box {
            border: 1px solid #d1d5db;
            padding: 11px 8px;
            text-align: center;
            width: 25%;
        }

        .summary-label {
            font-size: 8px;
            color: #6b7280;
            text-transform: uppercase;
            margin-bottom: 5px;
        }

        .summary-value {
            font-size: 15px;
            font-weight: bold;
        }


        /* Sections */

        .section {
            margin-top: 18px;
        }

        .section-title {
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 3px;
        }

        .section-description {
            font-size: 9px;
            color: #6b7280;
            margin-bottom: 9px;
        }


        /* Tables */

        .report-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 14px;
        }

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

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

        .report-table tr {
            page-break-inside: avoid;
        }

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

        .center {
            text-align: center;
        }

        .status {
            text-transform: capitalize;
        }

        .empty {
            text-align: center;
            padding: 15px !important;
            color: #6b7280;
        }


        /* Totals */

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


        /* Footer */

        .footer {
            border-top: 1px solid #d1d5db;
            margin-top: 20px;
            padding-top: 8px;
            text-align: center;
            font-size: 8px;
            color: #6b7280;
        }

        .page-break {
            page-break-before: always;
        }
    </style>
</head>

<body>


{{-- ========================================================= --}}
{{-- HEADER --}}
{{-- ========================================================= --}}

<div class="header">

    <table class="header-table">

        <tr>

            <td class="header-left">

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

                <div class="subtitle">
                    Loan and payment collection report
                </div>

                <div class="date-range">

                    @if($fromDate && $toDate)

                        Date Range:
                        {{ \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 class="header-right">

                <div class="generated">
                    Generated
                </div>

                <div class="generated">
                    {{ now()->format('d-m-Y h:i A') }}
                </div>

            </td>

        </tr>

    </table>

</div>



{{-- ========================================================= --}}
{{-- SUMMARY --}}
{{-- ========================================================= --}}

<table class="summary-table">

    <tr>

        <td class="summary-box">

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

            <div class="summary-value">
                {{ $totalLoans }}
            </div>

        </td>


        <td class="summary-box">

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

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

        </td>


        <td class="summary-box">

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

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

        </td>


        <td class="summary-box">

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

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

        </td>

    </tr>

</table>



{{-- ========================================================= --}}
{{-- COLLECTION REPORT --}}
{{-- ========================================================= --}}

<div class="section">

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

    <div class="section-description">
        EMI and other payments received
    </div>


    <table class="report-table">

        <thead>

            <tr>

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

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

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

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

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

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

                <th style="width: 17%;">
                    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 for the selected date range.
                    </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>



{{-- ========================================================= --}}
{{-- LOAN REPORT --}}
{{-- ========================================================= --}}

<div class="section">

    <div class="section-title">
        Loan Report
    </div>

    <div class="section-description">
        Overview of loans
    </div>


    <table class="report-table">

        <thead>

            <tr>

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

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

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

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

                <th style="width: 17%;">
                    Outstanding
                </th>

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

            </tr>

        </thead>


        <tbody>

            @forelse($loans as $loan)

                <tr>

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


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


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


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


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


                    <td class="status">
                        {{ ucfirst($loan->status ?? '-') }}
                    </td>

                </tr>

            @empty

                <tr>

                    <td colspan="6" class="empty">
                        No loans found for the selected date range.
                    </td>

                </tr>

            @endforelse


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

                <tr class="total-row">

                    <td colspan="2" class="amount">
                        Total
                    </td>

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

                    <td>
                    </td>

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

                    <td>
                    </td>

                </tr>

            @endif

        </tbody>

    </table>

</div>



{{-- ========================================================= --}}
{{-- FOOTER --}}
{{-- ========================================================= --}}

<div class="footer">

    EMI Management System
    &nbsp; | &nbsp;
    Generated on {{ now()->format('d-m-Y h:i A') }}

</div>


</body>
</html>
