<?php
// === 0. SET TIMEZONE INDONESIA (WIB) ===
date_default_timezone_set('Asia/Jakarta');

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// === 1. PROTEKSI AKSES LOGIN ===
if (!isset($_SESSION['username']) && !isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// === 2. PANGGIL HEADER TERLEBIH DAHULU ===
include 'template/header.php';

// === 3. DETEKSI OTOMATIS VARIABEL KONEKSI DARI HEADER ===
$db = null;
if (isset($koneksi)) { $db = $koneksi; }
elseif (isset($conn)) { $db = $conn; }
elseif (isset($db_connect)) { $db = $db_connect; }
elseif (isset($link)) { $db = $link; }

// === 4. DAFTAR MASTER PASARAN ===
$master_pasaran = [
    ['key' => 'Sydney Pools', 'param' => 'SYDNEY', 'hari_ket' => '( Setiap Hari )', 'tutup' => '13:35', 'buka' => '13:58'], 
    ['key' => 'Sydney Lotto', 'param' => 'Sydney Lotto', 'hari_ket' => '( Setiap Hari )', 'tutup' => '13:45', 'buka' => '13:57'],
    ['key' => 'Singapore', 'param' => 'SINGAPORE', 'hari_ket' => '(Selasa, Jumat Off)', 'tutup' => '17:30', 'buka' => '17:50'],
    ['key' => 'Hongkong Pools', 'param' => 'HONGKONG POOLS', 'hari_ket' => '( Setiap Hari )', 'tutup' => '22:45', 'buka' => '23:08'],
    ['key' => 'Hongkong lotto', 'param' => 'HK LOTTO', 'hari_ket' => '( Setiap Hari )', 'tutup' => '22:55', 'buka' => '23:17'],
    ['key' => 'CHINA POOLS', 'param' => 'CHINA POOLS', 'hari_ket' => '( Setiap Hari )', 'tutup' => '15:00', 'buka' => '15:35'],
    ['key' => 'JAPAN POOLS', 'param' => 'JAPAN POOLS', 'hari_ket' => '( Setiap Hari )', 'tutup' => '16:50', 'buka' => '17:20'],
    ['key' => 'MAGNUM CAMBODIA', 'param' => 'MAGNUM CAMBODIA', 'hari_ket' => '( Setiap Hari )', 'tutup' => '11:30', 'buka' => '11:57'], 
    ['key' => 'TAIWAN', 'param' => 'TAIWAN', 'hari_ket' => '( Setiap Hari )', 'tutup' => '20:30', 'buka' => '20:58'],
    ['key' => 'CALIFORNIA D4', 'param' => 'CALIFORNIA', 'hari_ket' => '( Setiap Hari )', 'tutup' => '08:25', 'buka' => '08:50'],
    ['key' => 'FLORIDA EVE', 'param' => 'FLORIDA EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '08:30', 'buka' => '08:55'],
    ['key' => 'NEW YORK EVE', 'param' => 'NEW YORK EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '09:15', 'buka' => '09:35'],
    ['key' => 'MISSOURI EVE', 'param' => 'MISSOURI EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '08:45', 'buka' => '09:00'],
    ['key' => 'KENTUCKY EVE', 'param' => 'KENTUCKY EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '09:45', 'buka' => '10:15'],
    ['key' => 'CAROLINA EVE', 'param' => 'CAROLINA EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '10:15', 'buka' => '10:35'],
    ['key' => 'OREGON 7 PM', 'param' => 'OREGON 7 PM', 'hari_ket' => '( Setiap Hari )', 'tutup' => '08:45', 'buka' => '09:00'],
    ['key' => 'OREGON 10 PM', 'param' => 'OREGON 10 PM', 'hari_ket' => '( Setiap Hari )', 'tutup' => '11:45', 'buka' => '12:10'],
    ['key' => 'BULLSEYE', 'param' => 'BULLSEYE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '12:55', 'buka' => '13:15'], 
    ['key' => 'TOTO MACAU 13', 'param' => 'TOTO MACAU 13', 'hari_ket' => '( Setiap Hari )', 'tutup' => '12:55', 'buka' => '13:20'], 
    ['key' => 'TOTO MACAU 16', 'param' => 'TOTO MACAU 16', 'hari_ket' => '( Setiap Hari )', 'tutup' => '15:55', 'buka' => '16:20'], 
    ['key' => 'TOTO MACAU 19', 'param' => 'TOTO MACAU 19', 'hari_ket' => '( Setiap Hari )', 'tutup' => '18:55', 'buka' => '19:20'], 
    ['key' => 'TOTO MACAU 22', 'param' => 'TOTO MACAU 22', 'hari_ket' => '( Setiap Hari )', 'tutup' => '21:55', 'buka' => '22:20'], 
    ['key' => 'Toto Macau 23', 'param' => 'Toto macau 23', 'hari_ket' => '( Setiap Hari )', 'tutup' => '22:55', 'buka' => '23:20'], 
    ['key' => 'Toto Macau 00', 'param' => 'Toto Macau 00', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:01', 'buka' => '00:20'], 
    ['key' => 'GEORGIA C4 MID', 'param' => 'GEORGIA C4 MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '23:15', 'buka' => '23:40'],
    ['key' => 'MARYLAND MID', 'param' => 'MARYLAND MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '23:15', 'buka' => '23:40'],
    ['key' => 'NEW JERSEY MID', 'param' => 'NEW JERSEY MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '23:45', 'buka' => '00:15'], 
    ['key' => 'GERMANY PLUS', 'param' => 'GERMANY PLUS', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:00', 'buka' => '00:25'], 
    ['key' => 'KENTUCKY MID', 'param' => 'KENTUCKY MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:15', 'buka' => '00:40'], 
    ['key' => 'NEW YORK MID', 'param' => 'NEW YORK MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '01:15', 'buka' => '01:40'], 
    ['key' => 'PCSO', 'param' => 'PCSO', 'hari_ket' => '( Setiap Hari )', 'tutup' => '20:00', 'buka' => '20:20'], 
    ['key' => 'MISSOURI MID', 'param' => 'MISSOURI MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:30', 'buka' => '00:55'], 
    ['key' => 'CAROLINA DAY', 'param' => 'CAROLINA DAY', 'hari_ket' => '( Setiap Hari )', 'tutup' => '01:45', 'buka' => '02:15'],
    ['key' => 'OREGON 1 PM', 'param' => 'OREGON 1 PM', 'hari_ket' => '( Setiap Hari )', 'tutup' => '02:30', 'buka' => '03:05'], 
    ['key' => 'MOROCCO 4', 'param' => 'MOROCCO 4', 'hari_ket' => '( Setiap Hari )', 'tutup' => '02:30', 'buka' => '03:05'],
    ['key' => 'ILLINOIS EVE', 'param' => 'ILLINOIS EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '09:15', 'buka' => '09:55'], 
    ['key' => 'ILLINOIS MID', 'param' => 'ILLINOIS MID', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:45', 'buka' => '01:15'],
    ['key' => 'PENNSYLVANIA DAY', 'param' => 'PENNSYLVANIA DAY', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:15', 'buka' => '00:35'], 
    ['key' => 'VIRGINIA DAY', 'param' => 'VIRGINIA DAY', 'hari_ket' => '( Setiap Hari )', 'tutup' => '00:45', 'buka' => '01:05'],
     ['key' => 'PENNSYLVANIA EVE', 'param' => 'PENNSYLVANIA EVE', 'hari_ket' => '( Setiap Hari )', 'tutup' => '05:45', 'buka' => '06:15'],
];


// === 5. AMBIL DATA TERBARU DARI DATABASE ===
$db_data = [];
if ($db) {
    $query = mysqli_query($db, "SELECT * FROM pengeluaran_togel");
    if ($query) {
        while ($row = mysqli_fetch_assoc($query)) {
            // Key-nya menggunakan $row['pasaran'] (yang berisi nilai dari 'param')
            $db_data[$row['pasaran']] = $row; 
        } // <--- Tutup kurung kurawal untuk 'while'
    } // <--- Tutup kurung kurawal untuk 'if ($query)'
} // <--- Tutup kurung kurawal untuk 'if ($db)'

// Ambil jam server saat ini untuk perbandingan
$jam_sekarang = date('H:i');
?>

<style> 
    .grid-container { display: grid; grid-template-columns: 1fr; gap: 12px; margin-top: 10px; }
    @media(min-width: 600px) { .grid-container { grid-template-columns: repeat(2, 1fr); } }
    .card { background-color: #262626; border: 1px solid #3a3a3a; border-radius: 18px; padding: 12px; display: flex; justify-content: space-between; align-items: center; transition: all 0.3s; }
    .card-info { flex: 1; padding-right: 10px; }
    .card-title { color: #f1c40f; font-size: 16px; font-weight: bold; margin-bottom: 4px; }
    .card-day { color: #bbb; font-size: 12px; margin-bottom: 6px; }
    .card-time { font-size: 11px; background-color: #1a1a1a; padding: 4px 8px; border-radius: 4px; display: inline-block; margin-bottom: 8px; color: #aaa; border: 1px solid #333; }
    .card-date { font-size: 12px; color: #aaa; margin-bottom: 10px; }
    
    /* STYLE BUTTON PLAY (GOLD) */
    .btn-play { display: inline-block; width: 90px; text-align: center; padding: 6px 0; font-size: 11px; font-weight: bold; color: #000; background: linear-gradient(to right, #FEDB37, #FDB931, #9f7928); border-radius: 6px; text-decoration: none; text-transform: uppercase; }
    /* STYLE BUTTON CLOSED (GREY) */
    .btn-closed { display: inline-block; width: 90px; text-align: center; padding: 6px 0; font-size: 11px; font-weight: bold; color: #fff; background: #444; border-radius: 6px; text-decoration: none; text-transform: uppercase; cursor: not-allowed; opacity: 0.6; }

    .result-container { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
    .ball-container { display: flex; gap: 4px; }
    .ball { background: radial-gradient(circle, #ffffff 0%, #e0e0e0 100%); color: #000; width: 26px; height: 26px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 13px; box-shadow: inset -2px -2px 4px rgba(0,0,0,0.4), 1px 2px 3px rgba(0,0,0,0.3); } 
    .container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        /* Tambahan CSS untuk mengatur ukuran gambar */
        .container img {
            max-width: 90%;   /* Batasi lebar maksimal 90% layar */
            max-height: 90vh; /* Batasi tinggi maksimal 90% layar */
            object-fit: contain; /* Memastikan gambar tetap proporsional */
        }
</style>

<?php if ($mobile): ?>
    <div style="margin-top: 50px;">
<?php else: ?>
    <div style="margin-top: 170px;">
<?php endif; ?> 
    
<div style="width: 95%; margin: 0 auto 20px auto;"> 
           
           <div style="text-align: center; margin-bottom: 20px; position: relative; top: 20px;">
        <img src="/upload/togel/Gemini_Generated_Image_l0lzgal0lzgal0lz.png" 
             alt="Logo Pasaran" 
             style="max-width: 100%; height: auto; display: block; margin: 0 auto;">
    </div> 

                  <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;">
                    Pasaran Togel
                    <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="margin: 0 auto 15px auto; position: relative; width: 90%; max-width: 400px;">
        <i class="fa fa-search" aria-hidden="true" style="position: absolute; top: 10px; left: 15px; height: 18px; width: 18px; margin: 0; 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%);"></i>
        <input type="text" name="search" class="search" id="searchPasaran" placeholder="Cari Pasaran" style="font-family: Poppins-Regular; font-size: 12px; width: 100%; background-color: #1a1a1a; color: #fff; border: 1px solid #FFD700; border-radius: 25px; padding: 6px 15px 6px 35px; box-sizing: border-box;">
    </div> 
    
    <div class="grid-container" id="lottery-list">
        <?php 
        foreach ($master_pasaran as $pas): 
            $nama_p = $pas['key'];
            
            // Logika Jam Tutup Otomatis
            // Jika jam sekarang >= jam tutup DAN jam sekarang < jam buka, maka pasaran tutup.
            $is_open = true;
            if ($jam_sekarang >= $pas['tutup'] && $jam_sekarang < $pas['buka']) {
                $is_open = false;
            }

            $hari    = isset($db_data[$nama_p]) ? $db_data[$nama_p]['hari'] : date('l');
            $tanggal = isset($db_data[$nama_p]) ? $db_data[$nama_p]['tanggal'] : date('d/m/Y');
            $hasil   = isset($db_data[$nama_p]) ? $db_data[$nama_p]['hasil'] : '----';
            $balls   = str_split($hasil);
        ?>
        <div class="card" style="<?= !$is_open ? 'opacity: 0.8; border-color: #222;' : ''; ?>">
            <div class="card-info">
                <div class="card-title"><?= $nama_p; ?></div>
                <div class="card-day"><?= $pas['hari_ket']; ?></div>
                <div class="card-time">
                    Tutup: <?= $pas['tutup']; ?> | Buka: <?= $pas['buka']; ?> 
                    <span style="font-weight: bold; margin-left: 5px; color: <?= $is_open ? '#2ecc71' : '#e74c3c'; ?>;">
                        <?= $is_open ? '[ OPEN ]' : '[ CLOSED ]'; ?>
                    </span>
                </div>
                <div class="card-date"><?= $hari . ' ' . $tanggal; ?></div>
                
                <?php if ($is_open): ?>
                    <a href="betting.php?pasaran=<?= $pas['param']; ?>" class="btn-play">
                        <i class="fa fa-gamepad"></i> Play
                    </a>
                <?php else: ?>
                    <div class="btn-closed">
                        <i class="fa fa-lock"></i> Closed
                    </div>
                <?php endif; ?>
            </div>
            
            <div class="result-container" onclick="window.location.href='data_pengeluaran.php?pasaran=<?= urlencode($nama_p); ?>'" style="cursor:pointer;">
                <div class="ball-container">
                    <?php foreach ($balls as $b): ?>
                        <div class="ball"><?= $b; ?></div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div> 
        <?php endforeach; ?>
    </div>
</div>
<script>
document.getElementById('searchPasaran').addEventListener('input', function() {
    // 1. Ambil kata kunci pencarian dan ubah ke huruf kecil
    let keyword = this.value.toLowerCase();
    
    // 2. Ambil semua elemen card pasaran di dalam #lottery-list
    let cards = document.querySelectorAll('#lottery-list .card');
    
    cards.forEach(function(card) {
        // 3. Cari elemen judul pasaran di dalam card saat ini
        let titleElement = card.querySelector('.card-title');
        
        if (titleElement) {
            // 4. Ambil teks judul pasaran dan ubah ke huruf kecil
            let namaPasaran = titleElement.textContent.toLowerCase();
            
            // 5. Cek apakah nama pasaran mengandung kata kunci
            if (namaPasaran.includes(keyword)) {
                card.style.display = ""; // Tampilkan card jika cocok
            } else {
                card.style.display = "none"; // Sembunyikan card jika tidak cocok
            }
        }
    });
});
</script>
<?php include 'template/footer.php'; ?>