<?php
/** @var array<int,array> $payments */
/** @var int $parentId */
/** @var string $parentName */
?>
<div data-payments-title="<?= esc(trim(($parentName !== '' ? ($parentName . ' ') : '') . '(#' . (int)$parentId . ')')) ?>">
  <div class="p-3 border-bottom">
    <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
      <div>
        <strong>Payments</strong>
        <small class="text-muted ms-2">Parent <?= esc($parentName) ?> (ID #<?= (int)$parentId ?>)</small>
      </div>
    </div>
  </div>

  <div class="p-3">
    <div class="table-responsive">
      <table class="table table-sm align-middle mb-0">
        <thead class="table-light">
          <tr>
            <th>Date</th>
            <th>Invoice</th>
            <th class="text-end">Paid</th>
            <th class="text-end">Invoice Balance</th>
            <th>Method</th>
            <th>Payment Status</th>
            <th>Invoice Status</th>
            <th>School Year</th>
          </tr>
        </thead>
        <tbody>
          <?php if (empty($payments)): ?>
            <tr>
              <td colspan="8" class="text-center text-muted py-4">No payment</td>
            </tr>
          <?php else: ?>
            <?php foreach ($payments as $p): ?>
              <tr>
                <td><?= esc(!empty($p['payment_date']) ? local_date($p['payment_date'], 'm-d-Y') : '') ?></td>
                <td>
                  <?php $inv = trim((string)($p['invoice_number'] ?? '')); ?>
                  <?= $inv !== '' ? esc($inv) : ('#' . (int)($p['invoice_id'] ?? 0)) ?>
                </td>
                <td class="text-end">$<?= number_format((float)($p['paid_amount'] ?? 0), 2) ?></td>
                <td class="text-end">$<?= number_format((float)($p['invoice_current_balance'] ?? 0), 2) ?></td>
                <td><?= esc($p['payment_method'] ?? '') ?></td>
                <td><?= esc($p['payment_status'] ?? '') ?></td>
                <td><?= esc($p['invoice_status'] ?? '') ?></td>
                <td><?= esc($p['school_year'] ?? '') ?></td>
              </tr>
            <?php endforeach; ?>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
