<?php

$pageTitle = "CDS Attendance";

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

require_login();

$user = current_user();


/*
|--------------------------------------------------------------------------
| Logged-in User ID
|--------------------------------------------------------------------------
*/

$userId = (int) ($user['id'] ?? 0);


/*
|--------------------------------------------------------------------------
| Get Corps Member Profile
|--------------------------------------------------------------------------
*/

$memberStmt = $db->prepare("
    SELECT
        cm.id,
        cm.user_id,
        cm.state_code,
        cm.firstname,
        cm.lastname,
        cm.othername,
        cm.passport,
        cm.status

    FROM corps_members cm

    WHERE cm.user_id = ?

    LIMIT 1
");

$memberStmt->execute([$userId]);

$corpsMember = $memberStmt->fetch(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| Default Variables
|--------------------------------------------------------------------------
*/

$corpsMemberId = 0;

$cds = null;

$activeSession = null;

$alreadyMarkedToday = false;

$todayAttendance = null;

$attendanceData = [];

$attendanceStats = [

    'present' => 0,

    'absent' => 0,

    'excused' => 0,

    'total' => 0

];

$attendancePercentage = 0;


/*
|--------------------------------------------------------------------------
| Continue only if corps member exists
|--------------------------------------------------------------------------
*/

if ($corpsMember) {

    $corpsMemberId = (int) $corpsMember['id'];


    /*
    |--------------------------------------------------------------------------
    | Get Corps Member's CDS Group
    |--------------------------------------------------------------------------
    */

    $cdsStmt = $db->prepare("
        SELECT
            cg.id AS cds_group_id,
            cg.group_name,
            cg.group_code,
            cg.meeting_day,
            cg.meeting_time,
            cg.meeting_venue,
            cg.description,
            cg.status AS group_status,

            cgm.assigned_date,
            cgm.is_executive,
            cgm.executive_position,
            cgm.status AS membership_status

        FROM cds_group_members cgm

        INNER JOIN cds_groups cg
            ON cg.id = cgm.cds_group_id

        WHERE cgm.corps_member_id = ?

        AND cgm.status = 'Active'

        AND cg.status = 'Active'

        LIMIT 1
    ");

    $cdsStmt->execute([$corpsMemberId]);

    $cds = $cdsStmt->fetch(PDO::FETCH_ASSOC);


    /*
    |--------------------------------------------------------------------------
    | If member has CDS group
    |--------------------------------------------------------------------------
    */

    if ($cds) {

        /*
        |--------------------------------------------------------------------------
        | Find Active Attendance Session
        |--------------------------------------------------------------------------
        */

        $activeSessionStmt = $db->prepare("
            SELECT
                id,
                cds_group_id,
                session_token,
                session_date,
                valid_from,
                valid_until,
                status

            FROM qr_attendance_sessions

            WHERE cds_group_id = ?

            AND session_date = CURDATE()

            AND status = 'Active'

            AND NOW() BETWEEN valid_from AND valid_until

            ORDER BY id DESC

            LIMIT 1
        ");

        $activeSessionStmt->execute([
            $cds['cds_group_id']
        ]);

        $activeSession =
            $activeSessionStmt->fetch(PDO::FETCH_ASSOC);


        /*
        |--------------------------------------------------------------------------
        | Check Today's Attendance
        |--------------------------------------------------------------------------
        */

        $todayStmt = $db->prepare("
            SELECT
                id,
                attendance_status,
                attendance_time,
                attendance_method

            FROM cds_attendance

            WHERE corps_member_id = ?

            AND cds_group_id = ?

            AND attendance_date = CURDATE()

            LIMIT 1
        ");

        $todayStmt->execute([

            $corpsMemberId,

            $cds['cds_group_id']

        ]);

        $todayAttendance =
            $todayStmt->fetch(PDO::FETCH_ASSOC);


        if ($todayAttendance) {

            $alreadyMarkedToday = true;

        }

    }


    /*
    |--------------------------------------------------------------------------
    | Attendance Statistics
    |--------------------------------------------------------------------------
    */

    $statsStmt = $db->prepare("
        SELECT
            attendance_status,
            COUNT(*) AS total

        FROM cds_attendance

        WHERE corps_member_id = ?

        GROUP BY attendance_status
    ");

    $statsStmt->execute([$corpsMemberId]);


    while ($row = $statsStmt->fetch(PDO::FETCH_ASSOC)) {

        switch ($row['attendance_status']) {

            case 'Present':

                $attendanceStats['present'] =
                    (int) $row['total'];

                break;


            case 'Absent':

                $attendanceStats['absent'] =
                    (int) $row['total'];

                break;


            case 'Excused':

                $attendanceStats['excused'] =
                    (int) $row['total'];

                break;

        }


        $attendanceStats['total'] +=
            (int) $row['total'];

    }


    /*
    |--------------------------------------------------------------------------
    | Attendance Percentage
    |--------------------------------------------------------------------------
    */

    if ($attendanceStats['total'] > 0) {

        $attendancePercentage = round(

            (
                $attendanceStats['present']
                /
                $attendanceStats['total']
            )

            * 100,

            1

        );

    }


    /*
    |--------------------------------------------------------------------------
    | Attendance Calendar Data
    |--------------------------------------------------------------------------
    */

    $attendanceStmt = $db->prepare("
        SELECT
            attendance_date,
            attendance_status

        FROM cds_attendance

        WHERE corps_member_id = ?

        AND YEAR(attendance_date) = YEAR(CURDATE())

        ORDER BY attendance_date ASC
    ");

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


    while (
        $row =
        $attendanceStmt->fetch(PDO::FETCH_ASSOC)
    ) {

        $month =
            (int) date(
                'n',
                strtotime($row['attendance_date'])
            );

        $day =
            (int) date(
                'j',
                strtotime($row['attendance_date'])
            );


        switch ($row['attendance_status']) {

            case 'Present':

                $symbol = 'P';

                break;


            case 'Absent':

                $symbol = 'A';

                break;


            case 'Excused':

                $symbol = 'E';

                break;


            default:

                $symbol = '';

        }


        $attendanceData[$month][$day] =
            $symbol;

    }

}


/*
|--------------------------------------------------------------------------
| Months
|--------------------------------------------------------------------------
*/

$months = [

    1 => 'Jan',

    2 => 'Feb',

    3 => 'Mar',

    4 => 'Apr',

    5 => 'May',

    6 => 'Jun',

    7 => 'Jul',

    8 => 'Aug',

    9 => 'Sep',

    10 => 'Oct',

    11 => 'Nov',

    12 => 'Dec'

];


$currentYear = date('Y');


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

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

?>

<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">
        <?php if (isset($_SESSION['success'])): ?>

        <div class="alert alert-success alert-dismissible fade show">

            <i class="ri-checkbox-circle-line me-2"></i>

            <?= htmlspecialchars($_SESSION['success']) ?>

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

        </div>

        <?php unset($_SESSION['success']); ?>

        <?php endif; ?>


        <?php if (isset($_SESSION['error'])): ?>

        <div class="alert alert-danger alert-dismissible fade show">

            <i class="ri-error-warning-line me-2"></i>

            <?= htmlspecialchars($_SESSION['error']) ?>

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

        </div>

        <?php unset($_SESSION['error']); ?>

        <?php endif; ?>

        <div class="breadcrumb d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">
            <div class="">
                <h6 class="fw-semibold mb-0"><?= $pageTitle; ?></h6>
                <p class="text-neutral-600 mt-4 mb-0"><?= APP_SHORT_NAME ?> > <?= $pageTitle; ?></p>
            </div>
        </div>
        <!-- =========================================================
            TODAY'S CDS ATTENDANCE CARD
        ========================================================== -->
        <?php if(in_array($user['role'], ['Corps Member'])) { ?>
        <div class="mt-24">

            <div class="card h-100">

                <div class="card-body p-24">

                    <div class="d-flex gap-32 flex-md-row flex-column">


                        <!-- MEMBER PROFILE -->

                        <div class="max-w-300-px w-100 text-center">


                            <figure class="mb-24 w-120-px h-120-px mx-auto rounded-circle overflow-hidden">

                                <?php

                    $passport =
                        !empty($corpsMember['passport'])
                        ? $corpsMember['passport']
                        : '../assets/images/thumbs/teacher-details-img.png';

                    ?>

                                <img src="<?= htmlspecialchars($passport) ?>" alt="Corps Member"
                                    class="w-100 h-100 object-fit-cover">

                            </figure>


                            <h2 class="h6 text-primary-light mb-16 fw-semibold">

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

                            </h2>


                            <p class="mb-0">

                                State Code:

                                <span class="text-primary-600 fw-semibold">

                                    <?= htmlspecialchars(
                            $corpsMember['state_code']
                            ?? 'Not Available'
                        ) ?>

                                </span>

                            </p>


                            <p class="mb-0">

                                Role:

                                <span class="text-primary-light fw-semibold">

                                    <?= htmlspecialchars($user['role']) ?>

                                </span>

                            </p>

                        </div>


                        <!-- DIVIDER -->

                        <div>

                            <span class="h-100 w-1-px bg-neutral-200 d-block"></span>

                        </div>


                        <!-- ATTENDANCE CONTENT -->

                        <div class="flex-grow-1">


                            <div class="pb-16 border-bottom d-flex align-items-center justify-content-between gap-20">


                                <h3 class="h6 text-primary-light text-lg mb-0 fw-semibold">

                                    Today's CDS Attendance

                                </h3>


                                <?php if ($activeSession): ?>

                                <span class="bg-success-100 text-success-600 px-16 py-4 radius-4 fw-medium text-sm">

                                    Active Session

                                </span>

                                <?php else: ?>

                                <span class="bg-neutral-200 text-neutral-600 px-16 py-4 radius-4 fw-medium text-sm">

                                    No Active Session

                                </span>

                                <?php endif; ?>


                            </div>


                            <?php if (!$corpsMember): ?>


                            <!-- NO CORPS MEMBER PROFILE -->

                            <div class="text-center py-5">

                                <i class="ri-user-unfollow-line text-danger-600" style="font-size:60px;">
                                </i>


                                <h5 class="mt-3">

                                    Corps Member Profile Not Found

                                </h5>


                                <p class="text-secondary-light">

                                    Your user account is not linked to a corps member profile.

                                </p>

                            </div>


                            <?php elseif (!$cds): ?>


                            <!-- NO CDS GROUP -->

                            <div class="text-center py-5">

                                <i class="ri-team-line text-secondary-light" style="font-size:60px;">
                                </i>


                                <h5 class="mt-3">

                                    No CDS Group Assigned

                                </h5>


                                <p class="text-secondary-light">

                                    You have not been assigned to an active CDS group.

                                </p>

                            </div>


                            <?php else: ?>


                            <!-- CDS GROUP DETAILS -->

                            <div class="mt-16 d-flex flex-column gap-8">


                                <div class="d-flex gap-4">

                                    <span class="fw-semibold text-sm text-primary-light w-110-px">

                                        Group

                                    </span>

                                    <span class="fw-normal text-sm text-secondary-light">

                                        :
                                        <?= htmlspecialchars($cds['group_name']) ?>

                                    </span>

                                </div>


                                <div class="d-flex gap-4">

                                    <span class="fw-semibold text-sm text-primary-light w-110-px">

                                        Group Code

                                    </span>

                                    <span class="fw-normal text-sm text-secondary-light">

                                        :
                                        <?= htmlspecialchars($cds['group_code']) ?>

                                    </span>

                                </div>


                                <div class="d-flex gap-4">

                                    <span class="fw-semibold text-sm text-primary-light w-110-px">

                                        Meeting Day

                                    </span>

                                    <span class="fw-normal text-sm text-secondary-light">

                                        :
                                        <?= htmlspecialchars($cds['meeting_day']) ?>

                                    </span>

                                </div>


                                <div class="d-flex gap-4">

                                    <span class="fw-semibold text-sm text-primary-light w-110-px">

                                        Meeting Time

                                    </span>

                                    <span class="fw-normal text-sm text-secondary-light">

                                        :

                                        <?php if (!empty($cds['meeting_time'])): ?>

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

                                        <?php else: ?>

                                        Not Set

                                        <?php endif; ?>

                                    </span>

                                </div>


                                <div class="d-flex gap-4">

                                    <span class="fw-semibold text-sm text-primary-light w-110-px">

                                        Venue

                                    </span>

                                    <span class="fw-normal text-sm text-secondary-light">

                                        :
                                        <?= htmlspecialchars(
                                    $cds['meeting_venue']
                                    ?? 'Not Set'
                                ) ?>

                                    </span>

                                </div>

                            </div>


                            <!-- =================================================
                    ATTENDANCE ACTION
                    ================================================= -->


                            <div class="mt-24">


                                <?php if ($alreadyMarkedToday): ?>


                                <!-- ALREADY MARKED -->

                                <div class="alert alert-success mb-0">

                                    <div class="d-flex align-items-center gap-12">

                                        <i class="ri-checkbox-circle-fill" style="font-size:28px;">
                                        </i>


                                        <div>

                                            <strong>

                                                Attendance Already Marked

                                            </strong>


                                            <div>

                                                Status:

                                                <?= htmlspecialchars(
                                                $todayAttendance['attendance_status']
                                            ) ?>

                                            </div>


                                            <?php if (
                                            !empty(
                                                $todayAttendance['attendance_time']
                                            )
                                        ): ?>

                                            <div>

                                                Time:

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

                                            </div>

                                            <?php endif; ?>

                                        </div>

                                    </div>

                                </div>


                                <?php elseif ($activeSession): ?>


                                <!-- MARK ATTENDANCE -->

                                <form method="POST" action="../actions/mark_cds_attendance.php">


                                    <input type="hidden" name="session_token" value="<?= htmlspecialchars(
                                        $activeSession['session_token']
                                    ) ?>">


                                    <button type="submit" class="btn btn-success-600 px-24 py-12">

                                        <i class="ri-checkbox-circle-line me-2"></i>

                                        Mark CDS Attendance

                                    </button>


                                </form>


                                <p class="text-secondary-light text-sm mt-12 mb-0">

                                    Attendance marking closes at

                                    <strong>

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

                                    </strong>.

                                </p>


                                <?php else: ?>


                                <!-- NO ACTIVE SESSION -->

                                <div class="alert alert-warning mb-0">

                                    <i class="ri-time-line me-2"></i>

                                    There is currently no active attendance session
                                    for your CDS group.

                                </div>


                                <?php endif; ?>


                            </div>


                            <?php endif; ?>


                        </div>

                    </div>

                </div>

            </div>

        </div>
        <?php } ?>

        <div class="mt-24">
            <div class="my-16">
                <ul class="nav nav-pills bordered-tab mb-3" id="pills-tab" role="tablist">
                    <li class="nav-item" role="presentation">
                        <button
                            class="nav-link d-flex align-items-center gap-8 text-secondary-light fw-medium text-sm text-hover-primary-600 text-capitalize bg-transparent px-20 py-12 active"
                            id="pills-attendance-tab" data-bs-toggle="pill" data-bs-target="#pills-attendance"
                            type="button" role="tab" aria-controls="pills-attendance" aria-selected="false">
                            <span class="d-flex tab-icon line-height-1 text-md">
                                <i class="ri-calendar-check-line"></i>
                            </span>
                            Attendance
                        </button>
                    </li>
                </ul>


                <div class="tab-content" id="pills-tabContent">
                    <!-- Attendance tab start -->
                    <div class="tab-pane fade show active" id="pills-attendance" role="tabpanel"
                        aria-labelledby="pills-attendance-tab" tabindex="0">
                        <div class="shadow-1 radius-12 bg-base h-100 overflow-hidden">
                            <div
                                class="card-header border-bottom bg-base py-16 px-24 d-flex align-items-center justify-content-between">
                                <h6 class="text-lg fw-semibold mb-0">Attendance</h6>
                            </div>
                            <div class="card-body p-0">
                                <div class="px-20 pt-20">
                                    <div class="row row-cols-xxl-5 row-cols-lg-3 row-cols-sm-2 row-cols-1 g-3">
                                        <div class="col">
                                            <div
                                                class="card px-20 py-28 shadow-2 radius-8 h-100 border border-neutral-200 shadow-none gradient-bg-end-7">
                                                <div class="card-body p-0">
                                                    <div
                                                        class="d-flex flex-wrap align-items-center justify-content-between gap-1">
                                                        <div>
                                                            <h6 class="fw-semibold mb-2">
                                                                <?= $attendanceStats['present']; ?>
                                                            </h6>
                                                            <span class="fw-medium text-secondary-light text-sm">Total
                                                                Present</span>
                                                        </div>
                                                        <span
                                                            class="mb-0 w-48-px h-48-px bg-success-600 text-white flex-shrink-0 text-white d-flex justify-content-center align-items-center rounded-circle h6 mb-0">
                                                            <img src="../assets/images/icons/attendence-icon1.png"
                                                                alt="Present Icon">
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <div
                                                class="card px-20 py-28 shadow-2 radius-8 h-100 border border-neutral-200 shadow-none gradient-bg-end-8">
                                                <div class="card-body p-0">
                                                    <div
                                                        class="d-flex flex-wrap align-items-center justify-content-between gap-1">
                                                        <div>
                                                            <h6 class="fw-semibold mb-2">
                                                                <?= $attendanceStats['absent']; ?></h6>
                                                            <span class="fw-medium text-secondary-light text-sm">Total
                                                                Absent</span>
                                                        </div>
                                                        <span
                                                            class="mb-0 w-48-px h-48-px bg-danger-600 text-white flex-shrink-0 text-white d-flex justify-content-center align-items-center rounded-circle h6 mb-0">
                                                            <img src="../assets/images/icons/attendence-icon2.png"
                                                                alt="Absent Icon">
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <div
                                                class="card px-20 py-28 shadow-2 radius-8 h-100 border border-neutral-200 shadow-none gradient-bg-end-9">
                                                <div class="card-body p-0">
                                                    <div
                                                        class="d-flex flex-wrap align-items-center justify-content-between gap-1">
                                                        <div>
                                                            <h6 class="fw-semibold mb-2">
                                                                <?= $attendanceStats['excused']; ?></h6>
                                                            <span class="fw-medium text-secondary-light text-sm">Total
                                                                Excused</span>
                                                        </div>
                                                        <span
                                                            class="mb-0 w-48-px h-48-px bg-purple-600 text-white flex-shrink-0 text-white d-flex justify-content-center align-items-center rounded-circle h6 mb-0">
                                                            <img src="../assets/images/icons/attendence-icon3.png"
                                                                alt="Calendar Icon">
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <div
                                                class="card px-20 py-28 shadow-2 radius-8 h-100 border border-neutral-200 shadow-none gradient-bg-end-10">
                                                <div class="card-body p-0">
                                                    <div
                                                        class="d-flex flex-wrap align-items-center justify-content-between gap-1">
                                                        <div>
                                                            <h6 class="fw-semibold mb-2"><?= $attendancePercentage; ?>%
                                                            </h6>
                                                            <span
                                                                class="fw-medium text-secondary-light text-sm">Attendance
                                                                Rate</span>
                                                        </div>
                                                        <span
                                                            class="mb-0 w-48-px h-48-px bg-info-600 text-white flex-shrink-0 text-white d-flex justify-content-center align-items-center rounded-circle h6 mb-0">
                                                            <img src="../assets/images/icons/attendence-icon4.png"
                                                                alt="Clock Icon">
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col">
                                            <div
                                                class="card px-20 py-28 shadow-2 radius-8 h-100 border border-neutral-200 shadow-none gradient-bg-end-11">
                                                <div class="card-body p-0">
                                                    <div
                                                        class="d-flex flex-wrap align-items-center justify-content-between gap-1">
                                                        <div>
                                                            <h6 class="fw-semibold mb-2">
                                                                <?= $attendanceStats['total']; ?></h6>
                                                            <span class="fw-medium text-secondary-light text-sm">Total
                                                                Meetings</span>
                                                        </div>
                                                        <span
                                                            class="mb-0 w-48-px h-48-px bg-orange text-white flex-shrink-0 text-white d-flex justify-content-center align-items-center rounded-circle h6 mb-0">
                                                            <img src="../assets/images/icons/attendence-icon5.png"
                                                                alt="Holiday Icon">
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="mt-24 mb-16 mx-20">
                                    <div
                                        class="d-flex flex-wrap align-items-center gap-24 justify-content-between flex-wrap">
                                        <div class="d-flex flex-wrap align-items-center gap-16 ">
                                            <div class="">
                                                <select class="form-control form-select">
                                                    <option value="Jun 2025/2026">Jun 2025/2026</option>
                                                    <option value="Jun 2026/2027">Jun 2026/2027</option>
                                                    <option value="Jun 2027/2028">Jun 2027/2028</option>
                                                    <option value="Jun 2028/2029">Jun 2028/2029</option>
                                                </select>
                                            </div>
                                            <div class="dropdown">
                                                <button type="button"
                                                    class="px-12 py-8 border border-neutral-300 radius-8 d-flex align-items-center gap-20"
                                                    data-bs-toggle="dropdown" aria-expanded="false">
                                                    <span
                                                        class="d-flex align-items-center gap-1 text-secondary-light text-sm">
                                                        <i class="ri-file-upload-line text-md line-height-1"></i>
                                                        Export
                                                    </span>
                                                    <span class="">
                                                        <i class="ri-arrow-down-s-line"></i>
                                                    </span>
                                                </button>
                                                <ul class="dropdown-menu p-12 border bg-base shadow">
                                                    <li>
                                                        <button type="button"
                                                            class="dropdown-item px-16 py-8 rounded text-secondary-light bg-hover-neutral-200 text-hover-neutral-900 d-flex align-items-center gap-10"
                                                            data-bs-toggle="modal" data-bs-target="#exampleModalView">
                                                            <i class="ri-file-3-line"></i>
                                                            PDF
                                                        </button>
                                                    </li>
                                                    <li>
                                                        <button type="button"
                                                            class="dropdown-item px-16 py-8 rounded text-secondary-light bg-hover-neutral-200 text-hover-neutral-900 d-flex align-items-center gap-10"
                                                            data-bs-toggle="modal" data-bs-target="#exampleModalEdit">
                                                            <i class="ri-file-excel-line"></i>
                                                            Excel
                                                        </button>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                        <div class="d-flex align-items-center flex-wrap gap-8">
                                            <p class="text-primary-light text-sm fw-medium mb-0">
                                                Present:
                                                <span class="fw-semibold text-success-600">P </span>
                                            </p>
                                            <p class="text-primary-light text-sm fw-medium mb-0">
                                                Absent:
                                                <span class="fw-semibold text-danger-600">A </span>
                                            </p>
                                            <p class="text-primary-light text-sm fw-medium mb-0">
                                                Excused
                                                <span class="fw-semibold text-warning-600">E </span>
                                            </p>
                                        </div>
                                    </div>
                                </div>

                                <div class="table-responsive overflow-x-auto">

                                    <table class="table mb-0 table-heading-dark-mode">

                                        <thead>

                                            <tr>

                                                <th class="bg-neutral-100 text-sm text-primary-light px-10 py-16">

                                                    Month

                                                </th>


                                                <?php for ($day = 1; $day <= 31; $day++): ?>

                                                <th class="bg-neutral-100 text-sm text-primary-light px-10 py-16">

                                                    <?= $day ?>

                                                </th>

                                                <?php endfor; ?>

                                            </tr>

                                        </thead>


                                        <tbody>


                                            <?php foreach ($months as $monthNo => $monthName): ?>


                                            <?php

                                            $daysInMonth =
                                                cal_days_in_month(
                                                    CAL_GREGORIAN,
                                                    $monthNo,
                                                    $currentYear
                                                );

                                            ?>


                                            <tr>


                                                <td class="px-10 py-16 text-sm fw-semibold">

                                                    <?= $monthName ?>

                                                </td>


                                                <?php for ($day = 1; $day <= 31; $day++): ?>


                                                <?php if ($day <= $daysInMonth): ?>


                                                <?php

                                                $symbol =
                                                    $attendanceData[$monthNo][$day]
                                                    ?? '';

                                                ?>


                                                <td class="px-10 py-14 text-sm text-uppercase">


                                                    <?php if ($symbol === 'P'): ?>

                                                    <span class="attendance fw-semibold text-success-600"
                                                        title="Present">

                                                        P

                                                    </span>


                                                    <?php elseif ($symbol === 'A'): ?>

                                                    <span class="attendance fw-semibold text-danger-600" title="Absent">

                                                        A

                                                    </span>


                                                    <?php elseif ($symbol === 'E'): ?>

                                                    <span class="attendance fw-semibold text-warning-600"
                                                        title="Excused">

                                                        E

                                                    </span>


                                                    <?php else: ?>

                                                    <span class="attendance">

                                                    </span>

                                                    <?php endif; ?>


                                                </td>


                                                <?php else: ?>


                                                <!-- Day does not exist in this month -->

                                                <td class="px-10 py-14 text-sm bg-neutral-50">

                                                    -

                                                </td>


                                                <?php endif; ?>


                                                <?php endfor; ?>


                                            </tr>


                                            <?php endforeach; ?>


                                        </tbody>

                                    </table>

                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Attendance tab end -->
                </div>
            </div>
        </div>
    </div>
</main>

<!-- =========================================================
     MARK CDS ATTENDANCE MODAL
========================================================== -->

<div class="modal fade" id="markAttendanceModal" tabindex="-1" aria-hidden="true">

    <div class="modal-dialog modal-dialog-centered max-w-600-px">

        <div class="modal-content radius-16 bg-base">


            <div class="modal-header">

                <h6 class="text-lg fw-semibold text-primary-light mb-0">

                    Mark CDS Attendance

                </h6>


                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">

                </button>

            </div>


            <div class="modal-body p-24">


                <?php if ($activeSession && $cds): ?>


                <div class="text-center mb-24">

                    <iconify-icon icon="mdi:qrcode-scan" width="70" class="text-primary-600">

                    </iconify-icon>


                    <h6 class="mt-16 mb-8">

                        <?= htmlspecialchars(
                                $cds['group_name']
                            ) ?>

                    </h6>


                    <p class="text-secondary-light mb-0">

                        Enter or scan the attendance code provided
                        for today's CDS meeting.

                    </p>

                </div>


                <form action="../actions/mark_cds_attendance.php" method="POST">


                    <input type="hidden" name="session_id" value="<?= (int) $activeSession['id'] ?>">


                    <div class="mb-20">

                        <label class="form-label fw-semibold text-primary-light">

                            Attendance Token

                        </label>


                        <input type="text" name="attendance_token" class="form-control radius-8"
                            placeholder="Enter attendance token" required>

                    </div>


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

                        <i class="ri-checkbox-circle-line me-2"></i>

                        Submit Attendance

                    </button>


                </form>


                <?php else: ?>


                <div class="alert alert-warning mb-0">

                    Attendance session is no longer available.

                </div>


                <?php endif; ?>


            </div>

        </div>

    </div>

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