<?php
include("admin/connection.php");
include('include/header.php');

$slug = $_GET['slug'];
$loc = mysqli_query($con, "SELECT * FROM `locations` where slug='$slug'");
while ($row = mysqli_fetch_array($loc)) {
    $location_name = $row['location_name'];
    $slug = $row['slug'];
    $Image = $row['Image'];
    $id = $row['id'];
    $description = $row['description'];
}
?>
<style>
    p {
        margin-bottom: 15px;
        text-align: justify;
    }

    .service-details__img img {
        aspect-ratio: 2 / 1;
    }

    h3 {
        margin-bottom: 15px;
        margin-top: 15px;
    }
</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="page-header__shape-1">
        <img src="<?php path() ?>assets/images/shapes/page-header-shape-1.png" alt="">
    </div>
    <div class="container">
        <div class="page-header__inner">
            <h3><?php echo $location_name; ?></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><?php echo $location_name; ?></li>
                </ul>
            </div>
        </div>
    </div>
</section>
<!--Page Header End-->

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

                    </div>
                </div>
            <?php
            }
            ?>
            <div class="col-xl-4 col-lg-5">
                <div class="service-details__sidebar">
                    <div class="service-details__services-box">
                        <h3 class="service-details__services-title">All Location</h3>
                        <ul class="sidebar__category-list list-unstyled">
                            <?php
                            // Fetch top categories
                            $loc = mysqli_query(
                                $con,
                                "SELECT slug, location_name FROM locations ORDER BY id DESC LIMIT 10"
                            );

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

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

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

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

                                    // Escape full title
                                    $fullTitle = htmlspecialchars($s['location_name'], 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(); ?>locations/<?php echo $slug; ?>">
                                            <?php echo $limitedTitle; ?>
                                            <span class="icon-next"></span>
                                        </a>
                                    </li>
                                <?php
                                }
                            } else {
                                ?>
                                <li>No products found</li>
                            <?php } ?>
                        </ul>
                    </div>


                </div>
            </div>
        </div>
    </div>
</section>
<!--Service Details End-->



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