<?php
declare(strict_types=1);
require __DIR__ . '/_common.php';
ec_require_coordinator();

$PAGE_TITLE = 'Marking';
$ACTIVE     = 'marking';

list($scopeSql, $scopeParams) = coord_branch_scope('b');

$hasMarkersTbl = tbl($mysqli, 'lms_assessment_markers');
$joinMarkers   = $hasMarkersTbl ? "LEFT JOIN lms_assessment_markers m ON m.assessment_id = a.id" : "";
$selectMarkers = $hasMarkersTbl ? "COUNT(DISTINCT m.lecturer_id) AS markers" : "NULL AS markers";

$sql = "
  SELECT a.id, a.title, a.available_from, a.due_at, $selectMarkers
  FROM lms_assessments a
  JOIN lms_assessment_batches ab ON ab.assessment_id = a.id
  JOIN batches b ON b.id = ab.batch_id
  $joinMarkers
  WHERE $scopeSql
  GROUP BY a.id, a.title, a.available_from, a.due_at
  ORDER BY a.id DESC
";
$rows = qall($mysqli, $sql, $scopeParams, str_repeat('i', count($scopeParams)));
?>
<!doctype html><html lang="en"><head>
  <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Marking</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
</head><body>
<div class="ec-shell">
  <?php include __DIR__ . '/partials/sidebar.php'; ?>
  <div class="ec-content">
    <?php include __DIR__ . '/partials/topbar.php'; ?>
    <div class="wrap container-fluid">
      <div class="card section-card">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
          <strong>Markers & Assignments</strong>
          <a class="btn btn-outline-primary btn-sm" href="/elms/verification/assign_markers.php">
            <i class="bi bi-person-plus"></i> Assign Markers
          </a>
        </div>
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-hover mb-0">
              <thead class="table-light">
                <tr><th>Assessment</th><th>Open</th><th>Due</th><th class="text-end">Markers</th></tr>
              </thead>
              <tbody>
              <?php if (!$rows): ?>
                <tr><td colspan="4" class="text-center text-muted py-4">No data.</td></tr>
              <?php else: foreach($rows as $r): ?>
                <tr>
                  <td class="fw-semibold"><?php echo h($r['title']); ?></td>
                  <td><?php echo h($r['available_from']); ?></td>
                  <td><?php echo h($r['due_at']); ?></td>
                  <td class="text-end"><?php echo isset($r['markers']) ? (int)$r['markers'] : '—'; ?></td>
                </tr>
              <?php endforeach; endif; ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
      <?php if (!$hasMarkersTbl): ?>
        <div class="alert alert-info mt-3">
          <strong>Note:</strong> We didn’t find <code>lms_assessment_markers</code>. The page still works,
          but the “Markers” count shows “—”. Use <em>Assign Markers</em> to manage allocations.
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>
</body></html>
