<?php
$pageTitle = 'Blog Posts';
require_once dirname(__DIR__) . '/includes/header.php';

// Handle delete
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    db()->query("DELETE FROM blog_posts WHERE id=$id");
    flash('Blog post deleted.', 'success');
    redirect(ADMIN_URL . '/pages/blog.php');
}

// Search
$search = trim($_GET['q'] ?? '');
$where = $search ? "WHERE title LIKE '%" . db()->real_escape_string($search) . "%' OR category LIKE '%" . db()->real_escape_string($search) . "%'" : '';

$posts = db()->query("SELECT * FROM blog_posts $where ORDER BY created_at DESC")->fetch_all(MYSQLI_ASSOC);
?>

<div class="page-header">
    <h1>Blog Posts</h1>
    <div class="page-header-actions">
        <a href="blog-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> New Post</a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <div class="search-bar" style="width:280px">
            <i class="fas fa-search"></i>
            <form method="GET">
                <input type="text" name="q" class="form-control" placeholder="Search posts..." value="<?= e($search) ?>">
            </form>
        </div>
        <span style="color:var(--muted);font-size:13px"><?= count($posts) ?> posts</span>
    </div>
    <div class="table-wrap">
        <?php if (empty($posts)): ?>
            <div style="padding:48px;text-align:center;color:var(--muted)">
                <i class="fas fa-rss" style="font-size:36px;margin-bottom:12px;display:block;opacity:.3"></i>
                No blog posts yet. <a href="blog-add.php" style="color:var(--brand)">Create your first post</a>
            </div>
        <?php else: ?>
        <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 $p): ?>
            <tr>
                <td>
                    <div style="font-weight:600;margin-bottom:2px"><?= e($p['title']) ?></div>
                    <div style="font-size:11px;color:var(--muted)">/blog/<?= e($p['slug']) ?></div>
                </td>
                <td><span class="badge badge-blue"><?= e($p['category'] ?: '—') ?></span></td>
                <td>
                    <span class="badge <?= $p['status']==='published' ? 'badge-green' : 'badge-yellow' ?>">
                        <?= ucfirst($p['status']) ?>
                    </span>
                </td>
                <td style="color:var(--muted);font-size:13px;white-space:nowrap">
                    <?= $p['published_at'] ? date('M j, Y', strtotime($p['published_at'])) : date('M j, Y', strtotime($p['created_at'])) ?>
                </td>
                <td>
                    <div style="display:flex;gap:6px">
                        <a href="blog-edit.php?id=<?= $p['id'] ?>" class="btn btn-secondary btn-sm btn-icon" title="Edit"><i class="fas fa-edit"></i></a>
                        <button onclick="confirmDelete('blog.php?delete=<?= $p['id'] ?>','<?= e(addslashes($p['title'])) ?>')" class="btn btn-danger btn-sm btn-icon" title="Delete"><i class="fas fa-trash"></i></button>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
