<?php

require_once __DIR__ . "/data/contact.php";
require_once __DIR__ . "/includes/header.php";
require_once __DIR__ . "/admin/inc/config.php";


function e($value): string
{
    return htmlspecialchars(
        (string)($value ?? ''),
        ENT_QUOTES,
        'UTF-8'
    );
}


/*
|--------------------------------------------------------------------------
| PRODUCT ENQUIRY - SAME PAGE
|--------------------------------------------------------------------------
*/

$enquiry_success = '';
$enquiry_error = '';

if (
    $_SERVER['REQUEST_METHOD'] === 'POST' &&
    isset($_POST['product_enquiry'])
) {

    $customer_name =
        trim($_POST['name'] ?? '');

    $customer_email =
        trim($_POST['email'] ?? '');

    $customer_phone =
        trim($_POST['phone'] ?? '');

    $customer_product =
        trim($_POST['product_name'] ?? '');

    $customer_message =
        trim($_POST['message'] ?? '');


    if (
        $customer_name === '' ||
        $customer_email === '' ||
        $customer_phone === '' ||
        $customer_product === ''
    ) {

        $enquiry_error =
            'Please fill in all required fields.';
    } elseif (
        !filter_var(
            $customer_email,
            FILTER_VALIDATE_EMAIL
        )
    ) {

        $enquiry_error =
            'Please enter a valid email address.';
    } else {

        try {

            $stmt = $pdo->prepare("
                INSERT INTO tbl_customer
                (
                    name,
                    email,
                    phone,
                    product_name,
                    message,
                    created_at
                )
                VALUES
                (
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    NOW()
                )
            ");

            $stmt->execute([
                $customer_name,
                $customer_email,
                $customer_phone,
                $customer_product,
                $customer_message
            ]);


            $enquiry_success =
                'Your enquiry has been submitted successfully.';


            $_POST = [];
        } catch (PDOException $e) {

            error_log(
                'Product Enquiry Error: ' .
                    $e->getMessage()
            );

            $enquiry_error =
                'Unable to submit your enquiry. Please try again.';
        }
    }
}


/*
|--------------------------------------------------------------------------
| PRODUCT SLUG
|--------------------------------------------------------------------------
*/

$product_slug =
    trim(
        (string)(
            $_GET['slug']
            ?? ''
        )
    );


$selected_product = null;
$selected_category = null;


if ($product_slug !== '') {

    $statement = $pdo->prepare("
        SELECT
            s.*,
            c.top_category_name,
            c.top_category_slug
        FROM tbl_service s
        LEFT JOIN tbl_top_category c
            ON s.top_category_id = c.id
        WHERE s.product_slug = ?
        AND s.status = 1
        LIMIT 8
    ");

    $statement->execute([
        $product_slug
    ]);

    $selected_product =
        $statement->fetch(
            PDO::FETCH_ASSOC
        );


    if ($selected_product) {

        $selected_category = [
            'id' =>
            $selected_product['top_category_id'],

            'category' =>
            $selected_product['top_category_name'],

            'slug' =>
            $selected_product['top_category_slug']
        ];
    }
}


/*
|--------------------------------------------------------------------------
| PRODUCT NOT FOUND
|--------------------------------------------------------------------------
*/

if (!$selected_product) {

    http_response_code(404);

?>

    <section class="page-hero product-detail-hero">

        <div class="container-fluid px-lg-5">

            <div class="page-hero-inner">

                <span class="eyebrow">
                    Product
                </span>

                <h1>
                    Product <span>Not Found</span>
                </h1>

                <p>
                    The product you are looking for could not be found.
                </p>

                <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>

                    <span>
                        Not Found
                    </span>

                </div>

            </div>

        </div>

    </section>


    <section class="product-not-found">

        <div class="container">

            <div class="product-not-found-inner">

                <i class="bi bi-box-seam"></i>

                <h2>
                    Product Not Available
                </h2>

                <p>
                    Please return to our products and select another product.
                </p>

                <a
                    href="products.php"
                    class="btn btn-brand">

                    View Products

                    <i class="bi bi-arrow-right"></i>

                </a>

            </div>

        </div>

    </section>

<?php

    require_once __DIR__ . "/includes/footer.php";

    exit;
}


/*
|--------------------------------------------------------------------------
| PRODUCT DATA
|--------------------------------------------------------------------------
*/

$category_name =
    $selected_product['top_category_name']
    ?? 'Products';


$category_slug =
    $selected_product['top_category_slug']
    ?? '';


$product_name =
    $selected_product['product_name']
    ?? 'Product';


$product_slug =
    $selected_product['product_slug']
    ?? '';


$product_short_description =
    $selected_product['short_description']
    ?? '';


$product_description =
    $selected_product['description']
    ?? '';


$product_content =
    $selected_product['content']
    ?? '';


$product_image =
    $selected_product['main_image']
    ?? '';


$application_data =
    $selected_product['application']
    ?? '';


$brochure =
    $selected_product['brochure']
    ?? '';


/*
|--------------------------------------------------------------------------
| APPLICATIONS
|--------------------------------------------------------------------------
*/

$applications = [];

if (!empty($application_data)) {

    $decoded_applications =
        json_decode(
            $application_data,
            true
        );

    if (is_array($decoded_applications)) {

        $applications =
            $decoded_applications;
    }
}


/*
|--------------------------------------------------------------------------
| LATEST PRODUCTS - 10 PRODUCTS
|--------------------------------------------------------------------------
*/

$explore_products = [];

$stmt = $pdo->prepare("
    SELECT
        s.id,
        s.product_name,
        s.product_slug,
        s.main_image,
        c.top_category_slug
    FROM tbl_service s
    LEFT JOIN tbl_top_category c
        ON s.top_category_id = c.id
    WHERE s.status = 1
    AND c.id != 13
    ORDER BY s.id DESC
    LIMIT 9
");

$stmt->execute();

$explore_products =
    $stmt->fetchAll(
        PDO::FETCH_ASSOC
    );


/*
|--------------------------------------------------------------------------
| CONTACT BANNER
|--------------------------------------------------------------------------
*/

$contact_banner =
    $contact_page['banner_image']
    ?? '';

?>

<style>

/* =========================================================
   PREMIUM PRODUCT DETAIL PAGE
========================================================= */

.product-detail-hero{
    position:relative;
    min-height:430px;
    display:flex;
    align-items:center;
    overflow:hidden;
    background-size:cover;
    background-position:center center;
    background-repeat:no-repeat;
    color:#fff;
}

.product-detail-hero:before{
    content:"";
    position:absolute;
    inset:0;
    background:
        linear-gradient(
            90deg,
            rgba(0,0,0,.72) 0%,
            rgba(0,0,0,.48) 40%,
            rgba(0,0,0,.18) 100%
        );
}

.product-detail-hero:after{
    content:"";
    position:absolute;
    width:420px;
    height:420px;
    right:-180px;
    top:-180px;
    border:1px solid rgba(255,255,255,.14);
    border-radius:50%;
    box-shadow:
        0 0 0 50px rgba(255,255,255,.025),
        0 0 0 100px rgba(255,255,255,.018);
    animation:heroOrbit 12s linear infinite;
}

.product-detail-hero .page-hero-inner{
    position:relative;
    z-index:2;
    animation:heroContent .8s ease both;
}

.product-detail-breadcrumb a{
    color:var(--brand);
    text-decoration:none;
}


/* =========================================================
   PRODUCT DETAIL SECTION
========================================================= */

.product-detail-section{
    position:relative;
    padding:90px 0 110px;
    background:
        radial-gradient(
            circle at 10% 10%,
            rgba(237,28,36,.035),
            transparent 30%
        ),
        #fff;
}

.product-detail-section:before{
    content:"";
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:1px;
    background:linear-gradient(
        90deg,
        transparent,
        #e5e5e5,
        transparent
    );
}


/* =========================================================
   TOP AREA
========================================================= */

.product-detail-top{
    display:grid;
    grid-template-columns:
        minmax(0, 1.65fr)
        minmax(420px, 1fr);
    gap:65px;
    align-items:start;
}


/* =========================================================
   PRODUCT IMAGE
========================================================= */

.product-detail-gallery{
    width:100%;
    position:relative;
}

.product-detail-main-image{
    position:relative;
    width:100%;
    height:620px;
    overflow:hidden;
    border-radius:24px;
    background:#f3f5f7;
    border:1px solid #e7eaed;
    box-shadow:
        0 20px 55px rgba(18,30,45,.10),
        0 5px 15px rgba(18,30,45,.05);
}

.product-detail-main-image:before{
    content:"";
    position:absolute;
    z-index:2;
    inset:0;
    pointer-events:none;
    background:
        linear-gradient(
            135deg,
            rgba(255,255,255,.16),
            transparent 35%,
            transparent 70%,
            rgba(0,0,0,.05)
        );
}

.product-detail-main-image img{
    width:100%;
    height:100%;
    display:block;
    object-fit:cover;
    transition:
        transform .8s cubic-bezier(.2,.7,.2,1),
        filter .5s ease;
}

.product-detail-main-image:hover img{
    transform:scale(1.035);
    filter:saturate(1.04);
}

.product-detail-main-image:after{
    content:"";
    position:absolute;
    width:120px;
    height:120px;
    right:-55px;
    bottom:-55px;
    border-radius:50%;
    background:rgba(255,255,255,.12);
}


/* =========================================================
   ENQUIRY CARD
========================================================= */

.product-enquiry-card{
    position:relative;
    overflow:hidden;
    background:#fff;
    border:1px solid #e3e7eb;
    border-radius:20px;
    padding:34px 34px 38px;
    box-shadow:
        0 22px 60px rgba(18,35,52,.09),
        0 3px 10px rgba(18,35,52,.035);
    animation:cardReveal .8s ease both;
}


.product-enquiry-card:after{
    content:"";
    position:absolute;
    width:170px;
    height:170px;
    right:-90px;
    top:-90px;
    border-radius:50%;
    background:rgba(237,28,36,.035);
    pointer-events:none;
}

.product-enquiry-card h2{
    position:relative;
    z-index:1;
    margin:0 0 12px;
    color:var(--brand);
    font-size:31px;
    line-height:1.15;
    font-weight:800;
    letter-spacing:-.5px;
}

.enquiry-subtitle{
    position:relative;
    z-index:1;
    margin:0 0 13px;
    color:#141414;
    font-size:16px;
    line-height:1.65;
}


/* =========================================================
   FORM
========================================================= */

.product-enquiry-form{
    position:relative;
    z-index:2;
    width:100%;
}

.form-group{
    position:relative;
    margin-bottom:10px;
}

.form-group label{
    display:block;
    margin-bottom:8px;
    color:#292929;
    font-size:14px;
    font-weight:700;
    letter-spacing:.2px;
}

.form-group input,
.form-group textarea{
    width:100%;
    border:1px solid #dce1e6;
    border-radius:10px;
    background:#fafbfc;
    color:#333;
    font-family:inherit;
    font-size:15px;
    padding:15px 17px;
    outline:none;
    box-sizing:border-box;
    transition:
        border-color .25s ease,
        background .25s ease,
        box-shadow .25s ease,
        transform .25s ease;
}

.form-group input{
    height:45px;
}

.form-group textarea{
    min-height:120px;
    resize:vertical;
}

.form-group input::placeholder,
.form-group textarea::placeholder{
    color:#9299a1;
}

.form-group input:hover,
.form-group textarea:hover{
    background:#fff;
    border-color:#cbd2d9;
}

.form-group input:focus,
.form-group textarea:focus{
    background:#fff;
    border-color:var(--brand);
    transform:translateY(-1px);
    box-shadow:
        0 0 0 3px rgba(237,28,36,.07),
        0 8px 22px rgba(20,30,40,.055);
}


/* =========================================================
   ENQUIRY BUTTON
========================================================= */

.product-enquiry-btn{
    position:relative;
    overflow:hidden;
    width:100%;
    height:58px;
    border:0;
    border-radius:10px;
    background:var(--brand);
    color:#fff;
    font-size:17px;
    font-weight:700;
    cursor:pointer;
    box-shadow:
        0 10px 24px rgba(237,28,36,.18);
    transition:
        transform .25s ease,
        box-shadow .25s ease,
        background .25s ease;
}

.product-enquiry-btn:before{
    content:"";
    position:absolute;
    top:0;
    left:-120%;
    width:80%;
    height:100%;
    background:linear-gradient(
        90deg,
        transparent,
        rgba(255,255,255,.24),
        transparent
    );
    transform:skewX(-20deg);
}

.product-enquiry-btn:hover{
    background:var(--brand);
    transform:translateY(-3px);
    box-shadow:
        0 15px 30px rgba(237,28,36,.25);
}

.product-enquiry-btn:hover:before{
    animation:buttonShine .8s ease;
}

.product-enquiry-btn:active{
    transform:translateY(-1px);
}


/* =========================================================
   ENQUIRY MESSAGES
========================================================= */

.enquiry-success,
.enquiry-error{
    position:relative;
    margin-bottom:18px;
    padding:14px 16px;
    border-radius:10px;
    font-size:14px;
    font-weight:600;
    animation:messageSlide .45s ease both;
}

.enquiry-success{
    background:#eefaf2;
    border:1px solid #c4e8cf;
    color:#18733a;
}

.enquiry-error{
    background:#fff3f3;
    border:1px solid #f1c7c7;
    color:#b42318;
}


/* =========================================================
   LOWER AREA
========================================================= */

.product-detail-lower{
    display:grid;
    grid-template-columns:
        minmax(0, 1.7fr)
        minmax(350px, .95fr);
    gap:65px;
    margin-top:40px;
    align-items:start;
}


/* =========================================================
   DESCRIPTION
========================================================= */

.product-detail-description-column{
    min-width:0;
}

.product-detail-description-content{
    position:relative;
    color:#202020;
}

.product-short-description{
    color:#141414;
    font-size:17px;
    line-height:1.85;
    margin-bottom:12px;
}

.product-description{
    color:#202020;
    font-size:16px;
    line-height:1.9;
}

.product-description p{
    margin-bottom:25px;
}

.product-description strong{
    font-weight:800;
    color:#171717;
}

.product-description h2,
.product-description h3{
    position:relative;
    margin-top:40px;
    margin-bottom:20px;
    color:#111;
    font-weight:800;
}

.product-description h2{
    font-size:30px;
}

.product-description h3{
    font-size:24px;
}

.product-description ul{
    padding-left:20px;
}

.product-description li{
    margin-bottom:10px;
}


/* =========================================================
   DETAIL BLOCK
========================================================= */

.product-detail-block{
    position:relative;
    margin-top:50px;
}

.product-detail-block h2{
    position:relative;
    display:inline-block;
    margin:0 0 28px;
    color:#111;
    font-size:29px;
    line-height:1.2;
    font-weight:800;
}

.product-detail-block h2:after{
    content:"";
    position:absolute;
    left:0;
    bottom:-9px;
    width:45px;
    height:3px;
    border-radius:5px;
    background:var(--brand);
}


/* =========================================================
   APPLICATIONS
========================================================= */

.product-application-list{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:16px;
}

.application-item{
    position:relative;
    display:flex;
    align-items:center;
    gap:13px;
    min-height:65px;
    padding:16px 18px;
    overflow:hidden;
    background:linear-gradient(
        135deg,
        #f8fafc,
        #f2f5f7
    );
    border:1px solid #e5e9ed;
    border-radius:11px;
    font-size:16px;
    color:#303030;
    transition:
        transform .3s ease,
        border-color .3s ease,
        box-shadow .3s ease,
        background .3s ease;
}

.application-item:before{
    content:"";
    position:absolute;
    left:0;
    top:0;
    width:3px;
    height:100%;
    background:var(--brand);
    transform:scaleY(0);
    transform-origin:center;
    transition:transform .3s ease;
}

.application-item:hover{
    background:#fff;
    border-color:rgba(237,28,36,.25);
    transform:translateY(-4px);
    box-shadow:0 12px 28px rgba(20,35,50,.08);
}

.application-item:hover:before{
    transform:scaleY(1);
}

.application-item i{
    color:var(--brand);
    font-size:19px;
    flex-shrink:0;
}


/* =========================================================
   BROCHURE
========================================================= */

.product-brochure{
    margin-top:35px;
}


/* =========================================================
   LATEST PRODUCTS
========================================================= */

.latest-products-sidebar{
    width:100%;
}

.latest-products-box{
    position:relative;
    border:1px solid #e2e6ea;
    background:#fff;
    border-radius:16px;
    overflow:hidden;
    box-shadow:
        0 18px 45px rgba(18,35,52,.07);
}

.latest-products-title{
    position:relative;
    padding:24px 25px;
    color:#151515;
    font-size:27px;
    line-height:1.2;
    font-weight:800;
    border-bottom:1px solid #e7e9eb;
    background:
        linear-gradient(
            135deg,
            #fff,
            #f8fafb
        );
}

/* .latest-products-title:after{
    content:"";
    position:absolute;
    left:25px;
    bottom:-2px;
    width:45px;
    height:3px;
    background:var(--brand);
} */

.latest-products-list{
    width:100%;
}

.latest-product-item{
    position:relative;
    display:flex;
    align-items:center;
    gap:18px;
    min-height:120px;
    padding:19px 22px;
    border-bottom:1px solid #e9ebed;
    text-decoration:none;
    background:#fff;
    overflow:hidden;
    transition:
        background .3s ease,
        padding-left .3s ease;
}

.latest-product-item:last-child{
    border-bottom:0;
}

/* .latest-product-item:before{
    content:"";
    position:absolute;
    left:0;
    top:0;
    width:3px;
    height:100%;
    background:var(--brand);
    transform:scaleY(0);
    transition:transform .3s ease;
} */

.latest-product-item:hover{
    background:#f8fafc;
    padding-left:27px;
}

.latest-product-item:hover:before{
    transform:scaleY(1);
}

.latest-product-image{
    width:145px;
    height:100px;
    flex:0 0 145px;
    overflow:hidden;
    border-radius:10px;
    background:#f1f3f5;
    border:1px solid #e6e9ec;
}

.latest-product-image img{
    width:100%;
    height:100%;
    display:block;
    object-fit:cover;
    transition:transform .5s cubic-bezier(.2,.7,.2,1);
}

.latest-product-item:hover .latest-product-image img{
    transform:scale(1.08);
}

.latest-product-name{
    color:#292929;
    font-size:16px;
    line-height:1.4;
    font-weight:700;
    transition:color .25s ease;
}

.latest-product-item:hover .latest-product-name{
    color:var(--brand);
}

.latest-product-empty{
    padding:30px;
    color:#777;
    text-align:center;
}


/* =========================================================
   KEYFRAME ANIMATIONS
========================================================= */

@keyframes heroContent{
    from{
        opacity:0;
        transform:translateY(25px);
    }
    to{
        opacity:1;
        transform:translateY(0);
    }
}

@keyframes cardReveal{
    from{
        opacity:0;
        transform:translateY(25px);
    }
    to{
        opacity:1;
        transform:translateY(0);
    }
}

@keyframes messageSlide{
    from{
        opacity:0;
        transform:translateY(-10px);
    }
    to{
        opacity:1;
        transform:translateY(0);
    }
}

@keyframes heroOrbit{
    from{
        transform:rotate(0deg);
    }
    to{
        transform:rotate(360deg);
    }
}

@keyframes gradientMove{
    0%{
        background-position:0% 50%;
    }
    100%{
        background-position:200% 50%;
    }
}

@keyframes buttonShine{
    0%{
        left:-120%;
    }
    100%{
        left:140%;
    }
}


/* =========================================================
   TABLET
========================================================= */

@media(max-width:1199px){

    .product-detail-top{
        grid-template-columns:
            minmax(0, 1.4fr)
            minmax(360px, 1fr);
        gap:40px;
    }

    .product-detail-lower{
        gap:40px;
    }

    .product-detail-main-image{
        height:550px;
    }

    .product-enquiry-card{
        padding:30px;
    }

    .product-enquiry-card h2{
        font-size:30px;
    }

    .latest-product-image{
        width:125px;
        height:90px;
        flex-basis:125px;
    }

    .latest-product-name{
        font-size:15px;
    }
}


/* =========================================================
   MOBILE / TABLET
========================================================= */

@media(max-width:991px){

    .product-detail-top{
        grid-template-columns:1fr;
        gap:40px;
    }

    .product-detail-lower{
        grid-template-columns:1fr;
        gap:50px;
    }

    .product-detail-main-image{
        height:500px;
    }

    .product-enquiry-card{
        max-width:100%;
    }
}


/* =========================================================
   MOBILE
========================================================= */

@media(max-width:767px){

    .product-detail-section{
        padding:50px 0 65px;
    }

    .product-detail-main-image{
        height:390px;
        border-radius:15px;
    }

    .product-enquiry-card{
        padding:28px 20px 30px;
        border-radius:15px;
    }

    .product-enquiry-card h2{
        font-size:27px;
    }

    .enquiry-subtitle{
        font-size:15px;
        margin-bottom:25px;
    }

    .form-group{
        margin-bottom:16px;
    }

    .form-group label{
        font-size:14px;
    }

    .form-group input{
        height:56px;
    }

    .form-group input,
    .form-group textarea{
        font-size:15px;
        padding:14px 15px;
    }

    .form-group textarea{
        min-height:125px;
    }

    .product-enquiry-btn{
        height:58px;
        font-size:16px;
    }

    .product-short-description{
        font-size:16px;
        line-height:1.75;
    }

    .product-description{
        font-size:16px;
        line-height:1.8;
    }

    .product-detail-block{
        margin-top:40px;
    }

    .product-detail-block h2{
        font-size:25px;
    }

    .product-application-list{
        grid-template-columns:1fr;
        gap:12px;
    }

    .application-item{
        font-size:15px;
        min-height:60px;
    }

    .latest-products-title{
        padding:20px;
        font-size:24px;
    }

    .latest-products-title:after{
        left:20px;
    }

    .latest-product-item{
        padding:15px;
        gap:14px;
    }

    .latest-product-item:hover{
        padding-left:19px;
    }

    .latest-product-image{
        width:100px;
        height:78px;
        flex-basis:100px;
    }

    .latest-product-name{
        font-size:15px;
    }
}


/* =========================================================
   SMALL MOBILE
========================================================= */

@media(max-width:480px){

    .product-detail-main-image{
        height:330px;
        border-radius:12px;
    }

    .product-enquiry-card{
        padding:24px 17px 26px;
    }

    .product-enquiry-card h2{
        font-size:24px;
    }

    .latest-product-image{
        width:85px;
        height:68px;
        flex-basis:85px;
    }

    .latest-product-name{
        font-size:14px;
    }
}


/* =========================================================
   ACCESSIBILITY
========================================================= */

@media(prefers-reduced-motion:reduce){

    .product-detail-hero:after,
    .product-detail-hero .page-hero-inner,
    .product-enquiry-card,
    .product-enquiry-card:before{
        animation:none;
    }

    *,
    *:before,
    *:after{
        transition:none !important;
    }
}


/* =========================================================
   MOBILE PRODUCT PAGE ORDER
   BANNER
   ↓
   PRODUCT CONTENT
   ↓
   IMAGE
   ↓
   KEY FEATURES / APPLICATIONS / OTHER CONTENT
   ↓
   LATEST PRODUCTS
   ↓
   ENQUIRY LAST
========================================================= */

@media (max-width: 767px) {

    /* Main product section */
    .product-detail-section .container {
        display: flex !important;
        flex-direction: column !important;
    }

    /* Allow top and lower sections to share one mobile order */
    .product-detail-top,
    .product-detail-lower {
        display: contents !important;
    }

    /* Allow description column/content to share the same order */
    .product-detail-description-column,
    .product-detail-description-content {
        display: contents !important;
    }

    /* =====================================================
       PRODUCT CONTENT
    ===================================================== */

  .product-short-description { 
    order: 1; 
    width: 100%; 
    margin-bottom: 20px; 
  }

   .product-description { 
     order: 3; 
     width: 100%; 
   }

    /* =====================================================
       PRODUCT IMAGE
    ===================================================== */

    .product-detail-gallery {
        order: 2; 
        width: 100%; 
        margin-top: 25px; 
        margin-bottom: 30px;
     }

    .product-detail-main-image {
        width: 100%;
        height: 330px;
        border-radius: 12px;
    }

    /* =====================================================
       APPLICATIONS
    ===================================================== */

    .product-detail-block:has(.product-application-list) {
        order: 4;
        width: 100%;
    }

    /* =====================================================
       PRODUCT DETAILS / OTHER CONTENT
    ===================================================== */

    .product-detail-block:has(.product-description) {
        order: 5;
        width: 100%;
    }

    /* =====================================================
       BROCHURE
    ===================================================== */

    .product-brochure {
        order: 6;
        width: 100%;
    }

    /* =====================================================
       LATEST PRODUCTS
    ===================================================== */

    .latest-products-sidebar {
        order: 7;
        width: 100%;
        margin-top: 30px;
    }

    /* =====================================================
       ENQUIRY - ALWAYS LAST
    ===================================================== */

    .product-enquiry-card {
        order: 99;
        width: 100%;
        margin-top: 35px;
    }

}




    /* Locations Section */
    .locations-section {
        padding: 70px 0;
        background: #f7f9fc;
    }

    .locations-heading {
        max-width: 750px;
        margin: 0 auto 40px;
        text-align: center;
    }

    .locations-eyebrow {
        display: inline-block;
        margin-bottom: 10px;
        color: var(--brand, #ed1c24);
        font-size: 14px;
        font-weight: 700;
        letter-spacing: 1.5px;
        text-transform: uppercase;
    }

    .locations-heading h2 {
        margin: 0 0 15px;
        color: #172033;
        font-size: clamp(28px, 3vw, 40px);
        font-weight: 800;
        line-height: 1.25;
    }

    .locations-heading p {
        margin: 0;
        color: black;
        font-size: 25px;
        line-height: 1.8;
    }

    /* Location Cards Grid */
    .locations-grid {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 22px;
    }

    /* Individual Location Card */
    .location-card {
        position: relative;
        display: flex;
        align-items: center;
        gap: 15px;
        min-width: 0;
        min-height: 100px;
        padding: 22px;
        overflow: hidden;
        background: #fff;
        border: 1px solid #e2e9f3;
        border-radius: 18px;
        color: #172033;
        text-decoration: none;
        box-shadow: 0 3px 12px rgba(15, 23, 42, .035);
        transition:
            transform .3s ease,
            box-shadow .3s ease,
            border-color .3s ease;
    }

    .location-card:hover {
        transform: translateY(-5px);
        border-color: var(--brand, #ed1c24);
        box-shadow: 0 14px 30px rgba(15, 23, 42, .09);
    }

    /* Location Icon */
    .location-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        flex: 0 0 48px;
        width: 48px;
        height: 48px;
        border-radius: 14px;
        background: #fff0f0;
        color: var(--brand, #ed1c24);
        font-size: 23px;
        transition: background .3s ease, color .3s ease;
    }

    .location-card:hover .location-icon {
        background: var(--brand, #ed1c24);
        color: #fff;
    }

    /* Location Name */
    .location-card h3 {
        margin: 0;
        color: #202b3c;
        font-size: 17px;
        font-weight: 700;
        line-height: 1.5;
        overflow-wrap: anywhere;
        transition: color .3s ease;
    }

    .location-card:hover h3 {
        color: var(--brand, #ed1c24);
    }

    /* Optional Explore Link */
    .location-link {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        margin-left: auto;
        color: #64748b;
        font-size: 13px;
        font-weight: 600;
        white-space: nowrap;
    }

    .location-card:hover .location-link {
        color: var(--brand, #ed1c24);
    }

    /* Tablet */
    @media (max-width: 1199.98px) {
        .locations-grid {
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 18px;
        }

        .location-card {
            padding: 18px;
        }
    }

    /* Mobile */
    @media (max-width: 767.98px) {
        .locations-section {
            padding: 48px 0;
        }

        .locations-heading {
            margin-bottom: 28px;
        }

        .locations-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 14px;
        }

        .location-card {
            align-items: flex-start;
            flex-direction: column;
            gap: 12px;
            min-height: 135px;
            padding: 18px;
            border-radius: 15px;
        }

        .location-icon {
            width: 42px;
            height: 42px;
            flex-basis: 42px;
            font-size: 20px;
        }

        .location-card h3 {
            font-size: 15px;
        }

        .location-link {
            margin-left: 0;
        }
    }

    /* Small Mobile */
    @media (max-width: 420px) {
        .locations-section .container {
            padding-left: 16px;
            padding-right: 16px;
        }

        .locations-grid {
            gap: 12px;
        }

        .location-card {
            padding: 14px;
            min-height: 125px;
        }

        .location-card h3 {
            font-size: 14px;
        }
    }


</style>


<section
    class="page-hero product-detail-hero"
    style="
        background-image:
            linear-gradient(
                90deg,
                rgba(0,0,0,.82) 0%,
                rgba(0,0,0,.52) 50%,
                rgba(0,0,0,.25) 100%
            ),
            url('<?= $site['base_url'] ?><?= e($contact_banner) ?>');
    ">

    <div class="container-fluid px-lg-5">

        <div class="page-hero-inner">

            <!-- <span class="eyebrow">
                <?= e($category_name) ?>
            </span> -->

            <h1>
                <?= e($product_name) ?>
            </h1>

            <div class="product-detail-breadcrumb">

                <a href="<?= $site['base_url'] ?>index.php">
                    Home
                </a>

                <i class="bi bi-chevron-right"></i>

                <a href="<?= $site['base_url'] ?>products.php">
                    Products
                </a>

                <i class="bi bi-chevron-right"></i>

                <span>
                    <?= e($product_name) ?>
                </span>

            </div>

        </div>

    </div>

</section>


<section class="product-detail-section">

    <div class="container">

        <div class="product-detail-top">


            <!-- PRODUCT IMAGE -->

            <div class="product-detail-gallery">

                <div class="product-detail-main-image">

                    <?php if (!empty($product_image)): ?>

                        <img
                            src="<?= rtrim($site['base_url'], '/') ?>/admin/assets/images/products/<?= e($product_image) ?>"
                            alt="<?= e($product_name) ?>">

                    <?php else: ?>

                        <img
                            src="<?= rtrim($site['base_url'], '/') ?>/admin/assets/images/products/default.jpg"
                            alt="<?= e($product_name) ?>">

                    <?php endif; ?>

                </div>

            </div>


            <!-- ENQUIRY FORM -->

            <div class="product-enquiry-card">

                <h2>
                    Product Enquiry
                </h2>

                <p class="enquiry-subtitle">
                    Interested in this product? Send us your enquiry.
                </p>


                <?php if (!empty($enquiry_success)): ?>

                    <div class="enquiry-success">
                        <?= e($enquiry_success) ?>
                    </div>

                <?php endif; ?>


                <?php if (!empty($enquiry_error)): ?>

                    <div class="enquiry-error">
                        <?= e($enquiry_error) ?>
                    </div>

                <?php endif; ?>


                <form
                    action=""
                    method="POST"
                    class="product-enquiry-form">

                    <input
                        type="hidden"
                        name="product_enquiry"
                        value="1">


                    <div class="form-group">

                        <label>
                            Name *
                        </label>

                        <input
                            type="text"
                            name="name"
                            placeholder="Enter your name"
                            value="<?= e(
                                        $_POST['name'] ?? ''
                                    ) ?>"
                            required>

                    </div>


                    <div class="form-group">

                        <label>
                            Email *
                        </label>

                        <input
                            type="email"
                            name="email"
                            placeholder="Enter your email"
                            value="<?= e(
                                        $_POST['email'] ?? ''
                                    ) ?>"
                            required>

                    </div>


                    <div class="form-group">

                        <label>
                            Phone *
                        </label>

                        <input
                            type="tel"
                            name="phone"
                            placeholder="Enter your phone number"
                            value="<?= e(
                                        $_POST['phone'] ?? ''
                                    ) ?>"
                            required>

                    </div>


                    <div class="form-group">

                        <label>
                            Product *
                        </label>

                        <input
                            type="text"
                            name="product_name"
                            value="<?= e($product_name) ?>"
                            readonly
                            required>

                    </div>


                    <div class="form-group">

                        <label>
                            Message
                        </label>

                        <textarea
                            name="message"
                            rows="4"
                            placeholder="Enter your enquiry"><?= e(
                                                                    $_POST['message'] ?? ''
                                                                ) ?></textarea>

                    </div>


                    <button
                        type="submit"
                        class="product-enquiry-btn">

                        Send Enquiry

                    </button>

                </form>

            </div>

        </div>


        <!-- LOWER CONTENT -->

        <div class="product-detail-lower">


            <!-- PRODUCT DESCRIPTION -->

            <div class="product-detail-description-column">

                <div class="product-detail-description-content">


                    <?php if (!empty($product_short_description)): ?>

                        <div class="product-short-description">

                            <?= nl2br(
                                e($product_short_description)
                            ) ?>

                        </div>

                    <?php endif; ?>


                    <?php if (!empty($product_description)): ?>

                        <div class="product-description">

                            <?= $product_description ?>

                        </div>

                    <?php endif; ?>


                    <?php if (!empty($applications)): ?>

                        <div class="product-detail-block">

                            <h2>
                                Applications
                            </h2>

                            <div class="product-application-list">

                                <?php foreach (
                                    $applications
                                    as $application
                                ): ?>

                                    <?php

                                    if (is_array($application)) {

                                        $application_text =
                                            $application['name']
                                            ?? $application['title']
                                            ?? $application['value']
                                            ?? '';
                                    } else {

                                        $application_text =
                                            $application;
                                    }

                                    $application_text =
                                        trim(
                                            (string)$application_text
                                        );

                                    if (
                                        $application_text === ''
                                    ) {
                                        continue;
                                    }

                                    ?>

                                    <div class="application-item">

                                        <i class="bi bi-check2"></i>

                                        <span>

                                            <?= e(
                                                $application_text
                                            ) ?>

                                        </span>

                                    </div>

                                <?php endforeach; ?>

                            </div>

                        </div>

                    <?php endif; ?>


                    <?php if (!empty($product_content)): ?>

                        <div class="product-detail-block">

                            <h2>
                                Product Details
                            </h2>

                            <div class="product-description">

                                <?= $product_content ?>

                            </div>

                        </div>

                    <?php endif; ?>


                    <?php if (!empty($brochure)): ?>

                        <div class="product-brochure">

                            <a
                                href="<?= e($brochure) ?>"
                                target="_blank"
                                rel="noopener"
                                class="btn btn-brand">

                                Download Brochure

                                <i class="bi bi-download"></i>

                            </a>

                        </div>

                    <?php endif; ?>

                </div>

            </div>


            <!-- LATEST PRODUCTS -->

            <aside class="latest-products-sidebar"

                <div class="latest-products-box">

                <div class="latest-products-title">
                    Related Products
                </div>


                <div class="latest-products-list">

                    <?php if (!empty($explore_products)): ?>

                        <?php foreach (
                            $explore_products
                            as $latest
                        ): ?>

                            <?php

                            $latest_name =
                                $latest['product_name']
                                ?? 'Product';

                            $latest_slug =
                                $latest['product_slug']
                                ?? '';
                                $latest_category_slug = $latest['top_category_slug'] ?? '';

                            $latest_image =
                                $latest['main_image']
                                ?? '';

                            ?>


                            <a href="<?= rtrim($site['base_url'], '/') ?>/products/<?= e($latest_category_slug) ?>/<?= e($latest_slug) ?>" class="product-category-item"> 

                                <div class="latest-product-image">

                                    <?php if (!empty($latest_image)): ?>

                                        <img
                                            src="<?= rtrim($site['base_url'], '/') ?>/admin/assets/images/products/<?= e($latest_image) ?>"
                                            alt="<?= e($latest_name) ?>"
                                            loading="lazy">

                                    <?php else: ?>

                                        <img
                                            src="<?= rtrim($site['base_url'], '/') ?>/admin/assets/images/products/default.jpg"
                                            alt="<?= e($latest_name) ?>"
                                            loading="lazy">

                                    <?php endif; ?>

                                </div>


                                <div class="latest-product-name">

                                    <?= e(
                                        $latest_name
                                    ) ?>

                                </div>

                            </a>

                        <?php endforeach; ?>

                    <?php else: ?>

                        <div class="latest-product-empty">
                            No products available.
                        </div>

                    <?php endif; ?>

                </div>

        </div>

        </aside>

    </div>

    </div>

</section>





<?php
$productLocations = [];

if (!empty($selected_product['id'])) {
    $stmt = $pdo->prepare("
        SELECT DISTINCT
            l.id,
            l.location_name,
            l.slug
        FROM tbl_service_location AS sl
        INNER JOIN tbl_location AS l
            ON l.id = sl.location_id
        WHERE sl.service_id = ?
          AND l.status = 1
        ORDER BY l.location_name ASC
    ");

    $stmt->execute([
        (int) $selected_product['id']
    ]);

    $productLocations = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>

<?php if (!empty($productLocations)): ?>
    <section class="locations-section">
        <div class="container">

            <div class="locations-heading">
                <!--<h2>-->
                <!--    Locations for <?= e($selected_product['product_name']) ?>-->
                <!--</h2>-->
                <p>Explore the locations available for this product.</p>
            </div>

            <div class="locations-grid">
                <?php foreach ($productLocations as $location): ?>
                   <a
                        href="<?= e(
                            rtrim($site['base_url'], '/') .
                            '/location/' .
                            rawurlencode($selected_product['product_slug']) .
                            '/' .
                            rawurlencode($location['slug'])
                        ) ?>"
                        class="location-card"
                    >
                        <span class="location-icon">
                            <i class="bi bi-geo-alt-fill"></i>
                        </span>
                    
                        <h3><?= e($location['location_name']) ?></h3>
                    </a>
                <?php endforeach; ?>
            </div>

        </div>
    </section>
<?php endif; ?>



<?php require_once __DIR__ . "/includes/footer.php"; ?>