<main id="main-route">
  <div class="main-content home">

    <!-- ================= SLIDER / BANNER ================= -->
    <section class="home__slider">
      <div class="swiper-container main-slider">
        <div class="swiper-wrapper">
          <?php
          $query = mysqli_query($koneksi, "SELECT * FROM tb_banner WHERE status = 'active'");
          while ($data = mysqli_fetch_array($query)) {
          ?>
            <div class="swiper-slide">
              <a href="#">
                <img
                  src="../uploads/fotobanner/<?php echo $data['gambar'] ?>"
                  alt="Banner Image">
              </a>
            </div>
          <?php } ?>
        </div>
        <div class="swiper-pagination"></div>
      </div>
    </section>
    <!-- ================= END SLIDER ================= -->


    <!-- ================= PAGE WRAPPER ================= -->
    <div class="sgx-slot-page">
      <div class="sgx-container">

        <h5>TOGEL<span class="dot"></span></h5>

        <!-- ================= CATEGORY ================= -->
        <section class="home-category">
          <div class="sgx-container">
            <div class="category-scroll">

              <a class="cat-item" href="#">
                <i class="fas fa-fire"></i>
                <span>Top</span>
              </a>
              <a class="cat-item" href="?page=slot">
                <i class="fas fa-dice"></i>
                <span>Slots</span>
              </a>
              <a class="cat-item active" href="?page=togel">
                <i class="fas fa-hashtag"></i>
                <span>Togel</span>
              </a>
              <a class="cat-item" href="?page=casino">
                <i class="fas fa-chess"></i>
                <span>Kasino</span>
              </a>
              <a class="cat-item" href="?page=arcade">
                <i class="fas fa-gamepad"></i>
                <span>E-Game</span>
              </a>
              <a class="cat-item" href="?page=fishing">
                <i class="fas fa-fish"></i>
                <span>Fishing</span>
              </a>
              <a class="cat-item" href="?page=sportbok">
                <i class="fas fa-futbol"></i>
                <span>Sportbok</span>
              </a>

            </div>
          </div>
        </section>
        <!-- ================= END CATEGORY ================= -->

          <!-- ================= FILTER BAR ================= -->
          <div class="sgx-header-filter">

            <!-- ===== LEFT : PROVIDER FILTER ===== -->
            <div class="sgx-filter-left">

            <?php
            
            $togelList = [  
             [  
                'nama' => 'HONGKONG',  
                'kode' => 'p23530',  
                'logo' => '../../uploads/game_pic/provider/togel/hongkong.jpg'  
              ],  
              [  
                'nama' => '4D-MACAU',  
                'kode' => 'm17',  
                'logo' => '../../uploads/game_pic/provider/togel/totomacau.jpg'  
              ],  
              [  
                'nama' => 'SYDNEY',  
                'kode' => 'p23517',  
                'logo' => '../../uploads/game_pic/provider/togel/sydney.jpg'  
              ],  
              [  
                'nama' => 'SINGAPORE',  
                'kode' => 'p23521',  
                'logo' => '../../uploads/game_pic/provider/togel/singapore.jpg'  
              ],  
              [  
                'nama' => 'OREGON 4',  
                'kode' => 'p23514',  
                'logo' => '../../uploads/game_pic/provider/togel/oregon.jpg'  
              ],  
              [  
                'nama' => 'PSCO',  
                'kode' => 'p23526',  
                'logo' => '../../uploads/game_pic/provider/togel/psco.jpg'  
              ],  
              [  
                'nama' => 'BULLSEYE',  
                'kode' => 'p23516',  
                'logo' => '../../uploads/game_pic/provider/togel/bullseye.jpg'  
              ],  
              [  
                'nama' => 'TAIWAN',  
                'kode' => 'p23527',  
                'logo' => '../../uploads/game_pic/provider/togel/taiwan.jpg'  
              ],  
            ];  
            
            // Ambil provider unik dari $togelList
            $providerList = [];
            foreach ($togelList as $togel) {
                // Gunakan nama sebagai key
                $key = $togel['nama'];
                $providerList[$key] = [
                    'name' => $togel['nama'],
                    'icon' => $togel['logo']
                ];
            }
            
            // Provider aktif dari URL, default TOTO WUHAN
            $id_provider = $_GET['provider'] ?? 'TOTO WUHAN';
            if (!isset($providerList[$id_provider])) {
                $id_provider = array_key_first($providerList); // fallback ke provider pertama
            }
            
            $currentProvider = $providerList[$id_provider];
            ?>
            
            <!-- ================= SELECT PROVIDER ================= -->
            <div class="sgx-filter-left">
            
              <!-- Trigger provider -->
              <div class="sgx-provider-selected">
                <img src="<?= $currentProvider['icon']; ?>" alt="<?= $currentProvider['name']; ?>">
                <span><?= $currentProvider['name']; ?></span>
                <i class="fa fa-chevron-down"></i>
              </div>
            
              <!-- Dropdown / List -->
              <div class="sgx-provider-dropdown">
                <?php foreach ($providerList as $key => $p): ?>
                  <div class="sgx-provider-item" data-provider="<?= $key; ?>">
                    <img src="<?= $p['icon']; ?>" alt="<?= $p['name']; ?>">
                    <span><?= $p['name']; ?></span>
                  </div>
                <?php endforeach; ?>
              </div>
            
            </div>
            </div>

          <!-- ===== RIGHT : LIVE JP + SEARCH ===== -->
          <div class="sgx-filter-right">

            <!-- Live JP Button -->
            <button class="sgx-livejp-btn">
              <span class="sgx-dot"></span> Live JP
            </button>

            <!-- Search Box -->
            <div class="sgx-search-box">
              <input
                type="text"
                id="sgxSearchInput"
                placeholder="Cari game..."
              >
              <button class="sgx-search-btn sgx-search-icon" id="sgxSearchBtn">
                <i class="fa fa-search"></i>
              </button>
            </div>

          </div>
        </div>
          <!-- ================= END FILTER BAR ================= -->


        <!-- ================= TOGEL GRID ================= -->
         <div class="container" id="togelGrid">
          <!-- DATA VIA AJAX -->
        </div>

      </div>
    </div>

  </div>
</main>

<!-- ================= CSS ================= -->
<link
  rel="stylesheet"
  id="templateStyle"
  type="text/css"
  href="luxury-them/custom/css/slot.css?v=2.6.0.0"
>


<!-- ================= SCRIPT ================= -->
<script>
document.addEventListener('DOMContentLoaded', () => {

  const grid = document.getElementById('togelGrid');

  const shimmer = () => {
    grid.innerHTML = '';
    for (let i = 0; i < 4; i++) {
      grid.innerHTML += '<div class="sgx-shimmer"></div>';
    }
  };

  const loadTogel = () => {
    shimmer();

    fetch(`template/page/game/ajax/ajax-togel.php`)
      .then(r => r.text())
      .then(h => {
        grid.innerHTML = h;
        startTogelTimer();
        checkResults(); // jalankan setelah card muncul
      });
  };

  loadTogel();
});
</script>

 <script>
    function checkResults() {
      const cards = document.querySelectorAll(".togel-card");
      
      cards.forEach(card => {
        const title = card.querySelector(".togel-title").textContent;
        const numberEl = card.querySelector(".togel-number");
        
        const mapping = {
          "HONGKONG": "p23530",
          "4D-MACAU": "m17",
          "SYDNEY": "p23517",
          "SINGAPORE": "p23521",
          "OREGON 4": "p23514",
          "PSCO": "p23526",
          "BULLSEYE": "p23516",
          "TAIWAN": "p23527"
        };
        
        const code = mapping[title];
        if (!code) return;
        
        fetch("template/page/game/ajax/check-result.php?code=" + code)
          .then(res => res.json())
          .then(data => {
            if (data.updated) {
              numberEl.textContent = data.result;
            }
          });
      });
    }
</script>

<script>
document.addEventListener('DOMContentLoaded', () => {
  const providerTrigger = document.querySelector('.sgx-provider-selected');
  const providerDropdown = document.querySelector('.sgx-provider-dropdown');

  // toggle dropdown
  providerTrigger.addEventListener('click', () => {
    providerDropdown.classList.toggle('active');
  });


});
</script>



<style>
/* dropdown provider */
.sgx-provider-dropdown {
  display: none;
  position: absolute;
  background: #111;
  border: 1px solid #333;
  margin-top: 6px;
  border-radius: 6px;
  overflow: hidden;
  z-index: 10;
  max-height: 300px;
  overflow-y: auto;
}

.sgx-provider-dropdown.active {
  display: block;
}

.sgx-provider-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.sgx-provider-item:hover {
  background: rgba(255,255,255,0.05);
}

.sgx-provider-item img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
}
</style>