<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Event Participant Lists</title>
    <style>
        @page {
            margin: 24px;
        }

        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 11px;
            color: #1f2933;
        }

        h1 {
            margin: 0 0 6px;
            font-size: 20px;
        }

        .meta {
            margin: 0 0 18px;
            color: #52606d;
            font-size: 10px;
        }

        .event-card {
            margin-bottom: 22px;
            page-break-inside: avoid;
        }

        .event-title {
            margin: 0 0 8px;
            padding: 8px 10px;
            background: #f5f7fa;
            border: 1px solid #d9e2ec;
            font-size: 14px;
            font-weight: 700;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        th,
        td {
            border: 1px solid #d9e2ec;
            padding: 6px 7px;
            vertical-align: top;
            word-wrap: break-word;
        }

        th {
            background: #f5f7fa;
            text-align: left;
            font-weight: 700;
        }

        tfoot th {
            background: #f5f7fa;
        }

        .small {
            color: #52606d;
            font-size: 10px;
        }

        .status-paid {
            color: #166534;
            font-weight: 700;
        }

        .status-unpaid {
            color: #b42318;
            font-weight: 700;
        }

        .status-neutral {
            color: #52606d;
            font-weight: 700;
        }
    </style>
</head>
<body>
    <?php
        $title = !empty($filterEventId) ? 'Event Participant List' : 'All Event Participant Lists';
        if (!empty($filterParentId)) {
            $title .= ' for Selected Parent';
        }
    ?>
    <h1><?= esc($title) ?></h1>
    <p class="meta">
        Generated on <?= esc($generatedAt) ?>
        | School Year: <?= esc($school_year ?: 'N/A') ?>
        | Semester: <?= esc($semester ?: 'N/A') ?>
    </p>

    <?php if (empty($groupedCharges)): ?>
        <p>No charges found for the selected filters.</p>
    <?php else: ?>
        <?php foreach ($groupedCharges as $eventId => $data): ?>
            <?php
                $rows = $data['rows'] ?? [];
                $totalParticipants = 0;
                $totalCharged = 0.0;
            ?>
            <section class="event-card">
                <h2 class="event-title"><?= esc($data['label'] ?? 'N/A') ?></h2>
                <table>
                    <thead>
                    <tr>
                        <th style="width: 4%;">ID</th>
                        <th style="width: 15%;">Parent Name</th>
                        <th style="width: 14%;">Student Name</th>
                        <th style="width: 15%;">External Info</th>
                        <th style="width: 11%;">Class Section</th>
                        <th style="width: 8%;">Charged Amount</th>
                        <th style="width: 10%;">Created</th>
                        <th style="width: 7%;">Waiver</th>
                        <th style="width: 7%;">Fees Paid</th>
                        <th style="width: 9%;">Payment</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($rows as $charge): ?>
                        <?php
                            $isParticipating = ($charge['participation'] ?? '') === 'yes';
                            $isEventPaid = !empty($charge['event_paid']);
                            $feeAmount = (float) ($charge['event_amount'] ?? 0);
                            $hasBalanceRecord = array_key_exists((int) ($charge['parent_id'] ?? 0), $parentBalances);
                            $parentBalance = $hasBalanceRecord ? (float) $parentBalances[(int) $charge['parent_id']] : null;
                            $waiverSigned = !empty($charge['waiver_signed']);
                            $feeIsPaid = $isParticipating && ($isEventPaid || ($hasBalanceRecord && $parentBalance <= 0));

                            $studentName = !empty($charge['student_firstname'])
                                ? trim(($charge['student_firstname'] ?? '') . ' ' . ($charge['student_lastname'] ?? ''))
                                : '';
                            $externalName = trim(($charge['external_firstname'] ?? '') . ' ' . ($charge['external_lastname'] ?? ''));
                            $displayName = $studentName ?: ($externalName ?: '—');
                            $externalNote = trim((string) ($charge['external_note'] ?? ''));
                            $externalParentLabel = trim(($charge['external_parent_firstname'] ?? '') . ' ' . ($charge['external_parent_lastname'] ?? ''));
                            $externalParentPhone = trim((string) ($charge['external_parent_phone'] ?? ''));
                            $externalParentEmail = trim((string) ($charge['external_parent_email'] ?? ''));
                            $parentPhone = trim((string) ($charge['parent_cellphone'] ?? ''));
                            $standardParentName = trim(($charge['parent_firstname'] ?? '') . ' ' . ($charge['parent_lastname'] ?? ''));
                            $parentColumnName = $externalParentLabel ?: ($standardParentName ?: '—');
                            $isExternalParticipant = $externalName !== '';
                            $infoPhone = $isExternalParticipant ? $externalParentPhone : $parentPhone;
                            $infoEmail = $isExternalParticipant ? $externalParentEmail : '';
                            $classSectionName = $classSectionNames[$charge['class_section_id'] ?? ''] ?? '—';

                            if ($isParticipating) {
                                $totalParticipants++;
                                $totalCharged += $feeAmount;
                            }
                        ?>
                        <tr>
                            <td><?= esc($charge['id'] ?? '') ?></td>
                            <td><?= esc($parentColumnName) ?></td>
                            <td>
                                <?= esc($displayName) ?>
                                <?php if ($externalNote !== ''): ?>
                                    <div class="small"><?= esc($externalNote) ?></div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($infoPhone !== ''): ?>
                                    <div class="small"><?= esc($infoPhone) ?></div>
                                <?php endif; ?>
                                <?php if ($infoEmail !== ''): ?>
                                    <div class="small"><?= esc($infoEmail) ?></div>
                                <?php endif; ?>
                                <?php if ($infoPhone === '' && $infoEmail === ''): ?>
                                    <span class="small">—</span>
                                <?php endif; ?>
                            </td>
                            <td><?= esc($classSectionName) ?></td>
                            <td>$<?= esc(number_format($feeAmount, 2)) ?></td>
                            <td><?= esc(!empty($charge['created_at']) ? local_datetime($charge['created_at'], 'm-d-Y H:i') : '') ?></td>
                            <td class="<?= $waiverSigned ? 'status-paid' : 'status-neutral' ?>">
                                <?= $waiverSigned ? 'Signed' : 'Unsigned' ?>
                            </td>
                            <td class="<?= $feeIsPaid ? 'status-paid' : 'status-unpaid' ?>">
                                <?= $feeIsPaid ? 'Paid' : 'Unpaid' ?>
                            </td>
                            <td class="<?= $isEventPaid ? 'status-paid' : 'status-unpaid' ?>">
                                <?= $isEventPaid ? 'Paid' : 'Unpaid' ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                    <tr>
                        <th colspan="5">Totals</th>
                        <th>$<?= esc(number_format($totalCharged, 2)) ?></th>
                        <th colspan="4"><?= esc($totalParticipants) ?> participating</th>
                    </tr>
                    </tfoot>
                </table>
            </section>
        <?php endforeach; ?>
    <?php endif; ?>
</body>
</html>
