<?php

$pageTitle = "Activity Report";

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

require_login();

$user = current_user();

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

if ($activityId <= 0) {

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

    exit;

}

$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.");

}

$stats = $db->prepare("

SELECT

COUNT(*) total,

SUM(attendance_status='Present') present,

SUM(attendance_status='Absent') absent,

SUM(attendance_status='Excused') excused

FROM camp_activity_attendance

WHERE activity_id=?

");

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

$data = $stats->fetch(PDO::FETCH_ASSOC);

$total = (int)$data['total'];

$present = (int)$data['present'];

$absent = (int)$data['absent'];

$excused = (int)$data['excused'];

$attendanceRate =

$total > 0

?

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 Report

                </p>

            </div>

            <div>

                <button onclick="window.print()" class="btn btn-info">

                    Print

                </button>

                <a href="export-attendance-excel.php?activity=<?= $activityId ?>" class="btn btn-success">

                    Excel

                </a>

                <a href="export-attendance-pdf.php?activity=<?= $activityId ?>" class="btn btn-danger">

                    PDF

                </a>

            </div>

        </div>

        <div class="row">

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

                <div class="card">

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

                        <h2><?= $total ?></h2>

                        <h6>Total Assigned</h6>

                    </div>

                </div>

            </div>

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

                <div class="card">

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

                        <h2 class="text-success">

                            <?= $present ?>

                        </h2>

                        <h6>Present</h6>

                    </div>

                </div>

            </div>

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

                <div class="card">

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

                        <h2 class="text-danger">

                            <?= $absent ?>

                        </h2>

                        <h6>Absent</h6>

                    </div>

                </div>

            </div>

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

                <div class="card">

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

                        <h2 class="text-warning">

                            <?= $excused ?>

                        </h2>

                        <h6>Excused</h6>

                    </div>

                </div>

            </div>

        </div>

        <div class="card mt-24">

            <div class="card-body">

                <h4>

                    Attendance Percentage

                </h4>

                <div id="attendanceChart"></div>

            </div>

        </div>

        <script>
        var options = {

            series: [

                <?= $present ?>,

                <?= $absent ?>,

                <?= $excused ?>

            ],

            labels: [

                'Present',

                'Absent',

                'Excused'

            ],

            chart: {

                type: 'donut',

                height: 350

            },

            colors: [

                '#28a745',

                '#dc3545',

                '#ffc107'

            ]

        };

        new ApexCharts(

            document.querySelector("#attendanceChart"),

            options

        ).render();
        </script>

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