<?php
/**
 * 后台：SEO 设置 —— 页面清单
 * 每个页面独立编辑，点击卡片进入 admin/seo/edit/{key}。
 */
$pages = $pages ?? [];
$rows  = $rows ?? [];
function seo_len(string $s): int { return mb_strlen($s, 'UTF-8'); }
function seo_kwcount(string $s): int {
    if ($s === '') return 0;
    return count(array_filter(array_map('trim', explode(',', $s)), function ($x) { return $x !== ''; }));
}
function seo_badge(int $n, int $min, string $emptyTxt): string {
    if ($n === 0) return '<span class="badge badge-none">' . $emptyTxt . '</span>';
    $cls = $n >= $min ? 'badge-ok' : 'badge-warn';
    return '<span class="badge ' . $cls . '">' . $n . ($min > 0 ? '（≥' . $min . '）' : '') . '</span>';
}
?>
<div class="page-head">
    <div>
        <h1>SEO 设置</h1>
        <p class="muted">逐页维护搜索结果展示信息。点击任一页面进入独立编辑页，带实时字数提示。</p>
    </div>
    <div class="page-head-actions">
        <a class="btn btn-ghost" href="<?= e(site_url('admin/seo/edit/home')) ?>">编辑首页 SEO</a>
        <a class="btn btn-ghost" href="<?= e(site_url('sitemap.xml')) ?>" target="_blank" rel="noopener">查看 sitemap.xml</a>
    </div>
</div>

<?= flash_html() ?>

<div class="seo-grid">
    <?php foreach ($pages as $key => $meta):
        $v     = $rows[$key] ?? [];
        $title = $v['title'] ?? '';
        $desc  = $v['description'] ?? '';
        $tLen  = seo_len($title);
        $dLen  = seo_len($desc);
        $kwN   = seo_kwcount($v['keywords'] ?? '');
        $noindex = !empty($v['noindex']);
    ?>
        <a class="seo-card" href="<?= e(site_url('admin/seo/edit/' . $key)) ?>">
            <div class="seo-card-head">
                <span class="seo-card-title"><?= e($meta['label']) ?></span>
                <code class="mono seo-card-key"><?= e($key) ?></code>
            </div>
            <div class="seo-card-badges">
                <?= seo_badge($tLen, $meta['title_min'], '标题未设置') ?>
                <?= seo_badge($dLen, $meta['desc_min'], '描述未设置') ?>
                <?= seo_badge($kwN, $meta['kw_min'], '无关键词') ?>
                <?php if ($noindex): ?><span class="badge badge-noidx">noindex</span><?php endif; ?>
            </div>
            <div class="seo-card-preview"><?= e($title ?: '（未设置标题）') ?></div>
            <div class="seo-card-desc"><?= e($desc ?: '（未设置描述）') ?></div>
            <div class="seo-card-go">编辑此页 ›</div>
        </a>
    <?php endforeach; ?>
</div>

<style>
.seo-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
@media (max-width: 820px) { .seo-grid { grid-template-columns: 1fr; } }
.seo-card { display: block; background: #fff; border: 1px solid #e6eaf0; border-radius: 14px; padding: 16px 18px; text-decoration: none; color: inherit; transition: border-color .15s, box-shadow .2s, transform .12s; }
.seo-card:hover { border-color: #7dd3fc; box-shadow: 0 12px 28px -18px rgba(14,165,233,.7); transform: translateY(-2px); }
.seo-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.seo-card-title { font-size: 16px; font-weight: 800; }
.seo-card-key { background: #f1f5f9; color: #64748b; padding: 2px 8px; border-radius: 7px; font-size: 12px; }
.seo-card-badges { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.seo-card-preview { font-size: 14px; font-weight: 700; color: #0f172a; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seo-card-desc { font-size: 13px; color: #64748b; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.seo-card-go { margin-top: 12px; font-size: 13px; font-weight: 700; color: #0ea5e9; }
.badge-ok { background: #dcfce7; color: #15803d; }
.badge-warn { background: #fef3c7; color: #b45309; }
.badge-none { background: #f1f5f9; color: #94a3b8; }
.badge-noidx { background: #fee2e2; color: #dc2626; }
</style>
