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

<?= $this->section('content') ?>
<?php
    $submitter = $recipient_parent ?? [];
    $primary = $primary_parent ?? [];
    $students = $students ?? [];
    $typeLabel = $type_label ?? 'Attendance Update';
    $reportDateFmt = $report_date_fmt ?? $report_date ?? '';
    $dateLabels = $date_labels ?? [];
    if (empty($dateLabels) && $reportDateFmt) {
        $dateLabels = [$reportDateFmt];
    }

    $submitterName = trim(($submitter['firstname'] ?? '') . ' ' . ($submitter['lastname'] ?? '')) ?: 'Parent/Guardian';
    $primaryName = trim(($primary['firstname'] ?? '') . ' ' . ($primary['lastname'] ?? ''));
?>
<div style="font-family:Arial, Helvetica, sans-serif; font-size:15px; color:#333; line-height:1.5;">
    <p><strong>Parent Submission Received</strong></p>

    <p>
        <?= esc($submitterName) ?> submitted an <?= esc(strtolower($typeLabel)) ?> request:        
    </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;">Notes</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:</strong> <?= esc($reason) ?></p>
    <?php endif; ?>

    <p style="margin-top:16px;"><strong>Contact Info</strong></p>
    <ul style="padding-left:20px; margin-top:0;">
        <li>Submitter Email: <?= esc($submitter['email'] ?? 'N/A') ?></li>
        <?php if (!empty($submitter['cellphone'])): ?>
            <li>Submitter Phone: <?= esc($submitter['cellphone']) ?></li>
        <?php endif; ?>
        <?php if ($primaryName && $primaryName !== $submitterName): ?>
            <li>Primary Parent: <?= esc($primaryName) ?> (<?= esc($primary['email'] ?? 'N/A') ?>)</li>
        <?php endif; ?>
    </ul>

    <?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 style="margin-top:20px; font-size:14px; color:#555;">
        Logged automatically by the Parent Attendance Report form.
    </p>
</div>
<?= $this->endSection() ?>
