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

<?php
/**
 * $emailData keys (aligned with your payment receipt view style):
 * - title, parentName, schoolYear, semester
 * - invoiceId, invoiceNumber
 * - typeLabel, chargeTitle, chargeDesc, chargeType
 * - amountSigned, amountAbs
 * - invoiceTotal, preBalance, postBalance
 * - createdAt, dueDate
 * - portalLink, invoiceLink
 * - students (optional array)
 */
?>
<div style="font-family:Arial,Helvetica,sans-serif; font-size:14px; color:#212529; line-height:1.5;">
  <h2 style="margin:0 0 10px; font-size:18px;"><?= esc($title ?? 'Account Charge Update') ?></h2>
  <p style="margin:0 0 12px;">Hello <?= esc($parentName ?? 'Parent') ?>,</p>

  <p style="margin:0 0 12px;">
    An update was made to your account for
    <strong><?= esc($schoolYear ?? '') ?> <?= !empty($semester) ? '(' . esc($semester) . ')' : '' ?></strong>.
  </p>

  <table cellpadding="0" cellspacing="0" border="0" width="100%" style="border-collapse:collapse; margin:8px 0 16px;">
    <tr>
      <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa; width:35%;">Type</td>
      <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($typeLabel ?? '') ?></td>
    </tr>
    <tr>
      <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Title</td>
      <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($chargeTitle ?? '') ?></td>
    </tr>
    <?php if (!empty($chargeDesc)): ?>
      <tr>
        <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Details</td>
        <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= nl2br(esc($chargeDesc)) ?></td>
      </tr>
    <?php endif; ?>
    <tr>
      <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Amount</td>
      <td style="padding:8px 10px; border:1px solid #e9ecef;"><strong><?= esc($amountSigned ?? '') ?></strong> (absolute: <?= esc($amountAbs ?? '') ?>)</td>
    </tr>
    <?php if (!empty($dueDate)): ?>
      <tr>
        <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Due Date</td>
        <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($dueDate) ?></td>
      </tr>
    <?php endif; ?>
    <?php if (!empty($invoiceNumber) || !empty($invoiceId)): ?>
      <tr>
        <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Invoice</td>
        <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($invoiceNumber ?? ('INV-' . (string)($invoiceId ?? ''))) ?></td>
      </tr>
    <?php endif; ?>
    <tr>
      <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Invoice Total</td>
      <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($invoiceTotal ?? '$0.00') ?></td>
    </tr>
    <tr>
      <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Previous Balance</td>
      <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($preBalance ?? '$0.00') ?></td>
    </tr>
    <tr>
      <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">New Balance</td>
      <td style="padding:8px 10px; border:1px solid #e9ecef;"><strong><?= esc($postBalance ?? '$0.00') ?></strong></td>
    </tr>
    <?php if (!empty($createdAt)): ?>
      <tr>
        <td style="padding:8px 10px; border:1px solid #e9ecef; background:#f8f9fa;">Recorded At</td>
        <td style="padding:8px 10px; border:1px solid #e9ecef;"><?= esc($createdAt) ?></td>
      </tr>
    <?php endif; ?>
  </table>

  <p style="margin:0 0 18px;">
    <a href="<?= esc($portalLink ?? 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>

  <?php if (!empty($students) && is_array($students)): ?>
    <p style="margin:16px 0 8px;"><strong>Students on your account:</strong></p>
    <ul style="margin:0; padding-left:20px;">
      <?php foreach ($students as $s): ?>
        <li><?= esc($s['name'] ?? '') ?><?= !empty($s['class']) ? ' - ' . esc($s['class']) : '' ?></li>
      <?php endforeach; ?>
    </ul>
  <?php endif; ?>

  <p style="margin:18px 0 6px;">Thank you,</p>
     <p style="margin-top:20px;">Warm regards,<br>
        <strong>Al Rahma Sunday School</strong>
    </p>
</div>

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