<?php

declare(strict_types=1);

/**
 * BLOG 前台管理：功能設定、分類（僅管理員）。
 *
 * @var string $manageTab settings|categories
 * @var array{enabled:bool,posts_per_page:int,show_announcements_on_home:bool,home_announcements_limit:int,nav_label:string} $blogSettings
 * @var list<array<string,mixed>> $blogCategories
 * @var string $csrf
 * @var Closure $buildBlogUrl
 */

if (($manageTab ?? '') === '') {
    return;
}
?>
<section class="blog-manage" aria-label="BLOG 管理">
    <nav class="blog-manage-tabs" aria-label="BLOG 管理分頁">
        <a class="blog-manage-tabs__link<?= $manageTab === 'settings' ? ' is-active' : '' ?>" href="<?= htmlspecialchars($buildBlogUrl(null, null, false, 'settings'), ENT_QUOTES, 'UTF-8') ?>">功能設定</a>
        <a class="blog-manage-tabs__link<?= $manageTab === 'categories' ? ' is-active' : '' ?>" href="<?= htmlspecialchars($buildBlogUrl(null, null, false, 'categories'), ENT_QUOTES, 'UTF-8') ?>">分類管理</a>
        <a class="blog-manage-tabs__link" href="<?= htmlspecialchars($buildBlogUrl(null, null), ENT_QUOTES, 'UTF-8') ?>">返回文章列表</a>
    </nav>

    <?php if ($manageTab === 'settings'): ?>
        <div class="blog-manage-panel">
            <h2 class="blog-manage-panel__title">功能設定</h2>
            <form method="post" action="api/blog_action.php" class="blog-manage-form">
                <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                <input type="hidden" name="action" value="save_settings">
                <input type="hidden" name="return_to" value="blog.php?manage=settings">
                <div class="blog-field-row">
                    <label class="blog-check"><input type="checkbox" name="enabled" value="1"<?= !empty($blogSettings['enabled']) ? ' checked' : '' ?>> 啟用站內 BLOG（導覽與 blog.php）</label>
                </div>
                <div class="blog-field-row">
                    <label class="blog-check"><input type="checkbox" name="show_announcements_on_home" value="1"<?= !empty($blogSettings['show_announcements_on_home']) ? ' checked' : '' ?>> 首頁側欄顯示站內公告</label>
                </div>
                <div class="blog-field">
                    <label for="blog-settings-nav-label">導覽列名稱</label>
                    <input type="text" id="blog-settings-nav-label" name="nav_label" maxlength="24" value="<?= htmlspecialchars((string) ($blogSettings['nav_label'] ?? ''), ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="blog-field-row">
                    <div class="blog-field" style="flex:0 1 auto;min-width:8rem;">
                        <label for="blog-settings-ppp">列表每頁篇數</label>
                        <input type="number" id="blog-settings-ppp" name="posts_per_page" min="1" max="50" value="<?= (int) ($blogSettings['posts_per_page'] ?? 12) ?>">
                    </div>
                    <div class="blog-field" style="flex:0 1 auto;min-width:8rem;">
                        <label for="blog-settings-ann-limit">首頁公告則數</label>
                        <input type="number" id="blog-settings-ann-limit" name="home_announcements_limit" min="1" max="10" value="<?= (int) ($blogSettings['home_announcements_limit'] ?? 3) ?>">
                    </div>
                </div>
                <button type="submit" class="blog-btn blog-btn--primary">儲存設定</button>
            </form>
        </div>

    <?php elseif ($manageTab === 'categories'): ?>
        <div class="blog-manage-panel">
            <h2 class="blog-manage-panel__title">新增分類</h2>
            <form method="post" action="api/blog_action.php" class="blog-manage-inline-form">
                <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                <input type="hidden" name="action" value="save_category">
                <input type="hidden" name="category_id" value="0">
                <input type="hidden" name="return_to" value="blog.php?manage=categories">
                <input type="text" name="name" placeholder="分類名稱" required maxlength="64">
                <input type="text" name="slug" placeholder="slug（選填）" maxlength="64">
                <input type="number" name="sort_order" value="0" title="排序" style="width:5rem;">
                <button type="submit" class="blog-btn blog-btn--primary">新增</button>
            </form>
        </div>

        <div class="blog-manage-panel">
            <h2 class="blog-manage-panel__title">現有分類</h2>
            <?php if ($blogCategories === []): ?>
                <p class="blog-empty">尚無分類。</p>
            <?php else: ?>
                <ul class="blog-manage-cat-list">
                    <?php foreach ($blogCategories as $cat): ?>
                        <?php $cid = (int) ($cat['id'] ?? 0); ?>
                        <li class="blog-manage-cat-item">
                            <form method="post" action="api/blog_action.php" class="blog-manage-inline-form blog-manage-inline-form--cat">
                                <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                                <input type="hidden" name="action" value="save_category">
                                <input type="hidden" name="category_id" value="<?= $cid ?>">
                                <input type="hidden" name="return_to" value="blog.php?manage=categories">
                                <input type="text" name="name" value="<?= htmlspecialchars((string) ($cat['name'] ?? ''), ENT_QUOTES, 'UTF-8') ?>" required maxlength="64">
                                <input type="text" name="slug" value="<?= htmlspecialchars((string) ($cat['slug'] ?? ''), ENT_QUOTES, 'UTF-8') ?>" maxlength="64" placeholder="slug">
                                <input type="number" name="sort_order" value="<?= (int) ($cat['sort_order'] ?? 0) ?>" style="width:4.5rem;" title="排序">
                                <label class="blog-check"><input type="checkbox" name="is_hidden" value="1"<?= !empty($cat['is_hidden']) ? ' checked' : '' ?>> 隱藏</label>
                                <button type="submit" class="blog-btn">儲存</button>
                            </form>
                            <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_category">
                                <input type="hidden" name="category_id" value="<?= $cid ?>">
                                <input type="hidden" name="return_to" value="blog.php?manage=categories">
                                <button type="submit" class="blog-btn blog-btn--danger">刪除</button>
                            </form>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</section>
