<?php

declare(strict_types=1);

/**
 * BLOG 發文／編輯表單（列表頁 compose、edit 與文章內頁 edit 共用）。
 *
 * @var int $blogFormPostId
 * @var string $blogFormHeading
 * @var string $blogFormReturnTo
 * @var string $blogFormTitle
 * @var string $blogFormBody
 * @var string $blogFormExcerpt
 * @var int $blogFormCategoryId
 * @var bool $blogFormIsAnn
 * @var bool $blogFormIsPublished
 * @var bool $blogFormIsHidden
 * @var list<array<string,mixed>> $blogCategories
 * @var string $csrf
 * @var string $blogEditorId
 */

$blogFormPostId = (int) ($blogFormPostId ?? 0);
$blogFormHeading = (string) ($blogFormHeading ?? ($blogFormPostId > 0 ? '編輯文章' : '發表新文章'));
$blogFormReturnTo = (string) ($blogFormReturnTo ?? ($blogFormPostId > 0 ? ('blog_post.php?id=' . $blogFormPostId) : 'blog.php'));
$blogFormTitle = (string) ($blogFormTitle ?? '');
$blogFormBody = (string) ($blogFormBody ?? '');
$blogFormExcerpt = (string) ($blogFormExcerpt ?? '');
$blogFormCategoryId = (int) ($blogFormCategoryId ?? 0);
$blogFormIsAnn = !empty($blogFormIsAnn);
$blogFormIsPublished = !isset($blogFormIsPublished) || !empty($blogFormIsPublished);
$blogFormIsHidden = !empty($blogFormIsHidden);
$blogEditorId = (string) ($blogEditorId ?? ($blogFormPostId > 0 ? 'blog-edit-body' : 'blog-body'));
$blogFormCancelUrl = isset($blogFormCancelUrl) && is_string($blogFormCancelUrl) ? $blogFormCancelUrl : '';
$isCreate = $blogFormPostId <= 0;
$blogFieldPrefix = preg_replace('/[^a-z0-9_-]/', '', strtolower($blogEditorId)) ?: 'blog-body';
?>
<section class="blog-compose"<?= $isCreate ? ' id="blog-compose"' : '' ?>>
    <<?= $isCreate ? 'h2' : 'h1' ?> class="blog-compose__title"><?= htmlspecialchars($blogFormHeading, ENT_QUOTES, 'UTF-8') ?></<?= $isCreate ? 'h2' : 'h1' ?>>
    <form method="post" action="api/blog_action.php" class="blog-compose-form">
        <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
        <input type="hidden" name="action" value="save_post">
        <?php if ($blogFormPostId > 0): ?>
            <input type="hidden" name="post_id" value="<?= $blogFormPostId ?>">
        <?php endif; ?>
        <input type="hidden" name="return_to" value="<?= htmlspecialchars($blogFormReturnTo, ENT_QUOTES, 'UTF-8') ?>">
        <div class="blog-field">
            <label for="<?= htmlspecialchars($blogFieldPrefix, ENT_QUOTES, 'UTF-8') ?>-title">標題</label>
            <input type="text" id="<?= htmlspecialchars($blogFieldPrefix, ENT_QUOTES, 'UTF-8') ?>-title" name="title" maxlength="200" required<?= $isCreate ? ' placeholder="文章標題"' : '' ?> value="<?= htmlspecialchars($blogFormTitle, ENT_QUOTES, 'UTF-8') ?>">
        </div>
        <div class="blog-field<?= $isCreate ? '-row' : '' ?>">
            <div class="blog-field"<?= $isCreate ? ' style="flex:1;min-width:180px;"' : '' ?>>
                <label for="<?= htmlspecialchars($blogFieldPrefix, ENT_QUOTES, 'UTF-8') ?>-category">分類</label>
                <select id="<?= htmlspecialchars($blogFieldPrefix, ENT_QUOTES, 'UTF-8') ?>-category" name="category_id">
                    <option value="0">— 未分類 —</option>
                    <?php foreach ($blogCategories as $cat): ?>
                        <?php $cid = (int) ($cat['id'] ?? 0); ?>
                        <option value="<?= $cid ?>"<?= $blogFormCategoryId === $cid ? ' selected' : '' ?>><?= htmlspecialchars((string) ($cat['name'] ?? ''), ENT_QUOTES, 'UTF-8') ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="blog-field">
            <label for="<?= htmlspecialchars($blogEditorId, ENT_QUOTES, 'UTF-8') ?>">內文</label>
            <?php
            $blogEditorBody = $blogFormBody;
            require __DIR__ . '/blog_editor.inc.php';
            ?>
        </div>
        <div class="blog-field">
            <label for="<?= htmlspecialchars($blogFieldPrefix, ENT_QUOTES, 'UTF-8') ?>-excerpt">摘要<?= $isCreate ? '（選填，列表用）' : '' ?></label>
            <input type="text" id="<?= htmlspecialchars($blogFieldPrefix, ENT_QUOTES, 'UTF-8') ?>-excerpt" name="excerpt" maxlength="400"<?= $isCreate ? ' placeholder="留空則自動擷取內文"' : '' ?> value="<?= htmlspecialchars($blogFormExcerpt, ENT_QUOTES, 'UTF-8') ?>">
        </div>
        <div class="blog-field-row">
            <label class="blog-check"><input type="checkbox" name="is_announcement" value="1"<?= $blogFormIsAnn ? ' checked' : '' ?>> <?= $isCreate ? '設為站內公告（首頁顯示）' : '站內公告' ?></label>
            <label class="blog-check"><input type="checkbox" name="is_published" value="1"<?= $blogFormIsPublished ? ' checked' : '' ?>> <?= $isCreate ? '立即發布' : '已發布' ?></label>
            <?php if (!$isCreate): ?>
                <label class="blog-check"><input type="checkbox" name="is_hidden" value="1"<?= $blogFormIsHidden ? ' checked' : '' ?>> 隱藏</label>
            <?php endif; ?>
        </div>
        <button type="submit" class="blog-btn blog-btn--primary"><?= $isCreate ? '儲存文章' : '儲存' ?></button>
        <?php if (!$isCreate && $blogFormCancelUrl !== ''): ?>
            <a class="blog-btn" href="<?= htmlspecialchars($blogFormCancelUrl, ENT_QUOTES, 'UTF-8') ?>">取消</a>
        <?php endif; ?>
    </form>
</section>
