<?= $this->extend('layout/email_layout') ?>

<?= $this->section('content') ?>
<?php
    $recipient = $recipient_parent ?? [];
    $parentName = trim(($recipient['firstname'] ?? '') . ' ' . ($recipient['lastname'] ?? ''));
    if ($parentName === '') {
        $parentName = 'Parent/Guardian';
    }
    $typeLabel = $type_label ?? 'Attendance Update';
    $reportDateFmt = $report_date_fmt ?? $report_date ?? '';
    $students = $students ?? [];
    $dateLabels = $date_labels ?? [];
    if (empty($dateLabels) && $reportDateFmt) {
        $dateLabels = [$reportDateFmt];
    }
?>
<div style="font-family:Arial, Helvetica, sans-serif; font-size:15px; color:#333; line-height:1.5;">
    <p>Dear <?= esc($parentName) ?>,</p>

    <p>
        Thank you for letting us know about your child's <?= esc(strtolower($typeLabel)) ?>.
        We have recorded your submission.
    </p>

    <?php if (!empty($students)): ?>
        <table style="width:100%; border-collapse:collapse; margin:12px 0; font-size:14px;">
            <thead>
                <tr>
                    <th style="text-align:left; border-bottom:1px solid #ddd; padding:6px;">Student</th>
                    <th style="text-align:left; border-bottom:1px solid #ddd; padding:6px;">Date</th>
                    <th style="text-align:left; border-bottom:1px solid #ddd; padding:6px;">Class</th>
                    <th style="text-align:left; border-bottom:1px solid #ddd; padding:6px;">Details</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($students as $student): ?>
                    <?php
                        $dateDisplay = $student['date_label'] ?? '';
                        if ($dateDisplay === '') {
                            $dateDisplay = !empty($student['date']) ? local_date($student['date'], 'm-d-Y') : '—';
                        }
                    ?>
                    <tr>
                        <td style="padding:6px; border-bottom:1px solid #f1f1f1;"><?= esc($student['name'] ?? 'Student') ?></td>
                        <td style="padding:6px; border-bottom:1px solid #f1f1f1;"><?= esc($dateDisplay) ?></td>
                        <td style="padding:6px; border-bottom:1px solid #f1f1f1;"><?= esc($student['class_label'] ?? 'Not Assigned') ?></td>
                        <td style="padding:6px; border-bottom:1px solid #f1f1f1;">
                            <?= esc($student['type_label'] ?? '') ?><br>
                            <?php if (!empty($student['arrival_time'])): ?>
                                Arrive ~ <?= esc($student['arrival_time']) ?><br>
                            <?php endif; ?>
                            <?php if (!empty($student['dismiss_time'])): ?>
                                Pickup @ <?= esc($student['dismiss_time']) ?><br>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <?php if (!empty($reason)): ?>
        <p><strong>Reason provided:</strong> <?= esc($reason) ?></p>
    <?php endif; ?>

    <?php if (!empty($semester) || !empty($school_year)): ?>
        <p style="margin-top:0;">
            Semester/Year: <?= esc(trim($semester . ' ' . $school_year)) ?>
        </p>
    <?php endif; ?>

    <?php if (!empty($submitted_at)): ?>
        <p style="margin-top:0;">Submitted: <?= esc($submitted_at) ?></p>
    <?php endif; ?>

    <p>
        Our admin team and your child's teacher will be notified. If you need to make a change,
        please update the form before the cutoff time or contact the school office.
    </p>

    <p style="margin-top:20px;">
        Jazakum Allahu khairan,<br>
        Al Rahma Sunday School
    </p>
</div>
<?= $this->endSection() ?>
