<?php
declare(strict_types=1);
require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../config/auth.php";
require_login();

$q = trim((string)($_GET['q'] ?? ''));

$sql = "
SELECT p.*, 
       d.name AS district_name, b.name AS block_name, g.name AS gp_name, s.name AS school_name,
       u.name AS created_by_name
FROM participants p
LEFT JOIN districts d ON d.id=p.district_id
LEFT JOIN blocks b ON b.id=p.block_id
LEFT JOIN gps g ON g.id=p.gp_id
LEFT JOIN schools s ON s.id=p.school_id
LEFT JOIN users u ON u.id=p.created_by
WHERE 1=1
";

$params = [];
if ($q !== '') {
  $sql .= " AND (p.full_name LIKE ? OR p.guardian_phone LIKE ? OR p.school_id LIKE ?)";
  $params[] = "%{$q}%";
  $params[] = "%{$q}%";
  $params[] = "%{$q}%";
}

$sql .= " ORDER BY p.created_at DESC LIMIT 1000";
$st = $pdo->prepare($sql);
$st->execute($params);
$rows = $st->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Participants";
require_once __DIR__ . "/partials/header.php";
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="mb-0">Participants</h5>
    <div class="text-muted small">Latest 1000 records</div>
  </div>
  <a class="btn btn-dark btn-sm" href="/school-crm/public/participant_add.php">+ Add Participant</a>
</div>

<div class="card card-soft shadow-soft p-3 mb-3">
  <form class="row g-2" method="get">
    <div class="col-12 col-md-10">
      <input class="form-control" name="q" value="<?= htmlspecialchars($q) ?>" placeholder="Search name / phone / school id">
    </div>
    <div class="col-12 col-md-2 d-grid">
      <button class="btn btn-outline-dark">Search</button>
    </div>
  </form>
</div>

<div class="card card-soft shadow-soft p-3">
  <div class="table-responsive">
    <table class="table table-hover align-middle mb-0">
      <thead>
        <tr>
          <th>Date</th>
          <th>Participant</th>
          <th>Guardian</th>
          <th>Category</th>
          <th>School</th>
          <th>Created By</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php if(!$rows): ?>
          <tr><td colspan="6" class="text-muted">No data.</td></tr>
        <?php endif; ?>

        <?php foreach($rows as $r): ?>
          <tr>
            <td class="text-nowrap"><?= htmlspecialchars(substr((string)$r['created_at'],0,10)) ?></td>
            <td>
              <div class="fw-semibold"><?= htmlspecialchars($r['full_name']) ?></div>
              <div class="text-muted small">Age: <?= (int)$r['age'] ?> • <?= htmlspecialchars($r['gender']) ?></div>
            </td>
            <td>
              <div><?= htmlspecialchars($r['guardian_name']) ?></div>
              <div class="text-muted small"><?= htmlspecialchars($r['guardian_phone']) ?></div>
            </td>
            <td class="text-nowrap">
              <span class="badge bg-dark"><?= htmlspecialchars($r['category']) ?></span>
              <?php if($r['category']==='GROUP'): ?>
                <div class="text-muted small"><?= htmlspecialchars($r['group_name'] ?? '-') ?> (<?= (int)($r['group_size'] ?? 0) ?>)</div>
              <?php endif; ?>
            </td>
            <td style="min-width:220px;">
              <div class="fw-semibold"><?= htmlspecialchars($r['school_name'] ?? '-') ?></div>
              <div class="text-muted small"><?= htmlspecialchars($r['district_name'] ?? '-') ?> / <?= htmlspecialchars($r['block_name'] ?? '-') ?> / <?= htmlspecialchars($r['gp_name'] ?? '-') ?></div>
            </td>
            <td><?= htmlspecialchars($r['created_by_name'] ?? '-') ?></td>
            <td class="text-nowrap">
              <a href="participants_view.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-info">View</a>
              <a href="participants_update.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-warning">Update</a>
              <a href="participants_delete.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this participant?');">Delete</a>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

<?php require_once __DIR__ . "/partials/footer.php"; ?>
