<?php ob_start(); ?>

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

<!-- Filter bar -->
<form method="GET" action="/admin/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/events" class="btn btn-secondary btn-sm"><?= __('admin.common.clear') ?></a>
    <?php endif; ?>
</form>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <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)): ?>
        <tr><td colspan="7" class="text-center text-muted"><?= __('admin.events.no_events') ?> <a href="/admin/events/create"><?= __('admin.events.create_first') ?></a></td></tr>
        <?php else: ?>
        <?php foreach ($events as $event): ?>
        <tr>
            <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/events/edit/<?= (int) $event['id'] ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <form method="POST" action="/admin/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'; ?>
