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

<div class="container mt-4">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <div>
      <h3 class="mb-0">Competition Winners</h3>
      <?php if (!empty($schoolYear)): ?>
        <div class="text-muted small">School Year: <?= esc($schoolYear) ?></div>
      <?php endif; ?>
    </div>
    <a class="btn btn-primary" href="/admin/competition-winners/create">+ New Competition</a>
  </div>

  <?php if (session('success')): ?>
    <div class="alert alert-success"><?= esc(session('success')) ?></div>
  <?php endif; ?>
  <?php if (session('error')): ?>
    <div class="alert alert-danger"><?= esc(session('error')) ?></div>
  <?php endif; ?>

  <table class="table table-bordered table-sm" data-no-mgmt-sticky>
    <thead class="table-light">
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>School Year</th>
        <th>Class Section</th>
        <th>Winners Rule</th>
        <th>Published</th>
        <th>Locked</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
    <?php foreach ($competitions as $c): ?>
      <?php
        $sectionId = (int) ($c['class_section_id'] ?? 0);
        $sectionName = $sectionId > 0 ? ($sectionMap[$sectionId] ?? $sectionId) : 'All';
        $isLocked = !empty($c['is_locked']);
      ?>
      <tr>
        <td><?= esc($c['id']) ?></td>
        <td><?= esc($c['title']) ?></td>
        <td><?= esc($c['school_year'] ?? '') ?></td>
        <td><?= esc($sectionName) ?></td>
        <td>Tiered per class</td>
        <td><?= $c['is_published'] ? 'Yes' : 'No' ?></td>
        <td><?= $isLocked ? 'Yes' : 'No' ?></td>
        <td>
          <a class="btn btn-sm btn-outline-secondary" href="/admin/competition-winners/<?= $c['id'] ?>/settings">Settings</a>
          <a class="btn btn-sm btn-outline-primary" href="/admin/competition-winners/<?= $c['id'] ?>">Enter Scores</a>
          <a class="btn btn-sm btn-outline-secondary" href="/admin/competition-winners/<?= $c['id'] ?>/preview">Preview</a>
          <a class="btn btn-sm btn-outline-secondary" href="/admin/competition-winners/<?= $c['id'] ?>/winners">Winners</a>
          <form method="post" action="/admin/competition-winners/<?= $c['id'] ?>/export-quiz" class="d-inline">
            <?= csrf_field() ?>
            <button class="btn btn-sm btn-outline-success" type="submit">Export Scores to Quiz</button>
          </form>
          <?php if ($isLocked): ?>
            <form method="post" action="/admin/competition-winners/<?= $c['id'] ?>/unlock" class="d-inline">
              <?= csrf_field() ?>
              <button class="btn btn-sm btn-outline-warning" type="submit" onclick="return confirm('Unlock this competition to allow edits?')">Unlock</button>
            </form>
          <?php else: ?>
            <form method="post" action="/admin/competition-winners/<?= $c['id'] ?>/lock" class="d-inline">
              <?= csrf_field() ?>
              <button class="btn btn-sm btn-outline-danger" type="submit" onclick="return confirm('Lock this competition to prevent edits?')">Lock</button>
            </form>
          <?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
</div>

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