<?php ob_start(); ?>

<div class="content-header">
    <h2><?= __('admin.pages.title') ?></h2>
    <a href="/admin/pages/create" class="btn btn-primary"><?= __('admin.pages.new') ?></a>
</div>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <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)): ?>
        <tr><td colspan="6" class="text-center text-muted"><?= __('admin.pages.no_pages') ?></td></tr>
        <?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>
                <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' : 'badge-draft' ?>">
                    <?= e($page['status']) ?>
                </span>
            </td>
            <td><?= e($page['template']) ?></td>
            <td><?= (int) $page['sort_order'] ?></td>
            <td class="actions">
                <a href="/admin/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/pages/edit/<?= (int) $page['id'] ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <?php if (!$isSystem): ?>
                <form method="POST" action="/admin/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/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'; ?>
