<?php
include("admin/connection.php");
include('include/header.php');
?>
<?php
// --- PAGINATION SETUP ---
$limit = 6; // items per page
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$page = max($page, 1); // avoid < 1
$offset = ($page - 1) * $limit;

// get total records
$totalQuery = mysqli_query($con, "SELECT COUNT(*) AS total FROM blog");
$totalRow = mysqli_fetch_assoc($totalQuery);
$total_records = (int) $totalRow['total'];
$total_pages = ($total_records > 0) ? (int) ceil($total_records / $limit) : 1;

// fetch only the rows for current page
$blogs = mysqli_query($con, "SELECT * FROM blog ORDER BY id DESC LIMIT $offset, $limit");
?>


<main class="main">

    <!-- breadcrumb -->
    <div class="site-breadcrumb" style="background: url(assets/img/breadcrumb/01.jpg)">
        <div class="container">
            <h2 class="breadcrumb-title">Our Blog</h2>
            <ul class="breadcrumb-menu">
                <li><a href="index.php">Home</a></li>
                <li class="active">Our Blog</li>
            </ul>
        </div>
    </div>
    <!-- breadcrumb end -->


    <!-- blog-area -->
    <div class="blog-area py-120">
        <div class="container">

            <div class="row g-4">
                <?php
                if ($blogs && mysqli_num_rows($blogs) > 0) {
                    while ($row = mysqli_fetch_assoc($blogs)) {
                        $title = htmlspecialchars($row['title']);
                        $description = htmlspecialchars($row['description']);
                        $slug = urlencode($row['slug']);
                        $Image = htmlspecialchars($row['Image']);
                ?>
                        <div class="col-md-6 col-lg-4">
                            <div class="blog-item wow fadeInUp" data-wow-delay=".25s">
                                <div class="blog-item-img">
                                    <img src="<?php path(); ?>admin/assets/images/blog/<?php echo $Image; ?>" alt="Thumb">
                                </div>

                                <div class="blog-item-info">
                                    <h4 class="blog-title">
                                        <a href="<?php echo path(); ?>blogs/<?php echo $slug; ?>"><?php echo $title; ?></a>
                                    </h4>
                                    <p class="para">
                                        <?php echo htmlspecialchars(strip_tags($row['description'])); ?>
                                    </p>
                                </div>

                                <a class="theme-btn" href="<?php echo path(); ?>blogs/<?php echo $slug; ?>">
                                    Read More <i class="fas fa-arrow-right"></i>
                                </a>
                            </div>
                        </div>
                    <?php
                    }
                } else {
                    // No posts found
                    ?>
                    <div class="col-12">
                        <p class="text-center">No blog posts found.</p>
                    </div>
                <?php } ?>
            </div>
            <!-- pagination -->
            <!-- pagination -->
            <?php
            // disable classes for bootstrap pagination
            $prev_disabled = ($page <= 1) ? 'disabled' : '';
            $next_disabled = ($page >= $total_pages) ? 'disabled' : '';
            ?>

            <div class="pagination-area mt-4">
                <ul class="pagination justify-content-center">
                    <!-- Previous -->
                    <li class="page-item <?php echo $prev_disabled; ?>">
                        <a class="page-link" href="blog.php?page=<?php echo max($page - 1, 1); ?>" aria-label="Previous" <?php echo ($prev_disabled ? 'tabindex="-1" aria-disabled="true"' : ''); ?>>
                            <i class="fas fa-arrow-left"></i>
                        </a>
                    </li>

                    <!-- Page Numbers: show full range (or you can change to a windowed range) -->
                    <?php for ($i = 1; $i <= $total_pages; $i++):
                        $active = ($i == $page) ? 'active' : '';
                    ?>
                        <li class="page-item <?php echo $active; ?>">
                            <a class="page-link" href="blog.php?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                        </li>
                    <?php endfor; ?>

                    <!-- Next -->
                    <li class="page-item <?php echo $next_disabled; ?>">
                        <a class="page-link" href="blog.php?page=<?php echo min($page + 1, $total_pages); ?>" aria-label="Next" <?php echo ($next_disabled ? 'tabindex="-1" aria-disabled="true"' : ''); ?>>
                            <i class="fas fa-arrow-right"></i>
                        </a>
                    </li>
                </ul>
            </div>



            <!-- pagination end -->
        </div>
    </div>
    <!-- blog-area end -->

</main>



<?php
include("include/footer.php");
?>