<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<?php $isSchoolYearReadonly = (bool) ($isSchoolYearReadonly ?? false); ?>
  <div class="container py-4">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <div>
      <h3 class="mb-0">Class Progress Reports</h3>
      <div class="text-muted">Review weekly submissions and compare Islamic Studies with Quran/Arabic.</div>
    </div>
    <?php if ($isSchoolYearReadonly): ?>
      <span class="btn btn-outline-secondary disabled" aria-disabled="true">Submit New Report</span>
    <?php else: ?>
      <a href="<?= base_url('teacher/progress/submit') ?>" class="btn btn-outline-secondary">Submit New Report</a>
    <?php endif; ?>
  </div>

  <?php
    $reportGroups = $reportGroups ?? [];
    $subjectSections = $subjectSections ?? [];
    $classSectionOptions = $classSectionOptions ?? [];
    $selectedSectionId = $selectedSectionId ?? null;
  ?>
  <?php if ($isSchoolYearReadonly): ?>
    <div class="alert alert-warning">This school year is read-only. Progress submissions can be viewed but not changed.</div>
  <?php endif; ?>
  <?php if (!empty($classSectionOptions)): ?>
    <form method="get" class="mb-3 row g-2 align-items-center">
      <div class="col-auto">
        <label class="form-label mb-0 small">Class / Section</label>
      </div>
      <div class="col-auto">
        <select name="class_section_id" class="form-select form-select-sm" onchange="this.form.submit()">
          <?php foreach ($classSectionOptions as $id => $label): ?>
            <option value="<?= esc($id) ?>" <?= $selectedSectionId == $id ? 'selected' : '' ?>><?= esc($label ?: 'Unknown Section') ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    </form>
  <?php endif; ?>
  <?php if (empty($reportGroups)): ?>
    <div class="alert alert-secondary">No reports submitted yet.</div>
  <?php else: ?>
    <div class="card shadow-sm">
      <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>
                  <?php if (!empty($group['class_section_name'])): ?>
                    <div class="text-muted small">Class: <?= esc($group['class_section_name']) ?></div>
                  <?php endif; ?>
                </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;
                        $statusBadge = $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 <?= $badgeClass ?>"><?= esc($statusBadge) ?></span>
                        </div>
                        <div class="small text-muted">
                          <?= $report ? esc($report['unit_title'] ?: '-') : 'No submission' ?>
                        </div>
                        <?php if ($report): ?>
                          <div class="small text-muted">
                            Submitted by: <?= esc(trim((string) ($report['teacher_name'] ?? '')) ?: 'Unknown') ?>
                          </div>
                        <?php endif; ?>
                      </div>
                    <?php endforeach; ?>
                  </div>
                </td>
                <td class="text-end">
                  <?php if ($exampleReport): ?>
                    <a href="<?= base_url('teacher/progress/view/' . $exampleReport['id']) ?>" class="btn btn-sm btn-outline-primary">View Weekly Details</a>
                    <?php if (!$isSchoolYearReadonly): ?>
                      <a href="<?= base_url('teacher/progress/edit/' . $exampleReport['id']) ?>" class="btn btn-sm btn-outline-secondary ms-1">Edit</a>
                    <?php endif; ?>
                  <?php endif; ?>
                </td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  <?php endif; ?>
</div>
<?= $this->endSection() ?>
