<?php

$pageTitle = "Camp Activities Report";

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

require_login();

$user = current_user();

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

$totalActivities = $db->query("

SELECT COUNT(*)

FROM camp_activities

")->fetchColumn();

$attendanceRequired = $db->query("

SELECT COUNT(*)

FROM camp_activities

WHERE attendance_required='Yes'

")->fetchColumn();

$totalAttendance = $db->query("

SELECT COUNT(*)

FROM camp_activity_attendance

")->fetchColumn();

/*
|--------------------------------------------------------------------------
| FETCH ACTIVITIES
|--------------------------------------------------------------------------
*/

$stmt = $db->query("

SELECT

ca.*,

CONCAT(

co.firstname,

' ',

co.lastname

) supervisor,

COUNT(caa.id) attendance_count

FROM camp_activities ca

LEFT JOIN camp_officials co

ON co.id=ca.supervised_by

LEFT JOIN camp_activity_attendance caa

ON caa.activity_id=ca.id

GROUP BY ca.id

ORDER BY activity_date DESC

");

$activities=$stmt->fetchAll(PDO::FETCH_ASSOC);

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 flex-wrap align-items-center justify-content-between gap-3 mb-24">

            <div>

                <h6 class="fw-semibold mb-0">

                    <?= $pageTitle ?>

                </h6>

                <p class="text-neutral-500">

                    <?= APP_SHORT_NAME ?>

                    >

                    Reports

                    >

                    Camp Activities Report

                </p>

            </div>

            <div>

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

                    <i class="ri-file-excel-line"></i>

                    Export CSV

                </a>

                <a href="print-camp-activities.php" target="_blank" class="btn btn-primary">

                    <i class="ri-printer-line"></i>

                    Print Report

                </a>

            </div>

        </div>

        <div class="row gy-4 mb-24">

            <div class="col-lg-4">

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Activities

                        </h6>

                        <h2>

                            <?= $totalActivities ?>

                        </h2>

                    </div>

                </div>

            </div>

            <div class="col-lg-4">

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Attendance Required

                        </h6>

                        <h2 class="text-primary">

                            <?= $attendanceRequired ?>

                        </h2>

                    </div>

                </div>

            </div>

            <div class="col-lg-4">

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Attendance Recorded

                        </h6>

                        <h2 class="text-success">

                            <?= $totalAttendance ?>

                        </h2>

                    </div>

                </div>

            </div>

        </div>

        <div class="card">

            <div class="card-header">

                <h6>

                    Camp Activities Report

                </h6>

            </div>

            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>Activity</th>

                            <th>Date</th>

                            <th>Time</th>

                            <th>Supervisor</th>

                            <th>Attendance</th>

                            <th>Status</th>

                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

$count=1;

foreach($activities as $activity):

?>

                        <tr>

                            <td>

                                <?= $count++ ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= date('d M Y',strtotime($activity['activity_date'])) ?>

                            </td>

                            <td>

                                <?= date('h:i A',strtotime($activity['start_time'])) ?>

                                -

                                <?= date('h:i A',strtotime($activity['end_time'])) ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= $activity['attendance_count'] ?>

                            </td>

                            <td>

                                <?php

switch($activity['status']){

case "Upcoming":

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

break;

case "Ongoing":

echo '<span class="badge bg-primary">Ongoing</span>';

break;

case "Completed":

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

break;

default:

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

}

?>

                            </td>

                            <td>

                                <div class="dropdown">

                                    <button class="btn btn-light" data-bs-toggle="dropdown">

                                        <i class="ri-more-2-fill"></i>

                                    </button>

                                    <ul class="dropdown-menu">

                                        <li>

                                            <button class="dropdown-item view-activity-btn"
                                                data-name="<?= htmlspecialchars($activity['activity_name']) ?>"
                                                data-code="<?= htmlspecialchars($activity['activity_code']) ?>"
                                                data-date="<?= $activity['activity_date'] ?>"
                                                data-start="<?= $activity['start_time'] ?>"
                                                data-end="<?= $activity['end_time'] ?>"
                                                data-supervisor="<?= htmlspecialchars($activity['supervisor']) ?>"
                                                data-venue="<?= htmlspecialchars($activity['venue']) ?>"
                                                data-description="<?= htmlspecialchars($activity['activity_description']) ?>"
                                                data-attendance="<?= $activity['attendance_count'] ?>"
                                                data-status="<?= $activity['status'] ?>" data-bs-toggle="modal"
                                                data-bs-target="#viewActivityModal">

                                                <i class="ri-eye-line"></i>

                                                View

                                            </button>

                                        </li>

                                        <li>

                                            <a href="view-activity-attendance.php?activity=<?= $activity['id'] ?>"
                                                class="dropdown-item">

                                                <i class="ri-team-line"></i>

                                                Attendance

                                            </a>

                                        </li>

                                        <li>

                                            <a href="print-camp-activity.php?id=<?= $activity['id'] ?>" target="_blank"
                                                class="dropdown-item">

                                                <i class="ri-printer-line"></i>

                                                Print

                                            </a>

                                        </li>

                                    </ul>

                                </div>

                            </td>

                        </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

        <!-- ================= VIEW ACTIVITY ================= -->

        <div class="modal fade" id="viewActivityModal" tabindex="-1">

            <div class="modal-dialog modal-lg">

                <div class="modal-content">

                    <div class="modal-header">

                        <h5>

                            Camp Activity Details

                        </h5>

                        <button class="btn-close" data-bs-dismiss="modal">

                        </button>

                    </div>

                    <div class="modal-body">

                        <table class="table table-bordered">

                            <tr>

                                <th width="35%">

                                    Activity

                                </th>

                                <td id="viewActivityName"></td>

                            </tr>

                            <tr>

                                <th>

                                    Code

                                </th>

                                <td id="viewActivityCode"></td>

                            </tr>

                            <tr>

                                <th>

                                    Date

                                </th>

                                <td id="viewActivityDate"></td>

                            </tr>

                            <tr>

                                <th>

                                    Time

                                </th>

                                <td id="viewActivityTime"></td>

                            </tr>

                            <tr>

                                <th>

                                    Supervisor

                                </th>

                                <td id="viewSupervisor"></td>

                            </tr>

                            <tr>

                                <th>

                                    Venue

                                </th>

                                <td id="viewVenue"></td>

                            </tr>

                            <tr>

                                <th>

                                    Description

                                </th>

                                <td id="viewDescription"></td>

                            </tr>

                            <tr>

                                <th>

                                    Attendance

                                </th>

                                <td id="viewAttendance"></td>

                            </tr>

                            <tr>

                                <th>

                                    Status

                                </th>

                                <td id="viewStatus"></td>

                            </tr>

                        </table>

                    </div>

                </div>

            </div>

        </div>

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

        <script>
        $(document).on(

            'click',

            '.view-activity-btn',

            function() {

                $('#viewActivityName').text($(this).data('name'));

                $('#viewActivityCode').text($(this).data('code'));

                $('#viewActivityDate').text($(this).data('date'));

                $('#viewActivityTime').text(

                    $(this).data('start') +

                    ' - ' +

                    $(this).data('end')

                );

                $('#viewSupervisor').text($(this).data('supervisor'));

                $('#viewVenue').text($(this).data('venue'));

                $('#viewDescription').text($(this).data('description'));

                $('#viewAttendance').text($(this).data('attendance'));

                let status = $(this).data('status');

                if (status == "Upcoming") {

                    $('#viewStatus').html('<span class="badge bg-warning">Upcoming</span>');

                } else if (status == "Ongoing") {

                    $('#viewStatus').html('<span class="badge bg-primary">Ongoing</span>');

                } else if (status == "Completed") {

                    $('#viewStatus').html('<span class="badge bg-success">Completed</span>');

                } else {

                    $('#viewStatus').html('<span class="badge bg-danger">Cancelled</span>');

                }

            });
        </script>