<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<div class="container my-5">
    <h3 class="text-center text-success" style="font-family: Arial, sans-serif;">Events
        <span class="badge bg-info"><?= count($activeEvents) ?></span>
    </h3>

    <!-- Flash messages -->
    <?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 (empty($activeEvents)): ?>
        <div class="alert alert-info mb-3 d-inline-block">
            There are no active events at this time.
        </div>
    <?php else: ?>
        <?php foreach ($activeEvents as $event): ?>
            <?php $externalParticipants = $externalParticipantsByEvent[$event['id']] ?? []; ?>
            <div class="card mb-4">
                <div class="card-body">

                    <!-- Event Title -->
                    <h5 class="card-title">
                        <?= esc($event['event_name']) ?>
                        <small class="text-muted">(Expires: <?= esc(!empty($event['expiration_date']) ? local_date($event['expiration_date'], 'm-d-Y') : '') ?>)</small>
                    </h5>

                    <!-- Image -->
                    <div class="text-center mb-3">
                        <?php if ($event['flyer']): ?>
                            <img src="<?= base_url('uploads/' . $event['flyer']) ?>" class="img-fluid" alt="Event Flyer">
                        <?php else: ?>
                            <div class="text-muted">No Flyer</div>
                        <?php endif; ?>
                    </div>

                    <div class="mb-3">
                        <h6 class="mb-1">Description</h6>
                        <?php if (!empty($event['description'])): ?>
                            <div class="mb-0"><?= $event['description'] ?></div>
                        <?php else: ?>
                            <p class="mb-0">No description</p>
                        <?php endif; ?>
                    </div>

                    <?php if (!empty($externalParticipants)): ?>
                        <div class="mb-3">
                            <h6 class="mb-2">External Participants</h6>
                            <div class="table-responsive">
                                <table class="table table-sm table-bordered mb-0">
                                    <thead class="table-light">
                                    <tr>
                                        <th>Student Name</th>
                                        <th>Status</th>
                                        <th>Fee</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php foreach ($externalParticipants as $participant): ?>
                                        <?php
                                            $isPaid = !empty($participant['event_paid']) || ((float) ($participant['charged'] ?? 0) <= 0);
                                        ?>
                                        <tr>
                                            <td>
                                                <?= esc($participant['name']) ?> <span class="text-muted">(external)</span>
                                                <?php if (!empty($participant['note'])): ?>
                                                    <small class="text-muted d-block"><?= esc($participant['note']) ?></small>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <span class="badge bg-<?= $isPaid ? 'success' : 'danger' ?>">
                                                    <?= $isPaid ? 'Paid' : 'Unpaid' ?>
                                                </span>
                                            </td>
                                            <td class="text-nowrap">$<?= esc(number_format((float) ($participant['charged'] ?? 0), 2)) ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    <?php endif; ?>

                    <!-- Participation Table -->
                    <form method="post" action="<?= site_url('parent/updateParticipation') ?>">
                        <?= csrf_field() ?>
                        <input type="hidden" name="event_id" value="<?= $event['id'] ?>">

                        <div class="table-responsive">
                            <table class="table table-sm table-bordered">
                                <thead class="table-light">
                            <tr>
                                <th>Student Name</th>
                                <th class="text-center">Participate</th>
                            </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($yourStudents as $student): ?>
                                        <?php
                                        $key = $student['id'] . ':' . $event['id'];
                                        $current = $charges[$key]['participation'] ?? '';
                                        ?>
                                        <tr>
                                            <td><?= esc(trim(($student['firstname'] ?? '') . ' ' . ($student['lastname'] ?? ''))) ?></td>
                                            <td class="text-center align-middle">
                                                <div class="d-inline-flex align-items-center gap-3">
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="radio"
                                                            name="participation[<?= $key ?>]" value="yes"
                                                            <?= $current === 'yes' ? 'checked' : '' ?>>
                                                        <label class="form-check-label">Yes</label>
                                                    </div>
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="radio"
                                                            name="participation[<?= $key ?>]" value="no"
                                                            <?= $current === 'no' ? 'checked' : '' ?>>
                                                        <label class="form-check-label">No</label>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                            <button type="submit" class="btn btn-primary mt-2">Save</button>
                    </form>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

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