<?php

declare(strict_types=1);

/**
 * 店家頁 Tab：推薦技師、店家照片、OSM 地圖。
 * 依賴 store_main.php：$pdo、$sid、$photoUrls、$fateIncludeStoreMap、$storeMap*、$name、$fatePublicMember。
 */

$storeTabPhotoUrls = isset($photoUrls) && is_array($photoUrls) ? $photoUrls : [];
$storeTabPhotoCount = count($storeTabPhotoUrls);
$storeTabHasPhotos = $storeTabPhotoCount > 0;

$storeTabHasMap = !empty($fateIncludeStoreMap)
    && isset($storeMapEmbedUrl, $storeMapLat, $storeMapLng)
    && $storeMapEmbedUrl !== null
    && $storeMapLat !== null
    && $storeMapLng !== null;

$storeTabHasMasseurs = false;
$storeTabMasseurCount = 0;
if (isset($pdo) && $pdo instanceof PDO) {
    $storeTabStoreId = isset($sid) ? (int) $sid : (isset($storeId) ? (int) $storeId : 0);
    if ($storeTabStoreId > 0) {
        require_once dirname(__DIR__, 3) . '/includes/fate_masseur_rewards.php';
        require_once dirname(__DIR__, 3) . '/includes/fate_store_masseurs.php';
        $storeTabRewards = fate_masseur_rewards_settings();
        if ($storeTabRewards['feature_enabled']) {
            $storeTabHasMasseurs = true;
            fate_masseur_ensure_tables($pdo);
            $viewerId = is_array($fatePublicMember ?? null) ? (int) ($fatePublicMember['id'] ?? 0) : 0;
            $storeTabMasseurCount = count(fate_masseur_list_for_store($pdo, $storeTabStoreId, $viewerId));
        }
    }
}

if (!$storeTabHasMap && !$storeTabHasMasseurs && !$storeTabHasPhotos) {
    return;
}

$storeTabCount = ($storeTabHasMasseurs ? 1 : 0) + ($storeTabHasPhotos ? 1 : 0) + ($storeTabHasMap ? 1 : 0);
$storeTabShowBar = $storeTabCount >= 2;

if ($storeTabHasMasseurs) {
    $storeTabDefault = 'masseur';
} elseif ($storeTabHasPhotos) {
    $storeTabDefault = 'photos';
} else {
    $storeTabDefault = 'map';
}

$osmBrowseUrl = null;
$fateStoreMapH = 360;
if ($storeTabHasMap) {
    require_once dirname(__DIR__, 3) . '/includes/fate_store_map.php';
    $osmBrowseUrl = fate_store_map_osm_browse_url($storeMapLat, $storeMapLng);
    $fateStoreMapH = fate_store_map_height();
}
?>
<section class="store-card-fate store-card-fate--block store-extra-tabs" aria-label="推薦技師、照片與位置">
    <?php if ($storeTabShowBar): ?>
        <div class="store-tabs" role="tablist" aria-label="店家延伸資訊">
            <?php if ($storeTabHasMasseurs): ?>
                <button type="button" class="store-tabs__btn<?= $storeTabDefault === 'masseur' ? ' is-active' : '' ?>" role="tab" id="store-tab-btn-masseur" aria-selected="<?= $storeTabDefault === 'masseur' ? 'true' : 'false' ?>" aria-controls="store-tab-panel-masseur" data-store-tab="masseur"<?= $storeTabDefault !== 'masseur' ? ' tabindex="-1"' : '' ?>>
                    推薦技師<?php if ($storeTabMasseurCount > 0): ?><span class="store-tabs__badge"><?= (int) $storeTabMasseurCount ?></span><?php endif; ?>
                </button>
            <?php endif; ?>
            <?php if ($storeTabHasPhotos): ?>
                <button type="button" class="store-tabs__btn<?= $storeTabDefault === 'photos' ? ' is-active' : '' ?>" role="tab" id="store-tab-btn-photos" aria-selected="<?= $storeTabDefault === 'photos' ? 'true' : 'false' ?>" aria-controls="store-tab-panel-photos" data-store-tab="photos"<?= $storeTabDefault !== 'photos' ? ' tabindex="-1"' : '' ?>>
                    店家照片<span class="store-tabs__badge"><?= (int) $storeTabPhotoCount ?></span>
                </button>
            <?php endif; ?>
            <?php if ($storeTabHasMap): ?>
                <button type="button" class="store-tabs__btn<?= $storeTabDefault === 'map' ? ' is-active' : '' ?>" role="tab" id="store-tab-btn-map" aria-selected="<?= $storeTabDefault === 'map' ? 'true' : 'false' ?>" aria-controls="store-tab-panel-map" data-store-tab="map"<?= $storeTabDefault !== 'map' ? ' tabindex="-1"' : '' ?>>
                    位置地圖
                </button>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <?php if ($storeTabHasMasseurs): ?>
        <div class="store-tab-panel<?= $storeTabDefault === 'masseur' ? ' is-active' : '' ?>" role="tabpanel" id="store-tab-panel-masseur" aria-labelledby="store-tab-btn-masseur"<?= $storeTabDefault !== 'masseur' ? ' hidden' : '' ?>>
            <?php
            $masseurInTab = true;
            require __DIR__ . DIRECTORY_SEPARATOR . 'store_masseurs.php';
            ?>
        </div>
    <?php endif; ?>

    <?php if ($storeTabHasPhotos): ?>
        <div class="store-tab-panel<?= $storeTabDefault === 'photos' ? ' is-active' : '' ?>" role="tabpanel" id="store-tab-panel-photos" aria-labelledby="store-tab-btn-photos"<?= $storeTabDefault !== 'photos' ? ' hidden' : '' ?>>
            <?php require __DIR__ . DIRECTORY_SEPARATOR . 'store_photos_tab.php'; ?>
        </div>
    <?php endif; ?>

    <?php if ($storeTabHasMap): ?>
        <div class="store-tab-panel<?= $storeTabDefault === 'map' ? ' is-active' : '' ?>" role="tabpanel" id="store-tab-panel-map" aria-labelledby="store-tab-btn-map"<?= $storeTabDefault !== 'map' ? ' hidden' : '' ?>>
            <div class="store-tab-panel__inner store-map-tab" aria-label="位置">
                <div class="store-map-embed">
                    <iframe
                        src="<?= htmlspecialchars($storeMapEmbedUrl, ENT_QUOTES, 'UTF-8') ?>"
                        title="<?= htmlspecialchars($name . ' — OpenStreetMap 地圖', ENT_QUOTES, 'UTF-8') ?>"
                        width="100%"
                        height="<?= (int) $fateStoreMapH ?>"
                        style="width:100%;height:<?= (int) $fateStoreMapH ?>px;border:0;display:block;"
                        loading="lazy"
                        referrerpolicy="no-referrer-when-downgrade"></iframe>
                    <?php if ($name !== ''): ?>
                        <p class="store-map-marker-label" aria-label="地圖標記：<?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>">
                            <span class="store-map-marker-label__text"><?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?></span>
                        </p>
                    <?php endif; ?>
                </div>
                <p class="store-map-caption">
                    地圖資料 © <a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener noreferrer">OpenStreetMap</a> 貢獻者 ·
                    <a href="<?= htmlspecialchars($osmBrowseUrl, ENT_QUOTES, 'UTF-8') ?>" target="_blank" rel="noopener noreferrer">全螢幕瀏覽</a>
                </p>
            </div>
        </div>
    <?php endif; ?>
</section>
<?php if ($storeTabShowBar || $storeTabHasPhotos): ?>
<script>
(function () {
    var root = document.querySelector('.store-extra-tabs');
    if (!root) return;

    var btns = root.querySelectorAll('[data-store-tab]');
    var panels = root.querySelectorAll('.store-tab-panel');
    if (btns.length) {
        function activate(name) {
            btns.forEach(function (b) {
                var on = b.getAttribute('data-store-tab') === name;
                b.classList.toggle('is-active', on);
                b.setAttribute('aria-selected', on ? 'true' : 'false');
                b.tabIndex = on ? 0 : -1;
            });
            panels.forEach(function (p) {
                var on = (name === 'masseur' && p.id === 'store-tab-panel-masseur')
                    || (name === 'photos' && p.id === 'store-tab-panel-photos')
                    || (name === 'map' && p.id === 'store-tab-panel-map');
                p.classList.toggle('is-active', on);
                if (on) {
                    p.removeAttribute('hidden');
                } else {
                    p.setAttribute('hidden', '');
                }
            });
        }
        btns.forEach(function (btn) {
            btn.addEventListener('click', function () {
                activate(btn.getAttribute('data-store-tab'));
            });
        });
        var hash = (window.location.hash || '').replace(/^#/, '');
        if (hash.indexOf('masseur') === 0) {
            activate('masseur');
        } else if (hash === 'photos' || hash === 'storePhotos') {
            activate('photos');
        }
    }

    var photoBtn = document.getElementById('storePhotosToggleFate');
    var photoExtras = root.querySelectorAll('.store-photo-tile-fate--extra');
    if (photoBtn && photoExtras.length) {
        photoBtn.addEventListener('click', function () {
            var open = photoBtn.getAttribute('aria-expanded') === 'true';
            open = !open;
            photoBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
            photoBtn.textContent = open ? '收合照片' : '顯示更多照片';
            photoExtras.forEach(function (el) {
                el.classList.toggle('store-photo-tile-fate--extra-visible', open);
            });
        });
    }
})();
</script>
<?php endif; ?>
<?php
$fateImageLbJs = dirname(__DIR__, 3) . '/static/fate-image-lightbox.js';
$fateImageLbVer = is_file($fateImageLbJs) ? (string) filemtime($fateImageLbJs) : '1';
?>
<script src="./static/fate-image-lightbox.js?v=<?= htmlspecialchars($fateImageLbVer, ENT_QUOTES, 'UTF-8') ?>" defer></script>
