<?php



declare(strict_types=1);



/** @var array{id:int,username:string,display_name:string,avatar_rel:string}|null $fatePublicMember */

/** @var string $cityFilter */

/** @var string $sort */

/** @var string $storeSearchQ */
/** @var string|null $storeSearchMode */

/** @var Closure|null $buildHomeQuery */

/** @var int|null $storeSearchTotal */



if (!isset($fateBrand) || !is_array($fateBrand)) {

    require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_ui_settings.php';

    $fateBrand = fate_public_site_branding();

}

if (!function_exists('fate_ncoin_display_name')) {
    require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_ncoin_settings.php';
}
$sidebarNcoinNameEsc = htmlspecialchars(fate_ncoin_display_name(), ENT_QUOTES, 'UTF-8');



if (!isset($buildHomeQuery) || !is_callable($buildHomeQuery)) {

    require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_list_query.php';

    $buildHomeQuery = static function (array $overrides): string {

        return fate_build_home_list_query($_GET, $overrides);

    };

}



require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_member_profile.php';

require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_member_account.php';

if (!isset($fateLoginNext) || !is_string($fateLoginNext) || trim($fateLoginNext) === '') {
    $fateLoginNext = fate_public_login_next_from_request('./');
}
$sidebarLoginNextQ = rawurlencode($fateLoginNext);

$sidebarAvatarUrl = ($fatePublicMember !== null)

    ? fate_member_profile_avatar_url($fatePublicMember['avatar_rel'] ?? '')

    : null;

/** @var int|null $sidebarMemberNcoin */
$sidebarMemberNcoin = null;
if ($fatePublicMember !== null && isset($pdo) && $pdo instanceof PDO) {
    $sidebarMemberNcoin = fate_member_read_ncoin(
        $pdo,
        (int) $fatePublicMember['id'],
        (string) ($fatePublicMember['username'] ?? '')
    );
}



$sidebarSearchQ = trim((string) ($storeSearchQ ?? ''));

if (!isset($cityFilter) || !is_string($cityFilter)) {
    $cityFilter = '';
} else {
    $cityFilter = trim($cityFilter);
}

if (!isset($sort) || !is_string($sort)) {
    $sort = 'rating';
} else {
    $sort = strtolower(trim($sort));
    if (!in_array($sort, ['rating', 'name', 'recent'], true)) {
        $sort = 'rating';
    }
}

$sidebarListUrl = $buildHomeQuery(['page' => '1']);

$sidebarClearQUrl = $buildHomeQuery(['q' => null, 'page' => '1']);

if (!isset($fateForumNavVisible)) {
    require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_plugins.php';
    $fateForumNavVisible = fate_store_forum_plugin_active();
}

if (!isset($fateBlogNavVisible)) {
    require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_blog.php';
    $fateBlogNavVisible = fate_blog_nav_visible_for_frontend();
    $fateBlogNavLabel = fate_blog_nav_label();
}

?>

            <aside class="layout-sidebar" aria-label="側欄">

                <div class="sidebar-card sidebar-card--member">

                    <p class="sidebar-title">會員</p>

                    <?php if ($fatePublicMember !== null): ?>

                        <div class="sidebar-member-head">

                            <?php if ($sidebarAvatarUrl !== null): ?>

                                <img class="sidebar-avatar" src="<?= htmlspecialchars($sidebarAvatarUrl, ENT_QUOTES, 'UTF-8') ?>" alt="" width="44" height="44">

                            <?php endif; ?>

                            <div class="sidebar-member-head__text">

                                <?php $memberLabel = fate_public_member_display_label($fatePublicMember); ?>

                                <p class="sidebar-user-name">

                                    <?= htmlspecialchars($memberLabel, ENT_QUOTES, 'UTF-8') ?>

                                    <?php if ($sidebarMemberNcoin !== null): ?>

                                        <span class="sidebar-user-ncoin">

                                            <span class="sidebar-user-ncoin__label"><?= $sidebarNcoinNameEsc ?></span>

                                            <span class="sidebar-user-ncoin__amount"><?= htmlspecialchars(number_format($sidebarMemberNcoin), ENT_QUOTES, 'UTF-8') ?></span>

                                        </span>

                                    <?php endif; ?>

                                </p>

                                <p class="sidebar-user-meta"><?= htmlspecialchars($fatePublicMember['username'], ENT_QUOTES, 'UTF-8') ?></p>

                            </div>

                        </div>

                        <div class="sidebar-actions">

                            <a class="sidebar-btn" href="member_profile.php">我的帳號</a>

                            <a class="sidebar-btn sidebar-btn--ghost" href="logout.php">登出</a>

                            <?php if (!empty($fateAdminNavVisible)): ?>

                                <a class="sidebar-btn sidebar-btn--ghost" href="admin/">後台</a>

                            <?php endif; ?>

                        </div>

                    <?php else: ?>

                        <p class="sidebar-user-meta sidebar-user-hint">登入後可於此查看使用者資訊。</p>

                        <div class="sidebar-actions">

                            <a class="sidebar-btn" href="login.php?next=<?= htmlspecialchars($sidebarLoginNextQ, ENT_QUOTES, 'UTF-8') ?>">登入</a>

                            <a class="sidebar-btn sidebar-btn--ghost" href="register.php?next=<?= htmlspecialchars($sidebarLoginNextQ, ENT_QUOTES, 'UTF-8') ?>">註冊</a>

                            <?php if (!empty($fateAdminNavVisible)): ?>

                                <a class="sidebar-btn sidebar-btn--ghost" href="admin/">後台</a>

                            <?php endif; ?>

                        </div>

                    <?php endif; ?>

                </div>

                <div class="sidebar-card sidebar-card--search">

                    <p class="sidebar-title">店家搜尋</p>

                    <form method="get" action="index.php">

                        <input type="hidden" name="page" value="1">

                        <?php if ($cityFilter !== ''): ?>

                            <input type="hidden" name="city" value="<?= htmlspecialchars($cityFilter, ENT_QUOTES, 'UTF-8') ?>">

                        <?php endif; ?>

                        <?php if ($sort !== 'rating'): ?>

                            <input type="hidden" name="sort" value="<?= htmlspecialchars($sort, ENT_QUOTES, 'UTF-8') ?>">

                        <?php endif; ?>

                        <label class="sidebar-search-label" for="sidebar-q">店名、地址、電話或區域關鍵字</label>

                        <div class="sidebar-search-row">

                            <input class="sidebar-search" type="search" name="q" id="sidebar-q" maxlength="120" autocomplete="off"

                                   placeholder="輸入關鍵字…"

                                   value="<?= htmlspecialchars($sidebarSearchQ, ENT_QUOTES, 'UTF-8') ?>">

                            <button type="submit" class="sidebar-submit">搜尋</button>

                        </div>

                        <?php if ($sidebarSearchQ !== ''): ?>

                            <p class="sidebar-hint sidebar-hint--search">

                                目前關鍵字：<strong><?= htmlspecialchars($sidebarSearchQ, ENT_QUOTES, 'UTF-8') ?></strong>

                            </p>

                            <?php if (isset($storeSearchTotal) && $storeSearchTotal !== null): ?>

                                <p class="sidebar-hint">

                                    符合 <strong><?= (int) $storeSearchTotal ?></strong> 筆

                                    <?php if (($storeSearchMode ?? '') === FATE_STORE_SEARCH_MODE_FUZZY): ?>

                                        · <strong class="sidebar-hint--fuzzy">模糊搜尋結果</strong>

                                    <?php elseif ((int) $storeSearchTotal === 0): ?>

                                        ，請調整關鍵字或縣市

                                    <?php endif; ?>

                                </p>

                            <?php else: ?>

                                <p class="sidebar-hint">

                                    <a href="<?= htmlspecialchars($sidebarListUrl, ENT_QUOTES, 'UTF-8') ?>" class="text-accent-link">到探索列表查看結果</a>

                                </p>

                            <?php endif; ?>

                            <p class="sidebar-hint">

                                <a href="<?= htmlspecialchars($sidebarClearQUrl, ENT_QUOTES, 'UTF-8') ?>" class="text-accent-link">清除關鍵字</a>

                            </p>

                        <?php endif; ?>

                    </form>

                </div>

                <?php if (!empty($fateForumNavVisible)): ?>

                <div class="sidebar-card sidebar-card--forum">

                    <p class="sidebar-title">討論區</p>

                    <div class="sidebar-actions">

                        <a class="sidebar-btn" href="forum/">討論區首頁</a>

                    </div>

                </div>

                <?php endif; ?>

                <?php require __DIR__ . '/blog_sidebar.inc.php'; ?>

                <?php
                require_once dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_masseur_rewards.php';
                if (fate_masseur_weekly_recommend_enabled()) {
                    require __DIR__ . '/masseur_weekly_sidebar.inc.php';
                }
                ?>

            </aside>

