<?php

$pageTitle = "Camp Activities";

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

require_login();

$user = current_user();

$successMessage = '';
$errorMessage = '';

/*
|--------------------------------------------------------------------------
| FLASH MESSAGES
|--------------------------------------------------------------------------
*/

if (!empty($_SESSION['success_message'])) {

    $successMessage = $_SESSION['success_message'];

    unset($_SESSION['success_message']);

}

if (!empty($_SESSION['error_message'])) {

    $errorMessage = $_SESSION['error_message'];

    unset($_SESSION['error_message']);

}

/*
|--------------------------------------------------------------------------
| PROCESS FORM
|--------------------------------------------------------------------------
*/

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

    $action = $_POST['action'] ?? '';

    try {

        /*
        |--------------------------------------------------------------------------
        | ADD ACTIVITY
        |--------------------------------------------------------------------------
        */

        if ($action == 'add') {

            $activityName = trim($_POST['activity_name']);
            $activityCode = trim($_POST['activity_code']);
            $activityType = trim($_POST['activity_type']);
        
            $activityDescription = trim($_POST['activity_description']);
        
            $activityDate = $_POST['activity_date'];
        
            $startTime = $_POST['start_time'];
        
            $endTime = $_POST['end_time'];
        
            $venue = trim($_POST['venue']);
        
            $supervisedBy = !empty($_POST['supervised_by']) ? (int)$_POST['supervised_by'] : null;
        
            $attendanceRequired = $_POST['attendance_required'];
        
            $status = $_POST['status'];
        
            if (
        
                empty($activityName) ||
        
                empty($activityCode) ||
        
                empty($activityDate) ||
        
                empty($startTime) ||
        
                empty($endTime)
        
            ) {
        
                throw new Exception(
                    "Please complete all required fields."
                );
        
            }
        
            /*
            |--------------------------------------------------------------------------
            | CHECK DUPLICATE CODE
            |--------------------------------------------------------------------------
            */
        
            $check = $db->prepare("
                SELECT id
                FROM camp_activities
                WHERE activity_code=?
                LIMIT 1
            ");
        
            $check->execute([$activityCode]);
        
            if ($check->fetch()) {
        
                throw new Exception(
                    "Activity Code already exists."
                );
        
            }
        
            /*
            |--------------------------------------------------------------------------
            | INSERT
            |--------------------------------------------------------------------------
            */
        
            $stmt = $db->prepare("
        
                INSERT INTO camp_activities
                (
        
                    activity_name,
        
                    activity_code,
        
                    activity_type,
        
                    activity_description,
        
                    activity_date,
        
                    start_time,
        
                    end_time,
        
                    venue,
        
                    supervised_by,
        
                    attendance_required,
        
                    status
        
                )
        
                VALUES
        
                (
        
                    ?,?,?,?,?,?,?,?,?,?,?
        
                )
        
            ");
        
            $stmt->execute([
        
                $activityName,
        
                $activityCode,
        
                $activityType,
        
                $activityDescription,
        
                $activityDate,
        
                $startTime,
        
                $endTime,
        
                $venue,
        
                $supervisedBy,
        
                $attendanceRequired,
        
                $status
        
            ]);
        
            $_SESSION['success_message'] =
                "Camp Activity added successfully.";
        
            header("Location: camp-activities.php");
        
            exit;
        
        }

        /*
        |--------------------------------------------------------------------------
        | EDIT ACTIVITY
        |--------------------------------------------------------------------------
        */

        if ($action == "edit") {

            $activityId = (int)$_POST['activity_id'];
        
            $activityName = trim($_POST['activity_name']);
        
            $activityCode = trim($_POST['activity_code']);
        
            $activityType = trim($_POST['activity_type']);
        
            $activityDescription = trim($_POST['activity_description']);
        
            $activityDate = $_POST['activity_date'];
        
            $startTime = $_POST['start_time'];
        
            $endTime = $_POST['end_time'];
        
            $venue = trim($_POST['venue']);
        
            $supervisedBy = !empty($_POST['supervised_by']) ? (int)$_POST['supervised_by'] : null;
        
            $attendanceRequired = $_POST['attendance_required'];
        
            $status = $_POST['status'];
        
            $check = $db->prepare("
        
                SELECT id
        
                FROM camp_activities
        
                WHERE activity_code=?
        
                AND id!=?
        
            ");
        
            $check->execute([
        
                $activityCode,
        
                $activityId
        
            ]);
        
            if ($check->fetch()) {
        
                throw new Exception(
        
                    "Activity Code already exists."
        
                );
        
            }
        
            $stmt = $db->prepare("
        
                UPDATE camp_activities
        
                SET
        
                    activity_name=?,
        
                    activity_code=?,
        
                    activity_type=?,
        
                    activity_description=?,
        
                    activity_date=?,
        
                    start_time=?,
        
                    end_time=?,
        
                    venue=?,
        
                    supervised_by=?,
        
                    attendance_required=?,
        
                    status=?
        
                WHERE id=?
        
            ");
        
            $stmt->execute([
        
                $activityName,
        
                $activityCode,
        
                $activityType,
        
                $activityDescription,
        
                $activityDate,
        
                $startTime,
        
                $endTime,
        
                $venue,
        
                $supervisedBy,
        
                $attendanceRequired,
        
                $status,
        
                $activityId
        
            ]);
        
            $_SESSION['success_message'] =
                "Camp Activity updated successfully.";
        
            header("Location: camp-activities.php");
        
            exit;
        
        }

        /*
        |--------------------------------------------------------------------------
        | DELETE ACTIVITY
        |--------------------------------------------------------------------------
        */

        if($action=="delete"){

            $activityId=(int)$_POST['activity_id'];

            $attendanceCheck=$db->prepare("

                SELECT COUNT(*)

                FROM camp_activity_attendance

                WHERE activity_id=?

            ");

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

            if($attendanceCheck->fetchColumn()>0){

                throw new Exception(

                    "Cannot delete activity because attendance has already been taken."

                );

            }

            $stmt=$db->prepare("

                DELETE

                FROM camp_activities

                WHERE id=?

            ");

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

            $_SESSION['success_message']="Activity deleted successfully.";

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

            exit;

        }

    }

    catch(Throwable $e){

        $_SESSION['error_message']=$e->getMessage();

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

        exit;

    }

}

/*
|--------------------------------------------------------------------------
| ASSIGN MEMBERS
|--------------------------------------------------------------------------
*/

if ($action == "assign_members") {

    $activityId = (int)$_POST['activity_id'];

    $members = $_POST['members'] ?? [];

    if (empty($members)) {

        throw new Exception(

            "Please select at least one Corps Member."

        );

    }

    $stmt = $db->prepare("

        INSERT IGNORE INTO activity_members

        (

            activity_id,

            corps_member_id

        )

        VALUES

        (?,?)

    ");

    foreach ($members as $memberId) {

        $stmt->execute([

            $activityId,

            $memberId

        ]);

    }

    $_SESSION['success_message'] =

        "Members assigned successfully.";

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

    exit;

}

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

$activitiesStmt=$db->query("

SELECT

ca.*,

COUNT(caa.id) AS attendance_count

FROM camp_activities ca

LEFT JOIN camp_activity_attendance caa

ON ca.id=caa.activity_id

GROUP BY ca.id

ORDER BY activity_date DESC

");

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

/*
|--------------------------------------------------------------------------
| FETCH CORPS MEMBERS
|--------------------------------------------------------------------------
*/

$membersStmt=$db->query("

SELECT

cm.id,

cm.firstname,

cm.lastname,

cm.state_code,

cm.phone,

p.platoon_name

FROM corps_members cm

LEFT JOIN platoons p

ON p.id=cm.platoon_id

WHERE cm.status='Registered'

ORDER BY cm.firstname

");

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

/*
|--------------------------------------------------------------------------
| FETCH CAMP OFFICIALS (ADD THIS HERE)
|--------------------------------------------------------------------------
*/

$officialStmt = $db->query("
    SELECT
        id,
        firstname,
        lastname
    FROM camp_officials
    ORDER BY firstname
");

$officials = $officialStmt->fetchAll(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| LOAD PAGE
|--------------------------------------------------------------------------
*/

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

?>

<style>
.my-sidebar,
.edit-sidebar {

    position: fixed;

    top: 0;

    right: -500px;

    width: 480px;

    height: 100vh;

    background: #fff;

    z-index: 9999;

    transition: .35s;

    overflow-y: auto;

    box-shadow: -4px 0 20px rgba(0, 0, 0, .15);

}

.my-sidebar.active,
.edit-sidebar.active {

    right: 0;

}

.overlay {

    position: fixed;

    top: 0;

    left: 0;

    width: 100%;

    height: 100%;

    background: rgba(0, 0, 0, .45);

    z-index: 9998;

    display: none;

}

.overlay.active {

    display: block;

}

.assign-sidebar {

    position: fixed;

    top: 0;

    right: -520px;

    width: 500px;

    height: 100%;

    background: #fff;

    transition: .35s;

    z-index: 9999;

    overflow-y: auto;

    box-shadow: -3px 0 20px rgba(0, 0, 0, .15);

}

.assign-sidebar.active {

    right: 0;

}
</style>


<div class="overlay bg-black bg-opacity-50 w-100 h-100 position-fixed z-9 visibility-hidden opacity-0 duration-300">
</div>

<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 ?>

                    >

                    Camp Management

                    >

                    <?= $pageTitle ?>

                </p>

            </div>

            <button class="btn btn-primary-600 my-sidebar-btn">

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

                Add Activity

            </button>

        </div>

        <?php if($successMessage): ?>

        <div class="alert alert-success">

            <?= $successMessage ?>

        </div>

        <?php endif; ?>

        <?php if($errorMessage): ?>

        <div class="alert alert-danger">

            <?= $errorMessage ?>

        </div>

        <?php endif; ?>

        <div class="card">

            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>Activity</th>

                            <th>Code</th>

                            <th>Type</th>

                            <th>Date</th>

                            <th>Time</th>

                            <th>Venue</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>

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

                            </td>

                            <td>

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

                            </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['venue']) ?>

                            </td>

                            <td>

                                <span class="badge bg-info">

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

                                </span>

                            </td>

                            <td>

                                <?php

                        if($activity['status']=="Upcoming"){

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

                        }

                        elseif($activity['status']=="Ongoing"){

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

                        }

                        elseif($activity['status']=="Completed"){

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

                        }

                        else{

                            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-id="<?= $activity['id'] ?>"
                                                data-name="<?= htmlspecialchars($activity['activity_name']) ?>"
                                                data-code="<?= htmlspecialchars($activity['activity_code']) ?>"
                                                data-type="<?= htmlspecialchars($activity['activity_type']) ?>"
                                                data-date="<?= $activity['activity_date'] ?>"
                                                data-start="<?= date('h:i A', strtotime($activity['start_time'])) ?>"
                                                data-end="<?= date('h:i A', strtotime($activity['end_time'])) ?>"
                                                data-venue="<?= htmlspecialchars($activity['venue']) ?>"
                                                data-description="<?= htmlspecialchars($activity['activity_description']) ?>"
                                                data-status="<?= $activity['status'] ?>"
                                                data-attendance="<?= $activity['attendance_count'] ?>"
                                                data-bs-toggle="modal" data-bs-target="#viewActivityModal">

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

                                                View

                                            </button>

                                        </li>

                                        <li>

                                            <button class="dropdown-item edit-sidebar-btn"
                                                data-id="<?= $activity['id'] ?>"
                                                data-name="<?= htmlspecialchars($activity['activity_name']) ?>"
                                                data-code="<?= htmlspecialchars($activity['activity_code']) ?>"
                                                data-type="<?= $activity['activity_type'] ?>"
                                                data-date="<?= $activity['activity_date'] ?>"
                                                data-start="<?= substr($activity['start_time'],0,5) ?>"
                                                data-end="<?= substr($activity['end_time'],0,5) ?>"
                                                data-venue="<?= htmlspecialchars($activity['venue']) ?>"
                                                data-description="<?= htmlspecialchars($activity['activity_description']) ?>"
                                                data-status="<?= $activity['status'] ?>"
                                                data-supervisor="<?= $activity['supervised_by'] ?>">

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

                                                Edit

                                            </button>

                                        </li>

                                        <li>

                                            <button class="dropdown-item assign-members-btn"
                                                data-id="<?= $activity['id'] ?>"
                                                data-name="<?= htmlspecialchars($activity['activity_name']) ?>">

                                                <i class="ri-user-add-line"></i>

                                                Assign Members

                                            </button>

                                        </li>

                                        <li>

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

                                                <i class="ri-check-double-line"></i>

                                                Mark Attendance

                                            </a>

                                        </li>


                                        <li>

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

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

                                                View Attendance

                                            </a>

                                        </li>

                                        <li>

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

                                                <i class="ri-bar-chart-line"></i>

                                                Attendance Report

                                            </a>

                                        </li>

                                        <li>

                                            <button class="dropdown-item delete-activity-btn"
                                                data-id="<?= $activity['id'] ?>"
                                                data-name="<?= htmlspecialchars($activity['activity_name']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#deleteActivityModal">

                                                <i class="ri-delete-bin-line"></i>

                                                Delete

                                            </button>

                                        </li>

                                    </ul>

                                </div>
                            </td>

                        </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

    <!-- ================= ADD ACTIVITY SIDEBAR ================= -->

    <div class="my-sidebar">

        <div class="p-24 border-bottom d-flex justify-content-between align-items-center">

            <h5 class="mb-0">
                Add Camp Activity
            </h5>

            <button type="button" class="close-my-sidebar btn btn-light">

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

            </button>

        </div>

        <form method="POST">

            <input type="hidden" name="action" value="add">

            <div class="p-24">

                <div class="mb-20">

                    <label class="form-label">
                        Activity Name
                        <span class="text-danger">*</span>
                    </label>

                    <input type="text" name="activity_name" class="form-control" required>

                </div>

                <div class="mb-20">

                    <label class="form-label">
                        Activity Code
                        <span class="text-danger">*</span>
                    </label>

                    <input type="text" name="activity_code" class="form-control" required>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Activity Type

                    </label>

                    <select name="activity_type" class="form-select">

                        <option value="Orientation">

                            Orientation

                        </option>

                        <option value="Parade">

                            Parade

                        </option>

                        <option value="Lecture">

                            Lecture

                        </option>

                        <option value="Sport">

                            Sport

                        </option>

                        <option value="Drill">

                            Drill

                        </option>

                        <option value="Social">

                            Social

                        </option>

                        <option value="Other">

                            Other

                        </option>

                    </select>

                </div>

                <div class="row">

                    <div class="col-md-6 mb-20">

                        <label class="form-label">

                            Activity Date

                        </label>

                        <input type="date" name="activity_date" class="form-control" required>

                    </div>

                    <div class="row">

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

                            <label class="form-label">

                                Start Time

                            </label>

                            <input type="time" name="start_time" class="form-control" required>

                        </div>

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

                            <label class="form-label">

                                End Time

                            </label>

                            <input type="time" name="end_time" class="form-control" required>

                        </div>
                    </div>

                    <div class="col-md-6 mb-20">
                        <label class="form-label">

                            Supervisor

                        </label>

                        <select name="supervised_by" class="form-select">

                            <option value="">Select Supervisor</option>

                            <?php foreach($officials as $official): ?>

                            <option value="<?= $official['id'] ?>">

                                <?= htmlspecialchars($official['firstname'].' '.$official['lastname']) ?>

                            </option>

                            <?php endforeach; ?>

                        </select>

                    </div>
                </div>

                <div class="mb-20">

                    <label>

                        Attendance Required

                    </label>

                    <select name="attendance_required" class="form-select">

                        <option value="Yes">

                            Yes

                        </option>

                        <option value="No">

                            No

                        </option>

                    </select>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Venue

                    </label>

                    <input type="text" name="venue" class="form-control">

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Description

                    </label>

                    <textarea name="activity_description" rows="4" class="form-control"></textarea>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Status

                    </label>

                    <select name="status" class="form-select">

                        <option value="Upcoming">

                            Upcoming

                        </option>

                        <option value="Ongoing">

                            Ongoing

                        </option>

                        <option value="Completed">

                            Completed

                        </option>

                        <option value="Cancelled">

                            Cancelled

                        </option>

                    </select>

                </div>

            </div>

            <div class="p-24 border-top">

                <button type="submit" class="btn btn-primary w-100">

                    Save Activity

                </button>

            </div>

        </form>

    </div>


    <!-- ================= EDIT ACTIVITY SIDEBAR ================= -->

    <div class="edit-sidebar">

        <div class="p-24 border-bottom d-flex justify-content-between align-items-center">

            <h5 class="mb-0">

                Edit Camp Activity

            </h5>

            <button type="button" class="close-edit-sidebar btn btn-light">

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

            </button>

        </div>

        <form method="POST">

            <input type="hidden" name="action" value="edit">

            <input type="hidden" id="editActivityId" name="activity_id">

            <div class="p-24">

                <div class="mb-20">

                    <label class="form-label">

                        Activity Name

                    </label>

                    <input type="text" id="editActivityName" name="activity_name" class="form-control" required>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Activity Code

                    </label>

                    <input type="text" id="editActivityCode" name="activity_code" class="form-control" required>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Activity Type

                    </label>

                    <select id="editActivityType" name="activity_type" class="form-select">

                        <option value="Orientation">Orientation</option>

                        <option value="Parade">Parade</option>

                        <option value="Lecture">Lecture</option>

                        <option value="Sport">Sport</option>

                        <option value="Drill">Drill</option>

                        <option value="Social">Social</option>

                        <option value="Other">Other</option>

                    </select>

                </div>

                <div class="mb-20">
                    <label class="form-label">
                        Supervisor
                    </label>

                    <select id="editSupervisor" name="supervised_by" class="form-select">

                        <option value="">
                            Select Supervisor
                        </option>

                        <?php foreach($officials as $official): ?>

                        <option value="<?= $official['id'] ?>">

                            <?= htmlspecialchars(
                    $official['firstname'].' '.$official['lastname']
                ) ?>

                        </option>

                        <?php endforeach; ?>

                    </select>
                </div>

                <div class="row">

                    <div class="col-md-6 mb-20">

                        <label class="form-label">

                            Activity Date

                        </label>

                        <input type="date" id="editActivityDate" name="activity_date" class="form-control">

                    </div>


                </div>

                <div class="row">

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

                        <label class="form-label">

                            Start Time

                        </label>

                        <input type="time" id="editStartTime" name="start_time" class="form-control" required>

                    </div>

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

                        <label class="form-label">

                            End Time

                        </label>

                        <input type="time" id="editEndTime" name="end_time" class="form-control" required>

                    </div>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Venue

                    </label>

                    <input type="text" id="editVenue" name="venue" class="form-control">

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Description

                    </label>

                    <textarea id="editDescription" name="activity_description" rows="4" class="form-control"></textarea>

                </div>

                <div class="mb-20">

                    <label class="form-label">

                        Status

                    </label>

                    <select id="editStatus" name="status" class="form-select">

                        <option value="Upcoming">Upcoming</option>

                        <option value="Ongoing">Ongoing</option>

                        <option value="Completed">Completed</option>

                        <option value="Cancelled">Cancelled</option>

                    </select>

                </div>

            </div>

            <div class="p-24 border-top">

                <button class="btn btn-success w-100">

                    Update Activity

                </button>

            </div>

        </form>

    </div>

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

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

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

            <div class="modal-content">

                <div class="modal-header">

                    <h5 class="modal-title">

                        Camp Activity Details

                    </h5>

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

                </div>

                <div class="modal-body">

                    <table class="table table-bordered">

                        <tr>

                            <th width="35%">

                                Activity Name

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Activity Code

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Activity Type

                            </th>

                            <td id="viewActivityType"></td>

                        </tr>

                        <tr>

                            <th>

                                Activity Date

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Activity Time

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Venue

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Description

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Status

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Total Attendance

                            </th>

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

                        </tr>

                    </table>

                </div>

            </div>

        </div>

    </div>

    <!-- ================= DELETE ACTIVITY MODAL ================= -->

    <div class="modal fade" id="deleteActivityModal">

        <div class="modal-dialog">

            <div class="modal-content">

                <form method="POST">

                    <input type="hidden" name="action" value="delete">

                    <input type="hidden" name="activity_id" id="deleteActivityId">

                    <div class="modal-header">

                        <h5 class="modal-title">

                            Delete Camp Activity

                        </h5>

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

                    </div>

                    <div class="modal-body">

                        <div class="text-center">

                            <i class="ri-delete-bin-6-line text-danger" style="font-size:70px;"></i>

                            <h5 class="mt-3">

                                Delete Activity?

                            </h5>

                            <p>

                                Are you sure you want to delete

                                <strong id="deleteActivityName"></strong>

                                ?

                            </p>

                        </div>

                    </div>

                    <div class="modal-footer">

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

                            Cancel

                        </button>

                        <button type="submit" class="btn btn-danger">

                            Delete Activity

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

    <!-- ================= ASSIGN MEMBERS SIDEBAR ================= -->

    <div class="assign-sidebar">

        <div class="p-24 border-bottom d-flex justify-content-between">

            <h5>

                Assign Members

            </h5>

            <button class="close-assign-sidebar btn btn-light">

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

            </button>

        </div>

        <form method="POST">

            <input type="hidden" name="action" value="assign_members">

            <input type="hidden" id="assignActivityId" name="activity_id">

            <div class="p-24">

                <?php foreach($members as $member): ?>

                <div class="form-check mb-3">

                    <input class="form-check-input" type="checkbox" name="members[]" value="<?= $member['id'] ?>"
                        id="member<?= $member['id'] ?>">

                    <label class="form-check-label" for="member<?= $member['id'] ?>">

                        <strong>

                            <?= $member['state_code'] ?>

                        </strong>

                        -

                        <?= htmlspecialchars($member['firstname']." ".$member['lastname']) ?>

                    </label>

                </div>

                <?php endforeach; ?>

            </div>

            <div class="p-24 border-top">

                <button class="btn btn-primary w-100">

                    Assign Selected Members

                </button>

            </div>

        </form>

    </div>

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

    <script>
    $('.edit-sidebar-btn').click(function() {

        $('#editActivityId').val($(this).data('id'));

        $('#editActivityName').val($(this).data('name'));

        $('#editActivityCode').val($(this).data('code'));

        $('#editActivityType').val($(this).data('type'));

        $('#editActivityDate').val($(this).data('date'));

        $('#editStartTime').val($(this).data('start'));
        $('#editEndTime').val($(this).data('end'));

        $('#editVenue').val($(this).data('venue'));

        $('#editDescription').val($(this).data('description'));

        $('#editStatus').val($(this).data('status'));

        $('.edit-sidebar').addClass('active');

        $('.overlay').addClass('active');

    });

    $('.close-edit-sidebar, .overlay').click(function() {

        $('.edit-sidebar').removeClass('active');

        $('.overlay').removeClass('active');

    });

    $('.view-activity-btn').click(function() {

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

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

        $('#viewActivityType').text($(this).data('type'));

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

        $('#viewActivityTime').text(
            $(this).data('start') + ' - ' + $(this).data('end')
        );

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

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

        $('#viewStatus').text($(this).data('status'));

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

    });

    $('.delete-activity-btn').click(function() {

        $('#deleteActivityId').val($(this).data('id'));

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

    });

    $('.assign-members-btn').click(function() {

        $('#assignActivityId').val($(this).data('id'));

        $('.assign-sidebar').addClass('active');

        $('.overlay').addClass('active');

    });

    $('.close-assign-sidebar,.overlay').click(function() {

        $('.assign-sidebar').removeClass('active');

        $('.overlay').removeClass('active');

    });
    </script>