<?php

include("admin/connection.php");
include('include/header.php');
?>
<?php
$slug = $_GET['slug'];
$ser = mysqli_query($con, "SELECT * FROM `service` where slug='$slug'");
while ($row = mysqli_fetch_array($ser)) {
    $title = $row['title'];
    $slug = $row['slug'];
    $Image = $row['Image'];
    $id = $row['id'];
    $description = $row['description'];
}
?>
<style>
    /* Default styles (Desktop first) */
    h1 {
        font-size: 32px !important;
        margin-bottom: 10px;
        margin-top: 10px;
    }

    h2 {
        font-size: 28px !important;
        margin-bottom: 10px;
        margin-top: 10px;
    }

    h3 {
        font-size: 22px !important;
        margin-bottom: 10px;
        margin-top: 10px;
    }

    h4 {
        font-size: 16px !important;
        margin-bottom: 10px;
        margin-top: 10px;
    }

    /* Mobile View */
    @media (max-width: 768px) {
        h1 {
            font-size: 24px !important;
        }

        h2 {
            font-size: 20px !important;
        }

        h3 {
            font-size: 16px !important;
        }

        h4 {
            font-size: 14px !important;
        }
    }

    .blog-details__left p {
        text-align: justify;
    }

    .card-title {
        margin-bottom: 0.5rem;
        font-size: 17px;
    }
</style>
<!--Page Header Start-->
<section class="page-header">
    <div class="page-header__bg" style="background-image: url(<?php path() ?>assets/images/backgrounds/page-header-bg.jpg);">
    </div>
    <div class="container">
        <div class="page-header__inner">

            <h3><?php echo htmlspecialchars($title); ?></h3>
            <div class="thm-breadcrumb__inner">
                <ul class="thm-breadcrumb list-unstyled">
                    <li><a href="<?php path() ?>index.php">Home</a></li>
                    <li><span class="fas fa-angle-right"></span></li>
                    <li>Product Details</li>
                </ul>
            </div>
        </div>
    </div>
</section>
<!--Page Header End-->

<!--Blog Details Start -->
<section class="blog-details blog-details__left-sidebar">
    <div class="container">
        <div class="row">
            <div class="col-xl-8 col-lg-7">
                <?php
                // Fetch service details by ID (make sure $id is defined earlier)
                $ser = mysqli_query($con, "SELECT * FROM `service` WHERE id = '$id'");
                if ($row = mysqli_fetch_array($ser)) {
                    $Image = $row['Image'];
                    $title = $row['title'];
                    $description = $row['description'];
                ?>
                    <div class="blog-details__left">
                        <div class="blog-details__img-box-1">
                            <div class="blog-details__img">
                                <img src="<?php path(); ?>admin/assets/images/service/<?php echo htmlspecialchars($Image); ?>" alt="">
                            </div>

                        </div>
                        <div class="blog-details__content">
                            <p class="blog-details__text-1"><?php echo $description; ?></p>
                        </div>
                    </div>
                <?php
                }
                ?>
            </div>
            <!--Start Sidebar-->
            <div class="col-xl-4 col-lg-5">
                <div class="sidebar">
                    <!--Start Sidebar Single-->
                    <div class="sidebar__single sidebar__category wow fadeInUp" data-wow-delay=".1s">
                        <h3 class="sidebar__title">All Product</h3>
                        <ul class="sidebar__category-list list-unstyled">
                            <?php
                            // Fetch top categories
                            $ser = mysqli_query(
                                $con,
                                "SELECT slug, title FROM `service` ORDER BY id ASC"
                            );

                            // Check if records exist
                            if ($ser && mysqli_num_rows($ser) > 0) {

                                // Current slug (for active class)
                                $currentSlug = $_GET['slug'] ?? '';

                                while ($s = mysqli_fetch_assoc($ser)) {

                                    $slug = urlencode($s['slug']);

                                    // Escape full title
                                    $fullTitle = htmlspecialchars($s['title'], ENT_QUOTES, 'UTF-8');

                                    // Limit to 3 words
                                    $words = explode(' ', $fullTitle);
                                    if (count($words) > 3) {
                                        $limitedTitle = implode(' ', array_slice($words, 0, 3)) . '...';
                                    } else {
                                        $limitedTitle = $fullTitle;
                                    }

                                    // Active class logic
                                    $activeClass = ($currentSlug === $s['slug']) ? 'active' : '';
                            ?>
                                    <li class="<?php echo $activeClass; ?>">
                                        <a href="<?php path(); ?>product/<?php echo $slug; ?>">
                                            <?php echo $limitedTitle; ?>
                                            <span class="icon-next"></span>
                                        </a>
                                    </li>
                                <?php
                                }
                            } else {
                                ?>
                                <li>No products found</li>
                            <?php } ?>
                        </ul>
                    </div>
                    <!--End Sidebar Single-->
                </div>
            </div>
            <!--End Sidebar-->


        </div>



        <div class="row mt-5 text-center">
            <?php
            $services = json_decode($row['services_data'], true); // $row from your query
            if (!empty($services)) {
                foreach ($services as $service) {
                    $Ser_img = isset($service['Ser_img']) ? $service['Ser_img'] : '';
                    $content = isset($service['content']) ? $service['content'] : '';
            ?>
                    <div class="col-lg-3 mb-3">
                        <div class="card">
                            <img src="<?php path(); ?>/admin/assets/images/service/<?php echo htmlspecialchars($Ser_img); ?>" class="card-img-top" alt="...">
                            <div class="card-body">
                                <h5 class="card-title"><?php echo htmlspecialchars($content); ?></h5>

                            </div>
                        </div>
                    </div>
            <?php
                }
            }
            ?>
        </div>
    </div>
</section>
<!--Blog Details Start-->

<?php
include("include/footer.php");
?>