<?php
require_once __DIR__."/includes/header.php";

$slug=$_GET["slug"]??"";
$selected_product=null;
$parent_category=null;

foreach($products as $category){
    foreach($category["items"] as $item){
        if($item["slug"]===$slug){
            $selected_product=$item;
            $parent_category=$category;
            break 2;
        }
    }
}

if(!$selected_product){
    http_response_code(404);
}
?>

<?php if(!$selected_product): ?>

<section class="page-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="breadcrumb-wrap">
                <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 product range 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 else: ?>

<section class="page-hero product-detail-hero">

    <div class="container-fluid px-lg-5">

        <div class="page-hero-inner">

            <span class="eyebrow">
                <?=htmlspecialchars($parent_category["short_name"])?>
            </span>

            <h1>
                <?=htmlspecialchars($selected_product["name"])?>
            </h1>

            <p>
                <?=htmlspecialchars($selected_product["description"])?>
            </p>

            <div class="breadcrumb-wrap">

                <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($parent_category["slug"])?>">
                    <?=htmlspecialchars($parent_category["short_name"])?>
                </a>

                <i class="bi bi-chevron-right"></i>

                <span>
                    <?=htmlspecialchars($selected_product["name"])?>
                </span>

            </div>

        </div>

    </div>

</section>

<section class="product-detail-section">

    <div class="container-fluid px-lg-5">

        <div class="product-detail-grid">

            <div class="product-detail-image">

                <img
                    src="<?=htmlspecialchars($selected_product["image"])?>"
                    alt="<?=htmlspecialchars($selected_product["name"])?>">

                <div class="product-detail-image-label">

                    <span>MVSK INDUSTRIES LLP</span>

                    <strong>
                        <?=str_pad($parent_category["id"],2,"0",STR_PAD_LEFT)?>
                    </strong>

                </div>

            </div>

            <div class="product-detail-content">

                <span class="eyebrow">Product Overview</span>

                <h2>
                    <?=htmlspecialchars($selected_product["name"])?>
                </h2>

                <div class="section-line"></div>

                <p>
                    <?=htmlspecialchars($selected_product["description"])?>
                </p>

                <?php if(!empty($selected_product["features"])): ?>

                    <div class="product-detail-block">

                        <h3>Key Features</h3>

                        <div class="product-detail-features">

                            <?php foreach($selected_product["features"] as $feature): ?>

                                <div>
                                    <i class="bi bi-check2"></i>
                                    <span>
                                        <?=htmlspecialchars($feature)?>
                                    </span>
                                </div>

                            <?php endforeach; ?>

                        </div>

                    </div>

                <?php endif; ?>

                <?php if(!empty($selected_product["applications"])): ?>

                    <div class="product-detail-block">

                        <h3>Applications</h3>

                        <div class="product-detail-applications">

                            <?php foreach($selected_product["applications"] as $application): ?>

                                <span>
                                    <?=htmlspecialchars($application)?>
                                </span>

                            <?php endforeach; ?>

                        </div>

                    </div>

                <?php endif; ?>

                <div class="product-detail-actions">

                    <a href="contact.php#enquiry" class="btn btn-brand">
                        Request a Quote
                        <i class="bi bi-arrow-right"></i>
                    </a>

                    <a
                        href="product-category.php?slug=<?=urlencode($parent_category["slug"])?>"
                        class="product-back-link">

                        <i class="bi bi-arrow-left"></i>

                        <?=htmlspecialchars($parent_category["short_name"])?>

                    </a>

                </div>

            </div>

        </div>

    </div>

</section>

<?php if(!empty($selected_product["specifications"])): ?>

<section class="product-specifications-section">

    <div class="container-fluid px-lg-5">

        <div class="product-specifications-header">

            <span class="eyebrow">Technical Information</span>

            <h2>
                Product <span>Specifications</span>
            </h2>

        </div>

        <div class="product-specifications-table">

            <?php foreach($selected_product["specifications"] as $specification): ?>

                <div class="product-spec-row">

                    <span>
                        <?=htmlspecialchars($specification["label"])?>
                    </span>

                    <strong>
                        <?=htmlspecialchars($specification["value"])?>
                    </strong>

                </div>

            <?php endforeach; ?>

        </div>

    </div>

</section>

<?php endif; ?>

<section class="product-related-section">

    <div class="container-fluid px-lg-5">

        <div class="product-related-header">

            <div>

                <span class="eyebrow">More From This Category</span>

                <h2>
                    Related <span>Solutions</span>
                </h2>

            </div>

            <a
                href="product-category.php?slug=<?=urlencode($parent_category["slug"])?>"
                class="text-link">

                View Category
                <i class="bi bi-arrow-right"></i>

            </a>

        </div>

        <div class="product-related-grid">

            <?php foreach($parent_category["items"] as $related): ?>

                <?php if($related["slug"]===$selected_product["slug"]) continue; ?>

                <a
                    href="product.php?slug=<?=urlencode($related["slug"])?>"
                    class="product-related-card">

                    <div class="product-related-image">

                        <img
                            src="<?=htmlspecialchars($related["image"])?>"
                            alt="<?=htmlspecialchars($related["name"])?>"
                            loading="lazy">

                    </div>

                    <div class="product-related-content">

                        <span>
                            <?=htmlspecialchars($parent_category["short_name"])?>
                        </span>

                        <h3>
                            <?=htmlspecialchars($related["name"])?>
                        </h3>

                        <i class="bi bi-arrow-up-right"></i>

                    </div>

                </a>

            <?php endforeach; ?>

        </div>

    </div>

</section>

<section class="product-detail-cta">

    <div class="container-fluid px-lg-5">

        <div class="product-detail-cta-inner">

            <div>

                <span class="eyebrow">Have a Requirement?</span>

                <h2>
                    Let's Discuss Your
                    <span>Application</span>
                </h2>

                <p>
                    Talk to our team about your production requirements
                    and explore a suitable solution.
                </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 endif; ?>

<?php
require_once __DIR__."/includes/footer.php";
?>