<?php
if (isset($_GET['pasaran']) && isset($_GET['tanggal'])) {

  $pasaran = mysqli_real_escape_string($data, $_GET['pasaran']);
  $tanggal = mysqli_real_escape_string($data, $_GET['tanggal']);

  $gpred = "SELECT prediksi_togel.*, pasaran_web.gambar_pasaran FROM prediksi_togel INNER JOIN pasaran_web ON pasaran_web.nama_pasaranweb = pasaran_prediksi WHERE prediksi_togel.pasaran_prediksi = '$pasaran' AND DATE(prediksi_togel.tanggal_prediksi) = '$tanggal'";
  $qpred = mysqli_query($data, $gpred);

  echo '<div class="text-start mb-3">
            <button type="button" onclick="history.back();" class="btn btn-sm btn-warning"><i class="fas fa-arrow-left"></i> Beranda Prediksi</button>
        </div>';

  if (mysqli_num_rows($qpred) > 0) {
    $fpred = mysqli_fetch_assoc($qpred);

    $gpasr = $fpred['pasaran_prediksi'];
    $ggambar = $fpred['gambar_pasaran'];
    $tpred = $fpred['tanggal_prediksi'];
    $duad = $fpred['duad'];
    $tigad = $fpred['tigad'];
    $empatd = $fpred['empatd'];
    $colok_bebas = $fpred['colok_bebas'];
    $colok_duad = $fpred['colok_duad'];
    $colok_shio = $fpred['colok_shio'];
    $angka_main = $fpred['angka_main'];

    $default_gambar = empty($ggambar) ? 'images/icon-market/' . strtolower($gpasr) . '.webp' : $ggambar;

    echo '<h3 class="text-center text-uppercase">' . ftab("link_masukbo", "web_setting", "link_masukbo") . ' PREDIKSI ' . $gpasr . ' TOGEL HARI: ' . date("d M Y", strtotime($tanggal)) . ' </h3>
                    <hr class="w-75 d-block mx-auto">';

    echo '<div class="text-center wrap-pred">
                      <img class="img-fluid rounded my-3" width="120px " height="120px" src="' . $default_gambar . '" />
                        <h5>✅ ANGKA MAIN:</h5>
                        <h4 class="text-warning">( ' . $angka_main . ' )</h4>
                        <h5 class="my-2">TOP 4D (BB) :</h5>
                        <h4 class="text-warning">' . substr($empatd, 0, 29) . '</h4>
                        <h5 class="my-2">TOP 3D (BB) :</h5>
                        <h4 class="text-warning">' . substr($tigad, 0, 27) . '</h4>
                        <h5 class="my-2">TOP 2D (BB) :</h5>
                        <h4 class="text-warning">' . substr($duad, 0, 26) . '</h4>

                        <h5 class="mt-4">COLOK BEBAS :</h5>
                        <h4 class="text-warning">' . $colok_bebas . '</h4>
                        <h5 class="my-2">COLOK 2D :</h5>
                        <h4 class="text-warning">' . $colok_duad . '</h4>

                        <h5 class="my-2">SHIO JITU :</h5>
                        <h4 class="text-warning">' . $colok_shio . '</h4>
                        <img class="img-fluid" src="images/icon/' . $colok_shio . '.png" />
                  </div>';
  } else {
    echo '<p class="text-center text-danger text-uppercase">Mohon maaf Pasaran Togel ' . $pasaran . ' Tanggal ' . date("d M Y", strtotime($tanggal)) . ' belum tersedia!!. </p>
              ';
  }


  echo '<p class="text-center mt-5 text-uppercase">Prediksi ' . $pasaran . ' sebelumnya:</p>
            <hr class="w-75 d-block mx-auto">

            <div class="owl-carousel prediksi-sebelum my-3 owl-theme">';

  $prevpre = "SELECT prediksi_togel.*, pasaran_web.gambar_pasaran FROM prediksi_togel INNER JOIN pasaran_web ON prediksi_togel.pasaran_prediksi = pasaran_web.nama_pasaranweb WHERE prediksi_togel.pasaran_prediksi = '$pasaran' AND DATE(prediksi_togel.tanggal_prediksi) != '$tanggal' ORDER BY DATE(prediksi_togel.tanggal_prediksi) DESC LIMIT 15";
  $qprevpre = mysqli_query($data, $prevpre);

  if (mysqli_num_rows($qprevpre) > 0) {
    while ($fprevpre = mysqli_fetch_assoc($qprevpre)) {
      $titleprev = $fprevpre['pasaran_prediksi'];
      $dateprev = $fprevpre['tanggal_prediksi'];
      $gambarprev = empty($fprevpre['gambar_pasaran']) ? 'images/icon-market/' . $titleprev . '.webp' : $fprevpre['gambar_pasaran'];

      echo '<div class="item">
                            <div class="card">
                             <a href="?page=prediksi-togel&pasaran=' . $titleprev . '&tanggal=' . date("Y-m-d", strtotime($dateprev)) . '"> <img src="' . $gambarprev . '" class="card-img-top" alt="prediksi ' . $titleprev . ' sebelumnya ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '"></a>
                              <div class="card-body">
                               <a href="?page=prediksi-togel&pasaran=' . $titleprev . '&tanggal=' . date("Y-m-d", strtotime($dateprev)) . '"> <h6 class="card-title">Prediksi <span class="text-capitalize">' . $titleprev . '</span> ' . date("d M Y", strtotime($dateprev)) . '</h6></a>
                                <p class="card-text">Prediksi ' . $titleprev . ' sebelumnya pada tanggal ' . date("d M Y", strtotime($dateprev)) . ' di ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '.</p>
                                  <div class="d-grid">
                                      <a href="?page=prediksi-togel&pasaran=' . $titleprev . '&tanggal=' . date("Y-m-d", strtotime($dateprev)) . '" class="btn btn-warning"><i class="fas fa-eye"></i> Lihat Prediksi</a>
                                  </div>
                              </div>
                        </div>
                  </div>';
    }
  }

  echo '</div>';


  echo '<div class="my-3">';
  fwtab("prediksi_texts", "pasaran_web", "prediksi_texts", "nama_pasaranweb", $pasaran);
  echo '</div>';
} else {

  // Start Of Pagination of Prediksi

  $perPage = 30; // Items per page
  $currentPage = isset($_GET['no']) ? intval($_GET['no']) : 1;
  $start = ($currentPage - 1) * $perPage;

  // Fetch data with LIMIT and OFFSET
  $sql = "SELECT COUNT(*) AS total FROM prediksi_togel INNER JOIN pasaran_web ON pasaran_web.nama_pasaranweb = prediksi_togel.pasaran_prediksi ORDER BY DATE(prediksi_togel.tanggal_prediksi) DESC, pasaran_web.id";
  $result = $data->query($sql);
  $row = $result->fetch_assoc();
  $totalItems = $row['total'];

  $spre = "SELECT DISTINCT prediksi_togel.*, pasaran_web.gambar_pasaran FROM prediksi_togel INNER JOIN pasaran_web ON pasaran_web.nama_pasaranweb = prediksi_togel.pasaran_prediksi ORDER BY DATE(prediksi_togel.tanggal_prediksi) DESC, pasaran_web.id LIMIT $start, $perPage";
  $qpre = mysqli_query($data, $spre);

  // Total number of pages
  $totalPages = ceil($totalItems / $perPage);

  $startingNumber = $start + 1;

  $maxVisiblePages = 3; // Total number of page numbers to display
  $visiblePages = min($maxVisiblePages, $totalPages);

  $halfVisible = intval($visiblePages / 2);
  $startPage = max(1, $currentPage - $halfVisible);
  $endPage = min($totalPages, $startPage + $visiblePages - 1);

  if (mysqli_num_rows($qpre) > 0) {

    echo '<h3 class="text-center text-uppercase">PREDIKSI TOGEL HARI INI LENGKAP ' . ftab("link_masukbo", "web_setting", "link_masukbo") . ' ' . date("Y") . '</h3>
          <hr class="w-75 d-block mx-auto">';

    echo '<div class="row align-items-center">
        <div class="col-lg-9 text-light">
        </div>

        <div class="col-lg-3">
            <form>
                <select id="selectprediksi" class="form-select form-select-sm mb-3" aria-label="Select pasaran">
                <option value="0" id="pilihpass" selected>Cari Pasaran</option>';

    $spasran = "SELECT nama_pasaranweb FROM pasaran_web";
    $qpasran = mysqli_query($data, $spasran);

    if (mysqli_num_rows($qpasran) > 0) {
      while ($fpasran = mysqli_fetch_assoc($qpasran)) {
        $pasarn = $fpasran['nama_pasaranweb'];

        echo '<option class="pasaranv" value="' . $pasarn . '">' . $pasarn . '</option>';
      }
    }

    echo '</select>
            </form>
        </div>

            <div class="col-lg-12">
                    <div id="loading"><p>Mohon ditunggu...</p></div>
                    <div id="prediksicontent"></div>
            </div>

    </div>';


    echo '<div id="prediksi-blocks" class="row g-2">';

    if (isset($_GET['pasaran'])) {
      $pasa = mysqli_real_escape_string($data, $_GET['pasaran']);
      $qr = "SELECT pasaran_prediksi, tanggal_prediksi FROM prediksi_togel WHERE pasaran_prediksi = '$pasa' AND DATE(tanggal_prediksi) = '$todaydate' ORDER BY DATE(tanggal_prediksi) DESC LIMIT 1";
      $wr = mysqli_query($data, $qr);

      if (mysqli_num_rows($wr) == 0) {
        $prevpa = "SELECT prediksi_togel.pasaran_prediksi, prediksi_togel.tanggal_prediksi, pasaran_web.gambar_pasaran FROM prediksi_togel INNER JOIN pasaran_web ON prediksi_togel.pasaran_prediksi = pasaran.web_nama_pasaranweb WHERE prediksi_togel.pasaran_prediksi = '$pasa' AND DATE(prediksi_togel.tanggal_prediksi) < '$todaydate' ORDER BY DATE(prediksi_togel.tanggal_prediksi) DESC LIMIT 1";
        $qprevpa = mysqli_query($data, $prevpa);

        $fprevpa = mysqli_fetch_assoc($qprevpa);
        $dprev = $fprevpa['tanggal_prediksi'];

        echo '<div class="col-lg-12">

                        <div class="prediksi-card active-pasaran rounded">
                          <div class="row g-1">
                                <div class="col-lg-3">
                                    <a href="?page=prediksi-togel&pasaran=' . $pasa . '&tanggal=' . date("Y-m-d", strtotime($dprev)) . '"> <img src="images/icon-market/' . $pasa . '.webp" width="163" height="105" class="d-block mx-auto" alt="gambar prediksi ' . $pasa . ' togel akurat bocoran ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '"></a>
                                </div>

                                <div class="col-lg-9 align-self-center">
                                    <a href="?page=prediksi-togel&pasaran=' . $pasa . '&tanggal=' . date("Y-m-d", strtotime($dprev)) . '"><h3 class="prediksi-title mb-1">Prediksi Togel ' . $pasa . ' Pools ' . date("d M Y", strtotime($dprev)) . ' ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '</h3></a>
                                    <small class="text-uppercase"><i class="fas fa-play-circle"></i> Pasang ' . $pasa . ' Pools Togel online</small>
                                    <p class="prediksi-desc my-2">Prediksi Togel ' . $pasa . ' Hari ini Tanggal ' . date("d M Y", strtotime($dprev)) . ' , Bocoran & Info Akurat yang pasti JP Togel Online Dari Bandar ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '. </p>

                                      <div class="d-lg-block float-none float-lg-end d-grid">
                                        <a href="?page=prediksi-togel&pasaran=' . $pasa . '&tanggal=' . date("Y-m-d", strtotime($dprev)) . '" class="btn btn-sm btn-warning fw-bold"><i class="fas fa-eye"></i> Lihat Prediksi</a>
                                      </div>
                                </div>

                          </div>
                          </div>

                  </div>
                  <!-- /Prediksi Blog Card -->';
      }
    }

    while ($fpre = mysqli_fetch_assoc($qpre)) {
      $prediksipas = $fpre['pasaran_prediksi'];
      $datepre = $fpre['tanggal_prediksi'];
      $gambarpas = empty($fpre['gambar_pasaran']) ? 'images/icon-market/' . $prediksipas . '.webp' : $fpre['gambar_pasaran'];

      echo '<!-- Prediksi Blog Card -->

              <div class="col-lg-12 ';

      if (isset($_GET['pasaran'])) {
        $pasa = mysqli_real_escape_string($data, $_GET['pasaran']);
        if ($prediksipas == $pasa) {
          echo 'order-1';
        } else {
          echo 'order-2';
        }
      }


      echo '">

                    <div class="prediksi-card ';

      if (isset($_GET['pasaran'])) {
        $pasa = mysqli_real_escape_string($data, $_GET['pasaran']);
        if ($prediksipas == $pasa) {
          echo 'active-pasaran';
        }
      }


      echo ' rounded">
                      <div class="row g-1">
                            <div class="col-lg-3">
                                <a href="?page=prediksi-togel&pasaran=' . $prediksipas . '&tanggal=' . date("Y-m-d", strtotime($datepre)) . '"> <img src="' . $gambarpas . '" width="163" height="105" class="d-block mx-auto" alt="gambar prediksi ' . $prediksipas . ' togel akurat bocoran ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '"></a>
                            </div>

                            <div class="col-lg-9 align-self-center">
                            <a href="?page=prediksi-togel&pasaran=' . $prediksipas . '&tanggal=' . date("Y-m-d", strtotime($datepre)) . '"><h3 class="prediksi-title mb-1">Prediksi Togel ' . $prediksipas . ' Pools ' . date("d M Y", strtotime($datepre)) . ' ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '</h3></a>
                                <small class="text-uppercase"><i class="fas fa-play-circle"></i> Pasang ' . $prediksipas . ' Pools Togel online</small>
                                <p class="prediksi-desc my-2">Prediksi Togel ' . $prediksipas . ' Hari ini Tanggal ' . date("d F Y", strtotime($datepre)) . ' , Bocoran & Info Akurat yang pasti JP Togel Online Dari Bandar ' . ftab("link_masukbo", "web_setting", "link_masukbo") . '. </p>

                                  <div class="d-lg-block float-none float-lg-end">
                                    <a href="?page=prediksi-togel&pasaran=' . $prediksipas . '&tanggal=' . date("Y-m-d", strtotime($datepre)) . '" class="btn btn-sm btn-warning btn-lihat fw-bold"><i class="fas fa-eye"></i> Lihat Prediksi</a>
                                  </div>
                            </div>

                      </div>
                      </div>

              </div>
              <!-- /Prediksi Blog Card -->';
    }

    echo '<div class="col-12 ';

    if (isset($_GET['pasaran'])) {
      $pasa = mysqli_real_escape_string($data, $_GET['pasaran']);
      if ($prediksipas == $pasa) {
        echo 'order-2';
      } else {
        echo 'order-3';
      }
    }


    echo '">
            <ul class="pagination justify-content-center justify-content-lg-end">';

    if ($currentPage > 1) {
      echo '<li class="page-item"><a class="page-link" href="?page=prediksi-togel&no=' . ($currentPage - 1) . '">&laquo;</a></li>';
    } else {
      echo '<li class="page-item disabled"><a class="page-link" href="#">&laquo;</a></li>';
    }

    if ($totalPages > $maxVisiblePages) {
      if ($startPage > 1) {
        echo '<li class="page-item"><a class="page-link" href="?page=prediksi-togel&no=1">1</a></li>';
        if ($startPage > 2) {
          echo '<li class="page-item disabled"><a class="page-link" href="#">...</a></li>';
        }
      }

      for ($i = $startPage; $i <= $endPage; $i++) {
        echo '<li class="page-item ';
        if ($i === $currentPage) {
          echo 'active';
        }
        echo '"><a class="page-link" href="?page=prediksi-togel&no=' . $i . '">' . $i . '</a></li>';
      }

      if ($endPage < $totalPages) {
        if ($endPage < $totalPages - 1) {
          echo '<li class="page-item disabled"><a class="page-link" href="#">...</a></li>';
        }
        echo '<li class="page-item"><a class="page-link" href="?page=prediksi-togel&no=' . $totalPages . '">' . $totalPages . '</a></li>';
      }
    } else {
      for ($i = 1; $i <= $totalPages; $i++) {
        echo '<li class="page-item ';
        if ($i === $currentPage) {
          echo 'active';
        }
        echo '"><a class="page-link" href="?page=prediksi-togel&no=' . $i . '">' . $i . '</a></li>';
      }
    }

    // Next button
    if ($currentPage < $totalPages) {
      echo '<li class="page-item"><a class="page-link" href="?page=prediksi-togel&no=' . ($currentPage + 1) . '">&raquo;</a></li>';
    } else {
      echo '<li class="page-item disabled"><a class="page-link" href="#">&raquo;</a></li>';
    }

    echo '</ul>
           </div>';

    // End Of Pagination of Prediksi

  } else {
    echo '<div class="col-lg-12">

                <div class="prediksi-card rounded">
                    <p>Tidak ada Prediksi Hari ini!</p>
                </div>
                </div>';
  }
  echo '</div>';
}
