<?php
session_start();
require_once "config/db.php";

$sql= "SELECT username, score
FROM users
ORDER BY score DESC
LIMIT 8";
$nama = $_SESSION['username'];
$score = 0;

$getscore = "SELECT score FROM users WHERE username ='$nama'";
$result_me = mysqli_query($db,$getscore);

if ($result_me && mysqli_num_rows($result_me)>0){
    $row = mysqli_fetch_assoc($result_me);
    $score = $row['score'];
}

$queryrank = "SELECT COUNT(*) as ranking FROM users WHERE score > $score";
$resultrank = mysqli_query($db, $queryrank);
$rowrank = mysqli_fetch_assoc($resultrank);
$realrank = $rowrank['ranking']+1;

$result = mysqli_query($db, $sql);
$leaderboard = [];

if($result){
    $leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
}

?>

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>codebeater</title>
        <link rel="stylesheet" href="css/global.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <audio id="bgm" src="/assets/music/leaderboard.mp3" loop autoplay></audio>
        <div class="container-board">
        <a class="back" href="onboard.php" data-aos="fade-in" data-aos-duration="3000">✖</a>            
            <div class="chara">
                <div class="character">
                    <h1 style="text-align: center; padding-right: 45px; color: yellow;">Knight</h1>
                    <img src="/assets/Design/KnightPixLE2-exporgift.gif" alt="knight">
                </div>
            </div>
            <div class="board">
                <table>
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Username</th>
                            <th>Score</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $peringkat = 1;
                        if (!empty($leaderboard)) {
                        foreach ($leaderboard as $pemain) :
                    ?>
                        <tr>
                            <td><?php echo $peringkat; ?></td>
                            <td><?php echo htmlspecialchars($pemain['username']); ?></td>
                            
                            <td><?php echo $pemain['score']; ?> PTS</td>
                        </tr>

                    <?php 
                        $peringkat++;
                        endforeach;
                    } else {
                        // Jika data kosong
                        echo '<tr><td colspan="3" style="text-align: center;">Belum ada pemain</td></tr>';
                    }
                    ?>
                    </tbody>
                    <tbody>
                        <?php 
                        $peringkat = 1;
                        
                    ?>
                    </tbody>
                </table>
                <table>
                    <tbody>
                        <tr>
                            <td><?php echo $realrank; ?></td>
                            <td><?php echo $nama; ?></td>     
                            <td><?php echo $score; ?> PTS</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <script>
        document.addEventListener("DOMContentLoaded", () => {
            const bgm = document.getElementById("bgm");
            bgm.loop = true
            bgm.volume = 0.2
            bgm.play()

            
        });
    </script>
    </body>

    </html>