<div class="title-mb-prediksi">
    <h2 class="text-uppercase">PREDIKSI TOGEL HARI INI LENGKAP <?php echo ftab("link_masukbo", "web_setting", "link_masukbo") . date(" Y") ?></h2>
</div>

<?php
echo '<div class="row mt-3 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>';
?>

<div id="prediksi-blocks">
    <?php
    if (isset($_GET['pre'])) {
        $p = mysqli_real_escape_string($data, $_GET['pre']);

        $perPage = 20; // 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 WHERE pasaran_prediksi = '$p'";
        $result = $data->query($sql);
        $row = $result->fetch_assoc();
        $totalItems = $row['total'];


        $spre = "SELECT pasaran_prediksi, tanggal_prediksi FROM prediksi_togel WHERE pasaran_prediksi = '$p' ORDER BY tanggal_prediksi DESC 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) {
            while ($fpre = mysqli_fetch_assoc($qpre)) {

                echo '<div class="p-prediksi my-3">
                    <a class="d-flex justify-content-between" href="?page=prediksi-togel&pasaran=' . $fpre['pasaran_prediksi'] . '&tanggal=' . date("Y-m-d", strtotime($fpre['tanggal_prediksi'])) . '">
                        <span class="text-uppercase">PREDIKSI TOGEL ' . $fpre['pasaran_prediksi'] . ' <small>(' . date("d/m/Y", strtotime($fpre['tanggal_prediksi'])) . ')</small> </span>
                        <i class="fas fa-chevron-right mt-1"></i>
                    </a>
                    </div>';
            }

            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-mobile&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-mobile&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-mobile&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-mobile&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-mobile&no=' . $i . '">' . $i . '</a></li>';
                }
            }

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

            echo '</ul>';
        }
    } else {

        $perPage = 20; // 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";
        $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) {
            while ($fpre = mysqli_fetch_assoc($qpre)) {

                echo '<div class="p-prediksi my-3">
                    <a class="d-flex justify-content-between" href="?page=prediksi-togel&pasaran=' . $fpre['pasaran_prediksi'] . '&tanggal=' . date("Y-m-d", strtotime($fpre['tanggal_prediksi'])) . '">
                        <span class="text-uppercase">PREDIKSI TOGEL ' . $fpre['pasaran_prediksi'] . ' <small>(' . date("d/m/Y", strtotime($fpre['tanggal_prediksi'])) . ')</small> </span>
                        <i class="fas fa-chevron-right mt-1"></i>
                    </a>
                    </div>';
            }

            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-mobile&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-mobile&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-mobile&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-mobile&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-mobile&no=' . $i . '">' . $i . '</a></li>';
                }
            }

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

            echo '</ul>';
        }
    }

    ?>
</div>