<?php

declare(strict_types=1);

/**
 * 會員頁 — 我推薦的店家分頁。
 *
 * @var list<array{spa_store_id:int,store_name:string,display_address:string,location_text:string,listing_kind:string,listing_label:string,is_approved:int,synced_at:string}> $memberStores
 */

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

?>
<div class="member-profile-tab-panel__inner">
    <div class="member-profile-stores-head">
        <div>
            <h3 class="member-profile-tab-subtitle">我推薦的店家</h3>
            <p class="sub member-profile-hint member-profile-stores-head__hint">管理您向本站提交的店家資訊與審核進度。<strong>定點店家</strong>需可公開地址；<strong>非定點服務</strong>僅填服務區域、不公開路名。技師資料請在建檔審核通過後，至該店家頁面新增。</p>
        </div>
        <div class="member-profile-stores-head__actions">
            <a href="member_add_store.php" class="member-profile-btn member-profile-btn--primary"><?= htmlspecialchars(fate_store_listing_tab_label(FATE_STORE_LISTING_FIXED), ENT_QUOTES, 'UTF-8') ?></a>
            <a href="member_add_store_mobile.php" class="member-profile-btn member-profile-btn--ghost"><?= htmlspecialchars(fate_store_listing_tab_label(FATE_STORE_LISTING_MOBILE), ENT_QUOTES, 'UTF-8') ?></a>
        </div>
    </div>

    <?php if ($memberStores === []): ?>
        <p class="member-profile-empty">目前尚無推薦的店家。定點請用「<?= htmlspecialchars(fate_store_listing_tab_label(FATE_STORE_LISTING_FIXED), ENT_QUOTES, 'UTF-8') ?>」；非定點請用「<?= htmlspecialchars(fate_store_listing_tab_label(FATE_STORE_LISTING_MOBILE), ENT_QUOTES, 'UTF-8') ?>」。</p>
    <?php else: ?>
        <div class="member-profile-table-wrap">
            <table class="member-profile-ncoin-hist member-profile-stores-table">
                <thead>
                    <tr>
                        <th>店名</th>
                        <th>型態</th>
                        <th>地址</th>
                        <th>送審時間</th>
                        <th>狀態</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($memberStores as $store): ?>
                        <?php
                        $kind = fate_store_listing_normalize_kind((string) ($store['listing_kind'] ?? FATE_STORE_LISTING_FIXED));
                        $listingLabel = fate_store_listing_tab_label($kind);
                        ?>
                        <tr>
                            <td data-label="店名">
                                <a class="member-profile-stores-table__link" href="store.php?id=<?= (int) $store['spa_store_id'] ?>">
                                    <?= htmlspecialchars($store['store_name'], ENT_QUOTES, 'UTF-8') ?>
                                </a>
                            </td>
                            <td data-label="型態">
                                <?php if ($kind === FATE_STORE_LISTING_MOBILE): ?>
                                    <span class="member-profile-stores-kind member-profile-stores-kind--mobile">
                                        <?= htmlspecialchars($listingLabel, ENT_QUOTES, 'UTF-8') ?>
                                    </span>
                                <?php else: ?>
                                    <span class="member-profile-stores-kind member-profile-stores-kind--plain"><?= htmlspecialchars($listingLabel, ENT_QUOTES, 'UTF-8') ?></span>
                                <?php endif; ?>
                            </td>
                            <td data-label="地址"><?= htmlspecialchars($store['location_text'] ?? $store['display_address'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td class="member-profile-ncoin-hist__when" data-label="送審時間"><?= htmlspecialchars(substr($store['synced_at'], 0, 16), ENT_QUOTES, 'UTF-8') ?></td>
                            <td data-label="狀態">
                                <?php if ((int) $store['is_approved'] === 1): ?>
                                    <span class="member-profile-stores-status member-profile-stores-status--ok">已通過</span>
                                <?php else: ?>
                                    <span class="member-profile-stores-status member-profile-stores-status--pending">審核中</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
