<?php

declare(strict_types=1);

/**
 * BLOG 文章內頁主內容（default / minimal 共用）。
 *
 * @var array<string,mixed> $blogPost
 * @var bool $blogCanManage
 * @var bool $editMode
 * @var list<array<string,mixed>> $blogCategories
 * @var int $postId
 * @var string $csrf
 * @var string $msgOk
 * @var string $msgBad
 * @var string $fateBlogNavLabel
 */

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

$title = (string) ($blogPost['title'] ?? '');
$body = (string) ($blogPost['body_html'] ?? '');
$catName = trim((string) ($blogPost['category_name'] ?? ''));
$date = fate_blog_format_date((string) ($blogPost['published_at'] ?? $blogPost['created_at'] ?? ''));
$isAnn = (int) ($blogPost['is_announcement'] ?? 0) === 1;
$isPublished = (int) ($blogPost['is_published'] ?? 1) === 1;
$isHidden = (int) ($blogPost['is_hidden'] ?? 0) === 1;
?>
<article class="blog-post-article">
    <?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 ($editMode): ?>
        <?php
        $blogFormPostId = $postId;
        $blogFormTitle = $title;
        $blogFormBody = $body;
        $blogFormExcerpt = (string) ($blogPost['excerpt'] ?? '');
        $blogFormCategoryId = (int) ($blogPost['category_id'] ?? 0);
        $blogFormIsAnn = $isAnn;
        $blogFormIsPublished = $isPublished;
        $blogFormIsHidden = $isHidden;
        $blogFormReturnTo = 'blog_post.php?id=' . $postId;
        $blogFormCancelUrl = 'blog_post.php?id=' . $postId;
        $blogEditorId = 'blog-edit-body';
        require __DIR__ . '/blog_post_form.inc.php';
        ?>
    <?php else: ?>
        <div class="blog-post-article__card">
            <header class="blog-post-article__head">
                <a class="blog-post-article__back" href="blog.php">← 返回<?= htmlspecialchars($fateBlogNavLabel, ENT_QUOTES, 'UTF-8') ?></a>
                <h1 class="blog-post-article__title"><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></h1>
                <div class="blog-post-article__meta">
                    <?php if ($isAnn): ?><span class="blog-post-article__badge blog-post-article__badge--ann">公告</span><?php endif; ?>
                    <?php if ($catName !== ''): ?><span class="blog-post-article__badge blog-post-article__badge--cat"><?= htmlspecialchars($catName, ENT_QUOTES, 'UTF-8') ?></span><?php endif; ?>
                    <?php if ($date !== ''): ?><time class="blog-post-article__date" datetime="<?= htmlspecialchars(substr($date, 0, 10), ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($date, ENT_QUOTES, 'UTF-8') ?></time><?php endif; ?>
                </div>
            </header>
            <div class="blog-post-article__body masseur-review__body-inner masseur-review__body-inner--rich">
                <?= fate_blog_body_html($body) ?>
            </div>
        </div>

        <?php if ($blogCanManage): ?>
            <div class="blog-post-admin">
                <a class="blog-btn" href="blog_post.php?id=<?= (int) $postId ?>&edit=1">編輯</a>
                <form method="post" action="api/blog_action.php" class="blog-inline-form" onsubmit="return confirm('確定要刪除此文章？');">
                    <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                    <input type="hidden" name="action" value="delete_post">
                    <input type="hidden" name="post_id" value="<?= (int) $postId ?>">
                    <input type="hidden" name="return_to" value="blog.php">
                    <button type="submit" class="blog-btn blog-btn--danger">刪除</button>
                </form>
                <form method="post" action="api/blog_action.php" class="blog-inline-form">
                    <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                    <input type="hidden" name="action" value="toggle_hidden">
                    <input type="hidden" name="post_id" value="<?= (int) $postId ?>">
                    <input type="hidden" name="hidden" value="<?= $isHidden ? '0' : '1' ?>">
                    <input type="hidden" name="return_to" value="blog_post.php?id=<?= (int) $postId ?>">
                    <button type="submit" class="blog-btn"><?= $isHidden ? '恢復顯示' : '隱藏文章' ?></button>
                </form>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</article>
