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

$sql = "SELECT * FROM users WHERE role='member' ORDER BY id DESC";
$stmt = $pdo->query($sql);
$members = $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>Members</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">

<h1>Members</h1>

<div class="table-card">

<table class="member-table">

    <thead>

        <tr>

            <th>ID</th>
            <th>Member</th>
            <th>Phone</th>
            <th>Wallet</th>
            <th>Status</th>
            <th>Action</th>

        </tr>

    </thead>

    <tbody>

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

        <tr>

            <td><?= $member['id']; ?></td>

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

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

            <td class="wallet">

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

            </td>

            <td>

                <?php

                if($member['status']=="active"){

                    echo '<span class="badge success">Active</span>';

                }elseif($member['status']=="pending"){

                    echo '<span class="badge warning">Pending</span>';

                }else{

                    echo '<span class="badge danger">Suspended</span>';

                }

                ?>

            </td>

<td class="action-buttons">

<a href="member_details.php?id=<?= $member['id']; ?>" class="success-btn" title="View Member">
<i class="fas fa-eye"></i>
</a>

<a href="fund_wallet.php?id=<?= $member['id']; ?>" class="fund-btn" title="Fund Wallet">
<i class="fas fa-wallet"></i>
</a>

<a href="edit_member.php?id=<?= $member['id']; ?>" class="primary-btn" title="Edit Member">
<i class="fas fa-pen"></i>
</a>

<?php if($member['status']=="active"): ?>

<a href="toggle_member.php?id=<?= $member['id']; ?>" class="delete-btn" title="Suspend Member">
<i class="fas fa-user-slash"></i>
</a>

<?php else: ?>

<a href="toggle_member.php?id=<?= $member['id']; ?>" class="success-btn" title="Activate Member">
<i class="fas fa-user-check"></i>
</a>

<?php endif; ?>

</td>

        </tr>

    <?php endforeach; ?>

    </tbody>

</table>

</div>
</div>

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

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<?php if(isset($_SESSION['success'])): ?>

<script>

Swal.fire({

icon:'success',

title:'Success',

text:'<?= $_SESSION["success"]; ?>',

confirmButtonColor:'#0D6EFD'

});

</script>

<?php unset($_SESSION['success']); endif; ?>


<?php if(isset($_SESSION['error'])): ?>

<script>

Swal.fire({

icon:'error',

title:'Error',

text:'<?= $_SESSION["error"]; ?>',

confirmButtonColor:'#DC2626'

});

</script>

<?php unset($_SESSION['error']); endif; ?>

</body>

</html>