<?php ob_start(); ?>

<?php admin_content_header(__('admin.events.title'), 'calendar', [
    ['url' => admin_url('events/categories'), 'label' => __('admin.event_categories.title')],
    ['url' => admin_url('events/create'), 'label' => __('admin.events.new'), 'class' => 'btn-primary'],
]); ?>

<!-- Filter bar -->
<form method="GET" action="<?= admin_url('events') ?>" class="filter-bar">
    <input type="text" name="search" value="<?= e($search ?? '') ?>" placeholder="<?= __('admin.events.search_placeholder') ?>">

    <select name="publish_status">
        <option value=""><?= __('admin.events.all_publish_statuses') ?></option>
        <option value="draft"     <?= ($publishStatus ?? '') === 'draft'     ? 'selected' : '' ?>><?= __('admin.common.draft') ?></option>
        <option value="published" <?= ($publishStatus ?? '') === 'published' ? 'selected' : '' ?>><?= __('admin.common.published') ?></option>
    </select>

    <select name="event_status">
        <option value=""><?= __('admin.events.all_event_statuses') ?></option>
        <option value="scheduled"  <?= ($eventStatus ?? '') === 'scheduled'  ? 'selected' : '' ?>><?= __('admin.events.scheduled') ?></option>
        <option value="postponed"  <?= ($eventStatus ?? '') === 'postponed'  ? 'selected' : '' ?>><?= __('admin.events.postponed') ?></option>
        <option value="cancelled"  <?= ($eventStatus ?? '') === 'cancelled'  ? 'selected' : '' ?>><?= __('admin.events.cancelled') ?></option>
    </select>

    <select name="category_id">
        <option value=""><?= __('admin.events.all_categories') ?></option>
        <?php foreach ($categories as $cat): ?>
        <option value="<?= (int) $cat['id'] ?>" <?= (int) ($categoryId ?? 0) === (int) $cat['id'] ? 'selected' : '' ?>>
            <?= e($cat['name']) ?>
        </option>
        <?php endforeach; ?>
    </select>

    <select name="featured">
        <option value="" <?= ($featured ?? -1) === -1 ? 'selected' : '' ?>><?= __('admin.common.all') ?></option>
        <option value="1" <?= ($featured ?? -1) === 1  ? 'selected' : '' ?>><?= __('admin.events.featured_only') ?></option>
        <option value="0" <?= ($featured ?? -1) === 0  ? 'selected' : '' ?>><?= __('admin.events.non_featured') ?></option>
    </select>

    <button type="submit" class="btn btn-secondary btn-sm"><?= __('admin.common.filter') ?></button>
    <?php if (($search ?? '') !== '' || ($publishStatus ?? '') !== '' || ($eventStatus ?? '') !== '' || ($categoryId ?? 0) > 0 || ($featured ?? -1) !== -1): ?>
    <a href="<?= admin_url('events') ?>" class="btn btn-secondary btn-sm"><?= __('admin.common.clear') ?></a>
    <?php endif; ?>
</form>

<?php
$bulkUrl = admin_url('events/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.events.start') ?></th>
            <th><?= __('admin.events.location') ?></th>
            <th><?= __('admin.event_categories.title') ?></th>
            <th><?= __('admin.common.status') ?></th>
            <th><?= __('admin.events.event_status') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($events)): ?>
        <?php admin_empty_state(__('admin.events.no_events'), 'calendar', admin_url('events/create'), __('admin.events.create_first'), true, 8); ?>
        <?php else: ?>
        <?php foreach ($events as $event): ?>
        <tr>
            <td class="bulk-check-cell">
                <input type="checkbox" data-bulk-item value="<?= (int) $event['id'] ?>" aria-label="Select <?= e($event['title']) ?>">
            </td>
            <td>
                <strong><?= e($event['title']) ?></strong>
                <?php if ($event['is_featured']): ?>
                <span class="badge badge-warning">★ <?= __('admin.events.featured_badge') ?></span>
                <?php endif; ?>
            </td>
            <td>
                <?= e(format_date($event['start_date'])) ?>
                <?php if (!empty($event['start_time'])): ?>
                <span class="text-muted"><?= e(substr($event['start_time'], 0, 5)) ?></span>
                <?php endif; ?>
            </td>
            <td><?= e($event['location'] ?? '—') ?></td>
            <td><?= e($event['category_name'] ?? '—') ?></td>
            <td>
                <span class="badge <?= $event['publish_status'] === 'published' ? 'badge-success' : 'badge-draft' ?>">
                    <?= e($event['publish_status']) ?>
                </span>
            </td>
            <td>
                <?php
                $evtBadgeClass = match($event['event_status']) {
                    'postponed' => 'badge-warning',
                    'cancelled' => 'badge-danger',
                    default     => 'badge-success',
                };
                ?>
                <span class="badge <?= $evtBadgeClass ?>">
                    <?= e($event['event_status']) ?>
                </span>
            </td>
            <td class="actions">
                <?php if ($event['publish_status'] === 'published'): ?>
                <a href="/events/<?= e($event['slug']) ?>" class="btn btn-sm btn-secondary" target="_blank" title="<?= __('admin.common.view') ?>"><?= __('admin.common.view') ?></a>
                <?php endif; ?>
                <a href="<?= admin_url('events/edit/' . (int) $event['id']) ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <form method="POST" action="<?= admin_url('events/delete/' . (int) $event['id']) ?>" class="inline-form"
                      onsubmit="return confirm('<?= __('admin.events.confirm_delete', [':title' => e($event['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'; ?>
