<?php 
session_start();
include 'template/header.php'; 
require_once 'db/koneksi.php'; // Menggunakan require_once sesuai struktur history Anda

// Tangkap nama pasaran dari klik bola di depan
$pasaran_req = isset($_GET['pasaran']) ? trim($_GET['pasaran']) : 'Sydney Pools';

// Ambil riwayat angka dari tabel pengeluaran_togel menggunakan variabel $koneksi
$p_safe = mysqli_real_escape_string($koneksi, $pasaran_req);
$sql = "SELECT * FROM pengeluaran_togel WHERE pasaran = '$p_safe' ORDER BY id DESC LIMIT 50";
$query = mysqli_query($koneksi, $sql);
?>

<div style="margin-top: 50px; font-family: sans-serif;">
    <div style="padding: 21px 0; background-color:var(--secondary-color);">
        
        <div style="width: 95%; margin: 0 auto 20px auto;">
                <div
                    style="font-family: Teko-SemiBold; text-align: center; font-size: 24px; color: transparent !important; -webkit-background-clip: text; background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); padding: 10px 0;">
                    Result Terahkir <br> <?= htmlspecialchars($pasaran_req); ?>
                    <hr
                        style="background: linear-gradient(#000 0 0) padding-box, linear-gradient(to left, #FEDB37, #8A6E2F) border-box; border-top: 2px solid transparent; margin: 0 auto; width: 70px;">
                </div>

        <div style="width: 90%; margin: 0 auto 20px auto; display: block; overflow: auto;">
            <table width="100%" style="font-size: 13px; border-collapse: collapse; text-align: center; color: #fff; background-color: #1a1a1a; border: 1px solid #333;">
                <thead>
                    <tr style="background-color: #494848; color: #FEDB37; font-weight: bold; height: 45px;">
                        <td style="padding: 10px; border: 1px solid #333;">HARI</td>
                        <td style="padding: 10px; border: 1px solid #333;">TANGGAL</td>
                        <td style="padding: 10px; border: 1px solid #333;">NOMOR RESULT</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if ($query && mysqli_num_rows($query) > 0) {
                        while ($row = mysqli_fetch_assoc($query)) {
                            // Memecah 4 digit angka menjadi bola-bola kecil
                            $digits = str_split($row['hasil']);
                            
                            echo "<tr style='border-bottom: 1px solid #333; height: 55px;'>";
                            echo "<td style='border: 1px solid #333;'>".htmlspecialchars($row['hari'])."</td>";
                            echo "<td style='border: 1px solid #333;'>".htmlspecialchars($row['tanggal'])."</td>";
                            echo "<td style='border: 1px solid #333;'>";
                            
                            // Render bola angka
                            foreach ($digits as $d) {
                                echo "<span style='display: inline-block; width: 28px; height: 28px; background: radial-gradient(circle, #fff 0%, #ccc 100%); color: #000; border-radius: 50%; font-weight: bold; line-height: 28px; text-align: center; margin: 0 3px; box-shadow: 1px 1px 3px rgba(0,0,0,0.5);'>$d</span>";
                            }
                            
                            echo "</td>";
                            echo "</tr>";
                        }
                    } else {
                        echo "<tr><td colspan='3' style='padding: 30px; color: #888;'>Belum ada data result untuk pasaran ini.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'template/footer.php'; ?>