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

<?= $this->section('email_content') ?>
<h2>Withdrawal Complete</h2>

<p>Dear <?= esc($parentName ?? 'Parent') ?>,</p>

<?php if (!empty($withdrawals) && is_array($withdrawals)): ?>
    <p>This email confirms the following students have been officially withdrawn from our school:</p>
    <table style="width:100%; border-collapse:collapse; 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;">Effective Date</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($withdrawals as $w): ?>
                <tr>
                    <td style="padding:6px; border-bottom:1px solid #f1f1f1;"><?= esc($w['name'] ?? '') ?></td>
                    <td style="padding:6px; border-bottom:1px solid #f1f1f1;"><?= esc($w['withdrawalDate'] ?? '') ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php else: ?>
    <?php
    // $studentName can be string or array; $withdrawalDate is a single date (optional)
    $isMulti = isset($studentName) && is_array($studentName) && !empty($studentName);
    ?>
    <?php if ($isMulti): ?>
        <p>This email confirms the following <?= count($studentName) ?> students have been officially withdrawn from our school<?= !empty($withdrawalDate) ? ' effective ' . esc($withdrawalDate) : '' ?>:</p>
        <ul style="padding-left:20px;">
            <?php foreach ($studentName as $name): ?>
                <li><?= esc($name) ?></li>
            <?php endforeach; ?>
        </ul>
    <?php else: ?>
        <p>
            This email confirms that <?= esc($studentName ?? 'your student') ?>
            has been officially withdrawn from our school<?= !empty($withdrawalDate) ? ' effective ' . esc($withdrawalDate) : '' ?>.
        </p>
    <?php endif; ?>
<?php endif; ?>

<p>The refund process has been completed. You should see the credit in your account within 3–5 business days.</p>

<?php
// Build a readable label for the farewell line if multiple names
if (isset($studentName) && is_array($studentName) && !empty($studentName)) {
    $names = array_map(static fn($n) => trim($n), $studentName);
    if (count($names) === 1) {
        $farewell = $names[0];
    } elseif (count($names) === 2) {
        $farewell = $names[0] . ' and ' . $names[1];
    } else {
        $last = array_pop($names);
        $farewell = implode(', ', $names) . ', and ' . $last;
    }
} else {
    $farewell = $studentName ?? 'your student';
}
?>

<p>We're sorry to see you go and wish <?= esc($farewell) ?> all the best in their future educational endeavors.</p>
<p>If you would like to re-enroll in the future, please don't hesitate to contact us.</p>

<p style="margin:16px 0;">
    <a href="<?= base_url() ?>" target="_blank" rel="noopener"
        style="display:inline-block; padding:10px 16px; text-decoration:none; border-radius:6px;
            background:#198754; color:#ffffff; font-weight:600; font-size:16px;">
        Access My Account
    </a>
</p>

<p>Best regards,<br>Registrar's Office</p>
<?= $this->endSection() ?>