<?php

require_once __DIR__ . "/data/products.php";
require_once __DIR__ . "/data/contact.php";
require_once __DIR__ . "/includes/header.php";


/*
|--------------------------------------------------------------------------
| Get URL Parameters
|--------------------------------------------------------------------------
*/

$category_slug = $_GET['category'] ?? '';
$product_slug  = $_GET['product'] ?? '';

$category_slug = trim($category_slug);
$product_slug  = trim($product_slug);


/*
|--------------------------------------------------------------------------
| Find Product Category
|--------------------------------------------------------------------------
*/

$selected_category = null;

foreach ($products as $category) {

    if (!is_array($category)) {
        continue;
    }

    if (
        isset($category['slug']) &&
        $category['slug'] === $category_slug
    ) {

        $selected_category = $category;

        break;
    }
}


/*
|--------------------------------------------------------------------------
| Find Individual Product
|--------------------------------------------------------------------------
*/

$selected_product = null;

if ($selected_category !== null) {

    $items = $selected_category['items'] ?? [];

    if (is_array($items)) {

        foreach ($items as $item) {

            if (!is_array($item)) {
                continue;
            }

            if (
                isset($item['slug']) &&
                $item['slug'] === $product_slug
            ) {

                $selected_product = $item;

                break;
            }
        }
    }
}


/*
|--------------------------------------------------------------------------
| 404 if Product Not Found
|--------------------------------------------------------------------------
*/

if ($selected_category === null || $selected_product === null) {

    http_response_code(404);

}


/*
|--------------------------------------------------------------------------
| Product Variables
|--------------------------------------------------------------------------
*/

$category_name =
    $selected_category['category']
    ?? 'Products';

$product_name =
    $selected_product['name']
    ?? 'Product';

$product_description =
    $selected_product['description']
    ?? '';

$product_image =
    $selected_product['image']
    ?? '';

$features =
    $selected_product['features']
    ?? [];

$applications =
    $selected_product['applications']
    ?? [];

$specifications =
    $selected_product['specifications']
    ?? [];


/*
|--------------------------------------------------------------------------
| Make Sure Arrays
|--------------------------------------------------------------------------
*/

if (!is_array($features)) {
    $features = [];
}

if (!is_array($applications)) {
    $applications = [];
}

if (!is_array($specifications)) {
    $specifications = [];
}

?>

<style>

.product-detail-hero {

    position: relative;

    min-height: 430px;

    display: flex;

    align-items: center;

    background-size: cover;

    background-position: center;

    background-repeat: no-repeat;

    color: #fff;

}


.product-detail-hero .page-hero-inner {

    position: relative;

    z-index: 2;

}


.product-detail-hero h1 {

    color: #fff;

    max-width: 850px;

}


.product-detail-hero .eyebrow {

    color: var(--brand);

}


.product-detail-breadcrumb {

    display: flex;

    align-items: center;

    gap: 10px;

    margin-top: 25px;

    font-size: 14px;

}


.product-detail-breadcrumb a {

    color: var(--brand);

    text-decoration: none;

}


.product-detail-breadcrumb span {

    color: rgba(255,255,255,.8);

}


/* =========================
   Product Detail
========================= */

.product-detail-section {

    padding: 90px 0;

}


.product-detail-grid {

    display: grid;

    grid-template-columns: 45% 55%;

    gap: 60px;

    align-items: start;

}


.product-detail-image {

    position: relative;

    overflow: hidden;

    border-radius: 18px;

    background: #f5f6f7;

}


.product-detail-image img {

    display: block;

    width: 100%;

    height: auto;

    object-fit: cover;

}


.product-detail-content .eyebrow {

    margin-bottom: 12px;

}


.product-detail-content h2 {

    margin-bottom: 20px;

}


.product-detail-description {

    font-size: 17px;

    line-height: 1.8;

    color: #555;

    margin-bottom: 35px;

}


/* =========================
   Detail Blocks
========================= */

.product-detail-block {

    margin-top: 45px;

}


.product-detail-block h3 {

    margin-bottom: 22px;

}


.product-detail-list {

    display: grid;

    grid-template-columns: repeat(2, 1fr);

    gap: 15px;

    padding: 0;

    margin: 0;

    list-style: none;

}


.product-detail-list li {

    display: flex;

    align-items: flex-start;

    gap: 10px;

    padding: 15px 18px;

    background: #f7f8fa;

    border-radius: 10px;

    color: #444;

    line-height: 1.6;

}


.product-detail-list li i {

    color: var(--brand);

    font-size: 19px;

    flex-shrink: 0;

}


/* =========================
   Specifications
========================= */

.product-specifications {

    width: 100%;

    border-collapse: collapse;

}


.product-specifications tr {

    border-bottom: 1px solid #e5e5e5;

}


.product-specifications td {

    padding: 15px 10px;

    vertical-align: top;

}


.product-specifications td:first-child {

    width: 35%;

    font-weight: 700;

    color: #222;

}


/* =========================
   CTA
========================= */

.product-detail-cta {

    margin-top: 70px;

    padding: 45px;

    border-radius: 18px;

    background: #f5f7f9;

    display: flex;

    align-items: center;

    justify-content: space-between;

    gap: 30px;

}


.product-detail-cta h3 {

    margin: 0 0 8px;

}


.product-detail-cta p {

    margin: 0;

    color: #666;

}


/* =========================
   Mobile
========================= */

@media (max-width: 991px) {

    .product-detail-grid {

        grid-template-columns: 1fr;

        gap: 40px;

    }

}


@media (max-width: 767px) {

    .product-detail-hero {

        min-height: 350px;

        padding: 65px 0;

    }


    .product-detail-hero h1 {

        font-size: 34px;

        line-height: 1.2;

    }


    .product-detail-section {

        padding: 60px 0;

    }


    .product-detail-list {

        grid-template-columns: 1fr;

    }


    .product-detail-cta {

        padding: 30px;

        flex-direction: column;

        align-items: flex-start;

    }

}

</style>


<!-- =========================
     Product Detail Hero
========================= -->

<section
    class="page-hero product-detail-hero"
    style="
        background-image:
            linear-gradient(
                90deg,
                rgba(0,0,0,.82) 0%,
                rgba(0,0,0,.50) 55%,
                rgba(0,0,0,.20) 100%
            ),
            url('<?= htmlspecialchars(
                $product_image !== ''
                    ? $product_image
                    : ($contact_page['banner_image'] ?? ''),
                ENT_QUOTES,
                'UTF-8'
            ) ?>');
    "
>

    <div class="container-fluid px-lg-5">

        <div class="page-hero-inner">

            <span class="eyebrow">
                <?= htmlspecialchars(
                    $category_name,
                    ENT_QUOTES,
                    'UTF-8'
                ) ?>
            </span>

            <h1>
                <?= htmlspecialchars(
                    $product_name,
                    ENT_QUOTES,
                    'UTF-8'
                ) ?>
            </h1>

            <div class="product-detail-breadcrumb">

                <a href="index.php">
                    Home
                </a>

                <i class="bi bi-chevron-right"></i>

                <a href="products.php">
                    Products
                </a>

                <i class="bi bi-chevron-right"></i>

                <a
                    href="product-category.php?slug=<?= urlencode($category_slug) ?>"
                >
                    <?= htmlspecialchars(
                        $category_name,
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>
                </a>

                <i class="bi bi-chevron-right"></i>

                <span>
                    <?= htmlspecialchars(
                        $product_name,
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>
                </span>

            </div>

        </div>

    </div>

</section>


<!-- =========================
     Product Detail Section
========================= -->

<section class="product-detail-section">

    <div class="container">

        <div class="product-detail-grid">


            <!-- Product Image -->

            <div class="product-detail-image">

                <?php if ($product_image !== ''): ?>

                    <img
                        src="<?= htmlspecialchars(
                            $product_image,
                            ENT_QUOTES,
                            'UTF-8'
                        ) ?>"
                        alt="<?= htmlspecialchars(
                            $product_name,
                            ENT_QUOTES,
                            'UTF-8'
                        ) ?>"
                    >

                <?php endif; ?>

            </div>


            <!-- Product Content -->

            <div class="product-detail-content">

                <span class="eyebrow">
                    <?= htmlspecialchars(
                        $category_name,
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>
                </span>


                <h2>
                    <?= htmlspecialchars(
                        $product_name,
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>
                </h2>


                <?php if ($product_description !== ''): ?>

                    <div class="product-detail-description">

                        <?= nl2br(
                            htmlspecialchars(
                                $product_description,
                                ENT_QUOTES,
                                'UTF-8'
                            )
                        ) ?>

                    </div>

                <?php endif; ?>


                <!-- Features -->

                <?php if (!empty($features)): ?>

                    <div class="product-detail-block">

                        <h3>
                            Key Features
                        </h3>

                        <ul class="product-detail-list">

                            <?php foreach ($features as $feature): ?>

                                <?php if (trim((string)$feature) === '') continue; ?>

                                <li>

                                    <i class="bi bi-check2"></i>

                                    <?= htmlspecialchars(
                                        (string)$feature,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </li>

                            <?php endforeach; ?>

                        </ul>

                    </div>

                <?php endif; ?>


                <!-- Applications -->

                <?php if (!empty($applications)): ?>

                    <div class="product-detail-block">

                        <h3>
                            Applications
                        </h3>

                        <ul class="product-detail-list">

                            <?php foreach ($applications as $application): ?>

                                <?php if (trim((string)$application) === '') continue; ?>

                                <li>

                                    <i class="bi bi-arrow-right"></i>

                                    <?= htmlspecialchars(
                                        (string)$application,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </li>

                            <?php endforeach; ?>

                        </ul>

                    </div>

                <?php endif; ?>

            </div>

        </div>


        <!-- =========================
             Specifications
        ========================= -->

        <?php if (!empty($specifications)): ?>

            <div class="product-detail-block">

                <h3>
                    Technical Specifications
                </h3>


                <table class="product-specifications">

                    <tbody>

                        <?php foreach ($specifications as $key => $value): ?>

                            <tr>

                                <td>
                                    <?= htmlspecialchars(
                                        (string)$key,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>
                                </td>

                                <td>
                                    <?= htmlspecialchars(
                                        (string)$value,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>
                                </td>

                            </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        <?php endif; ?>


        <!-- =========================
             CTA
        ========================= -->

        <div class="product-detail-cta">

            <div>

                <h3>
                    Looking for this solution?
                </h3>

                <p>
                    Share your application and production requirements
                    with our team.
                </p>

            </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";

?>