<?php ob_start(); ?>

<?php admin_content_header(__('admin.blog.title'), 'pen-line', [
    ['url' => '/admin/blog/tags', 'label' => __('admin.nav.tags')],
    ['url' => '/admin/blog/categories', 'label' => __('admin.nav.categories')],
    ['url' => '/admin/blog/create', 'label' => __('admin.blog.new'), 'class' => 'btn-primary'],
]); ?>

<?php
$bulkUrl = '/admin/blog/bulk';
$bulkActions = [
    ['action' => 'publish', 'label' => __('admin.common.bulk_publish'), 'confirm' => __('admin.common.bulk_confirm_status')],
    ['action' => 'draft',   'label' => __('admin.common.bulk_draft'),   'confirm' => __('admin.common.bulk_confirm_status')],
    ['action' => 'delete',  'label' => __('admin.common.bulk_delete'),  'class' => 'btn-bulk-danger', 'confirm' => __('admin.common.bulk_confirm_delete')],
];
include dirname(__DIR__) . '/layouts/_bulk_bar.php';
?>

<div class="table-responsive">
<table class="data-table" data-bulk-table>
    <thead>
        <tr>
            <th class="bulk-check-cell"><input type="checkbox" data-bulk-select-all aria-label="Select all"></th>
            <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)): ?>
        <?php admin_empty_state(__('admin.blog.no_posts'), 'pen-line', '/admin/blog/create', __('admin.blog.create_first'), true, 8); ?>
        <?php else: ?>
        <?php foreach ($posts as $post): ?>
        <tr>
            <td class="bulk-check-cell">
                <input type="checkbox" data-bulk-item value="<?= (int) $post['id'] ?>" aria-label="Select <?= e($post['title']) ?>">
            </td>
            <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"><?= icon('pen-line', 18) ?></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' : ($post['status'] === 'scheduled' ? 'badge-warning' : 'badge-draft') ?>">
                    <?= __('admin.common.' . $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'; ?>
