<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>

        <!-- Swiper Control -->
        <div class="swiper-pagination"></div>
      </div>
  </section>
    <!-- ================= END SLIDER ================= -->


    <!-- ================= SLOT PAGE WRAPPER ================= -->
    <div id="sgx-slot-wrapper">

      <div class="sgx-slot-page">
        <div class="sgx-container">
          
                    <!-- Title -->
          <h5>FISHING GAME<span class="dot"></span></h5>

    <!-- ================= HOME 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" href="?page=togel">
             <i class="fas fa-sort-numeric-up"></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 active" 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
              // Provider aktif dari URL
              $id_provider = $_GET['provider'] ?? 'JILI';

              // Daftar provider (whitelist / aman)
              $provider_map = [
                'JILI' => ['name' => 'JILI', 'icon' => 'p-jl.png']
              ];

              // Fallback provider
              if (!isset($provider_map[$id_provider])) {
                $id_provider = 'CQ9';
              }

              $current_provider = $provider_map[$id_provider];
              ?>

              <!-- Provider Trigger -->
              <div class="sgx-provider-selected">
                <img src="../uploads/game_pic/provider/<?= $current_provider['icon']; ?>">
                <span><?= $current_provider['name']; ?></span>
                <i class="fa fa-chevron-down"></i>
              </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 ================= -->

          <!-- ================= GAME GRID ================= -->
          <div class="sgx-game-grid" id="gameGrid">
            <!-- DATA VIA AJAX -->
          </div>

        </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"
>

<!-- ================= PROVIDER BOTTOM SHEET ================= -->
<div class="sgx-provider-sheet" id="providerSheet">

  <div class="sgx-sheet-overlay"></div>

  <div class="sgx-sheet-content">

    <div class="sgx-sheet-header">
      <span>Penyedia</span>
      <button class="sgx-sheet-close">&times;</button>
    </div>

    <div class="sgx-sheet-body">
      <?php foreach ($provider_map as $key => $provider): ?>
        <label class="sgx-sheet-item">
          <input
            type="radio"
            name="provider"
            value="<?= $key; ?>"
            <?= $key == $id_provider ? 'checked' : ''; ?>
          >
          <span class="sgx-sheet-name"><?= $provider['name']; ?></span>
          <img src="../uploads/game_pic/provider/<?= $provider['icon']; ?>">
        </label>
      <?php endforeach; ?>
    </div>

    <div class="sgx-sheet-footer">
      <button class="sgx-reset">Atur ulang</button>
      <button class="sgx-confirm">Konfirmasi</button>
    </div>

  </div>
</div>

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

  const grid        = document.getElementById('gameGrid');
  const sheet       = document.getElementById('providerSheet');
  const overlay     = document.querySelector('.sgx-sheet-overlay');
  const closeBtn    = document.querySelector('.sgx-sheet-close');
  const confirmBtn  = document.querySelector('.sgx-confirm');
  const resetBtn    = document.querySelector('.sgx-reset');
  const radios      = document.querySelectorAll('input[name="provider"]');
  const selected    = document.querySelector('.sgx-provider-selected');
  const searchInput = document.getElementById('sgxSearchInput');

  let currentProvider = '<?= $id_provider ?>';

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

  /* ===== LOAD GAME VIA AJAX ===== */
  const loadGames = (provider) => {
    currentProvider = provider;
    shimmer();

    fetch(`template/page/game/ajax/ajax-fishing.php?provider=${provider}`)
      .then(r => r.text())
      .then(h => {
        grid.innerHTML = h;
        applySearch();
      });
  };

  /* Load awal */
  loadGames(currentProvider);

  /* ===== OPEN / CLOSE BOTTOM SHEET ===== */
  selected.addEventListener('click', () => {
    sheet.style.display = 'block';
  });

  const closeSheet = () => {
    sheet.style.display = 'none';
  };

  overlay.onclick = closeBtn.onclick = closeSheet;

  /* ===== RESET PROVIDER ===== */
  resetBtn.onclick = () => {
    radios.forEach(r => r.checked = false);
  };

  /* ===== CONFIRM PROVIDER ===== */
  confirmBtn.onclick = () => {
    const checked = document.querySelector('input[name="provider"]:checked');
    if (!checked) return;

    const label = checked.closest('.sgx-sheet-item');

    selected.innerHTML = `
      ${label.querySelector('img').outerHTML}
      <span>${label.querySelector('.sgx-sheet-name').innerText}</span>
      <i class="fa fa-chevron-down"></i>
    `;

    loadGames(checked.value);
    closeSheet();
  };

  /* ===== SEARCH GAME ===== */
  const applySearch = () => {
    const keyword = searchInput.value.toLowerCase();
    document.querySelectorAll('.sgx-game-card').forEach(card => {
      const name = card.dataset.name || '';
      card.style.display = name.includes(keyword) ? '' : 'none';
    });
  };

  searchInput.addEventListener('input', applySearch);

});
</script>