<?php

declare(strict_types=1);

/**
 * BLOG 列表主內容（default / minimal 共用）。
 *
 * @var bool $blogCanManage
 * @var list<array<string,mixed>> $blogCategories
 * @var list<array<string,mixed>> $blogPosts
 * @var int $blogTotal
 * @var int $blogPages
 * @var int $page
 * @var int $categoryFilter
 * @var bool $compose
 * @var bool $editMode
 * @var int $editPostId
 * @var array<string,mixed>|null $editPost
 * @var string $manageTab
 * @var array{enabled:bool,posts_per_page:int,show_announcements_on_home:bool,home_announcements_limit:int,nav_label:string} $blogSettings
 * @var Closure $buildBlogUrl
 * @var string $csrf
 * @var string $msgOk
 * @var string $msgBad
 * @var string $fateBlogNavLabel
 */

require_once dirname(__DIR__, 3) . '/includes/fate_blog.php';

$showList = ($manageTab ?? '') === '' && !$compose && !$editMode;
?>
<main class="blog-page">
    <div class="blog-page__head">
        <h1 class="blog-page__title"><?= htmlspecialchars($fateBlogNavLabel, ENT_QUOTES, 'UTF-8') ?></h1>
        <?php if ($blogCanManage): ?>
            <div class="blog-page__actions">
                <?php if (!$compose && !$editMode): ?>
                    <a class="blog-btn blog-btn--primary" href="<?= htmlspecialchars($buildBlogUrl($categoryFilter > 0 ? $categoryFilter : null, $page > 1 ? $page : null, true), ENT_QUOTES, 'UTF-8') ?>">發表文章</a>
                <?php elseif ($compose): ?>
                    <a class="blog-btn" href="<?= htmlspecialchars($buildBlogUrl($categoryFilter > 0 ? $categoryFilter : null, $page > 1 ? $page : null), ENT_QUOTES, 'UTF-8') ?>">返回列表</a>
                <?php elseif ($editMode && $editPostId > 0): ?>
                    <a class="blog-btn" href="<?= htmlspecialchars($buildBlogUrl($categoryFilter > 0 ? $categoryFilter : null, $page > 1 ? $page : null), ENT_QUOTES, 'UTF-8') ?>">返回列表</a>
                    <a class="blog-btn" href="<?= htmlspecialchars(fate_blog_post_url($editPostId), ENT_QUOTES, 'UTF-8') ?>">查看文章</a>
                <?php endif; ?>
                <?php if (!$compose && !$editMode): ?>
                    <a class="blog-btn" href="<?= htmlspecialchars($buildBlogUrl(null, null, false, 'settings'), ENT_QUOTES, 'UTF-8') ?>">管理設定</a>
                    <a class="blog-btn" href="<?= htmlspecialchars($buildBlogUrl(null, null, false, 'categories'), ENT_QUOTES, 'UTF-8') ?>">分類管理</a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>

    <?php if ($msgOk !== ''): ?>
        <p class="blog-flash blog-flash--ok"><?= htmlspecialchars($msgOk, ENT_QUOTES, 'UTF-8') ?></p>
    <?php endif; ?>
    <?php if ($msgBad !== ''): ?>
        <p class="blog-flash blog-flash--bad"><?= htmlspecialchars($msgBad, ENT_QUOTES, 'UTF-8') ?></p>
    <?php endif; ?>

    <?php if ($blogCanManage && ($manageTab ?? '') !== ''): ?>
        <?php require __DIR__ . '/blog_manage.inc.php'; ?>
    <?php endif; ?>

    <?php if ($compose && $blogCanManage): ?>
        <?php
        $blogFormPostId = 0;
        $blogFormReturnTo = 'blog.php';
        $blogEditorId = 'blog-body';
        require __DIR__ . '/blog_post_form.inc.php';
        ?>
    <?php endif; ?>

    <?php if ($editMode && $blogCanManage && is_array($editPost ?? null)): ?>
        <?php
        $blogFormPostId = $editPostId;
        $blogFormTitle = (string) ($editPost['title'] ?? '');
        $blogFormBody = (string) ($editPost['body_html'] ?? '');
        $blogFormExcerpt = (string) ($editPost['excerpt'] ?? '');
        $blogFormCategoryId = (int) ($editPost['category_id'] ?? 0);
        $blogFormIsAnn = (int) ($editPost['is_announcement'] ?? 0) === 1;
        $blogFormIsPublished = (int) ($editPost['is_published'] ?? 1) === 1;
        $blogFormIsHidden = (int) ($editPost['is_hidden'] ?? 0) === 1;
        $blogFormReturnTo = 'blog.php?edit=' . $editPostId;
        $blogFormCancelUrl = $buildBlogUrl($categoryFilter > 0 ? $categoryFilter : null, $page > 1 ? $page : null);
        $blogEditorId = 'blog-list-edit-body';
        require __DIR__ . '/blog_post_form.inc.php';
        ?>
    <?php endif; ?>

    <?php if ($showList): ?>
    <?php if ($blogCategories !== []): ?>
        <ul class="blog-cats">
            <li><a class="blog-cats__link<?= $categoryFilter <= 0 ? ' is-active' : '' ?>" href="<?= htmlspecialchars($buildBlogUrl(null, null), ENT_QUOTES, 'UTF-8') ?>">全部</a></li>
            <?php foreach ($blogCategories as $cat): ?>
                <?php $cid = (int) ($cat['id'] ?? 0); ?>
                <li><a class="blog-cats__link<?= $categoryFilter === $cid ? ' is-active' : '' ?>" href="<?= htmlspecialchars($buildBlogUrl($cid, null), ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars((string) ($cat['name'] ?? ''), ENT_QUOTES, 'UTF-8') ?></a></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>

    <?php if ($blogPosts === []): ?>
        <p class="blog-empty">尚無文章。</p>
    <?php else: ?>
        <ul class="blog-list">
            <?php foreach ($blogPosts as $post): ?>
                <?php
                $pid = (int) ($post['id'] ?? 0);
                $title = (string) ($post['title'] ?? '');
                $excerpt = trim((string) ($post['excerpt'] ?? ''));
                $catName = trim((string) ($post['category_name'] ?? ''));
                $date = fate_blog_format_date((string) ($post['published_at'] ?? $post['created_at'] ?? ''));
                $isAnn = (int) ($post['is_announcement'] ?? 0) === 1;
                $isDraft = (int) ($post['is_published'] ?? 1) !== 1;
                $isHidden = (int) ($post['is_hidden'] ?? 0) === 1;
                ?>
                <li class="blog-card">
                    <div class="blog-card__meta">
                        <?php if ($isAnn): ?><span class="blog-card__badge">公告</span><?php endif; ?>
                        <?php if ($catName !== ''): ?><span><?= htmlspecialchars($catName, ENT_QUOTES, 'UTF-8') ?></span><?php endif; ?>
                        <?php if ($date !== ''): ?><time datetime="<?= htmlspecialchars(substr($date, 0, 10), ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($date, ENT_QUOTES, 'UTF-8') ?></time><?php endif; ?>
                        <?php if ($blogCanManage && $isDraft): ?><span>草稿</span><?php endif; ?>
                        <?php if ($blogCanManage && $isHidden): ?><span>已隱藏</span><?php endif; ?>
                    </div>
                    <h2 class="blog-card__title"><a href="<?= htmlspecialchars(fate_blog_post_url($pid), ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></a></h2>
                    <?php if ($excerpt !== ''): ?>
                        <p class="blog-card__excerpt"><?= htmlspecialchars($excerpt, ENT_QUOTES, 'UTF-8') ?></p>
                    <?php endif; ?>
                    <?php if ($blogCanManage): ?>
                        <div class="blog-card__actions">
                            <a class="blog-btn blog-btn--sm" href="<?= htmlspecialchars($buildBlogUrl(null, null, false, null, $pid), ENT_QUOTES, 'UTF-8') ?>">編輯</a>
                            <a class="blog-btn blog-btn--sm" href="<?= htmlspecialchars(fate_blog_post_url($pid), ENT_QUOTES, 'UTF-8') ?>">查看</a>
                        </div>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>

        <?php if ($blogPages > 1): ?>
            <nav class="blog-pager" aria-label="文章分頁">
                <?php if ($page > 1): ?>
                    <a class="blog-btn" href="<?= htmlspecialchars($buildBlogUrl($categoryFilter > 0 ? $categoryFilter : null, $page - 1), ENT_QUOTES, 'UTF-8') ?>">上一頁</a>
                <?php endif; ?>
                <span>第 <?= (int) $page ?> / <?= (int) $blogPages ?> 頁（共 <?= (int) $blogTotal ?> 篇）</span>
                <?php if ($page < $blogPages): ?>
                    <a class="blog-btn" href="<?= htmlspecialchars($buildBlogUrl($categoryFilter > 0 ? $categoryFilter : null, $page + 1), ENT_QUOTES, 'UTF-8') ?>">下一頁</a>
                <?php endif; ?>
            </nav>
        <?php endif; ?>
    <?php endif; ?>
    <?php endif; ?>
</main>
