<?php
session_start();
include '../config/database.php';
if ($_SESSION['role'] !== 'admin') { exit; }

// Logika Ubah Status
if (isset($_GET['upgrade'])) {
    $id = $_GET['upgrade'];
    mysqli_query($conn, "UPDATE users SET status_akun = 'pro' WHERE id = $id");
    header("Location: users.php");
}
if (isset($_GET['downgrade'])) {
    $id = $_GET['downgrade'];
    mysqli_query($conn, "UPDATE users SET status_akun = 'free' WHERE id = $id");
    header("Location: users.php");
}

$users = mysqli_query($conn, "SELECT * FROM users WHERE role = 'siswa'");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Kelola Siswa</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-5 bg-light">
    <div class="container card p-4 shadow-sm border-0">
        <h3>Daftar Siswa</h3>
        <table class="table mt-4">
            <thead>
                <tr>
                    <th>Nama</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php while($u = mysqli_fetch_assoc($users)): ?>
                <tr>
                    <td><?php echo $u['nama']; ?></td>
                    <td><span class="badge bg-<?php echo ($u['status_akun'] == 'pro' ? 'success' : 'secondary'); ?>"><?php echo $u['status_akun']; ?></span></td>
                    <td>
                        <?php if($u['status_akun'] == 'free'): ?>
                            <a href="?upgrade=<?php echo $u['id']; ?>" class="btn btn-sm btn-success">Jadikan PRO</a>
                        <?php else: ?>
                            <a href="?downgrade=<?php echo $u['id']; ?>" class="btn btn-sm btn-danger">Hapus PRO</a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
        <a href="index.php">Kembali ke Dashboard</a>
    </div>
</body>
</html>