<?php
// participants_view.php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/auth.php';
require_login();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$id) {
    echo "Invalid participant ID.";
    exit;
}


$sql = "
SELECT p.*, 
  d.name AS district_name, b.name AS block_name, g.name AS gp_name, s.name AS school_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
WHERE p.id = ?
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
$participant = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$participant) {
    echo "Participant not found.";
    exit;
}

$page_title = "View Participant";
require_once __DIR__ . "/partials/header.php";
?>
<div class="container py-4">
  <h4>Participant Details</h4>
<table class="table table-bordered w-auto">
    <tr><th>Participant ID</th><td><?= htmlspecialchars($participant['reg_no']) ?></td></tr>
    <tr><th>Full Name</th><td><?= htmlspecialchars($participant['full_name']) ?></td></tr>
    <tr><th>Gender</th><td><?= htmlspecialchars($participant['gender']) ?></td></tr>
    <tr><th>Age</th><td><?= htmlspecialchars($participant['age']) ?></td></tr>
    <tr><th>Category</th><td><?= htmlspecialchars($participant['category']) ?></td></tr>
    <tr><th>District</th><td><?= htmlspecialchars($participant['district_name'] ?? '-') ?></td></tr>
    <tr><th>Block</th><td><?= htmlspecialchars($participant['block_name'] ?? '-') ?></td></tr>
    <tr><th>Gram Panchayet</th><td><?= htmlspecialchars($participant['gp_name'] ?? '-') ?></td></tr>
    <tr><th>School</th><td><?= htmlspecialchars($participant['school_name'] ?? '-') ?></td></tr>
    <tr><th>Guardian Name</th><td><?= htmlspecialchars($participant['guardian_name']) ?></td></tr>
    <tr><th>Guardian Phone</th><td><?= htmlspecialchars($participant['guardian_phone']) ?></td></tr>
</table>

<h5 class="mt-4">Group Members</h5>
<table class="table table-striped">
    <thead>
        <tr>
            <th>Member Name</th>
            <th>Role</th>
            <th>Contact</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $groupSql = "SELECT * FROM group_members WHERE participant_id = ?";
        $groupStmt = $pdo->prepare($groupSql);
        $groupStmt->execute([$id]);
        $members = $groupStmt->fetchAll(PDO::FETCH_ASSOC);
        
        if ($members) {
            foreach ($members as $member) {
                echo "<tr>";
                echo "<td>" . htmlspecialchars($member['name']) . "</td>";
                echo "<td>" . htmlspecialchars($member['role'] ?? '-') . "</td>";
                echo "<td>" . htmlspecialchars($member['contact'] ?? '-') . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='3' class='text-muted'>No group members found</td></tr>";
        }
        ?>
    </tbody>
</table>
  <a href="participants.php" class="btn btn-secondary">Back</a>
</div>
<?php require_once __DIR__ . "/partials/footer.php"; ?>
