<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>

<div class="container mt-4">
        <h2 class="text-center mt-4 mb-3">Event Managment</h2>

    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success"><?= session()->getFlashdata('success') ?></div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger"><?= session()->getFlashdata('error') ?></div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('email_status')): ?>
        <div class="alert alert-info"><?= session()->getFlashdata('email_status') ?></div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('email_error')): ?>
        <div class="alert alert-warning"><?= session()->getFlashdata('email_error') ?></div>
    <?php endif; ?>

<?php if (empty($events)): ?>
        <div class="alert alert-info mb-3 d-inline-block">No events found.</div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="table table-bordered no-mgmt-sticky">
            <thead>
                <tr>
                    <th>Event Name</th>
                    <th>Category</th>
                    <th>Description</th>
                    <th>Event Fees</th>
                    <th>Expiration Date</th>
                    <th>Semester</th>
                    <th>School Year</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($events as $event): ?>
                    <tr>
                        <td><?= esc($event['event_name']) ?></td>
                        <td><?= esc($event['event_category'] ?? '—') ?></td>
                        <td><?= esc(!empty($event['description']) ? $event['description'] : '—') ?></td>
                        <td>$<?= esc(number_format($event['amount'], 2)) ?></td>
                        <td><?= esc(!empty($event['expiration_date']) ? local_date($event['expiration_date'], 'm-d-Y') : '') ?></td>
                        <td><?= esc($event['semester']) ?></td>
                        <td><?= esc($event['school_year']) ?></td>
                        <td>
                            <a href="<?= site_url('administrator/events/edit/' . $event['id']) ?>" class="btn btn-sm btn-warning">Edit</a>
                            <form action="<?= site_url('administrator/events/delete/' . $event['id']) ?>" method="post" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this event?')">Delete</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
 
    <?php endif; ?>
           <div class="d-flex gap-2 mb-3 justify-content-end">
    <a href="<?= site_url('administrator/events/create') ?>" class="btn btn-primary mb-3">Create New Event</a>
    <a href="<?= site_url('administrator/event-charges') ?>" class="btn btn-secondary mb-3">List Event Participants</a>
    </div>
</div>

<?= $this->endSection() ?>
