<?php

require_once __DIR__ . "/data/contact.php";
require_once __DIR__ . "/includes/header.php";
require_once 'admin/inc/config.php';

$statement = $pdo->prepare("
    SELECT
        id,
        top_category_name,
        top_category_slug,
        banner
    FROM tbl_top_category
    WHERE status = 1
    ORDER BY id ASC
");
$statement->execute();
$categories = $statement->fetchAll(PDO::FETCH_ASSOC);

$statement = $pdo->prepare("
    SELECT
        id,
        top_category_id,
        product_name,
        product_slug,
        main_image
    FROM tbl_service
    WHERE status = 1
    ORDER BY id DESC
");
$statement->execute();
$productRows = $statement->fetchAll(PDO::FETCH_ASSOC);

$productsByCategory = [];

foreach ($productRows as $product) {
    $categoryId = (int)$product['top_category_id'];

    if (!isset($productsByCategory[$categoryId])) {
        $productsByCategory[$categoryId] = [];
    }

    $productsByCategory[$categoryId][] = $product;
}

function e($value): string
{
    return htmlspecialchars(
        (string)($value ?? ''),
        ENT_QUOTES,
        'UTF-8'
    );
}

?>

<style>
    .products-page-hero {
        position: relative;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        color: #fff;
    }

    .products-page-hero .page-hero-inner {
        position: relative;
        z-index: 2;
    }

    .products-page-hero .eyebrow {
        color: var(--brand);
    }

    .products-page-hero h1 {
        color: #fff;
    }

    .products-page-hero .breadcrumb-wrap {
        color: #fff;
    }

    .products-page-hero .breadcrumb-wrap a {
        color: var(--brand);
    }

    @media (max-width: 767px) {

        .products-page-hero {
            padding: 65px 0;
            background-position: center center;
        }

        .products-page-hero h1 {
            font-size: 34px;
            line-height: 1.2;
        }

        .products-page-hero .breadcrumb-wrap {
            flex-wrap: wrap;
            gap: 7px;
            margin-top: 22px;
            font-size: 14px;
        }

    }
  
</style>


<section
    class="page-hero products-page-hero"
    style="
        background-image:
            linear-gradient(
                90deg,
                rgba(0, 0, 0, .78) 0%,
                rgba(0, 0, 0, .38) 45%,
                rgba(0, 0, 0, .12) 80%
            ),
            url('<?= $site['base_url'] ?><?= e($contact_page['banner_image'] ?? '') ?>');
    ">

    <div class="container-fluid px-lg-5">

        <div class="page-hero-inner">

            <h1>
                Explore Our
                Products
            </h1>

            <div class="breadcrumb-wrap">

                <a href="index.php">
                    Home
                </a>

                <i class="bi bi-chevron-right"></i>

                <span>
                    Products
                </span>

            </div>

        </div>

    </div>

</section>


<section class="products-page-section">

    <div class="container-fluid px-lg-5">

        <div class="products-page-intro">

            <div>

                <span class="eyebrow">
                    Product Range
                </span>

                <h2>
                    Engineered Solutions for
                    Industrial Applications
                </h2>

            </div>

            <p>
                From individual coating equipment to complete industrial
                systems, our product range is structured around practical
                production requirements.
            </p>

        </div>


        <div class="products-page-grid">

            <?php if (!empty($categories)): ?>

                <?php foreach ($categories as $category): ?>

                    <?php
                    $categoryId = (int)$category['id'];

                    $categoryProducts =
                        $productsByCategory[$categoryId] ?? [];

                    $categoryImage = $category['banner'] ?? '';

                    if (empty($categoryImage) && !empty($categoryProducts)) {
                        $categoryImage =
                            $categoryProducts[0]['main_image'] ?? '';
                    }

                    if (empty($categoryImage)) {
                        $categoryImage = rtrim($site['base_url'], '/') . '/assets/images/products/default.jpg';
                    }
                    ?>

                    <a
                        href="product-category.php?slug=<?= urlencode(
                                                            $category['top_category_slug'] ?? ''
                                                        ) ?>"
                        class="product-page-card">

                        <div class="product-page-image">

                            <img
                                src="<?= rtrim($site['base_url'], '/') ?>/admin/assets/images/category/<?= e($categoryImage) ?>"
                                alt="<?= e($category['top_category_name']) ?>"
                                loading="lazy">

                            <div class="product-page-overlay"></div>

                        </div>


                        <div class="product-page-content">

                            <!-- <span class="product-page-label">
                                Product Category
                            </span> -->

                            <h3>
                                <?= e($category['top_category_name']) ?>
                            </h3>


                            <?php if (!empty($categoryProducts)): ?>

                                <div class="product-page-items">

                                    <?php foreach ($categoryProducts as $product): ?>

                                        <span>
                                            <?= e($product['product_name']) ?>
                                        </span>

                                    <?php endforeach; ?>

                                </div>

                            <?php endif; ?>


                            <span class="product-page-link">

                                Explore Category

                                <i class="bi bi-arrow-right"></i>

                            </span>

                        </div>

                    </a>

                <?php endforeach; ?>

            <?php else: ?>

                <div class="col-12">

                    <p>
                        No products available.
                    </p>

                </div>

            <?php endif; ?>

        </div>

    </div>

</section>


<!--<section class="products-page-cta">-->

<!--    <div class="container-fluid px-lg-5">-->

<!--        <div class="products-page-cta-inner">-->

<!--            <div>-->

<!--                <span class="eyebrow">-->
<!--                    Need a Custom Solution?-->
<!--                </span>-->

<!--                <h2>-->
<!--                    Tell Us About Your-->
<!--                    Application-->
<!--                </h2>-->

<!--            </div>-->

<!--            <a-->
<!--                href="contact.php#enquiry"-->
<!--                class="btn btn-brand">-->
<!--                Request a Quote-->
<!--                <i class="bi bi-arrow-right"></i>-->
<!--            </a>-->

<!--        </div>-->

<!--    </div>-->

<!--</section>-->


<?php
require_once __DIR__ . "/includes/footer.php";
?>