<?php

$pageTitle = "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;
}

/*
|--------------------------------------------------------------------------
| FETCH ACTIVITY
|--------------------------------------------------------------------------
*/

$stmt = $db->prepare("

SELECT *

FROM camp_activities

WHERE id=?

LIMIT 1

");

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

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

if (!$activity) {

    die("Activity not found.");

}

/*
|--------------------------------------------------------------------------
| FETCH ATTENDANCE
|--------------------------------------------------------------------------
*/

$attendanceStmt = $db->prepare("

SELECT

caa.id,

cm.state_code,

cm.firstname,

cm.lastname,

cm.phone,

caa.attendance_status,

caa.attendance_time,

caa.remarks

FROM camp_activity_attendance caa

INNER JOIN corps_members cm

ON cm.id = caa.corps_member_id

WHERE caa.activity_id=?

ORDER BY cm.firstname

");

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

$attendance = $attendanceStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| SUMMARY
|--------------------------------------------------------------------------
*/

$summaryStmt = $db->prepare("

SELECT

SUM(attendance_status='Present') Present,

SUM(attendance_status='Absent') Absent,

SUM(attendance_status='Excused') Excused,

COUNT(*) Total

FROM camp_activity_attendance

WHERE activity_id=?

");

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

$summary = $summaryStmt->fetch(PDO::FETCH_ASSOC);

$present = (int)$summary['Present'];

$absent = (int)$summary['Absent'];

$excused = (int)$summary['Excused'];

$total = (int)$summary['Total'];

$rate = $total
    ? round(($present/$total)*100,2)
    : 0;

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 mb-24">

            <div>

                <h5>

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

                </h5>

                <p>

                    Attendance Records

                </p>

            </div>

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

                Back

            </a>

        </div>

        <div class="row">

            <div class="col-md-3">

                <div class="card">

                    <div class="card-body text-center">

                        <h3>

                            <?= $present ?>

                        </h3>

                        <p>

                            Present

                        </p>

                    </div>

                </div>

            </div>

            <div class="col-md-3">

                <div class="card">

                    <div class="card-body text-center">

                        <h3>

                            <?= $absent ?>

                        </h3>

                        <p>

                            Absent

                        </p>

                    </div>

                </div>

            </div>

            <div class="col-md-3">

                <div class="card">

                    <div class="card-body text-center">

                        <h3>

                            <?= $excused ?>

                        </h3>

                        <p>

                            Excused

                        </p>

                    </div>

                </div>

            </div>

            <div class="col-md-3">

                <div class="card">

                    <div class="card-body text-center">

                        <h3>

                            <?= $rate ?>%

                        </h3>

                        <p>

                            Attendance Rate

                        </p>

                    </div>

                </div>

            </div>

        </div>

        <div class="card mt-24">

            <div class="card-body">

                <table class="table bordered-table data-table" id="dataTable">

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>State Code</th>

                            <th>Name</th>

                            <th>Phone</th>

                            <th>Status</th>

                            <th>Time</th>

                            <th>Remarks</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

$count=1;

foreach($attendance as $row):

?>

                        <tr>

                            <td>

                                <?= $count++ ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= htmlspecialchars(

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

) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars($row['phone']) ?>

                            </td>

                            <td>

                                <?php

switch($row['attendance_status']){

case "Present":

echo '<span class="badge bg-success">Present</span>';

break;

case "Absent":

echo '<span class="badge bg-danger">Absent</span>';

break;

default:

echo '<span class="badge bg-warning">Excused</span>';

}

?>

                            </td>

                            <td>

                                <?=

$row['attendance_time']

?

date(

"d M Y h:i A",

strtotime($row['attendance_time'])

)

:

"-"

?>

                            </td>

                            <td>

                                <?= htmlspecialchars($row['remarks']) ?>

                            </td>

                        </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

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