<?php ob_start(); ?>

<?php admin_content_header(__('admin.pages.title'), 'file-text', [
    ['url' => admin_url('pages/create'), 'label' => __('admin.pages.new'), 'class' => 'btn-primary'],
]); ?>

<?php
$bulkUrl = admin_url('pages/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><?= __('admin.common.title') ?></th>
            <th><?= __('admin.common.slug') ?></th>
            <th><?= __('admin.common.status') ?></th>
            <th><?= __('admin.pages.template') ?></th>
            <th><?= __('admin.pages.order') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($pages)): ?>
        <?php admin_empty_state(__('admin.pages.no_pages'), 'file-text', '', '', true, 7); ?>
        <?php else: ?>
        <?php
        $systemTypes = \App\Models\Page::systemPageTypes();
        $typeLabels = [
            'home' => 'Homepage', 'contact' => 'Contact', 'gallery' => 'Gallery',
            'blog' => 'Blog', 'events' => 'Events', 'downloads' => 'Downloads',
        ];
        ?>
        <?php foreach ($pages as $page): ?>
        <?php $isSystem = in_array($page['page_type'] ?? 'page', $systemTypes, true); ?>
        <tr>
            <td class="bulk-check-cell">
                <?php if (!$isSystem): ?>
                <input type="checkbox" data-bulk-item value="<?= (int) $page['id'] ?>" aria-label="Select <?= e($page['title']) ?>">
                <?php endif; ?>
            </td>
            <td>
                <strong><?= e($page['title']) ?></strong>
                <?php if ($page['is_homepage']): ?>
                <span class="badge badge-primary"><?= __('admin.pages.homepage_badge') ?></span>
                <?php endif; ?>
                <?php if ($isSystem && ($page['page_type'] ?? 'page') !== 'page'): ?>
                <span class="badge badge-info"><?= e($typeLabels[$page['page_type']] ?? ucfirst($page['page_type'])) ?></span>
                <?php endif; ?>
            </td>
            <td><code>/<?= e($page['slug']) ?></code></td>
            <td>
                <span class="badge <?= $page['status'] === 'published' ? 'badge-success' : ($page['status'] === 'scheduled' ? 'badge-warning' : 'badge-draft') ?>">
                    <?= __('admin.common.' . $page['status']) ?>
                </span>
            </td>
            <td><?= e($page['template']) ?></td>
            <td><?= (int) $page['sort_order'] ?></td>
            <td class="actions">
                <a href="<?= admin_url('preview/page/' . (int) $page['id']) ?>" class="btn btn-sm btn-secondary" target="_blank" title="<?= e(__('admin.common.preview')) ?>"><?= __('admin.common.preview') ?></a>
                <a href="<?= admin_url('pages/edit/' . (int) $page['id']) ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <?php if (!$isSystem): ?>
                <form method="POST" action="<?= admin_url('pages/clone/' . (int) $page['id']) ?>" class="inline-form"
                      onsubmit="return confirm('<?= e(__('admin.pages.confirm_duplicate', [':title' => $page['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_url('pages/delete/' . (int) $page['id']) ?>" class="inline-form" onsubmit="return confirm('<?= e(__('admin.pages.confirm_delete')) ?>')">
                    <?= $csrf->field() ?>
                    <button type="submit" class="btn btn-sm btn-danger"><?= __('admin.common.delete') ?></button>
                </form>
                <?php endif; ?>
            </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
</div>

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