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

<div class="container-xxl py-4">
  <div class="d-flex align-items-center justify-content-between flex-wrap gap-2 mb-3">
    <h2 class="mb-0">Parent Attendance Reports</h2>
    <small class="text-muted">Submitted by parents (absent/late/early dismissal)</small>
  </div>

  <form method="get" class="row gy-2 gx-3 align-items-end mb-4">
    <div class="col-md-3">
      <label class="form-label">School Year</label>
      <input type="text" class="form-control" name="school_year" value="<?= esc($schoolYear ?? ($_GET['school_year'] ?? '')) ?>" placeholder="e.g. 2025-2026">
    </div>
    <div class="col-md-2">
      <label class="form-label">Semester</label>
      <?php $semVal = (string)($semester ?? ($_GET['semester'] ?? '')); ?>
      <select name="semester" class="form-select">
        <option value="">—</option>
        <option value="Fall"   <?= (strcasecmp($semVal,'Fall')===0?'selected':'') ?>>Fall</option>
        <option value="Spring" <?= (strcasecmp($semVal,'Spring')===0?'selected':'') ?>>Spring</option>
      </select>
    </div>
    <div class="col-md-2">
      <label class="form-label">Start Date</label>
      <input type="date" class="form-control" name="start" value="<?= esc($start ?? local_date(utc_now(), 'Y-m-d')) ?>">
    </div>
    <div class="col-md-2">
      <label class="form-label">End Date</label>
      <input type="date" class="form-control" name="end" value="<?= esc($end ?? '') ?>">
    </div>
    <div class="col-md-3 d-flex gap-2">
      <button type="submit" class="btn btn-secondary mt-4">Apply</button>
      <a class="btn btn-outline-secondary mt-4" href="<?= site_url('attendance/parent-reports') ?>">Reset</a>
    </div>
  </form>

  <div class="card shadow-sm">
    <div class="card-body table-responsive">
      <table class="table table-striped align-middle">
        <thead>
          <tr>
            <th>Date</th>
            <th>Student</th>
            <th>Class/Section</th>
            <th>Type</th>
            <th>Time</th>
            <th>Reason</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <?php if (!empty($rows)): ?>
            <?php foreach ($rows as $r): ?>
              <tr>
                <td><?= esc(!empty($r['report_date']) ? local_date($r['report_date'], 'm-d-Y') : '') ?></td>
                <td><?= esc(($r['firstname'] ?? '') . ' ' . ($r['lastname'] ?? '')) ?></td>
                <td><?= esc($r['class_section_name'] ?? '—') ?></td>
                <td class="text-capitalize"><?= esc(str_replace('_',' ', $r['type'])) ?></td>
                <td>
                  <?php if ($r['type'] === 'late' && !empty($r['arrival_time'])): ?>
                    Arrival: <?= esc(substr($r['arrival_time'], 0, 5)) ?>
                  <?php elseif ($r['type'] === 'early_dismissal' && !empty($r['dismiss_time'])): ?>
                    Dismiss: <?= esc(substr($r['dismiss_time'], 0, 5)) ?>
                  <?php else: ?>
                    —
                  <?php endif; ?>
                </td>
                <td style="max-width: 360px; white-space: normal;"><?= esc($r['reason'] ?? '') ?></td>
                <td><span class="badge bg-<?= ($r['status'] === 'processed' ? 'success' : ($r['status'] === 'seen' ? 'info' : 'secondary')) ?>
                "><?= esc(ucfirst($r['status'] ?? 'new')) ?></span></td>
              </tr>
            <?php endforeach; ?>
          <?php else: ?>
            <tr>
              <td colspan="7" class="text-center text-muted">No reports found for the selected range.</td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

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