<?php
require_once __DIR__ . "/../includes/admin_auth.php"; // Must be first
require_once '../includes/db.php';

// ==========================
// ACTION HANDLER
// ==========================
if (isset($_GET['action']) && isset($_GET['id'])) {

    $id = intval($_GET['id']);
    $action = $_GET['action'];

    if ($action === 'delete') {
        $stmt = $conn->prepare("UPDATE users SET status='deleted' WHERE id=?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
    }

    if ($action === 'suspend') {
        $stmt = $conn->prepare("UPDATE users SET status='suspended' WHERE id=?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
    }

    if ($action === 'activate') {
        $stmt = $conn->prepare("UPDATE users SET status='active' WHERE id=?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
    }

    if ($action === 'deactivate') {
        $stmt = $conn->prepare("UPDATE users SET status='deactivated' WHERE id=?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
    }

    header("Location: manage_users.php");
    exit;
}

// ==========================
// FETCH USERS
// ==========================
$result = $conn->query("SELECT * FROM users ORDER BY id DESC");

ob_start();
?>

<div class="main-content">

    <h2 class="page-title">Manage Users</h2>

    <div class="panel">

        <table class="table">

            <thead>
                <tr>
                    <th>Photo</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Account #</th>
                    <th>Balance</th>
                    <th>Currency</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>

            <tbody>

                <?php while ($user = $result->fetch_assoc()): ?>

                    <tr>

                        <td>
                            <img src="../uploads/profile_photos/<?= $user['profile_photo'] ?>"
                                 style="width:40px;height:40px;border-radius:50%;">
                        </td>

                        <td><?= htmlspecialchars($user['full_name']) ?></td>
                        <td><?= htmlspecialchars($user['email']) ?></td>
                        <td><?= $user['account_number'] ?></td>

                        <td>
                            <?= $user['currency'] ?>
                            <?= number_format($user['balance'], 2) ?>
                        </td>

                        <td><?= $user['currency'] ?></td>

                        <td>
                            <span class="status <?= $user['status'] ?>">
                                <?= ucfirst($user['status']) ?>
                            </span>
                        </td>

                        <td>

                            <a class="btn-small" href="edit_user.php?id=<?= $user['id'] ?>">Edit</a>

                            <a class="btn-small warn"
                               href="?action=suspend&id=<?= $user['id'] ?>">Suspend</a>

                            <a class="btn-small success"
                               href="?action=activate&id=<?= $user['id'] ?>">Activate</a>

                            <a class="btn-small danger"
                               href="?action=delete&id=<?= $user['id'] ?>"
                               onclick="return confirm('Delete this user?')">Delete</a>

                        </td>

                    </tr>

                <?php endwhile; ?>

            </tbody>

        </table>

    </div>

</div>

<?php
$content = ob_get_clean();
$page_title = "Manage Users";
include '../components/admin_layout.php';
?>