<?php
require_once 'admin/inc/config.php';
require_once __DIR__ . "/data/site.php";
require_once __DIR__ . "/data/contact.php";

$statement = $pdo->prepare("
    SELECT
        id,
        slug,
        title,
        excerpt,
        content,
        image,
        author,
        date,
        category
    FROM blogs
    WHERE status = 1
    ORDER BY date DESC, id DESC
");

$statement->execute();

$blogs = $statement->fetchAll(PDO::FETCH_ASSOC);


/**
 * Escape output safely
 */
function e($value): string
{
    return htmlspecialchars(
        (string)($value ?? ''),
        ENT_QUOTES,
        'UTF-8'
    );
}


/**
 * Search and category filters
 */
$search = trim($_GET['search'] ?? '');
$category = trim($_GET['category'] ?? '');

// $categories = [];

foreach ($blogs as $blog) {

    if (!empty($blog['category'])) {
        $categories[] = $blog['category'];
    }

}

// $categories = array_values(array_unique($categories));


/**
 * Filter blogs
 */
// $filteredBlogs = array_filter(
//     $blogs,
//     function ($blog) use ($search, $category) {

//         $title = $blog['title'] ?? '';
//         $excerpt = $blog['excerpt'] ?? '';
//         $blogCategory = $blog['category'] ?? '';

//         $matchesSearch =
//             $search === ''
//             || stripos($title, $search) !== false
//             || stripos($excerpt, $search) !== false;

//         $matchesCategory =
//             $category === ''
//             || $blogCategory === $category;

//         return $matchesSearch && $matchesCategory;
//     }
// );

?>

<?php require_once __DIR__ . "/includes/header.php"; ?>


<style>

.blog-section {
    width: 100%;
    padding: 80px 0;
    background: #ffffff;
    overflow: hidden;
}

.blog-section .container-fluid {
    width: 100%;
}

.blog-section h2 {
    margin: 0 0 18px;
    color: #171717;
    font-size: clamp(28px, 3vw, 46px);
    line-height: 1.2;
    font-weight: 700;
}

.blog-section p {
    color: #141414;
    font-size: 16px;
    line-height: 1.8;
}

.blog-filter-form {
    display: flex;
    align-items: stretch;
    gap: 15px;
    width: 100%;
    margin-bottom: 45px;
    flex-wrap: wrap;
}

.blog-search-box,
.blog-category-select {
    height: 52px;
    min-width: 0;
    border: 1px solid #dddddd;
    border-radius: 0;
    outline: none;
    background: #ffffff;
    color: #222222;
    padding: 0 16px;
    font-family: inherit;
    font-size: 15px;
    transition: border-color 0.3s ease;
}

.blog-search-box {
    flex: 1 1 280px;
}

.blog-category-select {
    flex: 0 1 230px;
    cursor: pointer;
}

.blog-search-box:focus,
.blog-category-select:focus {
    border-color: var(--brand, #e30613);
}

.blog-filter-btn {
    height: 52px;
    flex: 0 0 auto;
    padding: 0 30px;
    border: 1px solid var(--brand, #e30613);
    border-radius: 0;
    background: var(--brand, #e30613);
    color: #ffffff;
    font-family: inherit;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s ease;
}

.blog-filter-btn:hover {
    background: #171717;
    border-color: #171717;
}

.blog-card {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    min-width: 0;
    overflow: hidden;
    background: #ffffff;
    border-radius: 10px;
    border: 1px solid #e8e8e8;
    transition:
        transform 0.3s ease,
        box-shadow 0.3s ease;
}

.blog-card:hover {
    transform: translateY(-6px);
    box-shadow: 0 12px 35px rgba(0, 0, 0, 0.10);
}

.blog-card-image {
    width: 100%;
    height: 245px;
    overflow: hidden;
    background: #f2f2f2;
}

.blog-card-image img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.45s ease;
}

.blog-card:hover .blog-card-image img {
    transform: scale(1.06);
}

.blog-card-content {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    padding: 28px;
}

.blog-card-category {
    display: inline-block;
    margin-bottom: 12px;
    color: var(--brand, #e30613);
    font-size: 13px;
    font-weight: 800;
    letter-spacing: 1.2px;
    line-height: 1.5;
    text-transform: uppercase;
    overflow-wrap: anywhere;
}

.blog-card-title {
    margin: 0 0 14px;
    color: #171717;
    font-size: clamp(20px, 2vw, 25px);
    font-weight: 700;
    line-height: 1.35;
    overflow-wrap: anywhere;
}

.blog-card-title a {
    color: inherit;
    text-decoration: none;
    transition: color 0.3s ease;
}

.blog-card-title a:hover {
    color: var(--brand, #e30613);
}

.blog-card-excerpt {
    flex: 1;
    margin: 0 0 24px;
    color: #666666;
    font-size: 15px;
    line-height: 1.8;
    overflow-wrap: anywhere;
}

.blog-card-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    margin-top: auto;
    padding-top: 18px;
    border-top: 1px solid #eeeeee;
    color: #777777;
    font-size: 13px;
    line-height: 1.6;
}

.blog-card-meta span {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    max-width: 100%;
    overflow-wrap: anywhere;
}

.blog-card-meta i {
    font-size: 14px;
}

.blog-read-more {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    color: var(--brand, #e30613);
    font-size: 14px;
    font-weight: 700;
    text-decoration: none;
    white-space: nowrap;
    transition: color 0.3s ease;
}

.blog-read-more:hover {
    color: #171717;
}

.blog-read-more i {
    transition: transform 0.3s ease;
}

.blog-read-more:hover i {
    transform: translateX(4px);
}

.blog-no-results {
    width: 100%;
    padding: 55px 25px;
    text-align: center;
    background: #f7f7f7;
}

.blog-no-results h3 {
    margin-bottom: 12px;
    color: #171717;
    font-size: 28px;
    line-height: 1.3;
}

.blog-no-results p {
    margin: 0;
    color: #666666;
}

.blog-section .row {
    min-width: 0;
}

.blog-section [class*="col-"] {
    min-width: 0;
}

@media (max-width: 1199px) {

    .blog-section {
        padding: 70px 0;
    }

    .blog-card-image {
        height: 225px;
    }

    .blog-card-content {
        padding: 24px;
    }

    .blog-card-meta {
        align-items: flex-start;
        flex-direction: column;
    }

}

@media (max-width: 991px) {

    .blog-section {
        padding: 60px 0;
    }

    .blog-filter-form {
        gap: 12px;
    }

    .blog-search-box {
        flex: 1 1 100%;
    }

    .blog-category-select {
        flex: 1 1 220px;
    }

    .blog-filter-btn {
        flex: 0 0 auto;
    }

    .blog-card-image {
        height: 230px;
    }

    .blog-card-content {
        padding: 23px;
    }

    .blog-card-title {
        font-size: 22px;
    }

}

@media (max-width: 767px) {

    .blog-section {
        padding: 48px 0;
    }

    .blog-section h2 {
        font-size: 30px;
        line-height: 1.25;
    }

    .blog-section p {
        font-size: 15px;
        line-height: 1.7;
    }

    .blog-filter-form {
        display: flex;
        flex-direction: column;
        gap: 12px;
        margin-bottom: 32px;
    }

    .blog-search-box,
    .blog-category-select,
    .blog-filter-btn {
        width: 100%;
        max-width: 100%;
        flex: 1 1 auto;
    }

    .blog-search-box,
    .blog-category-select,
    .blog-filter-btn {
        height: 50px;
    }

    .blog-card-image {
        height: 230px;
    }

    .blog-card-content {
        padding: 22px;
    }

    .blog-card-title {
        font-size: 22px;
    }

    .blog-card-excerpt {
        font-size: 15px;
    }

    .blog-card-meta {
        flex-direction: row;
        align-items: center;
    }

}

@media (max-width: 480px) {

    .blog-section {
        padding: 40px 0;
    }

    .blog-section h2 {
        font-size: 26px;
    }

    .blog-section .container-fluid {
        padding-left: 18px !important;
        padding-right: 18px !important;
    }

    .blog-card-image {
        height: 205px;
    }

    .blog-card-content {
        padding: 19px;
    }

    .blog-card-category {
        font-size: 12px;
        letter-spacing: 0.8px;
    }

    .blog-card-title {
        font-size: 20px;
    }

    .blog-card-excerpt {
        font-size: 14px;
        line-height: 1.7;
    }

    .blog-card-meta {
        align-items: flex-start;
        flex-direction: column;
        gap: 12px;
    }

    .blog-read-more {
        font-size: 14px;
    }

    .blog-no-results {
        padding: 40px 18px;
    }

    .blog-no-results h3 {
        font-size: 24px;
    }

}

@media (max-width: 360px) {

    .blog-section .container-fluid {
        padding-left: 14px !important;
        padding-right: 14px !important;
    }

    .blog-card-image {
        height: 185px;
    }

    .blog-card-content {
        padding: 17px;
    }

    .blog-card-title {
        font-size: 19px;
    }

}



/* =========================================================
   PREMIUM BLOG PAGE ANIMATIONS
   Layout / structure remains unchanged
========================================================= */


/* ---------------------------------------------------------
   SECTION AMBIENT MOTION
--------------------------------------------------------- */

.blog-section {
    position: relative;
    isolation: isolate;
}

.blog-section::before {
    content: "";
    position: absolute;
    top: 8%;
    left: -180px;
    width: 380px;
    height: 380px;
    border-radius: 50%;
    background: radial-gradient(
        circle,
        rgba(227, 6, 19, 0.055) 0%,
        rgba(227, 6, 19, 0.025) 35%,
        transparent 70%
    );
    filter: blur(10px);
    pointer-events: none;
    z-index: -1;
    animation: blogAmbientMove 9s ease-in-out infinite alternate;
}

.blog-section::after {
    content: "";
    position: absolute;
    right: -180px;
    bottom: 5%;
    width: 400px;
    height: 400px;
    border-radius: 50%;
    background: radial-gradient(
        circle,
        rgba(11, 78, 162, 0.045) 0%,
        rgba(11, 78, 162, 0.02) 35%,
        transparent 70%
    );
    filter: blur(12px);
    pointer-events: none;
    z-index: -1;
    animation: blogAmbientMoveReverse 11s ease-in-out infinite alternate;
}

@keyframes blogAmbientMove {

    0% {
        transform: translate3d(0, 0, 0) scale(1);
    }

    50% {
        transform: translate3d(45px, 25px, 0) scale(1.08);
    }

    100% {
        transform: translate3d(15px, 55px, 0) scale(.96);
    }

}

@keyframes blogAmbientMoveReverse {

    0% {
        transform: translate3d(0, 0, 0) scale(1);
    }

    50% {
        transform: translate3d(-40px, -25px, 0) scale(1.06);
    }

    100% {
        transform: translate3d(-10px, -50px, 0) scale(.95);
    }

}


/* ---------------------------------------------------------
   SECTION HEADING ANIMATION
--------------------------------------------------------- */

.blog-section h2 {
    position: relative;
    width: fit-content;
    max-width: 100%;
}

.blog-section h2::after {
    content: "";
    display: block;
    width: 55px;
    height: 3px;
    margin-top: 15px;
    background: var(--brand, #e30613);
    transform-origin: left center;
    animation: blogHeadingLine 2.8s ease-in-out infinite;
}

@keyframes blogHeadingLine {

    0% {
        transform: scaleX(.45);
        opacity: .55;
    }

    50% {
        transform: scaleX(1);
        opacity: 1;
    }

    100% {
        transform: scaleX(.45);
        opacity: .55;
    }

}


/* ---------------------------------------------------------
   BLOG CARD ENTRANCE ANIMATION
--------------------------------------------------------- */

.blog-section .row.g-4 > [class*="col-"] {
    animation: blogCardReveal .8s cubic-bezier(.22, .61, .36, 1) both;
}

.blog-section .row.g-4 > [class*="col-"]:nth-child(1) {
    animation-delay: .08s;
}

.blog-section .row.g-4 > [class*="col-"]:nth-child(2) {
    animation-delay: .16s;
}

.blog-section .row.g-4 > [class*="col-"]:nth-child(3) {
    animation-delay: .24s;
}

.blog-section .row.g-4 > [class*="col-"]:nth-child(4) {
    animation-delay: .32s;
}

.blog-section .row.g-4 > [class*="col-"]:nth-child(5) {
    animation-delay: .40s;
}

.blog-section .row.g-4 > [class*="col-"]:nth-child(6) {
    animation-delay: .48s;
}

@keyframes blogCardReveal {

    0% {
        opacity: 0;
        transform: translateY(35px);
    }

    100% {
        opacity: 1;
        transform: translateY(0);
    }

}


/* ---------------------------------------------------------
   CARD PREMIUM HOVER
--------------------------------------------------------- */

.blog-card {
    position: relative;
    transform: translateY(0);
    transition:
        transform .45s cubic-bezier(.22, .61, .36, 1),
        box-shadow .45s ease,
        border-color .35s ease;
}

.blog-card::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(
        90deg,
        transparent,
        var(--brand, #e30613),
        transparent
    );
    transform: scaleX(0);
    transform-origin: center;
    transition: transform .5s ease;
    z-index: 3;
}

.blog-card:hover {
    transform: translateY(-9px);
    border-color: rgba(227, 6, 19, .18);
    box-shadow:
        0 18px 45px rgba(0, 0, 0, .12),
        0 4px 12px rgba(0, 0, 0, .05);
}

.blog-card:hover::before {
    transform: scaleX(1);
}


/* ---------------------------------------------------------
   IMAGE ANIMATION
--------------------------------------------------------- */

.blog-card-image {
    position: relative;
    overflow: hidden;
}

.blog-card-image::after {
    content: "";
    position: absolute;
    top: 0;
    left: -120%;
    width: 70%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255,255,255,.28),
        transparent
    );
    transform: skewX(-20deg);
    pointer-events: none;
    transition: none;
}

.blog-card:hover .blog-card-image::after {
    animation: blogImageShine .8s ease forwards;
}

@keyframes blogImageShine {

    0% {
        left: -120%;
    }

    100% {
        left: 150%;
    }

}

.blog-card-image img {
    transform: scale(1);
    transition:
        transform .8s cubic-bezier(.22, .61, .36, 1),
        filter .5s ease;
}

.blog-card:hover .blog-card-image img {
    transform: scale(1.08);
    filter: saturate(1.08);
}


/* ---------------------------------------------------------
   CATEGORY ANIMATION
--------------------------------------------------------- */

.blog-card-category {
    position: relative;
    transition:
        letter-spacing .35s ease,
        color .35s ease;
}

.blog-card:hover .blog-card-category {
    letter-spacing: 1.7px;
}


/* ---------------------------------------------------------
   TITLE ANIMATION
--------------------------------------------------------- */

.blog-card-title a {
    background-image: linear-gradient(
        var(--brand, #e30613),
        var(--brand, #e30613)
    );
    background-repeat: no-repeat;
    background-position: 0 100%;
    background-size: 0% 1px;
    transition:
        color .3s ease,
        background-size .4s ease;
}

.blog-card-title a:hover {
    background-size: 100% 1px;
}


/* ---------------------------------------------------------
   READ MORE ARROW
--------------------------------------------------------- */

.blog-read-more {
    position: relative;
    transition:
        color .3s ease,
        gap .3s ease;
}

.blog-read-more i {
    transition:
        transform .35s cubic-bezier(.22, .61, .36, 1);
}

.blog-card:hover .blog-read-more i {
    transform: translateX(7px);
}

.blog-card:hover .blog-read-more {
    gap: 8px;
}


/* ---------------------------------------------------------
   META BORDER MOTION
--------------------------------------------------------- */

.blog-card-meta {
    position: relative;
}

.blog-card-meta::before {
    content: "";
    position: absolute;
    left: 0;
    top: -1px;
    width: 0;
    height: 1px;
    background: var(--brand, #e30613);
    transition: width .5s ease;
}

.blog-card:hover .blog-card-meta::before {
    width: 55px;
}


/* ---------------------------------------------------------
   FILTER INPUT ANIMATION
--------------------------------------------------------- */

.blog-search-box,
.blog-category-select {
    transition:
        border-color .3s ease,
        box-shadow .3s ease,
        transform .3s ease;
}

.blog-search-box:focus,
.blog-category-select:focus {
    border-color: var(--brand, #e30613);
    box-shadow: 0 5px 20px rgba(227, 6, 19, .07);
    transform: translateY(-1px);
}

.blog-filter-btn {
    position: relative;
    overflow: hidden;
}

.blog-filter-btn::before {
    content: "";
    position: absolute;
    top: 0;
    left: -110%;
    width: 70%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255,255,255,.22),
        transparent
    );
    transform: skewX(-20deg);
}

.blog-filter-btn:hover::before {
    animation: filterButtonShine .7s ease forwards;
}

@keyframes filterButtonShine {

    0% {
        left: -110%;
    }

    100% {
        left: 140%;
    }

}


/* ---------------------------------------------------------
   MOBILE - KEEP ANIMATION LIGHT
--------------------------------------------------------- */

@media (max-width: 767px) {

    .blog-card:hover {
        transform: translateY(-5px);
    }

    .blog-card:hover .blog-card-image img {
        transform: scale(1.045);
    }

    .blog-section::before {
        left: -260px;
        opacity: .55;
    }

    .blog-section::after {
        right: -260px;
        opacity: .45;
    }

}


/* ---------------------------------------------------------
   ACCESSIBILITY
--------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {

    .blog-section::before,
    .blog-section::after,
    .blog-section h2::after,
    .blog-section .row.g-4 > [class*="col-"],
    .blog-card,
    .blog-card-image img,
    .blog-card-image::after,
    .blog-card::before,
    .blog-card-meta::before,
    .blog-filter-btn::before {
        animation: none !important;
        transition: none !important;
    }

}
</style>


<section
    class="contact-hero"
    style="background-image:url('<?= $site['base_url'] ?><?= e($contact_page["banner_image"] ?? "") ?>');">

    <div class="contact-hero-overlay"></div>

    <div class="container-fluid px-lg-5">

        <div class="contact-hero-content">

            <span class="contact-hero-eyebrow">
                <?= e($contact_page["eyebrow"] ?? "Insights & Updates") ?>
            </span>

            <h1>Blog</h1>

            <div class="contact-breadcrumb">

                <a href="index.php">Home</a>

                <i class="bi bi-chevron-right"></i>

                <strong>Blog</strong>

            </div>

        </div>

    </div>

</section>


<section class="blog-section">

    <div class="container-fluid px-lg-5">

        <div class="row mb-5">

            <div class="col-lg-8">

                <span class="contact-hero-eyebrow">
                    Our Latest Articles
                </span>

                <h2>
                    Insights, Ideas &amp; Industry Updates
                </h2>

                <p>
                    Explore the latest news, knowledge and updates from
                    MVSK Industries.
                </p>

            </div>

        </div>


        <div class="row g-4">

            <?php if (!empty($blogs)): ?>

                <?php foreach ($blogs as $blog): ?>

                    <div class="col-lg-4 col-md-6">

                        <article class="blog-card">

                            <div class="blog-card-image">

                                <img
                                    src="<?= e(
                                        !empty($blog['image'])
                                            ? $site['base_url'] . 'assets/images/blog/' .$blog['image']
                                            : 'assets/images/blog/default.jpg'
                                    ) ?>"
                                    alt="<?= e(
                                        $blog['title'] ?? 'Blog image'
                                    ) ?>"
                                    loading="lazy">

                            </div>


                            <div class="blog-card-content">

                                <?php if (!empty($blog['category'])): ?>

                                    <!-- <span class="blog-card-category">
                                        <?= e($blog['category']) ?>
                                    </span> -->

                                <?php endif; ?>


                                <h3 class="blog-card-title">

                                    <a
                                        href="<?= $site['base_url'] ?>blog/<?= urlencode(
                                            $blog['slug'] ?? ''
                                        ) ?>">

                                        <?= e($blog['title'] ?? '') ?>

                                    </a>

                                </h3>


                                <?php if (!empty($blog['excerpt'])): ?>

                                    <p class="blog-card-excerpt">
                                        <?= e($blog['excerpt']) ?>
                                    </p>

                                <?php endif; ?>


                                <div class="blog-card-meta">

                                    <a
                                        href="<?= $site['base_url'] ?>blog/<?= urlencode(
                                            $blog['slug'] ?? ''
                                        ) ?>"
                                        class="blog-read-more">

                                        Read More

                                        <i class="bi bi-arrow-right"></i>

                                    </a>

                                </div>

                            </div>

                        </article>

                    </div>

                <?php endforeach; ?>

            <?php else: ?>

                <div class="col-12">

                    <div class="blog-no-results">

                        <h3>No Articles Found</h3>

                        <p>
                            There are currently no active articles available.
                        </p>

                    </div>

                </div>

            <?php endif; ?>

        </div>

    </div>

</section>


<?php require_once __DIR__ . "/includes/footer.php"; ?>