<?php ob_start(); ?>

<?php admin_content_header(__('admin.redirects.title'), 'arrow-right-left', [
    ['url' => '/admin/redirects/create', 'label' => __('admin.redirects.btn_create'), 'class' => 'btn-primary'],
]); ?>

<?php
$bulkUrl = '/admin/redirects/bulk';
$bulkActions = [
    ['action' => 'activate',   'label' => __('admin.common.bulk_activate'),   'confirm' => __('admin.common.bulk_confirm_status')],
    ['action' => 'deactivate', 'label' => __('admin.common.bulk_deactivate'), '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.redirects.source_path') ?></th>
            <th><?= __('admin.redirects.target_url') ?></th>
            <th><?= __('admin.redirects.status_code') ?></th>
            <th><?= __('admin.redirects.hit_count') ?></th>
            <th><?= __('admin.redirects.is_active') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($redirects)): ?>
        <?php admin_empty_state(__('admin.redirects.no_entries'), 'arrow-right-left', '/admin/redirects/create', __('admin.redirects.btn_create'), true, 7); ?>
        <?php else: ?>
        <?php foreach ($redirects as $r): ?>
        <tr>
            <td class="bulk-check-cell">
                <input type="checkbox" data-bulk-item value="<?= (int) $r['id'] ?>" aria-label="Select redirect">
            </td>
            <td><code><?= e($r['source_path']) ?></code></td>
            <td><?= e($r['target_url']) ?></td>
            <td>
                <span class="badge <?= (int) $r['status_code'] === 301 ? 'badge-primary' : 'badge-secondary' ?>">
                    <?= (int) $r['status_code'] ?>
                </span>
            </td>
            <td><?= (int) $r['hit_count'] ?></td>
            <td>
                <span class="badge <?= $r['is_active'] ? 'badge-success' : 'badge-draft' ?>">
                    <?= $r['is_active'] ? __('admin.common.active') : __('admin.common.inactive') ?>
                </span>
            </td>
            <td class="actions">
                <a href="/admin/redirects/edit/<?= (int) $r['id'] ?>" class="btn btn-sm" title="<?= __('admin.common.edit') ?>">
                    <?= render_icon('pencil') ?>
                </a>
                <form method="POST" action="/admin/redirects/delete/<?= (int) $r['id'] ?>" class="inline-form"
                      onsubmit="return confirm('<?= __('admin.common.confirm_delete') ?>')">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-danger" title="<?= __('admin.common.delete') ?>">
                        <?= render_icon('trash-2') ?>
                    </button>
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
</div>

<?php if (!empty($pagination) && $pagination['pages'] > 1): ?>
<div class="pagination">
    <?php for ($i = 1; $i <= $pagination['pages']; $i++): ?>
        <?php if ($i === $pagination['page']): ?>
            <span class="pagination-current"><?= $i ?></span>
        <?php else: ?>
            <a href="/admin/redirects?page=<?= $i ?>" class="pagination-link"><?= $i ?></a>
        <?php endif; ?>
    <?php endfor; ?>
</div>
<?php endif; ?>

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