<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    require_once "../includes/config.php";

    // Top 10 bets
    $stmt = $conn->prepare("SELECT b.bet, u.username FROM bets b JOIN users u ON b.uid = u.uid ORDER BY b.bet DESC LIMIT 10;");
    $stmt->execute();
    $result = $stmt->get_result(); 
    $bets = $result->fetch_all(MYSQLI_ASSOC);
    $stmt->close();

    // Top 10 wins
    $stmt = $conn->prepare("SELECT b.win, u.username FROM wins b JOIN users u ON b.uid = u.uid ORDER BY b.win DESC LIMIT 10;");
    $stmt->execute();
    $result = $stmt->get_result();
    $wins = $result->fetch_all(MYSQLI_ASSOC);
    $stmt->close();

    // Top 10 users by money
    $stmt = $conn->prepare("SELECT * FROM users ORDER BY money DESC LIMIT 10");
    $stmt->execute();
    $result = $stmt->get_result();
    $users = $result->fetch_all(MYSQLI_ASSOC);
    $stmt->close();

?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Hit and Run — Leaderboard</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body style="width: 100%;">
    <div class="container w80">
        <div class="header">
            <div class="logo">🂡</div>
            <div class="title">
                <h2>Hit Or Run — Leaderboard</h2>
            </div>
        </div>

        <div class="board mb16">
            <div class="panel">
                <h3>Top Rich Mans</h3>
                <div id="leaderboard-wrap">
                    <table id="leaderboard">
                        <thead>
                            <tr>
                                <th class="rank">#</th>
                                <th>Player</th>
                                <th>Earnings</th>
                            </tr>
                        </thead>
                        <tbody id="board-body">
                            <?php foreach ($users as $index => $user): ?>
                                <tr>
                                    <td><?= $index + 1 ?></td>
                                    <td><?= htmlspecialchars($user['username']) ?></td>
                                    <td><?= htmlspecialchars($user['money']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="board mb16">
            <div class="panel">
                <h3>Top Crazy Mans</h3>
                <div id="leaderboard-wrap">
                    <table id="leaderboard">
                        <thead>
                            <tr>
                                <th class="rank">#</th>
                                <th>Player</th>
                                <th>Bets</th>
                            </tr>
                        </thead>
                        <tbody id="board-body">
                            <?php foreach ($bets as $index => $bet): ?>
                                <tr>
                                    <td><?= $index + 1 ?></td>
                                    <td><?= htmlspecialchars($bet['username']) ?></td>
                                    <td><?= htmlspecialchars($bet['bet']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="board mb16">
            <div class="panel">
                <h3>Top Lucky Mans</h3>
                <div id="leaderboard-wrap">
                    <table id="leaderboard">
                        <thead>
                            <tr>
                                <th class="rank">#</th>
                                <th>Player</th>
                                <th>Earnings</th>
                            </tr>
                        </thead>
                        <tbody id="board-body">
                            <?php foreach ($wins as $index => $win): ?>
                                <tr>
                                    <td><?= $index + 1 ?></td>
                                    <td><?= htmlspecialchars($win['username']) ?></td>
                                    <td><?= htmlspecialchars($win['win']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <footer>© 2025 Hit Or Run Leaderboard</footer>
    </div>
</body>
</html>
