<?php

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


/*
|--------------------------------------------------------------------------
| Filter
|--------------------------------------------------------------------------
*/

$allowedStatuses = [
    'all',
    'pending',
    'processing',
    'activated',
    'failed',
    'cancelled'
];

$selectedStatus =
    trim($_GET['status'] ?? 'all');

if (
    !in_array(
        $selectedStatus,
        $allowedStatuses,
        true
    )
) {

    $selectedStatus = 'all';

}


/*
|--------------------------------------------------------------------------
| Summary
|--------------------------------------------------------------------------
*/

$summaryStmt = $pdo->query("
    SELECT

        COUNT(*) AS total,

        SUM(
            CASE
                WHEN status = 'pending'
                THEN 1
                ELSE 0
            END
        ) AS pending_count,

        SUM(
            CASE
                WHEN status = 'processing'
                THEN 1
                ELSE 0
            END
        ) AS processing_count,

        SUM(
            CASE
                WHEN status = 'activated'
                THEN 1
                ELSE 0
            END
        ) AS activated_count

    FROM funding_account_requests
");

$summary =
    $summaryStmt->fetch(
        PDO::FETCH_ASSOC
    );


/*
|--------------------------------------------------------------------------
| Pagination
|--------------------------------------------------------------------------
*/

$fundingPerPage = 15;

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

$whereSql = "";

$queryParams = [];

if ($selectedStatus !== 'all') {

    $whereSql =
        " WHERE far.status = ? ";

    $queryParams[] =
        $selectedStatus;

}


/*
|--------------------------------------------------------------------------
| Count Requests
|--------------------------------------------------------------------------
*/

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

    FROM funding_account_requests far

    {$whereSql}
");

$countStmt->execute(
    $queryParams
);

$totalFundingRequests =
    (int)$countStmt->fetchColumn();

$totalFundingPages =
    max(
        1,
        (int)ceil(
            $totalFundingRequests /
            $fundingPerPage
        )
    );

if (
    $fundingPage >
    $totalFundingPages
) {

    $fundingPage =
        $totalFundingPages;

}

$fundingOffset =
    ($fundingPage - 1)
    *
    $fundingPerPage;


/*
|--------------------------------------------------------------------------
| Get Requests
|--------------------------------------------------------------------------
*/

$sql = "
    SELECT

        far.id,
        far.user_id,
        far.email,
        far.consent_given,
        far.consent_at,
        far.status,
        far.failure_reason,
        far.requested_at,
        far.updated_at,

        u.fullname,
        u.phone,
        u.wallet_balance,

        mva.customer_code,
        mva.validation_status,
        mva.bank_name,
        mva.account_number,
        mva.active,
        mva.assigned

    FROM funding_account_requests far

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

    LEFT JOIN member_virtual_accounts mva

        ON mva.user_id = far.user_id

        AND mva.provider = 'paystack'

        AND mva.environment = 'live'

    {$whereSql}

    ORDER BY far.requested_at DESC

    LIMIT {$fundingPerPage}

    OFFSET {$fundingOffset}
";

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

$stmt->execute(
    $queryParams
);

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

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

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

<title>
    Funding Account Requests - 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-funding-requests.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="funding-admin-page">


    <!-- HEADER -->

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


        <div>

            <span class="funding-admin-label">

                <i class="fas fa-building-columns"></i>

                PAYMENT ACCOUNTS

            </span>


            <h1>
                Funding Account Requests
            </h1>


            <p>
                Review members requesting personal
                wallet-funding accounts.
            </p>

        </div>


        <div class="funding-header-icon">

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

        </div>


    </div>



    <!-- SUMMARY -->

    <section class="funding-summary-grid">


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

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

            <div>

                <span>
                    Total Requests
                </span>

                <strong>

                    <?= number_format(
                        (int)(
                            $summary['total']
                            ?? 0
                        )
                    ); ?>

                </strong>

            </div>

        </div>



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

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

            <div>

                <span>
                    Pending
                </span>

                <strong>

                    <?= number_format(
                        (int)(
                            $summary['pending_count']
                            ?? 0
                        )
                    ); ?>

                </strong>

            </div>

        </div>



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

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

            <div>

                <span>
                    Processing
                </span>

                <strong>

                    <?= number_format(
                        (int)(
                            $summary['processing_count']
                            ?? 0
                        )
                    ); ?>

                </strong>

            </div>

        </div>



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

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

            <div>

                <span>
                    Activated
                </span>

                <strong>

                    <?= number_format(
                        (int)(
                            $summary['activated_count']
                            ?? 0
                        )
                    ); ?>

                </strong>

            </div>

        </div>


    </section>



    <!-- PAYSTACK NOTICE -->

    <div class="funding-review-notice">

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


        <div>

            <strong>
                Paystack activation is still under review
            </strong>

            <p>
                Member requests can be collected safely,
                but live virtual accounts should not be
                created until Complete Bliss receives the
                required Paystack approval.
            </p>

        </div>

    </div>



    <!-- TOOLBAR -->

    <section class="funding-toolbar">


        <div>

            <h2>
                Member Requests
            </h2>

            <p>

                <?= number_format(
                    $totalFundingRequests
                ); ?>

                request<?= $totalFundingRequests === 1
                    ? ''
                    : 's'; ?>

            </p>

        </div>


        <form
            method="GET"
            class="funding-filter"
        >

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

            <select
                name="status"
                onchange="this.form.submit()"
            >

                <option
                    value="all"
                    <?= $selectedStatus === 'all'
                        ? 'selected'
                        : ''; ?>
                >
                    All Requests
                </option>


                <?php foreach (
                    [
                        'pending',
                        'processing',
                        'activated',
                        'failed',
                        'cancelled'
                    ]
                    as
                    $status
                ): ?>

                    <option
                        value="<?= htmlspecialchars(
                            $status
                        ); ?>"
                        <?= $selectedStatus === $status
                            ? 'selected'
                            : ''; ?>
                    >

                        <?= ucfirst(
                            htmlspecialchars(
                                $status
                            )
                        ); ?>

                    </option>

                <?php endforeach; ?>


            </select>

        </form>


    </section>



    <!-- REQUEST LIST -->

    <?php if (!empty($requests)): ?>


        <section class="funding-request-list">


            <?php foreach (
                $requests
                as
                $request
            ): ?>


                <article class="funding-request-card">


                    <div class="funding-member-info">


                        <div class="funding-avatar">

                            <?= htmlspecialchars(
                                strtoupper(
                                    substr(
                                        $request['fullname'],
                                        0,
                                        1
                                    )
                                )
                            ); ?>

                        </div>


                        <div>

                            <h3>

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

                            </h3>


                            <span>

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

                                <?= htmlspecialchars(
                                    $request['phone']
                                ); ?>

                            </span>


                            <span>

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

                                <?= htmlspecialchars(
                                    $request['email']
                                ); ?>

                            </span>

                        </div>


                    </div>



                    <div class="funding-request-details">


                        <div>

                            <span>
                                Requested
                            </span>

                            <strong>

                                <?= date(
                                    'd M Y, h:i A',
                                    strtotime(
                                        $request[
                                            'requested_at'
                                        ]
                                    )
                                ); ?>

                            </strong>

                        </div>


                        <div>

                            <span>
                                Consent
                            </span>

                            <strong>

                                <?= $request['consent_given']
                                    ? 'Yes'
                                    : 'No'; ?>

                            </strong>

                        </div>


                        <div>

                            <span>
                                Wallet Balance
                            </span>

                            <strong>

                                ₦<?= number_format(
                                    $request[
                                        'wallet_balance'
                                    ],
                                    2
                                ); ?>

                            </strong>

                        </div>


                    </div>



                    <div class="funding-request-status">


                        <span
                            class="
                                funding-status
                                funding-status-<?= htmlspecialchars(
                                    $request['status']
                                ); ?>
                            "
                        >

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

                        </span>


                        <?php if (
                            !empty(
                                $request[
                                    'customer_code'
                                ]
                            )
                        ): ?>

                            <small>

                                <?= htmlspecialchars(
                                    $request[
                                        'customer_code'
                                    ]
                                ); ?>

                            </small>

                        <?php endif; ?>


                    </div>


                </article>


            <?php endforeach; ?>


        </section>



        <!-- PAGINATION -->

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


            <nav class="funding-pagination">


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


                    <a
                        href="?status=<?= urlencode(
                            $selectedStatus
                        ); ?>&page=<?= $fundingPage - 1; ?>"
                    >

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

                        Previous

                    </a>


                <?php endif; ?>


                <span>

                    Page

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

                    of

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

                </span>


                <?php if (
                    $fundingPage <
                    $totalFundingPages
                ): ?>


                    <a
                        href="?status=<?= urlencode(
                            $selectedStatus
                        ); ?>&page=<?= $fundingPage + 1; ?>"
                    >

                        Next

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

                    </a>


                <?php endif; ?>


            </nav>


        <?php endif; ?>


    <?php else: ?>


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

            <i class="fas fa-building-columns"></i>

            <h2>
                No Funding Requests
            </h2>

            <p>
                No member funding-account requests
                match this filter yet.
            </p>

        </div>


    <?php endif; ?>


</main>


</div>


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


</body>

</html>