<?php

declare(strict_types=1);
/** @var list<array<string,mixed>> $stores */
/** @var string|null $dbError */
/** @var string|null $storesError */
/** @var list<string> $cities */
/** @var Closure $buildQuery */
/** @var array{id:int,username:string,display_name:string,avatar_rel:string}|null $fatePublicMember */
/** @var string $storeSearchQ */
/** @var string $storeSearchMode */

/** @var list<array<string,mixed>> $weeklyTopStores */
/** @var string $weeklyStatsRangeLabel */
/** @var bool $fateAdminNavVisible */
/** @var bool $fateForumNavVisible */
/** @var bool $fateBlogNavVisible */
/** @var string $fateBlogNavLabel */
/** @var list<array<string,mixed>> $blogHomeAnnouncements */

if (!isset($fateBlogNavLabel) || $fateBlogNavLabel === '') {
    $fateBlogNavLabel = function_exists('fate_blog_nav_label') ? fate_blog_nav_label() : '公告／文章';
}

$fateSearchActive = ($storeSearchQ ?? '') !== '';
$fateSearchFuzzy = $fateSearchActive && (($storeSearchMode ?? '') === FATE_STORE_SEARCH_MODE_FUZZY);
$isMobileListingTab = ($listingFilter ?? FATE_STORE_LISTING_FIXED) === FATE_STORE_LISTING_MOBILE;
$weeklyHeroLabel = $isMobileListingTab ? '本週熱門非定點 TOP10' : '本週熱門定點 TOP10';
$weeklyHeroBadge = $isMobileListingTab ? '本週熱門非定點' : '本週熱門店';
$weeklyHeroAria = $isMobileListingTab ? '本週熱門非定點服務輪播' : '本週熱門定點店家輪播';

require_once dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_template.php';
require_once dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_store_listing.php';
require fate_template_theme_partial('head.php');
?>
<body>
    <header class="topbar">
        <div class="topbar-inner">
            <a class="brand" href="./"><?= htmlspecialchars($fateBrand['title_main'], ENT_QUOTES, 'UTF-8') ?> <span><?= htmlspecialchars($fateBrand['title_sub'], ENT_QUOTES, 'UTF-8') ?></span></a>
            <nav>
                <a href="./">探索列表</a>
                <?php if (!empty($fateBlogNavVisible)): ?>
                    <a href="blog.php"><?= htmlspecialchars($fateBlogNavLabel, ENT_QUOTES, 'UTF-8') ?></a>
                <?php endif; ?>
                <?php if (!empty($fateForumNavVisible)): ?>
                    <a href="forum/">討論區</a>
                <?php endif; ?>
                <?php
                if (!function_exists('fate_masseur_weekly_recommend_enabled')) {
                    require_once dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_masseur_rewards.php';
                }
                if (fate_masseur_weekly_recommend_enabled()):
                ?>
                    <a href="masseur_weekly.php">週推榜</a>
                <?php endif; ?>
                <?php if ($fatePublicMember !== null): ?>
                    <a href="member_profile.php">我的帳號</a>
                    <a href="logout.php">登出</a>
                <?php else: ?>
                    <a href="login.php">登入</a>
                    <a href="register.php">註冊</a>
                <?php endif; ?>
                <?php if (!empty($fateAdminNavVisible)): ?>
                    <a href="admin/">管理後台</a>
                <?php endif; ?>
            </nav>
        </div>
    </header>

    <div class="layout-shell">
        <div class="layout layout--home">
            <div class="layout-main">
                <?php require __DIR__ . '/partials/home_listing_tabs.inc.php'; ?>

                <section class="hero">
                    <?php if ($fateSearchActive): ?>
                        <div class="fate-search-summary<?= $fateSearchFuzzy ? ' fate-search-summary--fuzzy' : '' ?>" role="status" aria-live="polite">
                            <?php if ($fateSearchFuzzy): ?>
                                <span class="fate-search-summary__badge">模糊搜尋</span>
                            <?php endif; ?>
                            <h2 class="fate-search-summary__title"><?= $fateSearchFuzzy ? '模糊搜尋結果' : '搜尋結果' ?></h2>
                            <p class="fate-search-summary__meta">
                                關鍵字：<strong class="fate-search-summary__kw"><?= htmlspecialchars($storeSearchQ, ENT_QUOTES, 'UTF-8') ?></strong>
                                <span class="fate-search-summary__sep" aria-hidden="true">·</span>
                                找到 <strong class="fate-search-summary__count"><?= (int) $storeTotal ?></strong> 筆<?= $fateSearchFuzzy ? '相近' : '' ?>店家
                            </p>
                            <?php if ($fateSearchFuzzy): ?>
                                <p class="fate-search-summary__note">精準搜尋無符合結果；以下為模糊比對（含錯字容錯），與關鍵字可能不完全一致。</p>
                            <?php endif; ?>
                            <a href="<?= htmlspecialchars($buildQuery(['q' => null, 'page' => '1']), ENT_QUOTES, 'UTF-8') ?>" class="fate-search-summary__clear btn-link text-accent-link">清除搜尋</a>
                        </div>
                    <?php endif; ?>

                    <div class="toolbar">
                        <div>
                            <label for="city">縣市</label>
                            <select id="city" name="city" form="f-filter">
                                <option value="">全部</option>
                                <?php foreach ($cities as $c): ?>
                                    <option value="<?= htmlspecialchars($c, ENT_QUOTES, 'UTF-8') ?>" <?= $cityFilter === $c ? 'selected' : '' ?>><?= htmlspecialchars($c, ENT_QUOTES, 'UTF-8') ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label for="sort">排序</label>
                            <select id="sort" name="sort" form="f-filter">
                                <option value="rating" <?= $sort === 'rating' ? 'selected' : '' ?>>評分優先</option>
                                <option value="name" <?= $sort === 'name' ? 'selected' : '' ?>>名稱</option>
                                <option value="recent" <?= $sort === 'recent' ? 'selected' : '' ?>>最近同步</option>
                            </select>
                        </div>
                        <form id="f-filter" method="get" action="index.php" style="display:inline;">
                            <input type="hidden" name="page" value="1">
                            <?php if ($isMobileListingTab): ?>
                                <input type="hidden" name="listing" value="mobile">
                            <?php endif; ?>
                            <?php if (($storeSearchQ ?? '') !== ''): ?>
                                <input type="hidden" name="q" value="<?= htmlspecialchars($storeSearchQ, ENT_QUOTES, 'UTF-8') ?>">
                            <?php endif; ?>
                            <button type="submit" class="btn-link">套用</button>
                            <?php if ($cityFilter !== '' || $sort !== 'rating' || ($storeSearchQ ?? '') !== ''): ?>
                                <a class="btn-link" href="<?= htmlspecialchars($buildQuery(['city' => null, 'sort' => null, 'q' => null, 'page' => null, 'listing' => $isMobileListingTab ? 'mobile' : null]), ENT_QUOTES, 'UTF-8') ?>">重設</a>
                            <?php endif; ?>
                        </form>
                        <div class="stats">
                            <?php if ($dbError === null && $storesError === null): ?>
                                <?php if (!$fateSearchActive): ?>
                                    共 <strong><?= (int) $storeTotal ?></strong> 筆<?= $isMobileListingTab ? '非定點服務' : '定點店家' ?>
                                <?php endif; ?>
                                <?php if ($lastSyncMax !== null): ?>
                                    <?php if (!$fateSearchActive): ?><span aria-hidden="true"> · </span><?php endif; ?>
                                    最近同步 <strong><?= htmlspecialchars($lastSyncMax, ENT_QUOTES, 'UTF-8') ?></strong>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </section>

                <?php if (!$fateSearchActive && ($weeklyTopStores ?? []) !== []): ?>
                    <?php $weeklySlideCount = count($weeklyTopStores); ?>
                    <section class="fate-weekly-slider fate-weekly-slider--listing-<?= $isMobileListingTab ? 'mobile' : 'fixed' ?><?= $weeklySlideCount < 2 ? ' fate-weekly-slider--single' : '' ?>"
                             id="fateWeeklyTopSlider"
                             aria-roledescription="carousel"
                             aria-label="<?= htmlspecialchars($weeklyHeroAria, ENT_QUOTES, 'UTF-8') ?>">
                        <div class="fate-weekly-slider__head">
                            <div class="fate-weekly-slider__titles">
                                <h2 class="fate-weekly-slider__label"><?= htmlspecialchars($weeklyHeroLabel, ENT_QUOTES, 'UTF-8') ?></h2>
                                <?php if (($weeklyStatsRangeLabel ?? '') !== ''): ?>
                                    <p class="fate-weekly-slider__sub"><?= htmlspecialchars($weeklyStatsRangeLabel, ENT_QUOTES, 'UTF-8') ?> · 依詳情頁瀏覽（本週）</p>
                                <?php endif; ?>
                            </div>
                            <?php if ($weeklySlideCount > 1): ?>
                                <div class="fate-weekly-slider__counter" aria-live="polite">
                                    <span data-fate-slider-index>1</span> / <?= (int) $weeklySlideCount ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="fate-weekly-slider__viewport">
                            <div class="fate-weekly-slider__track" data-fate-slider-track>
                                <?php foreach ($weeklyTopStores as $wi => $wr): ?>
                                    <?php
                                    $wsid = (int) ($wr['spa_store_id'] ?? 0);
                                    $wname = trim((string) ($wr['store_name'] ?? ''));
                                    if ($wname === '') {
                                        $wname = '店家 #' . $wsid;
                                    }
                                    $wcity = trim((string) ($wr['city_name'] ?? ''));
                                    $wdist = trim((string) ($wr['district_name'] ?? ''));
                                    $wloc = $isMobileListingTab
                                        ? fate_store_listing_location_text($wr)
                                        : trim($wcity . ($wcity !== '' && $wdist !== '' ? ' · ' : '') . $wdist);
                                    $whits = isset($wr['week_hits']) ? (int) $wr['week_hits'] : 0;
                                    $wrating = isset($wr['place_rating']) && $wr['place_rating'] !== '' && $wr['place_rating'] !== null
                                        ? (float) $wr['place_rating']
                                        : null;
                                    $wCover = fate_store_cover_src(
                                        isset($wr['cover_photo_path']) ? (string) $wr['cover_photo_path'] : null,
                                        isset($wr['photos_json']) ? (string) $wr['photos_json'] : null
                                    );
                                    $wImg = fate_spa_asset_url($wCover['path']);
                                    $wUrl = 'store.php?id=' . $wsid;
                                    $slideAlt = '本週熱門第 ' . ((int) $wi + 1) . ' 名：' . $wname;
                                    ?>
                                    <article class="fate-weekly-slider__slide"
                                             data-fate-slider-slide="<?= (int) $wi ?>"
                                             aria-hidden="<?= $wi === 0 ? 'false' : 'true' ?>">
                                        <a class="fate-weekly-slider__link" href="<?= htmlspecialchars($wUrl, ENT_QUOTES, 'UTF-8') ?>">
                                            <?php if ($wImg !== null): ?>
                                                <img class="fate-weekly-slider__cover" src="<?= htmlspecialchars($wImg, ENT_QUOTES, 'UTF-8') ?>"
                                                     onerror="this.onerror=null; this.src='<?= htmlspecialchars(fate_local_asset_url($wCover['path']) ?? '') ?>';"
                                                     alt="<?= htmlspecialchars($slideAlt, ENT_QUOTES, 'UTF-8') ?>"
                                                     loading="<?= $wi === 0 ? 'eager' : 'lazy' ?>" decoding="async">
                                            <?php else: ?>
                                                <div class="fate-weekly-slider__cover fate-weekly-slider__cover--empty" role="img" aria-label="<?= htmlspecialchars($slideAlt, ENT_QUOTES, 'UTF-8') ?>">NO IMAGE</div>
                                            <?php endif; ?>
                                            <div class="fate-weekly-slider__overlay" aria-hidden="true"></div>
                                            <div class="fate-weekly-slider__content">
                                                <div class="fate-weekly-slider__kicker">
                                                    <span class="fate-weekly-slider__rank" aria-label="排名第 <?= (int) $wi + 1 ?> 名">#<?= (int) $wi + 1 ?></span>
                                                    <span class="fate-weekly-slider__badge"><?= htmlspecialchars($weeklyHeroBadge, ENT_QUOTES, 'UTF-8') ?></span>
                                                </div>
                                                <h3 class="fate-weekly-slider__title"><?= htmlspecialchars($wname, ENT_QUOTES, 'UTF-8') ?></h3>
                                                <div class="fate-weekly-slider__meta">
                                                    <?php if ($wloc !== ''): ?>
                                                        <span><span class="fate-weekly-slider__meta-k"><?= $isMobileListingTab ? '區域' : '地區' ?></span><?= htmlspecialchars($wloc, ENT_QUOTES, 'UTF-8') ?></span>
                                                    <?php endif; ?>
                                                    <span><span class="fate-weekly-slider__meta-k">瀏覽</span>本週 <?= number_format($whits) ?> 次</span>
                                                    <?php if ($wrating !== null): ?>
                                                        <span><span class="fate-weekly-slider__meta-k">評分</span><?= htmlspecialchars(number_format($wrating, 1), ENT_QUOTES, 'UTF-8') ?></span>
                                                    <?php endif; ?>
                                                </div>
                                                <span class="fate-weekly-slider__cta" aria-hidden="true">查看店家 →</span>
                                            </div>
                                        </a>
                                    </article>
                                <?php endforeach; ?>
                            </div>
                            <?php if ($weeklySlideCount > 1): ?>
                                <button type="button" class="fate-weekly-slider__nav fate-weekly-slider__nav--prev" data-fate-slider-nav="prev" aria-label="上一張">‹</button>
                                <button type="button" class="fate-weekly-slider__nav fate-weekly-slider__nav--next" data-fate-slider-nav="next" aria-label="下一張">›</button>
                                <div class="fate-weekly-slider__dots" role="tablist" aria-label="幻燈片頁碼">
                                    <?php foreach ($weeklyTopStores as $wi => $_wr): ?>
                                        <button type="button" role="tab" class="fate-weekly-slider__dot<?= $wi === 0 ? ' is-active' : '' ?>"
                                                data-fate-slider-dot="<?= (int) $wi ?>"
                                                aria-selected="<?= $wi === 0 ? 'true' : 'false' ?>"
                                                aria-label="第 <?= (int) $wi + 1 ?> 張"></button>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </section>
                    <script>
                    (function () {
                        var root = document.getElementById('fateWeeklyTopSlider');
                        if (!root) return;
                        var track = root.querySelector('[data-fate-slider-track]');
                        var slides = root.querySelectorAll('[data-fate-slider-slide]');
                        var dots = root.querySelectorAll('[data-fate-slider-dot]');
                        var indexEl = root.querySelector('[data-fate-slider-index]');
                        var n = slides.length;
                        if (!track || n === 0) return;
                        var current = 0;
                        var timer = null;
                        var intervalMs = 5200;
                        var reduceMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
                        var touchStartX = null;

                        function show(index) {
                            current = (index % n + n) % n;
                            track.style.transform = 'translate3d(' + (-current * 100) + '%, 0, 0)';
                            slides.forEach(function (slide, i) {
                                slide.setAttribute('aria-hidden', i === current ? 'false' : 'true');
                            });
                            dots.forEach(function (dot, i) {
                                var on = i === current;
                                dot.classList.toggle('is-active', on);
                                dot.setAttribute('aria-selected', on ? 'true' : 'false');
                            });
                            if (indexEl) indexEl.textContent = String(current + 1);
                        }

                        function startAuto() {
                            if (reduceMotion || n <= 1) return;
                            stopAuto();
                            timer = setInterval(function () { show(current + 1); }, intervalMs);
                        }
                        function stopAuto() {
                            if (timer) { clearInterval(timer); timer = null; }
                        }

                        root.addEventListener('click', function (e) {
                            var nav = e.target.closest('[data-fate-slider-nav]');
                            if (nav) {
                                show(current + (nav.getAttribute('data-fate-slider-nav') === 'next' ? 1 : -1));
                                startAuto();
                                return;
                            }
                            var dot = e.target.closest('[data-fate-slider-dot]');
                            if (dot) {
                                show(parseInt(dot.getAttribute('data-fate-slider-dot'), 10) || 0);
                                startAuto();
                            }
                        });
                        root.addEventListener('mouseenter', stopAuto);
                        root.addEventListener('mouseleave', startAuto);

                        root.addEventListener('touchstart', function (e) {
                            if (!e.changedTouches || !e.changedTouches[0]) return;
                            touchStartX = e.changedTouches[0].clientX;
                            stopAuto();
                        }, { passive: true });
                        root.addEventListener('touchend', function (e) {
                            if (touchStartX === null || !e.changedTouches || !e.changedTouches[0]) return;
                            var dx = e.changedTouches[0].clientX - touchStartX;
                            touchStartX = null;
                            if (Math.abs(dx) < 40) { startAuto(); return; }
                            show(current + (dx < 0 ? 1 : -1));
                            startAuto();
                        }, { passive: true });

                        show(0);
                        startAuto();
                    })();
                    </script>
                <?php endif; ?>

                <?php if ($dbError !== null): ?>
                    <div class="grid"><div class="empty">無法連線資料庫：<code class="inline"><?= htmlspecialchars($dbError, ENT_QUOTES, 'UTF-8') ?></code></div></div>
                <?php elseif ($storesError !== null): ?>
                    <div class="grid">
                        <div class="empty">
                            無法讀取店家列表（可能尚未完成資料表設定）。<br><br>
                            <code class="inline"><?= htmlspecialchars($storesError, ENT_QUOTES, 'UTF-8') ?></code>
                        </div>
                    </div>
                <?php else: ?>
                    <?php require_once __DIR__ . '/../../includes/fate_store_listing.php'; ?>
                    <?php if ($fateSearchFuzzy && $stores !== []): ?>
                        <p class="fate-search-fuzzy-label" role="note">以下列表為<strong>模糊搜尋</strong>結果，非精準匹配。</p>
                    <?php endif; ?>
                    <div class="grid">
                        <?php if ($stores === []): ?>
                            <div class="empty">尚無符合條件的<?= $isMobileListingTab ? '非定點服務' : '定點店家' ?><?= ($storeSearchQ ?? '') !== '' ? '（含模糊搜尋）' : '' ?><?= ($storeSearchQ ?? '') !== '' ? '，請調整關鍵字或縣市' : '' ?>。<?php if (!$isMobileListingTab && !empty($fateAdminNavVisible)): ?>可至 <a href="admin/" class="text-accent-link">管理後台</a> 進行同步。<?php elseif (!$isMobileListingTab): ?>請待管理員同步資料。<?php else: ?>會員可至「我的帳號 → 我推薦的店家 → 非定點服務」提交。<?php endif; ?></div>
                        <?php endif; ?>
                        <?php foreach ($stores as $row): ?>
                            <?php
                            $sid = (int) ($row['spa_store_id'] ?? 0);
                            $name = trim((string) ($row['store_name'] ?? ''));
                            if ($name === '') {
                                $name = '店家 #' . $sid;
                            }
                            $isMobileListing = fate_store_listing_is_mobile($row);
                            $listingLabel = fate_store_listing_label(fate_store_listing_kind_from_row($row));
                            $addr = fate_store_listing_location_text($row);
                            $city = trim((string) ($row['city_name'] ?? ''));
                            $dist = trim((string) ($row['district_name'] ?? ''));
                            $regionText = trim($city . ($city !== '' && $dist !== '' ? ' · ' : '') . $dist);
                            $rating = isset($row['place_rating']) && $row['place_rating'] !== '' && $row['place_rating'] !== null
                                ? (float) $row['place_rating']
                                : null;
                            $rCount = isset($row['place_ratings_total']) ? (int) $row['place_ratings_total'] : null;
                            $storeLocalUrl = 'store.php?id=' . $sid;
                            $coverInfo = fate_store_cover_src(
                                isset($row['cover_photo_path']) ? (string) $row['cover_photo_path'] : null,
                                isset($row['photos_json']) ? (string) $row['photos_json'] : null
                            );
                            $imgUrl = fate_spa_asset_url($coverInfo['path']);
                            ?>
                            <article class="card card--listing-<?= $isMobileListing ? 'mobile' : 'fixed' ?>">
                                <div class="card-visual">
                                    <?php if ($rating !== null): ?>
                                        <span class="badge-rating" title="Google 評分">
                                            <span class="badge-rating__star" aria-hidden="true">★</span>
                                            <span class="badge-rating__score"><?= htmlspecialchars(number_format($rating, 1), ENT_QUOTES, 'UTF-8') ?></span>
                                            <?php if ($rCount !== null && $rCount > 0): ?>
                                                <span class="badge-rating__cnt">(<?= (int) $rCount ?>)</span>
                                            <?php endif; ?>
                                        </span>
                                    <?php endif; ?>
                                    <a class="card-visual__link"
                                       href="<?= htmlspecialchars($storeLocalUrl, ENT_QUOTES, 'UTF-8') ?>"
                                       aria-label="查看 <?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?> 詳情">
                                        <?php if ($imgUrl !== null): ?>
                                            <img src="<?= htmlspecialchars($imgUrl, ENT_QUOTES, 'UTF-8') ?>"
                                                 onerror="this.onerror=null; this.src='<?= htmlspecialchars(fate_local_asset_url($coverInfo['path']) ?? '') ?>';"
                                                 alt="<?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>"
                                                 loading="lazy"
                                                 decoding="async">
                                        <?php else: ?>
                                            <div class="ph">NO IMAGE</div>
                                        <?php endif; ?>
                                    </a>
                                </div>
                                <div class="card-body">
                                    <h2><a href="<?= htmlspecialchars($storeLocalUrl, ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?></a></h2>
                                    <?php if ($rating !== null): ?>
                                        <p class="card-rating-row" aria-label="Google 評分 <?= htmlspecialchars(number_format($rating, 1), ENT_QUOTES, 'UTF-8') ?>">
                                            <span class="card-rating-row__star" aria-hidden="true">★</span>
                                            <span class="card-rating-row__score"><?= htmlspecialchars(number_format($rating, 1), ENT_QUOTES, 'UTF-8') ?></span>
                                            <span class="card-rating-row__brand">Google</span>
                                            <?php if ($rCount !== null && $rCount > 0): ?>
                                                <span class="card-rating-row__cnt">(<?= (int) $rCount ?> 則評價)</span>
                                            <?php endif; ?>
                                        </p>
                                    <?php endif; ?>
                                    <?php if ($isMobileListing): ?>
                                        <div class="meta-line meta-line--listing"><span class="store-listing-badge store-listing-badge--mobile"><?= htmlspecialchars($listingLabel, ENT_QUOTES, 'UTF-8') ?></span></div>
                                    <?php elseif ($regionText !== ''): ?>
                                        <div class="meta-line meta-line--region"><?= htmlspecialchars($regionText, ENT_QUOTES, 'UTF-8') ?></div>
                                    <?php endif; ?>
                                    <?php if ($addr !== ''): ?>
                                        <div class="meta-line<?= $isMobileListing ? ' meta-line--service-area' : ' meta-line--address' ?>"><?= htmlspecialchars($addr, ENT_QUOTES, 'UTF-8') ?></div>
                                    <?php endif; ?>
                                    <div class="card-foot">
                                        <span>#<?= $sid ?></span>
                                        <a href="<?= htmlspecialchars($storeLocalUrl, ENT_QUOTES, 'UTF-8') ?>">詳細資訊</a>
                                    </div>
                                </div>
                            </article>
                        <?php endforeach; ?>
                    </div>

                    <?php if ($storeTotal > $perPage): ?>
                        <nav class="pager" aria-label="分頁">
                            <?php if ($page > 1): ?>
                                <a href="<?= htmlspecialchars($buildQuery(['page' => (string) ($page - 1)]), ENT_QUOTES, 'UTF-8') ?>">上一頁</a>
                            <?php else: ?>
                                <span class="disabled">上一頁</span>
                            <?php endif; ?>
                            <span class="cur"><?= (int) $page ?> / <?= (int) $totalPages ?></span>
                            <?php if ($page < $totalPages): ?>
                                <a href="<?= htmlspecialchars($buildQuery(['page' => (string) ($page + 1)]), ENT_QUOTES, 'UTF-8') ?>">下一頁</a>
                            <?php else: ?>
                                <span class="disabled">下一頁</span>
                            <?php endif; ?>
                        </nav>
                    <?php endif; ?>
                <?php endif; ?>
            </div>

            <?php require fate_template_theme_partial('sidebar.php'); ?>
        </div>
    </div>

<?php require fate_template_theme_partial('footer.php'); ?>
