<?php
include("admin/connection.php");
include('include/header.php');
?>
<?php
// Pagination setup
$limit = 3; // Show 3 blogs per page (you can change this)
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max($page, 1); // Avoid negative page
$offset = ($page - 1) * $limit;

// Total blog count
$totalQuery = mysqli_query($con, "SELECT COUNT(*) AS total FROM blog");
$totalRow = mysqli_fetch_assoc($totalQuery);
$total_records = $totalRow['total'];
$total_pages = ceil($total_records / $limit);

// Fetch paginated blog data
$blogs = mysqli_query($con, "SELECT * FROM `blog` ORDER BY id DESC LIMIT $offset, $limit");
?>
<style>
    .th-pagination ul li a.disabled {
        pointer-events: none;
        color: #ccc;
    }

    .th-pagination ul li a.active {
        background-color: #333;
        color: #fff;
    }
</style>
<div class="breadcumb-wrapper" data-bg-src="assets/img/hero/banner2.webp">
    <div class="container">
        <div class="breadcumb-content">
            <h1 class="breadcumb-title">Blog</h1>
            <ul class="breadcumb-menu">
                <li><a href="<?php path(); ?>/index.php">Home</a></li>
                <li>Blog</li>
            </ul>
        </div>
    </div>
</div>
<section class="th-blog-wrapper space-top space-extra-bottom">
    <div class="container">


        <div class="row gy-30 justify-content-center">
            <?php
            while ($row = mysqli_fetch_array($blogs)) {
                $title = $row['title'];
                $description = $row['description'];
                $slug = $row['slug'];
                $Image = $row['Image'];
            ?>
                <div class="col-lg-4 col-md-6">
                    <div class="blog-card">
                        <div class="blog-img">
                            <a href="<?php path(); ?>/blog-details.php?slug=<?php echo $slug; ?>">
                                <img src="<?php path(); ?>/admin/assets/images/blog/<?php echo $Image; ?>" alt="Image">
                            </a>
                        </div>
                        <div class="blog-content">
                            <h3 class="box-title">
                                <a href="<?php path(); ?>/blog-details.php?slug=<?php echo $slug; ?>">
                                    <?php echo htmlspecialchars($title); ?>
                                </a>
                            </h3>
                            <div class="box-text boxslide mb-4">
                                <?php echo htmlspecialchars(strip_tags($description)); ?>
                            </div>
                            <a href="<?php path(); ?>/blog-details.php?slug=<?php echo $slug; ?>" class="th-btn pill style3">
                                Read More
                            </a>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>

        <?php
        // Pagination logic
        $prev_disabled = ($page <= 1) ? 'disabled' : '';
        $next_disabled = ($page >= $total_pages) ? 'disabled' : '';
        ?>

        <div class="th-pagination text-center pt-4">
            <ul>

                <li>
                    <a href="blog.php?page=<?php echo max($page - 1, 1); ?>" class="<?php echo $prev_disabled; ?>">
                        <i class="far fa-arrow-left"></i>
                    </a>
                </li>


                <?php
                $start = max(1, $page - 1);
                $end = min($total_pages, $start + 2);

                for ($i = $start; $i <= $end; $i++) {
                    $active = ($i == $page) ? 'active' : '';
                ?>
                    <li>
                        <a class="<?php echo $active; ?>" href="blog.php?page=<?php echo $i; ?>">
                            <?php echo $i; ?>
                        </a>
                    </li>
                <?php } ?>

                <!-- Next Button -->
                <li>
                    <a href="blog.php?page=<?php echo min($page + 1, $total_pages); ?>" class="next-page <?php echo $next_disabled; ?>">
                        Next <i class="far fa-arrow-right"></i>
                    </a>
                </li>
            </ul>
        </div>

    </div>
</section>
<?php
include("include/footer.php");
?>