<!DOCTYPE html>
<html lang="en">

<head>
    <?php include("include/meta.php");?>
    <?php include("include/css.php");?>
</head>

<body>

    <!-- header area -->
    <?php include("include/header.php");?>
    <!-- header area end -->


    <main class="main">

        <!-- breadcrumb -->
        <?php
            $banner_sql = "SELECT * FROM data_table WHERE up_by = $x && page_type = 'slider' && recycle = '1' ORDER BY id DESC";
            $banner_run = mysqli_query($con, $banner_sql);
            if ($banner_row = mysqli_fetch_assoc($banner_run)) {
            ?>
        <div class="site-breadcrumb" style="background: url(./login/upload/<?php echo $banner_row['image_one']?>)">
            <div class="container">
                <h2 class="breadcrumb-title">Our Blog</h2>
                <ul class="breadcrumb-menu">
                    <li><a href="index.html">Home</a></li>
                    <li class="active">Our Blog</li>
                </ul>
            </div>
        </div>
        <?php }?>
        <!-- breadcrumb end -->


        <!-- blog area -->
        <?php
            // Pagination setup
            $limit = 12; // number of blogs per page
            $page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
            $offset = ($page - 1) * $limit;

            // Count total blogs
            $count_sql = "SELECT COUNT(*) as total FROM data_table WHERE up_by = '$x' AND page_type = 'blog' AND recycle = '1'";
            $count_run = mysqli_query($con, $count_sql);
            $count_row = mysqli_fetch_assoc($count_run);
            $total_records = $count_row['total'];
            $total_pages = ceil($total_records / $limit);

            // Fetch paginated blogs
            $blog_sql = "SELECT * FROM data_table WHERE up_by = '$x' AND page_type = 'blog' AND recycle = '1' ORDER BY id DESC LIMIT $limit OFFSET $offset";
            $blog_run = mysqli_query($con, $blog_sql);

            if ($blog_run && mysqli_num_rows($blog_run) > 0) {
        ?>
        <div class="blog-area py-120">
            <div class="container">
                <div class="row">
                    <div class="col-lg-6 mx-auto">
                        <div class="site-heading text-center">
                            <span class="site-title-tagline"><i class="far fa-book-open-reader"></i> Our Blog</span>
                            <h2 class="site-title">Latest <span>Blog</span></h2>
                        </div>
                    </div>
                </div>
                <div class="row justify-content-center">
                    <?php while ($blog_row = mysqli_fetch_assoc($blog_run)) { ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="blog-item wow fadeInUp" data-wow-delay=".25s">
                            <div class="blog-date">
                                <i class="fal fa-calendar-alt"></i>
                                <?php echo date("M d, Y", strtotime($blog_row['create_date'] ?? 'now')); ?>
                            </div>
                            <div class="blog-item-img">
                                <img src="./login/upload/<?php echo $blog_row['image_thumbnail']; ?>"
                                    alt="<?php echo $blog_row['title']; ?>">
                            </div>
                            <div class="blog-item-info">
                                <div class="blog-item-meta">
                                    <ul>
                                        <li><a href="blog.php"><i class="far fa-user-circle"></i> By MACAW</a></li>
                                    </ul>
                                </div>
                                <h4 class="blog-title">
                                    <a href="blog-details.php?blog=<?= urlencode($blog_row["title"]); ?>">
                                        <?= $blog_row["title"]; ?>
                                    </a>
                                </h4>
                                <a class="theme-btn" href="blog-details.php?blog=<?= urlencode($blog_row["title"]); ?>">
                                    Read More <i class="fas fa-arrow-right-long"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                    <?php } ?>
                </div>

                <!-- Pagination -->
                <div class="pagination-area mt-4">
                    <div aria-label="Page navigation example">
                        <ul class="pagination justify-content-center">

                            <!-- Previous Button -->
                            <li class="page-item <?= ($page <= 1) ? 'disabled' : ''; ?>">
                                <a class="page-link" href="?page=<?= max(1, $page - 1); ?>" aria-label="Previous">
                                    <span aria-hidden="true"><i class="far fa-arrow-left"></i></span>
                                </a>
                            </li>

                            <!-- Page Numbers -->
                            <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
                            <li class="page-item <?= ($i == $page) ? 'active' : ''; ?>">
                                <a class="page-link" href="?page=<?= $i; ?>"><?= $i; ?></a>
                            </li>
                            <?php } ?>

                            <!-- Next Button -->
                            <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : ''; ?>">
                                <a class="page-link" href="?page=<?= min($total_pages, $page + 1); ?>"
                                    aria-label="Next">
                                    <span aria-hidden="true"><i class="far fa-arrow-right"></i></span>
                                </a>
                            </li>

                        </ul>
                    </div>
                </div>
                <!-- Pagination End -->
            </div>
        </div>
        <?php } ?>
        <!-- blog area end -->


    </main>


    <!-- footer area -->
    <?php include("include/footer.php");?>
    <!-- footer area end -->

    <!-- js -->
    <?php include("include/js.php");?>

</body>

</html>