
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require_once __DIR__ . '/admin/inc/config.php';
require_once __DIR__ . '/data/site.php';
require_once __DIR__ . '/data/contact.php';

if (!function_exists('e')) {
    function e($value): string {
        return htmlspecialchars((string)($value ?? ''), ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('locationDecodeHtml')) {
    function locationDecodeHtml($value): string {
        return html_entity_decode((string)($value ?? ''), ENT_QUOTES | ENT_HTML5, 'UTF-8');
    }
}

$productSlug = trim($_GET['product'] ?? '');
$locationSlug = trim($_GET['location'] ?? '');

if ($productSlug === '' || $locationSlug === '') {
    http_response_code(404);
    exit('Location page not found.');
}

$stmt = $pdo->prepare("
    SELECT
        s.*,
        s.id AS service_id,
        l.id AS location_id,
        l.location_name,
        l.slug AS location_slug,
        sl.short_description AS location_short_description,
        sl.content AS location_content,
        sl.description AS location_description
    FROM tbl_service_location AS sl
    INNER JOIN tbl_service AS s ON s.id = sl.service_id
    INNER JOIN tbl_location AS l ON l.id = sl.location_id
    WHERE s.product_slug = ?
      AND l.slug = ?
      AND s.status = 1
      AND l.status = 1
    LIMIT 1
");

$stmt->execute([$productSlug, $locationSlug]);
$page = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$page) {
    http_response_code(404);
    require_once __DIR__ . '/includes/header.php';
    ?>
    <section class="container py-5">
        <h1>Location page not found</h1>
        <p>Please check the product and location URL.</p>
        <a href="<?= e(rtrim($site['base_url'], '/') . '/locations') ?>">View all locations</a>
    </section>
    <?php
    require_once __DIR__ . '/includes/footer.php';
    exit;
}

$baseUrl = rtrim((string)($site['base_url'] ?? ''), '/');

$bannerImage = trim((string)($contact_page['banner_image'] ?? ''));

if ($bannerImage !== '') {
    if (!preg_match('~^https?://~i', $bannerImage) && strpos($bannerImage, '//') !== 0) {
        $bannerImage = '/' . ltrim($bannerImage, '/');
        $bannerImage = $baseUrl . $bannerImage;
    }
}

$productImage = trim((string)($page['main_image'] ?? ''));

$productImageUrl = $baseUrl . '/admin/assets/images/products/' .
    ($productImage !== '' ? rawurlencode(basename($productImage)) : 'default.jpg');

$defaultProductImageUrl = $baseUrl . '/admin/assets/images/products/default.jpg';

$otherLocationsStmt = $pdo->prepare("
    SELECT DISTINCT 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 sl.location_id != ?
      AND l.status = 1
    ORDER BY l.location_name ASC
");

$otherLocationsStmt->execute([$page['service_id'], $page['location_id']]);
$otherLocations = $otherLocationsStmt->fetchAll(PDO::FETCH_ASSOC);

$contentHtml = trim(locationDecodeHtml($page['location_content'] ?? ''));
$descriptionHtml = trim(locationDecodeHtml($page['location_description'] ?? ''));
$shortDescription = trim(locationDecodeHtml($page['location_short_description'] ?? ''));

require_once __DIR__ . '/includes/header.php';
?>

<style>
.location-detail-banner{display:block;width:100%;height:320px;background-color:#f5f5f5;background-position:center center;background-repeat:no-repeat;background-size:cover}
.location-detail-section{padding:45px 0 75px;background:#fff}
.location-detail-layout{display:grid;grid-template-columns:minmax(0,1.65fr) minmax(0,.9fr);gap:36px;align-items:start;width:100%;box-sizing:border-box}
.location-detail-content{min-width:0;width:100%}
.location-product-image{width:100%;margin-bottom:25px;overflow:hidden;background:#f5f5f5}
.location-product-image img{display:block;width:100%;height:auto;max-height:540px;object-fit:contain}
.location-city-label{display:block;margin:0 0 12px;color:#d71968;font-size:23px;line-height:1.4}
.location-detail-content>h1{margin:0 0 20px;color:#454545;font-size:clamp(30px,2vw,48px);font-weight:700;line-height:1.2;overflow-wrap:anywhere}
.location-intro{margin-bottom:28px;color:#333;font-size:18px;line-height:1.7;overflow-wrap:anywhere}
.location-intro p{margin:0 0 18px}
.location-seo-content{color:#333;font-size:17px;line-height:1.8;overflow-wrap:anywhere;word-break:normal}
.location-seo-content h1,.location-seo-content h2{margin:30px 0 15px;color:#333;font-size:30px;font-weight:600;line-height:1.35}
.location-seo-content h3{margin:25px 0 12px;color:#333;font-size:24px;font-weight:600;line-height:1.4}
.location-seo-content h4{margin:22px 0 12px;font-size:21px;font-weight:600}
.location-seo-content p{margin:0 0 20px}
.location-seo-content ul,.location-seo-content ol{margin:0 0 24px;padding-left:26px}
.location-seo-content li{margin-bottom:10px}
.location-seo-content img{display:block;max-width:100%;height:auto;margin:20px 0}
.location-seo-content iframe{max-width:100%}
.location-seo-content table{width:100%;max-width:100%;margin:20px 0;border-collapse:collapse}
.location-seo-content th,.location-seo-content td{padding:10px;border:1px solid #ddd}
.location-sidebar{display:block;width:100%;min-width:0;box-sizing:border-box;padding-left:24px;border-left:8px solid #d71968}
.location-sidebar h2{display:block;width:100%;margin:0 0 22px;color:#454545;font-size:32px;font-weight:500;line-height:1.3;overflow-wrap:normal;word-break:normal}
.other-location-list{display:block;width:100%;min-width:0;margin:0;padding:0;list-style:none}
.other-location-list li{display:block;width:100%;min-width:0;margin:0;padding:0;border-bottom:1px solid #ddd}
.other-location-list li a{display:flex;flex-direction:row;align-items:center;justify-content:flex-start;gap:14px;width:100%;min-width:0;min-height:60px;box-sizing:border-box;padding:12px 0;color:#4288ca;font-size:18px;line-height:1.5;text-decoration:none;transition:color .2s ease}
.other-location-list li a:hover{color:#14527d}
.other-location-pin{display:flex;align-items:center;justify-content:center;flex:0 0 36px;width:36px;height:36px;box-sizing:border-box;border-radius:50%;background:#d71968;color:#fff;font-size:18px}
.other-location-pin i{display:inline-block;line-height:1}
.other-location-name{display:block;flex:1 1 auto;min-width:0;margin:0;padding:0;white-space:normal;overflow-wrap:break-word;word-break:normal}
.no-other-locations{color:#666;font-size:16px;line-height:1.7}
@media(max-width:991px){
.location-detail-layout{grid-template-columns:minmax(0,1.4fr) minmax(0,.9fr);gap:24px}
.location-sidebar{padding-left:18px;border-left-width:5px}
.location-sidebar h2{font-size:27px}
.other-location-list li a{gap:12px;font-size:17px}
.location-intro,.location-seo-content{font-size:17px}
}
@media(max-width:767px){
.location-detail-banner{height:220px}
.location-detail-section{padding:30px 0 50px}
.location-detail-layout{grid-template-columns:minmax(0,1fr);gap:40px}
.location-product-image{margin-bottom:20px}
.location-product-image img{max-height:380px;object-fit:contain}
.location-city-label{font-size:20px}
.location-detail-content>h1{font-size:29px}
.location-intro,.location-seo-content{font-size:16px;line-height:1.8}
.location-seo-content h1,.location-seo-content h2{font-size:26px}
.location-seo-content h3{font-size:22px}
.location-sidebar{padding-left:18px;border-left-width:5px}
.location-sidebar h2{font-size:27px}
.other-location-list li a{min-height:65px;font-size:18px}
}
</style>

<?php if ($bannerImage !== ''): ?>
    <section class="location-detail-banner" style="background-image:url('<?= e($bannerImage) ?>');" aria-label="Page banner"></section>
<?php else: ?>
    <section class="location-detail-banner"></section>
<?php endif; ?>

<section class="location-detail-section">
    <div class="container">
        <div class="location-detail-layout">
            <main class="location-detail-content">
                <div class="location-product-image">
                    <img
                        src="<?= e($productImageUrl) ?>"
                        alt="<?= e($page['product_name']) ?> in <?= e($page['location_name']) ?>"
                        loading="lazy"
                        onerror="this.onerror=null;this.src='<?= e($defaultProductImageUrl) ?>';">
                </div>

                

                <!--<h1>-->
                <!--    <?= e($page['product_name']) ?> Manufacturer in <?= e($page['location_name']) ?>-->
                <!--</h1>-->

                <?php if ($shortDescription !== ''): ?>
                    <div class="location-intro">
                        <?= nl2br(e(strip_tags($shortDescription))) ?>
                    </div>
                <?php endif; ?>

                <div class="location-seo-content">
                    <?php if ($contentHtml !== ''): ?>
                        <?= $contentHtml ?>
                    <?php elseif ($descriptionHtml !== ''): ?>
                        <?= $descriptionHtml ?>
                    <?php endif; ?>
                </div>
            </main>

            <aside class="location-sidebar">
                <h2>Other Locations</h2>

                <?php if (!empty($otherLocations)): ?>
                    <ul class="other-location-list">
                        <?php foreach ($otherLocations as $otherLocation): ?>
                            <li>
                                <a
                                    href="<?= e($baseUrl . '/location/' . rawurlencode($page['product_slug']) . '/' . rawurlencode($otherLocation['slug'])) ?>"
                                    aria-label="<?= e($otherLocation['location_name']) ?>">
                                    <span class="other-location-pin" aria-hidden="true">
                                        <i class="bi bi-geo-alt-fill"></i>
                                    </span>
                                    <span class="other-location-name"><?= e($otherLocation['location_name']) ?></span>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php else: ?>
                    <p class="no-other-locations">No other locations are available for this product.</p>
                <?php endif; ?>
            </aside>
        </div>
    </div>
</section>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
