<?php
include("admin/connection.php");
include('include/header.php');
?>
<?php
// Pagination setup
$limit = 6; // 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");
?>
<!--Page Header Start-->
<section class="page-header">
    <div class="page-header__bg" style="background-image: url(<?php path() ?>assets/images/backgrounds/page-header-bg.jpg);">
    </div>
    <div class="page-header__shape-1">
        <img src="<?php path() ?>assets/images/shapes/page-header-shape-1.png" alt="">
    </div>
    <div class="container">
        <div class="page-header__inner">
            <h3>Blog </h3>
            <div class="thm-breadcrumb__inner">
                <ul class="thm-breadcrumb list-unstyled">
                    <li><a href="<?php path() ?>index.php">Home</a></li>
                    <li><span class="fas fa-angle-right"></span></li>
                    <li>Blog </li>
                </ul>
            </div>
        </div>
    </div>
</section>
<!--Page Header End-->

<!--Blog Page Start -->
<section class="blog-page">
    <div class="container">
        <div class="row">
            <?php
            while ($row = mysqli_fetch_array($blogs)) {
                $title = $row['title'];
                $description = $row['description'];
                $slug = $row['slug'];
                $Image = $row['Image'];
            ?>
                <!--Blog One Single Start-->
                <div class="col-xl-4 col-lg-6 col-md-6 wow fadeInLeft" data-wow-delay="100ms">
                    <div class="blog-one__single">
                        <div class="blog-one__single-inner">
                            <div class="blog-one__img-box">
                                <div class="blog-one__img">
                                    <img src="<?php path(); ?>admin/assets/images/blog/<?php echo $Image; ?>" alt="">
                                    <div class="blog-one__plus">
                                        <a href="<?php path(); ?>blogs/<?php echo $slug; ?>"><i class="fas fa-plus"></i></a>
                                    </div>

                                </div>

                            </div>
                            <div class="blog-one__content">

                                <h5 class="blog-one__title"><a href="<?php path(); ?>blogs/<?php echo $slug; ?>">
                                        <?php echo htmlspecialchars($title); ?></a></h5>
                                <p class="blog-one__text"><?php echo html_entity_decode(strip_tags($row['description'])); ?></p>
                            </div>
                            <div class="blog-one__read-more">
                                <a href="<?php path(); ?>blogs/<?php echo $slug; ?>">Read More<span class="fas fa-arrow-right"></span></a>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>
            <!--Blog One Single End-->

            <!--Blog List Pagination-->

            <?php
            // Pagination logic
            $prev_disabled = ($page <= 1) ? 'disabled' : '';
            $next_disabled = ($page >= $total_pages) ? 'disabled' : '';
            ?>
            <div class="blog-list__pagination">
                <ul class="pg-pagination list-unstyled">
                    <li class="prev <?php echo $prev_disabled; ?>">
                        <a class="page-link"
                            href="blog.php?page=<?php echo max($page - 1, 1); ?>"
                            aria-label="Previous">
                            <span aria-hidden="true"><i class="fa fa-arrow-left"></i></span>
                        </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 class="count <?php echo $active; ?>">
                            <a class="page-link" href="blog.php?page=<?php echo $i; ?>">
                                <?php echo $i; ?>
                            </a>
                        </li>
                    <?php } ?>
                    <li class="next <?php echo $next_disabled; ?>">
                        <a class="page-link"
                            href="blog.php?page=<?php echo min($page + 1, $total_pages); ?>"
                            aria-label="Next">
                            <span aria-hidden="true"><i class="fa fa-arrow-right"></i></span>
                        </a>
                    </li>

                </ul>
            </div>
        </div>
    </div>
</section>
<!--Blog Page End -->



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