<?php ob_start(); ?>

<div class="content-header">
    <h2><?= __('admin.blog.title') ?></h2>
    <div class="header-actions">
        <a href="/admin/blog/tags" class="btn btn-secondary"><?= __('admin.nav.tags') ?></a>
        <a href="/admin/blog/categories" class="btn btn-secondary"><?= __('admin.nav.categories') ?></a>
        <a href="/admin/blog/create" class="btn btn-primary"><?= __('admin.blog.new') ?></a>
    </div>
</div>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <th class="table-thumb-cell"></th>
            <th><?= __('admin.common.title') ?></th>
            <th><?= __('admin.blog.category') ?></th>
            <th><?= __('admin.blog.author') ?></th>
            <th><?= __('admin.common.status') ?></th>
            <th><?= __('admin.common.date') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($posts)): ?>
        <tr><td colspan="7" class="text-center text-muted"><?= __('admin.blog.no_posts') ?> <a href="/admin/blog/create"><?= __('admin.blog.create_first') ?></a></td></tr>
        <?php else: ?>
        <?php foreach ($posts as $post): ?>
        <tr>
            <td class="table-thumb-cell">
                <?php if (!empty($post['featured_image'])): ?>
                <img src="<?= e(thumb_url($post['featured_image'])) ?>"
                     alt=""
                     class="table-thumb">
                <?php else: ?>
                <div class="table-thumb-placeholder">✎</div>
                <?php endif; ?>
            </td>
            <td>
                <strong><?= e($post['title']) ?></strong>
            </td>
            <td><?= e($post['category_name'] ?? '—') ?></td>
            <td><?= e($post['author_name'] ?? '—') ?></td>
            <td>
                <span class="badge <?= $post['status'] === 'published' ? 'badge-success' : 'badge-draft' ?>">
                    <?= e($post['status']) ?>
                </span>
            </td>
            <td>
                <?php
                $date = $post['published_at'] ?? $post['created_at'];
                echo $date ? e(format_date($date)) : '—';
                ?>
            </td>
            <td class="actions">
                <a href="/admin/preview/blog/<?= (int) $post['id'] ?>" class="btn btn-sm btn-secondary" target="_blank" title="<?= e(__('admin.common.preview')) ?>"><?= __('admin.common.preview') ?></a>
                <?php if ($post['status'] === 'published'): ?>
                <a href="/blog/<?= e($post['slug']) ?>" class="btn btn-sm btn-secondary" target="_blank" title="View on site"><?= __('admin.common.view') ?></a>
                <?php endif; ?>
                <a href="/admin/blog/edit/<?= (int) $post['id'] ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <form method="POST" action="/admin/blog/clone/<?= (int) $post['id'] ?>" class="inline-form"
                      onsubmit="return confirm('<?= e(__('admin.blog.confirm_duplicate', [':title' => $post['title']])) ?>')">
                    <?= $csrf->field() ?>
                    <button type="submit" class="btn btn-sm btn-secondary" title="Duplicate as draft"><?= __('admin.common.duplicate') ?></button>
                </form>
                <form method="POST" action="/admin/blog/delete/<?= (int) $post['id'] ?>" class="inline-form"
                      onsubmit="return confirm('<?= e(__('admin.blog.confirm_delete', [':title' => $post['title']])) ?>')">
                    <?= $csrf->field() ?>
                    <button type="submit" class="btn btn-sm btn-danger"><?= __('admin.common.delete') ?></button>
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
