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

<div class="container mt-4">
  <?php $isEdit = ($mode ?? 'create') === 'edit'; ?>
  <?php $isLocked = $isEdit && !empty($competition['is_locked']); ?>
  <?php $lockedAttr = $isLocked ? 'disabled' : ''; ?>
  <h3><?= esc($isEdit ? 'Edit Competition' : 'Create Competition') ?></h3>

  <?php if ($isLocked): ?>
    <div class="alert alert-warning">This competition is locked. Unlock it to make changes.</div>
  <?php endif; ?>

  <?php if (!empty($errors)): ?>
    <div class="alert alert-danger">
      <ul class="mb-0">
        <?php foreach ($errors as $e): ?><li><?= esc($e) ?></li><?php endforeach; ?>
      </ul>
    </div>
  <?php endif; ?>

  <form method="post" action="<?= $isEdit ? '/admin/competition-winners/' . $competition['id'] . '/update' : '/admin/competition-winners/store' ?>" class="row g-3">
    <?= csrf_field() ?>

    <div class="col-md-8">
      <label class="form-label">Title *</label>
      <input class="form-control" name="title" value="<?= esc(old('title', $competition['title'] ?? '')) ?>" required <?= $lockedAttr ?>>
    </div>

    <div class="col-md-6">
      <label class="form-label">Class Section (optional)</label>
      <?php $oldSection = (string) old('class_section_id', (string) ($competition['class_section_id'] ?? '')); ?>
      <select class="form-select" name="class_section_id" <?= $lockedAttr ?>>
        <option value="">All classes</option>
        <?php foreach ($classSections as $section): ?>
          <?php
            $value = (string) ($section['class_section_id'] ?? '');
            $label = $section['class_section_name'] ?? $value;
            $selected = $value !== '' && $value === $oldSection ? 'selected' : '';
          ?>
          <option value="<?= esc($value) ?>" <?= $selected ?>><?= esc($label) ?></option>
        <?php endforeach; ?>
      </select>
      <div class="form-text">Leave empty to manage winners for all classes.</div>
    </div>

    <div class="col-md-3">
      <label class="form-label">Semester</label>
      <input class="form-control" name="semester" value="<?= esc(old('semester', $competition['semester'] ?? $defaultSemester ?? '')) ?>" <?= $lockedAttr ?>>
    </div>

    <div class="col-md-3">
      <label class="form-label">School Year</label>
      <input class="form-control" name="school_year" value="<?= esc(old('school_year', $competition['school_year'] ?? $defaultSchoolYear ?? '')) ?>" placeholder="2025-2026" <?= $lockedAttr ?>>
      <div class="form-text">Counts use the school year above.</div>
    </div>

    <div class="col-md-4">
      <label class="form-label">Start Date</label>
      <input class="form-control" name="start_date" type="date" value="<?= esc(old('start_date', $competition['start_date'] ?? '')) ?>" <?= $lockedAttr ?>>
    </div>

    <div class="col-md-4">
      <label class="form-label">End Date</label>
      <input class="form-control" name="end_date" type="date" value="<?= esc(old('end_date', $competition['end_date'] ?? '')) ?>" <?= $lockedAttr ?>>
    </div>

    <div class="col-12">
      <h5 class="mt-2">Winners per Class</h5>
      <div class="table-responsive">
        <table class="table table-bordered table-sm" data-no-mgmt-sticky>
          <thead class="table-light">
            <tr>
              <th>Class</th>
              <th style="width:140px;">Students</th>
              <th style="width:140px;">Auto Winners</th>
              <th style="width:150px;">Questions</th>
              <th style="width:120px;">1st</th>
              <th style="width:120px;">2nd</th>
              <th style="width:120px;">3rd</th>
              <th style="width:120px;">4th</th>
              <th style="width:120px;">5th</th>
              <th style="width:120px;">6th</th>
              <th style="width:160px;">Override</th>
              <th style="width:140px;">Final Winners</th>
            </tr>
          </thead>
          <tbody>
          <?php foreach ($classRows as $row): ?>
            <tr>
              <td><?= esc($row['class_section_name']) ?></td>
              <td><?= esc($row['student_count']) ?></td>
              <td><?= esc($row['auto_winners']) ?></td>
              <td>
                <input class="form-control form-control-sm" type="number" min="1"
                       name="question_counts[<?= esc($row['class_section_id']) ?>]"
                       value="<?= esc($row['question_count'] ?? '') ?>"
                       placeholder="Questions" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" step="0.01" min="0"
                       name="prizes[<?= esc($row['class_section_id']) ?>][1]"
                       value="<?= esc($row['prize_1'] ?? '') ?>"
                       placeholder="$" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" step="0.01" min="0"
                       name="prizes[<?= esc($row['class_section_id']) ?>][2]"
                       value="<?= esc($row['prize_2'] ?? '') ?>"
                       placeholder="$" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" step="0.01" min="0"
                       name="prizes[<?= esc($row['class_section_id']) ?>][3]"
                       value="<?= esc($row['prize_3'] ?? '') ?>"
                       placeholder="$" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" step="0.01" min="0"
                       name="prizes[<?= esc($row['class_section_id']) ?>][4]"
                       value="<?= esc($row['prize_4'] ?? '') ?>"
                       placeholder="$" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" step="0.01" min="0"
                       name="prizes[<?= esc($row['class_section_id']) ?>][5]"
                       value="<?= esc($row['prize_5'] ?? '') ?>"
                       placeholder="$" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" step="0.01" min="0"
                       name="prizes[<?= esc($row['class_section_id']) ?>][6]"
                       value="<?= esc($row['prize_6'] ?? '') ?>"
                       placeholder="$" <?= $lockedAttr ?>>
              </td>
              <td>
                <input class="form-control form-control-sm" type="number" min="1"
                       name="winner_overrides[<?= esc($row['class_section_id']) ?>]"
                       value="<?= esc($row['override_winners'] ?? '') ?>"
                       placeholder="Auto" <?= $lockedAttr ?>>
              </td>
              <td><?= esc($row['final_winners']) ?></td>
            </tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>

    <div class="col-12">
      <button class="btn btn-primary" <?= $lockedAttr ?>><?= esc($isEdit ? 'Update' : 'Create') ?></button>
      <a class="btn btn-outline-secondary" href="/admin/competition-winners">Cancel</a>
    </div>
  </form>
</div>

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