<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<div class="container py-4">
  <div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
    <div>
      <h3 class="mb-0">Class Progress</h3>
      <div class="text-muted">Review the weekly reports your child’s teachers submit.</div>
    </div>
    <div class="text-end text-muted small">
      Reports are grouped by student; click a name to expand weekly details.
    </div>
  </div>

  <?php if (! $hasStudents): ?>
    <div class="alert alert-info">
      We couldn’t find any current enrollment for your account. Once your child is assigned to a Sunday class, their teacher’s progress reports will appear here.
    </div>
  <?php endif; ?>

  <?php if (! empty($students)): ?>
    <div class="accordion" id="parentProgressAccordion">
      <?php foreach ($students as $index => $student): ?>
        <?php
          $studentId = (int) ($student['student_id'] ?? 0);
          $studentName = trim(($student['firstname'] ?? '') . ' ' . ($student['lastname'] ?? ''));
          $studentName = $studentName !== '' ? $studentName : 'Student';
          $className = $student['class_section_name'] ?? '';
          $collapseId = 'student-progress-' . $studentId;
          $headingId = 'student-progress-heading-' . $studentId;
          $reportGroups = $studentReportGroups[$studentId] ?? [];
        ?>
        <div class="accordion-item">
          <h2 class="accordion-header" id="<?= esc($headingId) ?>">
            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#<?= esc($collapseId) ?>" aria-expanded="false" aria-controls="<?= esc($collapseId) ?>">
              <div class="d-flex flex-column flex-md-row align-items-md-center gap-1 gap-md-3">
                <span class="fw-semibold"><?= esc($studentName) ?></span>
                <?php if ($className !== ''): ?>
                  <span class="text-muted small">Class: <?= esc($className) ?></span>
                <?php endif; ?>
              </div>
            </button>
          </h2>
          <div id="<?= esc($collapseId) ?>" class="accordion-collapse collapse" aria-labelledby="<?= esc($headingId) ?>" data-bs-parent="#parentProgressAccordion">
            <div class="accordion-body">
              <?php if (empty($reportGroups)): ?>
                <div class="alert alert-secondary mb-0">No reports submitted yet.</div>
              <?php else: ?>
                <div class="table-responsive">
                  <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                      <tr>
                        <th>Week</th>
                        <th>Subjects</th>
                        <th class="text-end">Details</th>
                      </tr>
                    </thead>
                    <tbody>
                      <?php foreach ($reportGroups as $group): ?>
                        <?php
                          $start = $group['week_start'] ?? '';
                          $end = $group['week_end'] ?? '';
                          $weekLabel = $start ? date('M d, Y', strtotime($start)) : '-';
                          if ($end) {
                            $weekLabel .= ' – ' . date('M d, Y', strtotime($end));
                          }
                          $reports = $group['reports'] ?? [];
                          $exampleReport = $reports ? reset($reports) : null;
                        ?>
                        <tr>
                          <td>
                            <div class="fw-semibold"><?= esc($weekLabel) ?></div>
                          </td>
                          <td>
                            <div class="d-flex flex-column gap-2">
                              <?php foreach ($subjectSections as $slug => $section): ?>
                                <?php
                                  $subjectName = $section['db_subject'] ?? $section['label'] ?? $slug;
                                  $report = $reports[$subjectName] ?? null;
                                  $statusLabel = $report ? ($report['status_label'] ?? 'Unknown') : 'No submission';
                                  $badgeClass = $report ? 'bg-secondary' : 'bg-light text-muted';
                                ?>
                                <div class="border rounded-3 p-2">
                                  <div class="d-flex justify-content-between align-items-center">
                                    <strong class="small mb-0"><?= esc($section['label'] ?? $subjectName) ?></strong>
                                    <span class="badge <?= esc($badgeClass) ?>"><?= esc($statusLabel) ?></span>
                                  </div>
                                  <div class="small text-muted">
                                    <?= $report ? esc($report['unit_title'] ?: '-') : 'No submission' ?>
                                  </div>
                                </div>
                              <?php endforeach; ?>
                            </div>
                          </td>
                          <td class="text-end">
                            <?php if ($exampleReport): ?>
                              <a href="<?= base_url('parent/progress/view/' . $exampleReport['id']) ?>" class="btn btn-sm btn-outline-primary">View Weekly Details</a>
                            <?php endif; ?>
                          </td>
                        </tr>
                      <?php endforeach; ?>
                    </tbody>
                  </table>
                </div>
              <?php endif; ?>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>
<?= $this->endSection() ?>
