<?php 
session_start();
if (!isset($_SESSION['admin'])) {
    header('Location: login.php');
    exit();
}

include 'db.php';

// Fetch leaderboard data and join with user data
$sql = "
    SELECT l.id, u.FullName, u.Username, l.game_title, l.score, l.experience_points, l.level, l.mission, l.date_played
    FROM leaderboard l
    JOIN users u ON l.user_id = u.UserID
    ORDER BY l.score DESC
";
$result = $conn->query($sql);

// Delete leaderboard entry
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $deleteSql = "DELETE FROM leaderboard WHERE id = $id";
    if ($conn->query($deleteSql) === TRUE) {
        echo "<div class='alert alert-success'>Leaderboard entry deleted successfully!</div>";
    } else {
        echo "<div class='alert alert-danger'>Error: " . $conn->error . "</div>";
    }
}

// Handle leaderboard update
if (isset($_POST['update_leaderboard'])) {
    $id = $_POST['id'];
    $score = $_POST['score'];
    $experience_points = $_POST['experience_points'];
    $level = $_POST['level'];
    $mission = $_POST['mission'];

    $updateSql = "UPDATE leaderboard SET score = '$score', experience_points = '$experience_points', level = '$level', mission = '$mission' WHERE id = $id";
    if ($conn->query($updateSql) === TRUE) {
        echo "<div class='alert alert-success'>Leaderboard entry updated successfully!</div>";
    } else {
        echo "<div class='alert alert-danger'>Error: " . $conn->error . "</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Leaderboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f0f4f8;
        }
        .sidebar {
            background-color: #343a40;
            min-height: 100vh;
            padding-top: 20px;
        }
        .sidebar a {
            color: #fff;
            text-decoration: none;
            padding: 10px;
            display: block;
            border-radius: 5px;
            margin: 5px;
            transition: background-color 0.3s;
        }
        .sidebar a:hover {
            background-color: #007bff;
        }
        .content {
            margin-left: 250px;
            padding: 30px;
        }
        h2 {
            color: #343a40;
        }
    </style>
</head>
<body>
    <div class="d-flex">
        <!-- Sidebar -->
        <div class="sidebar col-md-2">
            <h3 class="text-center text-white">Admin Panel</h3>
            <a href="index.php">Dashboard</a>
            <a href="games.php">Manage Games</a>
            <a href="game_categories.php">Manage Categories</a>
            <a href="leaderboard.php">Leaderboard</a>
            <a href="events.php">Manage Events</a>
            <a href="profile_management.php">Profile Management</a>
            <a href="logout.php">Logout</a>
        </div>

        <!-- Main Content -->
        <div class="content col-md-9">
            <div class="container py-5">
                <h2>Leaderboard</h2>
                <p>Here you can manage the leaderboard.</p>

                <!-- Leaderboard Table -->
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>User</th>
                            <th>Game Title</th>
                            <th>Score</th>
                            <th>Experience Points</th>
                            <th>Level</th>
                            <th>Mission</th>
                            <th>Date Played</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        if ($result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) { 
                        ?>
                        <tr>
                            <td><?php echo $row['FullName']; ?> (<?php echo $row['Username']; ?>)</td>
                            <td><?php echo $row['game_title']; ?></td>
                            <td><?php echo $row['score']; ?></td>
                            <td><?php echo $row['experience_points']; ?></td>
                            <td><?php echo $row['level']; ?></td>
                            <td><?php echo $row['mission']; ?></td>
                            <td><?php echo $row['date_played']; ?></td>
                            <td>
                                <!-- Edit Button -->
                                <button class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editModal<?php echo $row['id']; ?>">Edit</button>
                                
                                <!-- Delete Button -->
                                <a href="leaderboard.php?delete=<?php echo $row['id']; ?>" class="btn btn-danger">Delete</a>
                            </td>
                        </tr>

                        <!-- Edit Modal -->
                        <div class="modal fade" id="editModal<?php echo $row['id']; ?>" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="editModalLabel">Edit Leaderboard Entry</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body">
                                        <form method="post" action="leaderboard.php">
                                            <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                                            <div class="mb-3">
                                                <label for="score" class="form-label">Score</label>
                                                <input type="number" class="form-control" id="score" name="score" value="<?php echo $row['score']; ?>" required>
                                            </div>
                                            <div class="mb-3">
                                                <label for="experience_points" class="form-label">Experience Points</label>
                                                <input type="number" class="form-control" id="experience_points" name="experience_points" value="<?php echo $row['experience_points']; ?>" required>
                                            </div>
                                            <div class="mb-3">
                                                <label for="level" class="form-label">Level</label>
                                                <input type="number" class="form-control" id="level" name="level" value="<?php echo $row['level']; ?>" required>
                                            </div>
                                            <div class="mb-3">
                                                <label for="mission" class="form-label">Mission</label>
                                                <input type="text" class="form-control" id="mission" name="mission" value="<?php echo $row['mission']; ?>" required>
                                            </div>
                                            <button type="submit" name="update_leaderboard" class="btn btn-success">Update</button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <?php 
                            }
                        } else {
                            echo "<tr><td colspan='8'>No leaderboard data available</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS and dependencies -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
