<?php

date_default_timezone_set('Africa/Lagos');

include "../includes/admin_auth.php";
include "../config/database.php";

/*
|--------------------------------------------------------------------------
| REPORT FILTER VALUES
|--------------------------------------------------------------------------
*/

$filterFrom =
    trim($_GET['from'] ?? '');

$filterTo =
    trim($_GET['to'] ?? '');

$filterScheme =
    (int)($_GET['scheme'] ?? 0);

$filterMember =
    (int)($_GET['member'] ?? 0);

$filterType =
    trim($_GET['report_type'] ?? 'all');


/*
|--------------------------------------------------------------------------
| CONTRIBUTION FILTER CONDITIONS
|--------------------------------------------------------------------------
*/

$contributionWhere = "";
$contributionParams = [];


if ($filterFrom !== '') {

    $contributionWhere .= "
        AND DATE(
            COALESCE(
                c.paid_date,
                c.due_date
            )
        ) >= ?
    ";

    $contributionParams[] =
        $filterFrom;
}


if ($filterTo !== '') {

    $contributionWhere .= "
        AND DATE(
            COALESCE(
                c.paid_date,
                c.due_date
            )
        ) <= ?
    ";

    $contributionParams[] =
        $filterTo;
}


if ($filterScheme > 0) {

    $contributionWhere .= "
        AND s.plan_id = ?
    ";

    $contributionParams[] =
        $filterScheme;
}


if ($filterMember > 0) {

    $contributionWhere .= "
        AND s.user_id = ?
    ";

    $contributionParams[] =
        $filterMember;
}


/*
|--------------------------------------------------------------------------
| TOTAL PAID CONTRIBUTIONS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        COALESCE(SUM(c.amount), 0)

    FROM contributions c

    INNER JOIN subscriptions s
        ON c.subscription_id = s.id

    WHERE c.status = 'paid'

    $contributionWhere
");

$stmt->execute(
    $contributionParams
);

$totalPaidContributions =
    (float)$stmt->fetchColumn();


/*
|--------------------------------------------------------------------------
| OUTSTANDING CONTRIBUTIONS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        COUNT(*) AS total_count,

        COALESCE(
            SUM(c.amount),
            0
        ) AS total_amount

    FROM contributions c

    INNER JOIN subscriptions s
        ON c.subscription_id = s.id

    WHERE c.status != 'paid'

    $contributionWhere
");

$stmt->execute(
    $contributionParams
);

$outstandingData =
    $stmt->fetch(PDO::FETCH_ASSOC);


$outstandingContributionCount =
    (int)(
        $outstandingData['total_count']
        ?? 0
    );


$outstandingContributionAmount =
    (float)(
        $outstandingData['total_amount']
        ?? 0
    );


/*
|--------------------------------------------------------------------------
| DEFAULT FILTER CONDITIONS
|--------------------------------------------------------------------------
*/

$defaultWhere = "";
$defaultParams = [];


if ($filterFrom !== '') {

    $defaultWhere .= "
        AND sd.default_date >= ?
    ";

    $defaultParams[] =
        $filterFrom;
}


if ($filterTo !== '') {

    $defaultWhere .= "
        AND sd.default_date <= ?
    ";

    $defaultParams[] =
        $filterTo;
}


if ($filterScheme > 0) {

    $defaultWhere .= "
        AND sd.plan_id = ?
    ";

    $defaultParams[] =
        $filterScheme;
}


if ($filterMember > 0) {

    $defaultWhere .= "
        AND sd.user_id = ?
    ";

    $defaultParams[] =
        $filterMember;
}


/*
|--------------------------------------------------------------------------
| UNPAID DEFAULTS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        COUNT(*) AS total_count,

        COALESCE(
            SUM(sd.amount),
            0
        ) AS total_amount

    FROM subscription_defaults sd

    WHERE sd.status = 'unpaid'

    $defaultWhere
");

$stmt->execute(
    $defaultParams
);

$unpaidDefaultsData =
    $stmt->fetch(PDO::FETCH_ASSOC);


$unpaidDefaultsCount =
    (int)(
        $unpaidDefaultsData['total_count']
        ?? 0
    );


$unpaidDefaultsAmount =
    (float)(
        $unpaidDefaultsData['total_amount']
        ?? 0
    );


/*
|--------------------------------------------------------------------------
| SETTLED DEFAULTS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        COUNT(*) AS total_count,

        COALESCE(
            SUM(sd.amount),
            0
        ) AS total_amount

    FROM subscription_defaults sd

    WHERE sd.status = 'paid'

    $defaultWhere
");

$stmt->execute(
    $defaultParams
);

$settledDefaultsData =
    $stmt->fetch(PDO::FETCH_ASSOC);


$settledDefaultsCount =
    (int)(
        $settledDefaultsData['total_count']
        ?? 0
    );


$settledDefaultsAmount =
    (float)(
        $settledDefaultsData['total_amount']
        ?? 0
    );


/*
|--------------------------------------------------------------------------
| CLEARANCE FILTER CONDITIONS
|--------------------------------------------------------------------------
*/

$clearanceWhere = "";
$clearanceParams = [];


if ($filterFrom !== '') {

    $clearanceWhere .= "
        AND DATE(sc.clearance_date) >= ?
    ";

    $clearanceParams[] =
        $filterFrom;
}


if ($filterTo !== '') {

    $clearanceWhere .= "
        AND DATE(sc.clearance_date) <= ?
    ";

    $clearanceParams[] =
        $filterTo;
}


if ($filterScheme > 0) {

    $clearanceWhere .= "
        AND sc.plan_id = ?
    ";

    $clearanceParams[] =
        $filterScheme;
}


if ($filterMember > 0) {

    $clearanceWhere .= "
        AND sc.user_id = ?
    ";

    $clearanceParams[] =
        $filterMember;
}


/*
|--------------------------------------------------------------------------
| COMPLETED CLEARANCES
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT COUNT(*)

    FROM scheme_clearances sc

    WHERE sc.status = 'completed'

    $clearanceWhere
");

$stmt->execute(
    $clearanceParams
);

$completedClearances =
    (int)$stmt->fetchColumn();


/*
|--------------------------------------------------------------------------
| ACTIVE SUBSCRIPTION FILTER CONDITIONS
|--------------------------------------------------------------------------
*/

$subscriptionWhere = "";
$subscriptionParams = [];


if ($filterFrom !== '') {

    $subscriptionWhere .= "
        AND DATE(s.created_at) >= ?
    ";

    $subscriptionParams[] =
        $filterFrom;
}


if ($filterTo !== '') {

    $subscriptionWhere .= "
        AND DATE(s.created_at) <= ?
    ";

    $subscriptionParams[] =
        $filterTo;
}


if ($filterScheme > 0) {

    $subscriptionWhere .= "
        AND s.plan_id = ?
    ";

    $subscriptionParams[] =
        $filterScheme;
}


if ($filterMember > 0) {

    $subscriptionWhere .= "
        AND s.user_id = ?
    ";

    $subscriptionParams[] =
        $filterMember;
}


/*
|--------------------------------------------------------------------------
| ACTIVE SUBSCRIPTIONS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT COUNT(*)

    FROM subscriptions s

    WHERE s.status = 'active'

    $subscriptionWhere
");

$stmt->execute(
    $subscriptionParams
);

$activeSubscriptions =
    (int)$stmt->fetchColumn();

    /*
|--------------------------------------------------------------------------
| SCHEMES FOR REPORT FILTER
|--------------------------------------------------------------------------
*/

$stmt = $pdo->query("
    SELECT id, plan_name
    FROM plans
    ORDER BY plan_name ASC
");

$reportSchemes =
    $stmt->fetchAll(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| MEMBERS FOR REPORT FILTER
|--------------------------------------------------------------------------
*/

$stmt = $pdo->query("
    SELECT id, fullname
    FROM users
    WHERE role = 'member'
    ORDER BY fullname ASC
");

$reportMembers =
    $stmt->fetchAll(PDO::FETCH_ASSOC);



    /*
|--------------------------------------------------------------------------
| DETAILED REPORT RECORDS
|--------------------------------------------------------------------------
*/

$reportRows = [];


/*
|--------------------------------------------------------------------------
| CONTRIBUTIONS
|--------------------------------------------------------------------------
*/

if (
    $filterType === 'all' ||
    $filterType === 'contributions'
) {

    $sql = "
        SELECT

            c.id,
            c.contribution_no,

            COALESCE(
                c.paid_date,
                c.due_date
            ) AS record_date,

            c.amount,
            c.status,

            u.fullname,
            p.plan_name,

            s.id AS subscription_id

        FROM contributions c

        INNER JOIN subscriptions s
            ON c.subscription_id = s.id

        INNER JOIN users u
            ON s.user_id = u.id

        INNER JOIN plans p
            ON s.plan_id = p.id

        WHERE 1 = 1
    ";

    $params = [];


    if ($filterFrom !== '') {

        $sql .= "
            AND DATE(
                COALESCE(
                    c.paid_date,
                    c.due_date
                )
            ) >= ?
        ";

        $params[] = $filterFrom;

    }


    if ($filterTo !== '') {

        $sql .= "
            AND DATE(
                COALESCE(
                    c.paid_date,
                    c.due_date
                )
            ) <= ?
        ";

        $params[] = $filterTo;

    }


    if ($filterScheme > 0) {

        $sql .= "
            AND s.plan_id = ?
        ";

        $params[] = $filterScheme;

    }


    if ($filterMember > 0) {

        $sql .= "
            AND s.user_id = ?
        ";

        $params[] = $filterMember;

    }


    $stmt = $pdo->prepare($sql);

    $stmt->execute($params);

    $rows =
        $stmt->fetchAll(PDO::FETCH_ASSOC);


    foreach ($rows as $row) {

        $reportRows[] = [

            'date' =>
                $row['record_date'],

            'member' =>
                $row['fullname'],

            'scheme' =>
                $row['plan_name'],

            'type' =>
                'Contribution',

            'description' =>
                'Contribution #'
                . (int)$row['contribution_no'],

            'amount' =>
                (float)$row['amount'],

            'status' =>
                $row['status'],

            'code' =>
                ''

        ];

    }

}


/*
|--------------------------------------------------------------------------
| DEFAULTS
|--------------------------------------------------------------------------
*/

if (
    $filterType === 'all' ||
    $filterType === 'defaults'
) {

    $sql = "
        SELECT

            sd.id,
            sd.default_date,
            sd.amount,
            sd.status,
            sd.reason,

            u.fullname,
            p.plan_name

        FROM subscription_defaults sd

        INNER JOIN subscriptions s
            ON sd.subscription_id = s.id

        INNER JOIN users u
            ON sd.user_id = u.id

        INNER JOIN plans p
            ON sd.plan_id = p.id

        WHERE 1 = 1
    ";

    $params = [];


    if ($filterFrom !== '') {

        $sql .= "
            AND sd.default_date >= ?
        ";

        $params[] = $filterFrom;

    }


    if ($filterTo !== '') {

        $sql .= "
            AND sd.default_date <= ?
        ";

        $params[] = $filterTo;

    }


    if ($filterScheme > 0) {

        $sql .= "
            AND sd.plan_id = ?
        ";

        $params[] = $filterScheme;

    }


    if ($filterMember > 0) {

        $sql .= "
            AND sd.user_id = ?
        ";

        $params[] = $filterMember;

    }


    $stmt = $pdo->prepare($sql);

    $stmt->execute($params);

    $rows =
        $stmt->fetchAll(PDO::FETCH_ASSOC);


    foreach ($rows as $row) {

        $reportRows[] = [

            'date' =>
                $row['default_date'],

            'member' =>
                $row['fullname'],

            'scheme' =>
                $row['plan_name'],

            'type' =>
                'Default',

            'description' =>
                !empty($row['reason'])
                    ? $row['reason']
                    : 'Missed payment penalty',

            'amount' =>
                (float)$row['amount'],

            'status' =>
                $row['status'],

            'code' =>
                ''

        ];

    }

}


/*
|--------------------------------------------------------------------------
| CLEARANCES
|--------------------------------------------------------------------------
*/

if (
    $filterType === 'all' ||
    $filterType === 'clearances'
) {

    $sql = "
        SELECT

            sc.id,
            sc.clearance_date,
            sc.clearance_code,
            sc.total_defaults_settled,
            sc.status,

            u.fullname,
            p.plan_name

        FROM scheme_clearances sc

        INNER JOIN subscriptions s
            ON sc.subscription_id = s.id

        INNER JOIN users u
            ON sc.user_id = u.id

        INNER JOIN plans p
            ON sc.plan_id = p.id

        WHERE 1 = 1
    ";

    $params = [];


    if ($filterFrom !== '') {

        $sql .= "
            AND DATE(sc.clearance_date) >= ?
        ";

        $params[] = $filterFrom;

    }


    if ($filterTo !== '') {

        $sql .= "
            AND DATE(sc.clearance_date) <= ?
        ";

        $params[] = $filterTo;

    }


    if ($filterScheme > 0) {

        $sql .= "
            AND sc.plan_id = ?
        ";

        $params[] = $filterScheme;

    }


    if ($filterMember > 0) {

        $sql .= "
            AND sc.user_id = ?
        ";

        $params[] = $filterMember;

    }


    $stmt = $pdo->prepare($sql);

    $stmt->execute($params);

    $rows =
        $stmt->fetchAll(PDO::FETCH_ASSOC);


    foreach ($rows as $row) {

        $reportRows[] = [

            'date' =>
                $row['clearance_date'],

            'member' =>
                $row['fullname'],

            'scheme' =>
                $row['plan_name'],

            'type' =>
                'Clearance',

            'description' =>
                'Scheme clearance completed',

            'amount' =>
                (float)$row[
                    'total_defaults_settled'
                ],

            'status' =>
                $row['status'],

            'code' =>
                $row['clearance_code']

        ];

    }

}


/*
|--------------------------------------------------------------------------
| SORT NEWEST FIRST
|--------------------------------------------------------------------------
*/

usort(
    $reportRows,
    function ($a, $b) {

        return strtotime($b['date'])
            <=> strtotime($a['date']);

    }
);


/*
|--------------------------------------------------------------------------
| REPORT PAGINATION
|--------------------------------------------------------------------------
*/

$recordsPerPage =20;

$totalReportRows =
    count($reportRows);


$reportPage =
    max(
        1,
        (int)($_GET['page'] ?? 1)
    );


$totalPages =
    max(
        1,
        (int)ceil(
            $totalReportRows /
            $recordsPerPage
        )
    );


/*
|--------------------------------------------------------------------------
| Prevent Invalid Page Number
|--------------------------------------------------------------------------
*/

if ($reportPage > $totalPages) {

    $reportPage =
        $totalPages;

}


$reportOffset =
    ($reportPage - 1)
    * $recordsPerPage;


$paginatedReportRows =
    array_slice(
        $reportRows,
        $reportOffset,
        $recordsPerPage
    );


/*
|--------------------------------------------------------------------------
| Display Range
|--------------------------------------------------------------------------
*/

$showingFrom =
    $totalReportRows > 0
        ? $reportOffset + 1
        : 0;


$showingTo =
    min(
        $reportOffset
        + $recordsPerPage,
        $totalReportRows
    );


/*
|--------------------------------------------------------------------------
| Preserve Filters In Pagination Links
|--------------------------------------------------------------------------
*/

$paginationParams = [

    'from' =>
        $filterFrom,

    'to' =>
        $filterTo,

    'scheme' =>
        $filterScheme,

    'member' =>
        $filterMember,

    'report_type' =>
        $filterType

];

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta
    name="viewport"
    content="width=device-width, initial-scale=1.0"
>

<title>Reports - Complete Bliss</title>


<link
    rel="stylesheet"
    href="../assets/css/admin.css"
>

<link
    rel="stylesheet"
    href="../assets/css/responsive.css"
>

<link
    rel="stylesheet"
    href="../assets/css/admin-reports.css"
>

<link
    rel="stylesheet"
    href="../assets/css/admin-reports-responsive.css"
>

<link
    rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css"
>

</head>


<body>


<?php include "../includes/admin_sidebar.php"; ?>


<div class="sidebar-overlay"></div>


<?php include "../includes/admin_topbar.php"; ?>


<div class="main-content">


<main class="admin-reports-page">


    <!-- ==========================================
         PAGE HEADER
    =========================================== -->

    <div class="admin-reports-header">


        <div>

            <h1>

                <i class="fas fa-chart-line"></i>

                Reports

            </h1>

            <p>
                View financial and scheme performance
                across Complete Bliss.
            </p>

        </div>


        <div class="reports-date">

            <i class="fas fa-calendar-day"></i>

            <?= date('d M Y'); ?>

        </div>


    </div>



    <!-- ==========================================
         SUMMARY CARDS
    =========================================== -->

    <div class="reports-summary-grid">


        <!-- PAID CONTRIBUTIONS -->

        <div class="report-summary-card">


            <div class="report-summary-icon contributions">

                <i class="fas fa-coins"></i>

            </div>


            <div class="report-summary-content">

                <span>
                    Total Paid Contributions
                </span>

                <h2>

                    ₦<?= number_format(
                        $totalPaidContributions,
                        2
                    ); ?>

                </h2>

                <small>
                    Successfully paid contributions
                </small>

            </div>


        </div>



        <!-- OUTSTANDING CONTRIBUTIONS -->

        <div class="report-summary-card">


            <div class="report-summary-icon outstanding">

                <i class="fas fa-hourglass-half"></i>

            </div>


            <div class="report-summary-content">

                <span>
                    Outstanding Contributions
                </span>

                <h2>

                    ₦<?= number_format(
                        $outstandingContributionAmount,
                        2
                    ); ?>

                </h2>

                <small>

                    <?= $outstandingContributionCount; ?>

                    payment<?= $outstandingContributionCount === 1
                        ? ''
                        : 's'; ?>

                    outstanding

                </small>

            </div>


        </div>



        <!-- UNPAID DEFAULTS -->

        <div class="report-summary-card">


            <div class="report-summary-icon defaults">

                <i class="fas fa-triangle-exclamation"></i>

            </div>


            <div class="report-summary-content">

                <span>
                    Unpaid Defaults
                </span>

                <h2>

                    ₦<?= number_format(
                        $unpaidDefaultsAmount,
                        2
                    ); ?>

                </h2>

                <small>

                    <?= $unpaidDefaultsCount; ?>

                    unpaid default<?= $unpaidDefaultsCount === 1
                        ? ''
                        : 's'; ?>

                </small>

            </div>


        </div>



        <!-- SETTLED DEFAULTS -->

        <div class="report-summary-card">


            <div class="report-summary-icon settled">

                <i class="fas fa-circle-check"></i>

            </div>


            <div class="report-summary-content">

                <span>
                    Settled Defaults
                </span>

                <h2>

                    ₦<?= number_format(
                        $settledDefaultsAmount,
                        2
                    ); ?>

                </h2>

                <small>

                    <?= $settledDefaultsCount; ?>

                    default<?= $settledDefaultsCount === 1
                        ? ''
                        : 's'; ?>

                    cleared

                </small>

            </div>


        </div>



        <!-- ACTIVE SUBSCRIPTIONS -->

        <div class="report-summary-card">


            <div class="report-summary-icon schemes">

                <i class="fas fa-layer-group"></i>

            </div>


            <div class="report-summary-content">

                <span>
                    Active Scheme Subscriptions
                </span>

                <h2>

                    <?= number_format(
                        $activeSubscriptions
                    ); ?>

                </h2>

                <small>
                    Currently active subscriptions
                </small>

            </div>


        </div>



        <!-- COMPLETED CLEARANCES -->

        <div class="report-summary-card">


            <div class="report-summary-icon clearance">

                <i class="fas fa-clipboard-check"></i>

            </div>


            <div class="report-summary-content">

                <span>
                    Completed Clearances
                </span>

                <h2>

                    <?= number_format(
                        $completedClearances
                    ); ?>

                </h2>

                <small>
                    Successfully cleared schemes
                </small>

            </div>


        </div>


    </div>



<section class="reports-filter-card">


    <div class="reports-filter-heading">

        <div class="reports-filter-heading-icon">

            <i class="fas fa-sliders"></i>

        </div>

        <div>

            <h3>
                Detailed Reports
            </h3>

            <p>
                Select the information you want to view.
            </p>

        </div>

    </div>


    <form
        method="GET"
        action="reports.php"
        class="reports-filter-form"
    >


        <!-- FROM DATE -->

        <div class="reports-filter-group">

            <label for="from">
                From Date
            </label>

            <input
                type="date"
                name="from"
                id="from"
                value="<?= htmlspecialchars($filterFrom); ?>"
            >

        </div>



        <!-- TO DATE -->

        <div class="reports-filter-group">

            <label for="to">
                To Date
            </label>

            <input
                type="date"
                name="to"
                id="to"
                value="<?= htmlspecialchars($filterTo); ?>"
            >

        </div>



        <!-- SCHEME -->

        <div class="reports-filter-group">

            <label for="scheme">
                Scheme
            </label>

            <select
                name="scheme"
                id="scheme"
            >

                <option value="0">
                    All Schemes
                </option>

                <?php foreach ($reportSchemes as $scheme): ?>

                    <option
                        value="<?= (int)$scheme['id']; ?>"
                        <?= $filterScheme === (int)$scheme['id']
                            ? 'selected'
                            : ''; ?>
                    >

                        <?= htmlspecialchars(
                            $scheme['plan_name']
                        ); ?>

                    </option>

                <?php endforeach; ?>

            </select>

        </div>



        <!-- MEMBER -->

        <div class="reports-filter-group">

            <label for="member">
                Member
            </label>

            <select
                name="member"
                id="member"
            >

                <option value="0">
                    All Members
                </option>

                <?php foreach ($reportMembers as $member): ?>

                    <option
                        value="<?= (int)$member['id']; ?>"
                        <?= $filterMember === (int)$member['id']
                            ? 'selected'
                            : ''; ?>
                    >

                        <?= htmlspecialchars(
                            $member['fullname']
                        ); ?>

                    </option>

                <?php endforeach; ?>

            </select>

        </div>



        <!-- REPORT TYPE -->

        <div class="reports-filter-group">

            <label for="report_type">
                Report Type
            </label>

            <select
                name="report_type"
                id="report_type"
            >

                <option
                    value="all"
                    <?= $filterType === 'all'
                        ? 'selected'
                        : ''; ?>
                >
                    All Records
                </option>

                <option
                    value="contributions"
                    <?= $filterType === 'contributions'
                        ? 'selected'
                        : ''; ?>
                >
                    Contributions
                </option>

                <option
                    value="defaults"
                    <?= $filterType === 'defaults'
                        ? 'selected'
                        : ''; ?>
                >
                    Defaults
                </option>

                <option
                    value="clearances"
                    <?= $filterType === 'clearances'
                        ? 'selected'
                        : ''; ?>
                >
                    Clearances
                </option>

            </select>

        </div>



        <!-- BUTTONS -->

        <div class="reports-filter-actions">

            <button
                type="submit"
                class="reports-apply-btn"
            >

                <i class="fas fa-filter"></i>

                Apply Filters

            </button>


            <a
                href="reports.php"
                class="reports-reset-btn"
            >

                <i class="fas fa-rotate-left"></i>

                Reset

            </a>


            <a
    href="export_report.php?<?= http_build_query([
        'from' => $filterFrom,
        'to' => $filterTo,
        'scheme' => $filterScheme,
        'member' => $filterMember,
        'report_type' => $filterType
    ]); ?>"
    class="reports-download-btn"
>

    <i class="fas fa-file-excel"></i>

    Download Report

</a>

        </div>


    </form>


</section>


<!-- ==========================================
     DETAILED REPORT TABLE
=========================================== -->

<section class="reports-table-card">


    <div class="reports-table-header">


        <div>

            <h3>
                Report Results
            </h3>

<p>

    <?php if ($totalReportRows > 0): ?>

        Showing

        <strong>
            <?= $showingFrom; ?>
        </strong>

        –

        <strong>
            <?= $showingTo; ?>
        </strong>

        of

        <strong>
            <?= $totalReportRows; ?>
        </strong>

        records

    <?php else: ?>

        0 records found

    <?php endif; ?>

</p>

        </div>


    </div>


    <?php if (count($reportRows) > 0): ?>


        <div class="reports-table-wrap">


            <table class="reports-table">


                <thead>

                    <tr>

                        <th>Date</th>

                        <th>Member</th>

                        <th>Scheme</th>

                        <th>Type</th>

                        <th>Details</th>

                        <th>Amount</th>

                        <th>Status</th>

                    </tr>

                </thead>


                <tbody>


                <?php foreach ($paginatedReportRows as $row): ?>


                    <?php

                    $statusClass =
                        strtolower(
                            str_replace(
                                ' ',
                                '-',
                                $row['status']
                            )
                        );

                    ?>


                    <tr>


                        <!-- DATE -->

                        <td>

                            <strong class="report-date-value">

                                <?= date(
                                    'd M Y',
                                    strtotime(
                                        $row['date']
                                    )
                                ); ?>

                            </strong>

                        </td>



                        <!-- MEMBER -->

                        <td>

                            <strong>

                                <?= htmlspecialchars(
                                    $row['member']
                                ); ?>

                            </strong>

                        </td>



                        <!-- SCHEME -->

                        <td>

                            <?= htmlspecialchars(
                                $row['scheme']
                            ); ?>

                        </td>



                        <!-- TYPE -->

                        <td>

                            <?php if (
                                $row['type']
                                === 'Contribution'
                            ): ?>

                                <span class="report-type contribution">

                                    <i class="fas fa-coins"></i>

                                    Contribution

                                </span>


                            <?php elseif (
                                $row['type']
                                === 'Default'
                            ): ?>

                                <span class="report-type default">

                                    <i class="fas fa-triangle-exclamation"></i>

                                    Default

                                </span>


                            <?php else: ?>

                                <span class="report-type clearance">

                                    <i class="fas fa-clipboard-check"></i>

                                    Clearance

                                </span>

                            <?php endif; ?>

                        </td>



                        <!-- DETAILS -->

                        <td>

                            <span class="report-description">

                                <?= htmlspecialchars(
                                    $row['description']
                                ); ?>

                            </span>


                            <?php if (
                                !empty($row['code'])
                            ): ?>

                                <small class="report-clearance-code">

                                    <?= htmlspecialchars(
                                        $row['code']
                                    ); ?>

                                </small>

                            <?php endif; ?>

                        </td>



                        <!-- AMOUNT -->

                        <td>

                            <strong>

                                ₦<?= number_format(
                                    $row['amount'],
                                    2
                                ); ?>

                            </strong>

                        </td>



                        <!-- STATUS -->

                        <td>

                            <span
                                class="report-status <?= htmlspecialchars($statusClass); ?>"
                            >

                                <?= htmlspecialchars(
                                    ucfirst(
                                        $row['status']
                                    )
                                ); ?>

                            </span>

                        </td>


                    </tr>


                <?php endforeach; ?>


                </tbody>


            </table>


        </div>

        <?php if ($totalPages > 1): ?>




<div class="reports-pagination">


    <!-- PREVIOUS -->

    <?php if ($reportPage > 1): ?>

        <a
            href="?<?= htmlspecialchars(
                http_build_query(
                    array_merge(
                        $paginationParams,
                        [
                            'page' =>
                                $reportPage - 1
                        ]
                    )
                )
            ); ?>"
            class="reports-page-nav"
        >

            <i class="fas fa-chevron-left"></i>

            Previous

        </a>

    <?php else: ?>

        <span
            class="reports-page-nav disabled"
        >

            <i class="fas fa-chevron-left"></i>

            Previous

        </span>

    <?php endif; ?>



  <!-- PAGE NUMBERS -->

<div class="reports-page-numbers">

    <?php for (
        $pageNumber = 1;
        $pageNumber <= $totalPages;
        $pageNumber++
    ): ?>


        <?php if (
            $pageNumber === $reportPage
        ): ?>

            <span
                class="reports-page-number active"
            >

                <?= $pageNumber; ?>

            </span>


        <?php else: ?>

            <a
                href="?<?= htmlspecialchars(
                    http_build_query(
                        array_merge(
                            $paginationParams,
                            [
                                'page' =>
                                    $pageNumber
                            ]
                        )
                    )
                ); ?>"
                class="reports-page-number"
            >

                <?= $pageNumber; ?>

            </a>

        <?php endif; ?>


    <?php endfor; ?>

</div>



    <!-- NEXT -->

    <?php if (
        $reportPage < $totalPages
    ): ?>

        <a
            href="?<?= htmlspecialchars(
                http_build_query(
                    array_merge(
                        $paginationParams,
                        [
                            'page' =>
                                $reportPage + 1
                        ]
                    )
                )
            ); ?>"
            class="reports-page-nav"
        >

            Next

            <i class="fas fa-chevron-right"></i>

        </a>

    <?php else: ?>

        <span
            class="reports-page-nav disabled"
        >

            Next

            <i class="fas fa-chevron-right"></i>

        </span>

    <?php endif; ?>


</div>


<?php endif; ?>

    <?php else: ?>


        <div class="reports-empty-state">


            <div class="reports-empty-icon">

                <i class="fas fa-file-circle-xmark"></i>

            </div>


            <h3>
                No Records Found
            </h3>


            <p>
                No report records match the
                filters you selected.
            </p>


        </div>


    <?php endif; ?>


</section>


</main>


</div>


<script src="../assets/js/admin.js"></script>


</body>

</html>