<?php
require_once __DIR__ . '/../functions.php';
ensure_role(['Admin', 'Manager']);
$posts = pdo()->query('SELECT bp.*, bc.name AS category_name FROM blog_posts bp LEFT JOIN blog_categories bc ON bp.category_id = bc.id ORDER BY created_at DESC')->fetchAll();
$categories = pdo()->query('SELECT * FROM blog_categories ORDER BY name')->fetchAll();
?>
<?php include __DIR__ . '/includes/header.php'; ?>
    <section class="panel">
        <header>
            <h2>Blog Posts</h2>
            <button class="btn btn-primary" onclick="showBlogModal()">Create Post</button>
        </header>
        <div class="table-scroll">
            <table>
                <thead><tr><th>Title</th><th>Category</th><th>Status</th><th>Published</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($posts as $post): ?>
                    <tr data-post='<?= json_encode($post, JSON_HEX_APOS | JSON_HEX_QUOT) ?>'>
                        <td><?= clean($post['title']) ?></td>
                        <td><?= clean($post['category_name']) ?></td>
                        <td><?= clean($post['status']) ?></td>
                        <td><?= clean($post['created_at']) ?></td>
                        <td>
                            <button class="btn btn-secondary" onclick="editBlog(this)">Edit</button>
                            <button class="btn btn-danger" onclick="deleteItem('delete_blog', <?= $post['id'] ?>, 'admin/blog.php')">Delete</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
    <section class="panel">
        <header>
            <h2>Blog Categories</h2>
            <button class="btn btn-primary" onclick="showBlogCategoryModal()">Add Category</button>
        </header>
        <div class="table-scroll">
            <table>
                <thead><tr><th>Thumbnail</th><th>Name</th><th>Status</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($categories as $category): ?>
                    <tr data-category='<?= json_encode($category, JSON_HEX_APOS | JSON_HEX_QUOT) ?>'>
                        <td>
                            <?php if (!empty($category['thumbnail'])): ?>
                                <img src="<?= BASE_URL . UPLOAD_URL . '/' . clean($category['thumbnail']) ?>" alt="Category thumbnail" class="table-thumb">
                            <?php else: ?>
                                <span class="table-thumb placeholder">No image</span>
                            <?php endif; ?>
                        </td>
                        <td><?= clean($category['name']) ?></td>
                        <td><?= clean($category['status'] ?? 'Published') ?></td>
                        <td>
                            <button class="btn btn-secondary" onclick="editBlogCategory(this)">Edit</button>
                            <button class="btn btn-danger" onclick="deleteItem('delete_blog_category', <?= $category['id'] ?>, 'admin/blog.php')">Delete</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
    <div id="blog-category-modal" class="modal hidden">
        <div class="modal-panel">
            <header>
                <h3 id="blog-category-modal-title">Add Blog Category</h3>
                <button class="close" onclick="closeModal('blog-category-modal')">×</button>
            </header>
            <form method="post" action="actions.php" enctype="multipart/form-data">
                <input type="hidden" name="csrf" value="<?= csrf_token() ?>">
                <input type="hidden" name="action" value="create_blog_category">
                <input type="hidden" name="id" value="">
                <label>Name</label>
                <input type="text" name="name" required>
                <label>SEO Title</label>
                <input type="text" name="seo_title" placeholder="Optional SEO title">
                <label>SEO Description</label>
                <textarea name="seo_description" rows="3" placeholder="Optional SEO description"></textarea>
                <label>Thumbnail</label>
                <input type="file" name="thumbnail" accept="image/*">
                <label>Status</label>
                <select name="status">
                    <option>Published</option>
                    <option>Draft</option>
                </select>
                <button type="submit" class="btn btn-primary">Save Category</button>
            </form>
        </div>
    </div>

    <div id="blog-modal" class="modal hidden">
        <div class="modal-panel big-modal">
            <header>
                <h3 id="blog-modal-title">Create Blog Post</h3>
                <button class="close" onclick="closeModal('blog-modal')">×</button>
            </header>
            <form id="blog-form" method="post" action="actions.php" enctype="multipart/form-data">
                <input type="hidden" name="csrf" value="<?= csrf_token() ?>">
                <input type="hidden" name="action" value="create_blog">
                <input type="hidden" name="id" value="">
                <label>Title</label>
                <input type="text" name="title" required>
                <label>Content</label>
                <textarea name="content" rows="6" required></textarea>
                <label>Category</label>
                <select name="category_id" required>
                    <?php foreach ($categories as $category): ?>
                        <option value="<?= $category['id'] ?>"><?= clean($category['name']) ?></option>
                    <?php endforeach; ?>
                </select>
                <label>Featured Image</label>
                <input type="file" name="featured_image" accept="image/*">
                <label>SEO Title</label>
                <input type="text" name="seo_title" placeholder="Optional SEO title">
                <label>SEO Description</label>
                <textarea name="seo_description" rows="3" placeholder="Optional SEO description"></textarea>
                <label>Status</label>
                <select name="status">
                    <option>Draft</option>
                    <option>Published</option>
                </select>
                <button type="submit" class="btn btn-primary">Save Post</button>
            </form>
        </div>
    </div>
<?php include __DIR__ . '/includes/footer.php'; ?>
