<?php
require_once __DIR__ . '/../config/database.php';

requireLogin();
requireAnyRole(['admin', 'super_admin']);

$db = Database::getInstance();

$status = isset($_GET['status']) ? trim($_GET['status']) : '';
$q = isset($_GET['q']) ? trim($_GET['q']) : '';

$where = [];
$params = [];

if ($status !== '') {
    $where[] = 'status = ?';
    $params[] = $status;
}
if ($q !== '') {
    $where[] = '(name LIKE ? OR description LIKE ?)';
    $params[] = "%$q%";
    $params[] = "%$q%";
}

$whereSql = $where ? ('WHERE ' . implode(' AND ', $where)) : '';

$categories = $db->getAll(
    "SELECT c.*, (SELECT COUNT(*) FROM courses WHERE category_id = c.id) AS course_count
     FROM categories c
     $whereSql
     ORDER BY name ASC
     LIMIT 200",
    $params
);

$pageTitle = 'Categories';
include __DIR__ . '/includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="h4 mb-0">Categories</h1>
    <p class="text-muted mb-0">Manage course categories</p>
  </div>
  <div>
    <a href="categories_edit.php" class="btn btn-primary">
      <i class="fas fa-plus me-2"></i>Add Category
    </a>
  </div>
</div>

<div class="card mb-4">
  <div class="card-header">
    <form method="get" class="row g-3">
      <div class="col-sm-6">
        <label class="form-label">Status</label>
        <select name="status" class="form-select">
          <option value="">All</option>
          <?php foreach (['active','inactive'] as $opt): ?>
            <option value="<?php echo $opt; ?>" <?php echo $status===$opt?'selected':''; ?>><?php echo ucfirst($opt); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-sm-6">
        <label class="form-label">Search</label>
        <input type="text" name="q" class="form-control" placeholder="name or description" value="<?php echo htmlspecialchars($q); ?>">
      </div>
      <div class="col-12">
        <button class="btn btn-primary" type="submit">Filter</button>
      </div>
    </form>
  </div>
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-striped table-hover mb-0">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Description</th>
            <th>Courses</th>
            <th>Status</th>
            <th>Created</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
          <?php if (!$categories): ?>
            <tr><td colspan="7" class="text-center text-muted py-4">No categories found.</td></tr>
          <?php else: foreach ($categories as $c): ?>
            <tr>
              <td><?php echo (int)$c['id']; ?></td>
              <td><?php echo htmlspecialchars($c['name'] ?? ''); ?></td>
              <td><?php echo htmlspecialchars(substr($c['description'] ?? '', 0, 100)); ?><?php echo strlen($c['description'] ?? '') > 100 ? '...' : ''; ?></td>
              <td><?php echo number_format((int)($c['course_count'] ?? 0)); ?></td>
              <td>
                <span class="badge bg-<?php echo $c['status']==='active'?'success':'secondary'; ?>">
                  <?php echo htmlspecialchars(ucfirst($c['status'] ?? '')); ?>
                </span>
              </td>
              <td><?php echo htmlspecialchars($c['created_at'] ?? ''); ?></td>
              <td class="text-nowrap">
                <a href="categories_edit.php?id=<?php echo (int)$c['id']; ?>" class="btn btn-sm btn-outline-primary me-1">
                  <i class="fas fa-edit"></i>
                </a>
                <a href="categories_view.php?id=<?php echo (int)$c['id']; ?>" class="btn btn-sm btn-outline-secondary me-1">
                  <i class="fas fa-eye"></i>
                </a>
              </td>
            </tr>
          <?php endforeach; endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
