<?php
/**
 * Event attendance - mark members as attended
 */

$stmt = $pdo->prepare('SELECT * FROM events WHERE id = ?');
$stmt->execute([$id]);
$event = $stmt->fetch();
if (!$event) {
    header('Location: ' . BASE_PATH . '/events');
    exit;
}

// Toggle attendance via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['member_id'])) {
    $member_id = (int)$_POST['member_id'];
    $attended = !empty($_POST['attended']) ? 1 : 0;

    $pdo->prepare('
        INSERT INTO event_attendance (event_id, member_id, attended) VALUES (?, ?, ?)
        ON DUPLICATE KEY UPDATE attended = VALUES(attended)
    ')->execute([$id, $member_id, $attended]);

    header('Location: ' . BASE_PATH . '/events/attendance/' . $id . '?toast=' . urlencode('Attendance updated.'));
    exit;
}

// Get all members with their attendance status for this event
$stmt = $pdo->prepare("
    SELECT m.id, m.first_name, m.last_name, ea.attended
    FROM members m
    LEFT JOIN event_attendance ea ON ea.member_id = m.id AND ea.event_id = ?
    ORDER BY m.last_name, m.first_name
");
$stmt->execute([$id]);
$members = $stmt->fetchAll();

$pageTitle = 'Attendance: ' . $event['title'];
require_once __DIR__ . '/../../includes/header.php';
?>

<div class="header">
    <h1>Attendance: <?= htmlspecialchars($event['title']) ?></h1>
    <div class="header-actions">
        <a href="<?= BASE_PATH ?>/events">Back to Events</a>
        <a href="<?= BASE_PATH ?>/logout">Logout</a>
    </div>
</div>

<p>
    <strong>Date:</strong> <?= date('l, F j, Y', strtotime($event['event_date'])) ?>
    <?php if ($event['event_time']): ?>
        at <?= date('g:i A', strtotime($event['event_time'])) ?>
    <?php endif; ?>
    <?php if ($event['location']): ?>
        | <strong>Location:</strong> <?= htmlspecialchars($event['location']) ?>
    <?php endif; ?>
</p>

<div class="card">
    <h2>Mark Attendance</h2>
    <p>Check the box next to each member who attended.</p>
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Member</th>
                    <th>Attended</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($members as $m): ?>
                <tr>
                    <td><?= htmlspecialchars($m['first_name'] . ' ' . $m['last_name']) ?></td>
                    <td>
                        <form method="post" style="display: inline;">
                            <input type="hidden" name="member_id" value="<?= $m['id'] ?>">
                            <input type="checkbox" name="attended" value="1" 
                                <?= !empty($m['attended']) ? 'checked' : '' ?>
                                onchange="this.form.submit()">
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php
$attendedCount = count(array_filter($members, function ($m) { return !empty($m['attended']); }));
?>
<p><strong><?= $attendedCount ?></strong> of <?= count($members) ?> members marked as attended.</p>

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