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

$PAGE_TITLE = 'Lecturers';
$ACTIVE     = 'lecturers';

/* Try lecturers table first */
if (tbl($mysqli, 'lecturers')) {
  $rows = qall($mysqli, "SELECT id, name, email, phone FROM lecturers ORDER BY name ASC");
} else {
  // fallback to users table with role like 'lecturer'
  $hasUsers = tbl($mysqli, 'users');
  $rows = $hasUsers ? qall($mysqli, "
    SELECT id, COALESCE(name, username) AS name, email, NULL AS phone
    FROM users
    WHERE role IN ('lecturer','teacher','instructor')
    ORDER BY COALESCE(name, username)
  ") : [];
}
?>
<!doctype html><html lang="en"><head>
  <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Lecturers</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"><strong>Lecturer Directory</strong></div>
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-hover mb-0">
              <thead class="table-light">
                <tr><th>Name</th><th>Email</th><th>Phone</th></tr>
              </thead>
              <tbody>
              <?php if (!$rows): ?>
                <tr><td colspan="3" class="text-center text-muted py-4">No lecturers found.</td></tr>
              <?php else: foreach($rows as $r): ?>
                <tr>
                  <td class="fw-semibold"><?php echo h($r['name'] ?? ''); ?></td>
                  <td><?php echo h($r['email'] ?? ''); ?></td>
                  <td><?php echo h($r['phone'] ?? ''); ?></td>
                </tr>
              <?php endforeach; endif; ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
      <?php if (!tbl($mysqli,'lecturers') && !tbl($mysqli,'users')): ?>
        <div class="alert alert-warning mt-3">No <code>lecturers</code> or <code>users</code> table found.</div>
      <?php endif; ?>
    </div>
  </div>
</div>
</body></html>
