<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once __DIR__ . '/../../includes/auth.php';
require_role(['super_admin','branch_admin']);

require_once __DIR__ . '/../../includes/helpers.php';
require_once __DIR__ . '/../../includes/db.php';
require_once __DIR__ . '/../../includes/scope.php';

$PAGE_TITLE = 'Courses';
$ACTIVE_MENU = 'courses';

// detect label/status cols
$labelCol = column_exists_local('courses','course_name') ? 'course_name' :
           (column_exists_local('courses','name') ? 'name' :
            (column_exists_local('courses','title') ? 'title' : 'id'));
$statusCol = column_exists_local('courses','status') ? 'status' : (column_exists_local('courses','active') ? 'active' : null);

$q = trim($_GET['q'] ?? '');
$sql = "SELECT id, $labelCol AS label".($statusCol? ", $statusCol AS st": "")." FROM courses";
$w = []; $types=''; $vals=[];
if ($q !== '') { $w[] = "$labelCol LIKE ?"; $types.='s'; $vals[] = "%$q%"; }
if ($w) $sql .= " WHERE ".implode(' AND ',$w);
$sql .= " ORDER BY label";

$stmt = $mysqli->prepare($sql);
if ($types) $stmt->bind_param($types, ...$vals);
$stmt->execute();
$courses = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

// open layout
$__layoutOpened=false;
foreach(['admin_layout_top.php','layout_top.php','admin_header.php','header_admin.php'] as $f){
  $p=__DIR__.'/../../includes/'.$f; if(is_file($p)){require_once $p; $__layoutOpened=true; break;}
}
if(!$__layoutOpened) echo '<div class="container-fluid p-3">';
?>
<div class="d-flex align-items-center justify-content-between mb-3">
  <h1 class="h5 m-0">Courses</h1>
  <form class="d-flex gap-2" method="get">
    <input type="text" class="form-control" name="q" placeholder="Search courses..." value="<?= esc($q) ?>" />
    <button class="btn btn-primary">Search</button>
    <a class="btn btn-outline-secondary" href="index.php">Reset</a>
  </form>
</div>

<div class="card">
  <div class="card-body table-responsive">
    <table class="table align-middle">
      <thead><tr>
        <th style="width:60%">Course</th>
        <th>Status</th>
        <th class="text-end">Actions</th>
      </tr></thead>
      <tbody>
      <?php if (!$courses): ?>
        <tr><td colspan="3" class="text-center text-muted">No courses found.</td></tr>
      <?php else: foreach ($courses as $c): ?>
        <tr>
          <td class="fw-semibold"><?= esc($c['label']) ?></td>
          <td>
            <?php if ($statusCol): ?>
              <?php
                $st = $c['st'];
                if ($statusCol==='active') $st = ((int)$st===1?'Active':'Inactive');
              ?>
              <span class="badge <?= ($st==='Active')?'bg-success':'bg-secondary' ?>"><?= esc($st) ?></span>
            <?php else: ?>—<?php endif; ?>
          </td>
          <td class="text-end">
            <a class="btn btn-sm btn-outline-primary" href="modules.php?course_id=<?= (int)$c['id'] ?>">Semesters & Modules</a>
            <a class="btn btn-sm btn-outline-success" href="content.php?course_id=<?= (int)$c['id'] ?>">Content</a>
            <a class="btn btn-sm btn-outline-dark" href="releases.php?course_id=<?= (int)$c['id'] ?>">Releases</a>
          </td>
        </tr>
      <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php
// close layout
$__layoutClosed=false;
foreach(['admin_layout_bottom.php','layout_bottom.php','admin_footer.php','footer_admin.php'] as $f){
  $p=__DIR__.'/../../includes/'.$f; if(is_file($p)){require_once $p; $__layoutClosed=true; break;}
}
if(!$__layoutClosed) echo '</div>';
