<?php
require_once __DIR__ . '/../../admin/_nav.php';
$pageTitle  = 'Assessments';
$sidebarNav = adminNav();
$u = function($p) { return \Core\View::url($p); };
?>
<div class="page-header">
  <div><h1>Assessments</h1></div>
  <button onclick="openModal('createExam')" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Create Assessment</button>
</div>
<div class="card">
  <div class="table-wrap">
    <table>
      <thead><tr><th>Name</th><th>Type</th><th>Term</th><th>Weight</th><th>Status</th><th>Actions</th></tr></thead>
      <tbody>
        <?php foreach($exams['data'] as $e): ?>
        <tr>
          <td><strong><?= htmlspecialchars($e['name']) ?></strong></td>
          <td><span class="badge badge-info"><?= strtoupper($e['type']) ?></span></td>
          <td><?= htmlspecialchars($e['year_name'].' - '.$e['term_name']) ?></td>
          <td><?= $e['weight'] ?>%</td>
          <td><span class="badge <?= $e['is_published']?'badge-success':'badge-warning' ?>"><?= $e['is_published']?'Published':'Draft' ?></span></td>
          <td style="display:flex;gap:6px">
            <button onclick="openEditModal(<?= htmlspecialchars(json_encode($e), ENT_QUOTES) ?>)" class="btn btn-sm btn-secondary" title="Edit"><i class="bi bi-pencil"></i></button>
            <a href="<?= $u('admin/exams/'.$e['id'].'/marks') ?>" class="btn btn-sm btn-secondary"><i class="bi bi-pencil-square"></i> Marks</a>
            <a href="<?= $u('admin/exams/'.$e['id'].'/results') ?>" class="btn btn-sm btn-primary"><i class="bi bi-bar-chart"></i> Results</a>
            <?php if(!$e['is_published']): ?>
            <form method="POST" action="<?= $u('admin/exams/'.$e['id'].'/publish') ?>" style="display:inline">
              <?= \Core\View::csrf() ?>
              <button type="submit" class="btn btn-sm btn-outline" onclick="return confirm('Publish results?')"><i class="bi bi-send"></i> Publish</button>
            </form>
            <?php else: ?>
            <span class="badge badge-success" style="align-self:center"><i class="bi bi-check-circle"></i> Published</span>
            <?php endif; ?>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if(empty($exams['data'])): ?><tr><td colspan="6" style="text-align:center;padding:40px;color:var(--gray)">No exams yet</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
  <div style="padding:14px"><?= \Core\View::paginate($exams, $u('admin/exams')) ?></div>
</div>

<!-- Edit Assessment Modal -->
<div class="modal" id="editExam">
  <div class="modal-box">
    <div class="card-header"><h5>Edit Assessment</h5><button onclick="closeModal('editExam')" style="background:none;border:none;cursor:pointer;font-size:20px">&times;</button></div>
    <div class="card-body">
      <form method="POST" id="editExamForm" action="">
        <?= \Core\View::csrf() ?>
        <div class="form-group"><label>Assessment Name *</label><input type="text" name="name" id="editExamName" required></div>
        <div class="grid g2">
          <div class="form-group">
            <label>Type *</label>
            <select name="type" id="editExamType" required>
              <option value="formative">Formative Assessment</option>
              <option value="summative">Summative Assessment</option>
              <option value="project">Project</option>
              <option value="portfolio">Portfolio</option>
              <option value="endterm">End of Term</option>
            </select>
          </div>
          <div class="form-group"><label>Weight (%)</label><input type="number" name="weight" id="editExamWeight" min="1" max="100"></div>
          <div class="form-group">
            <label>Term *</label>
            <select name="term_id" id="editExamTerm" required>
              <?php foreach($terms as $t): ?>
              <option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['year_name'].' - '.$t['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group"><label>Start Date</label><input type="date" name="start_date" id="editExamStart"></div>
          <div class="form-group"><label>End Date</label><input type="date" name="end_date" id="editExamEnd"></div>
        </div>
        <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Save Changes</button>
      </form>
    </div>
  </div>
</div>

<?php
$examBaseUrl = $u('admin/exams/');
$extraScripts = '<script>
function openEditModal(e) {
  document.getElementById("editExamForm").action = "'.$examBaseUrl.'" + e.id + "/update";
  document.getElementById("editExamName").value   = e.name;
  document.getElementById("editExamType").value   = e.type;
  document.getElementById("editExamWeight").value = e.weight;
  document.getElementById("editExamTerm").value   = e.term_id;
  document.getElementById("editExamStart").value  = e.start_date || "";
  document.getElementById("editExamEnd").value    = e.end_date || "";
  openModal("editExam");
}
</script>';
?>
<div class="modal" id="createExam">
  <div class="modal-box">
    <div class="card-header"><h5>Create Assessment</h5><button onclick="closeModal('createExam')" style="background:none;border:none;cursor:pointer;font-size:20px">&times;</button></div>
    <div class="card-body">
      <form method="POST" action="<?= $u('admin/exams/store') ?>">
        <?= \Core\View::csrf() ?>
        <div class="form-group"><label>Assessment Name *</label><input type="text" name="name" placeholder="e.g. Term 1 Formative Assessment" required></div>
        <div class="grid g2">
          <div class="form-group">
            <label>Type *</label>
            <select name="type" required>
              <option value="formative">Formative Assessment</option>
              <option value="summative" selected>Summative Assessment</option>
              <option value="project">Project</option>
              <option value="portfolio">Portfolio</option>
              <option value="endterm">End of Term</option>
            </select>
          </div>
          <div class="form-group"><label>Weight (%)</label><input type="number" name="weight" value="100" min="1" max="100"></div>
          <div class="form-group">
            <label>Term *</label>
            <select name="term_id" required>
              <option value="">Select Term</option>
              <?php foreach($terms as $t): ?>
              <option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['year_name'].' - '.$t['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group"><label>Start Date</label><input type="date" name="start_date"></div>
        </div>
        <button type="submit" class="btn btn-primary">Create Assessment</button>
      </form>
    </div>
  </div>
</div>
