<?php

$pageTitle = "CDS Groups Report";

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

require_login();

$user = current_user();

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

$totalGroups = $db->query("

SELECT COUNT(*)

FROM cds_groups

")->fetchColumn();

$totalMembers = $db->query("

SELECT COUNT(*)

FROM cds_group_members

WHERE status='Active'

")->fetchColumn();

/*
|--------------------------------------------------------------------------
| FETCH CDS GROUPS
|--------------------------------------------------------------------------
*/

$stmt = $db->query("

SELECT

cg.*,

COUNT(cgm.id) AS total_members

FROM cds_groups cg

LEFT JOIN cds_group_members cgm

ON cgm.cds_group_id = cg.id

AND cgm.status='Active'

GROUP BY cg.id

ORDER BY cg.group_name

");

$groups = $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

                    >

                    CDS Groups Report

                </p>

            </div>

            <div>

                <a href="export-cds-groups.php" class="btn btn-success">

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

                    Export CSV

                </a>

                <a href="print-cds-groups.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-6">

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Total CDS Groups

                        </h6>

                        <h2 class="fw-bold">

                            <?= $totalGroups ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Assigned Members

                        </h6>

                        <h2 class="fw-bold text-success">

                            <?= $totalMembers ?>

                        </h2>

                    </div>

                </div>

            </div>

        </div>

        <div class="card">

            <div class="card-header">

                <h6>

                    CDS Groups Report

                </h6>

            </div>

            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>Group Name</th>

                            <th>Code</th>

                            <th>Meeting Day</th>

                            <th>Attendance</th>

                            <th>Members</th>

                            <th>Status</th>

                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

$count=1;

foreach($groups as $group):

?>

                        <tr>

                            <td>

                                <?= $count++ ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

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

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= htmlspecialchars($group['attendance_type']) ?>

                            </td>

                            <td>

                                <?= $group['total_members'] ?>

                            </td>

                            <td>

                                <?php

if($group['status']=="Active"){

?>

                                <span class="badge bg-success">

                                    Active

                                </span>

                                <?php

}else{

?>

                                <span class="badge bg-danger">

                                    Inactive

                                </span>

                                <?php

}

?>

                            </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-group-btn"
                                                data-name="<?= htmlspecialchars($group['group_name']) ?>"
                                                data-code="<?= htmlspecialchars($group['group_code']) ?>"
                                                data-day="<?= htmlspecialchars($group['meeting_day']) ?>"
                                                data-time="<?= htmlspecialchars($group['meeting_time']) ?>"
                                                data-venue="<?= htmlspecialchars($group['meeting_venue']) ?>"
                                                data-attendance="<?= htmlspecialchars($group['attendance_type']) ?>"
                                                data-members="<?= $group['total_members'] ?>"
                                                data-description="<?= htmlspecialchars($group['description']) ?>"
                                                data-status="<?= htmlspecialchars($group['status']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#viewGroupModal">

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

                                                View

                                            </button>

                                        </li>

                                        <li>

                                            <a href="view-cds-group-members.php?id=<?= $group['id'] ?>"
                                                class="dropdown-item">

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

                                                View Members

                                            </a>

                                        </li>

                                        <li>

                                            <a href="print-cds-group.php?id=<?= $group['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 GROUP ================= -->

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

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

                <div class="modal-content">

                    <div class="modal-header">

                        <h5>

                            CDS Group 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%">

                                    Group Name

                                </th>

                                <td id="viewGroupName"></td>

                            </tr>

                            <tr>

                                <th>

                                    Group Code

                                </th>

                                <td id="viewGroupCode"></td>

                            </tr>

                            <tr>

                                <th>

                                    Meeting Day

                                </th>

                                <td id="viewMeetingDay"></td>

                            </tr>

                            <tr>

                                <th>

                                    Meeting Time

                                </th>

                                <td id="viewMeetingTime"></td>

                            </tr>

                            <tr>

                                <th>

                                    Meeting Venue

                                </th>

                                <td id="viewMeetingVenue"></td>

                            </tr>

                            <tr>

                                <th>

                                    Attendance Type

                                </th>

                                <td id="viewAttendanceType"></td>

                            </tr>

                            <tr>

                                <th>

                                    Members

                                </th>

                                <td id="viewMembers"></td>

                            </tr>

                            <tr>

                                <th>

                                    Description

                                </th>

                                <td id="viewDescription"></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-group-btn',

            function() {

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

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

                $('#viewMeetingDay').text($(this).data('day'));

                $('#viewMeetingTime').text($(this).data('time') || '-');

                $('#viewMeetingVenue').text($(this).data('venue') || '-');

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

                $('#viewMembers').text($(this).data('members'));

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

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

                if (status == "Active") {

                    $('#viewStatus').html(

                        '<span class="badge bg-success">Active</span>'

                    );

                } else {

                    $('#viewStatus').html(

                        '<span class="badge bg-danger">Inactive</span>'

                    );

                }

            });
        </script>