<?php

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

$sql = "SELECT
            rp.id,
            rp.amount,
            rp.receipt,
            rp.status AS payment_status,
            rp.created_at,
            u.id AS user_id,
            u.fullname,
            u.phone,
            u.email
        FROM registration_payments rp
        INNER JOIN users u ON rp.user_id = u.id
        ORDER BY rp.created_at DESC";

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

$registrations = $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>Registration Requests</title>

<link rel="stylesheet" href="../assets/css/admin.css">
<link rel="stylesheet" href="../assets/css/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">

    <div class="page-title">

    <h1>Registration Requests</h1>

    <p>
        Review new member registrations and approve their accounts.
    </p>

</div>

    <div class="table-container">

        <table class="admin-table">

            <thead>

                <tr>

                    <th>Name</th>

                    <th>Phone</th>

                    <th>Email</th>

                    <th>Amount</th>

                    <th>Receipt</th>

                    <th>Date</th>

                    <th>Status</th>

                    <th>Action</th>

                </tr>

            </thead>

            <tbody>

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

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

                <tr>

                    <td><?= htmlspecialchars($row['fullname']) ?></td>

                    <td><?= htmlspecialchars($row['phone']) ?></td>

                    <td><?= htmlspecialchars($row['email']) ?></td>

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

                    <td>

                        <a
class="btn-view"
target="_blank"
href="../assets/uploads/receipts/<?= $row['receipt']; ?>">

<i class="fas fa-eye"></i>&nbsp;View

</a>

                    </td>

                    <td><?= date("d M Y",strtotime($row['created_at'])) ?></td>

                    <td><span class="badge <?= $row['payment_status']; ?>">

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

</span></td>

                    <td>

                        <a class="btn-success"
                        href="approve_registration.php?id=<?= $row['id'] ?>">

                        Approve

                        </a>

                        <a class="btn-danger"
                        href="reject_registration.php?id=<?= $row['id'] ?>">

                        Reject

                        </a>

                    </td>

                </tr>

                <?php endforeach; ?>

            <?php else: ?>

                <tr>

                    <td colspan="8" align="center">

                        No Registration Requests Found.

                    </td>

                </tr>

            <?php endif; ?>

            </tbody>

        </table>

    </div>

</div>

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

</body>

</html>