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

<div class="container mt-4">
  <div class="d-flex justify-content-between align-items-center mb-2">
    <h3 class="mb-0">School Winners</h3>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="/admin/competition-winners/<?= $competition['id'] ?>/preview">Preview</a>
      <a class="btn btn-outline-secondary" href="/admin/competition-winners">Back</a>
    </div>
  </div>

  <div class="mb-3">
    <div><strong>Competition:</strong> <?= esc($competition['title']) ?></div>
    <div><strong>School Year:</strong> <?= esc($competition['school_year'] ?? '-') ?></div>
    <div><strong>Published:</strong> <?= !empty($competition['is_published']) ? 'Yes' : 'No' ?></div>
  </div>

  <?php
    $palette = [
      ['bg' => '#fde2c2', 'hover' => '#f8c996'],
      ['bg' => '#cfe6ff', 'hover' => '#b5d6ff'],
      ['bg' => '#c9f2e7', 'hover' => '#a6e6d4'],
      ['bg' => '#f7c8c7', 'hover' => '#f1a6a3'],
      ['bg' => '#fff1a8', 'hover' => '#ffe38a'],
      ['bg' => '#e4c7f2', 'hover' => '#d1a9e8'],
      ['bg' => '#d6d9de', 'hover' => '#c2c8cf'],
      ['bg' => '#c9efc5', 'hover' => '#aee3a9'],
    ];
    $paletteCount = count($palette);
  ?>

  <style>
    <?php foreach ($palette as $index => $colors): ?>
      .class-color-<?= $index ?> td { background-color: <?= $colors['bg'] ?>; }
      .class-color-<?= $index ?>:hover td { background-color: <?= $colors['hover'] ?>; }
    <?php endforeach; ?>
  </style>

  <?php if (empty($rows)): ?>
    <div class="alert alert-warning">No published winners yet. Publish winners to see the list.</div>
  <?php else: ?>
    <table class="table table-bordered table-sm" data-no-mgmt-sticky>
      <thead class="table-light">
        <tr>
          <th>Class</th>
          <th>School ID</th>
          <th>Student</th>
          <th>Photo Consent</th>
          <th>Rank</th>
          <th>Score</th>
          <th>Prize</th>
        </tr>
      </thead>
      <tbody>
      <?php $totalPrize = 0.0; ?>
      <?php foreach ($rows as $row): ?>
        <?php
          $classId = (int) ($row['class_section_id'] ?? 0);
          $classLabel = $row['class_section_name'] ?? ($classMap[$classId] ?? ('Class ' . $classId));
          $name = trim(($row['firstname'] ?? '') . ' ' . ($row['lastname'] ?? ''));
          $studentLabel = $name !== '' ? $name : ('Student #' . ($row['student_id'] ?? ''));
          $colorIndex = $paletteCount > 0 ? ($classId % $paletteCount) : 0;
          $questionCount = $questionCounts[$classId] ?? null;
          $scoreValue = $row['score'] ?? null;
          $scoreLabel = '-';
          $prizeValue = null;
          $photoConsentLabel = '-';
          if (array_key_exists('photo_consent', $row)) {
            $photoConsentLabel = ((int) $row['photo_consent'] === 1) ? 'Yes' : 'No';
          }
          if (isset($row['prize_amount']) && $row['prize_amount'] !== '' && is_numeric($row['prize_amount'])) {
            $prizeValue = (float) $row['prize_amount'];
            $totalPrize += $prizeValue;
          }
          if ($scoreValue !== null && $scoreValue !== '' && is_numeric($scoreValue)) {
            $scoreDisplay = number_format((float) $scoreValue, 0, '.', '');
            $scoreLabel = $scoreDisplay;
            if ($questionCount !== null) {
              $scoreLabel = $scoreDisplay . '/' . $questionCount;
            }
          }
        ?>
        <tr class="class-color-<?= esc((string) $colorIndex) ?>">
          <td><?= esc($classLabel) ?></td>
          <td><?= esc($row['school_id'] ?? $row['student_id'] ?? '-') ?></td>
          <td><?= esc($studentLabel) ?></td>
          <td><?= esc($photoConsentLabel) ?></td>
          <td><?= esc($row['rank'] ?? '-') ?></td>
          <td><?= esc($scoreLabel) ?></td>
          <td><?= esc($prizeValue !== null ? $prizeValue : '-') ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
      <tfoot>
        <tr>
          <th colspan="6" class="text-end">Total Prizes</th>
          <th><?= esc(number_format($totalPrize, 2, '.', '')) ?></th>
        </tr>
      </tfoot>
    </table>
    <div class="d-flex justify-content-end mt-3">
      <a class="btn btn-primary external-link" href="/admin/competition-winners/<?= $competition['id'] ?>/recognition-pdf" target="_blank" rel="noopener">
        Generate Recognition PDF
      </a>
    </div>
  <?php endif; ?>
</div>

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