<?php

$pageTitle = "Mark Activity Attendance";

require_once '../includes/auth.php';

require_login();

$user = current_user();

$activityId = (int)($_GET['activity'] ?? 0);

if($activityId <= 0){

    header("Location: camp-activities.php");

    exit;

}

$successMessage = "";
$errorMessage = "";

$activityStmt = $db->prepare("

SELECT *

FROM camp_activities

WHERE id=?

LIMIT 1

");

$activityStmt->execute([$activityId]);

$activity = $activityStmt->fetch(PDO::FETCH_ASSOC);

if(!$activity){

    die("Activity not found.");

}

$membersStmt = $db->prepare("

SELECT

cm.id,

cm.state_code,

cm.firstname,

cm.lastname,

cm.phone

FROM activity_members am

INNER JOIN corps_members cm

ON cm.id = am.corps_member_id

WHERE am.activity_id=?

ORDER BY cm.firstname

");

$membersStmt->execute([$activityId]);

$members = $membersStmt->fetchAll(PDO::FETCH_ASSOC);

if($_SERVER['REQUEST_METHOD']=="POST"){

    try{

        foreach($_POST['attendance'] as $memberId=>$status){

            $remarks = $_POST['remarks'][$memberId] ?? '';

            $check = $db->prepare("

                SELECT id

                FROM camp_activity_attendance

                WHERE activity_id=?

                AND corps_member_id=?

            ");

            $check->execute([

                $activityId,

                $memberId

            ]);

            if($check->fetch()){

                $update = $db->prepare("

                    UPDATE camp_activity_attendance

                    SET

                        attendance_status=?,

                        attendance_time=NOW(),

                        remarks=?

                    WHERE

                        activity_id=?

                    AND corps_member_id=?

                ");

                $update->execute([

                    $status,

                    $remarks,

                    $activityId,

                    $memberId

                ]);

            }else{

                $insert = $db->prepare("

                    INSERT INTO camp_activity_attendance(

                        activity_id,

                        corps_member_id,

                        attendance_status,

                        attendance_time,

                        remarks

                    )

                    VALUES(?,?,?,?,?)

                ");

                $insert->execute([

                    $activityId,

                    $memberId,

                    $status,

                    date('Y-m-d H:i:s'),

                    $remarks

                ]);

            }

        }

        $successMessage="Attendance saved successfully.";

    }

    catch(Throwable $e){

        $errorMessage=$e->getMessage();

    }

}

require_once '../includes/header.php';
require_once '../includes/sidebar.php';
?>

<main class="dashboard-main">

    <?php require_once '../includes/topbar.php'; ?>

    <div class="dashboard-main-body">

        <div class="breadcrumb d-flex justify-content-between align-items-center mb-24">

            <div>

                <h5>

                    <?= htmlspecialchars($activity['activity_name']) ?>

                </h5>

                <p>

                    Mark Attendance

                </p>

            </div>

            <a href="camp-activities.php" class="btn btn-light">

                Back

            </a>

        </div>

        <form method="POST">

            <div class="card">

                <div class="card-body">

                    <table class="table table-bordered">

                        <thead>

                            <tr>

                                <th>#</th>

                                <th>State Code</th>

                                <th>Name</th>

                                <th>Status</th>

                                <th>Remarks</th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php

$i=1;

foreach($members as $member):

?>

                            <tr>

                                <td>

                                    <?= $i++ ?>

                                </td>

                                <td>

                                    <?= htmlspecialchars($member['state_code']) ?>

                                </td>

                                <td>

                                    <?= htmlspecialchars(

$member['firstname']." ".$member['lastname']

) ?>

                                </td>

                                <td>

                                    <select name="attendance[<?= $member['id'] ?>]" class="form-select">

                                        <option value="Present">

                                            Present

                                        </option>

                                        <option value="Absent">

                                            Absent

                                        </option>

                                        <option value="Excused">

                                            Excused

                                        </option>

                                    </select>

                                </td>

                                <td>

                                    <input type="text" name="remarks[<?= $member['id'] ?>]" class="form-control"
                                        placeholder="Optional remarks">

                                </td>

                            </tr>

                            <?php endforeach; ?>

                        </tbody>

                    </table>

                    <button class="btn btn-primary">

                        Save Attendance

                    </button>

                </div>

            </div>

        </form>

        <?php require_once "../includes/footer.php"; ?>