<?php

$pageTitle = "CDS Groups";
require_once '../includes/auth.php';

require_login();

$user = current_user();
$isAdminOrLGI = in_array($user['role'], ['Administrator', 'LGI']);

$cds = [];

if(!$isAdminOrLGI) {

    $stmt = $db->prepare("
        SELECT
            cg.id AS group_id,
            cg.group_name,
            cg.group_code, 
            cg.meeting_day,
            cg.meeting_time,
            cg.meeting_venue,
            cg.description,
            cgm.assigned_date,
            cgm.is_executive,
            cgm.executive_position,
            cgm.status
        FROM cds_group_members cgm
        JOIN cds_groups cg
            ON cgm.cds_group_id = cg.id
        JOIN corps_members cm
            ON cgm.corps_member_id = cm.id
        WHERE cm.user_id = ?
        LIMIT 1
    ");

    $stmt->execute([$user['id']]);

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


    $groups = [];

    if($isAdminOrLGI) {

        $groupStmt = $db->prepare("
            SELECT
                cg.*,
                COUNT(cgm.id) AS total_members
                FROM cds_groups cg
                LEFT JOIN cds_group_members cgm
                    ON cg.id = cgm.cds_group_id
            GROUP BY cg.id
            ORDER BY cg.group_name ASC
            ");
        
        $groupStmt->execute();

        $groups = $groupStmt->fetchAll(PDO::FETCH_ASSOC);
    }

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

$members = [];

if($isAdminOrLGI) {

    $memberStmt = $db->prepare("
        SELECT
            cm.id,
            cm.state_code,
            CONCAT(
                cm.firstname,
                ' ',
                cm.lastname,
                ' ',
                IFNULL(cm.othername,'')
            ) AS fullname
        FROM corps_members cm
        ORDER BY fullname ASC
    ");

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

}


?>

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

        <?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">
                        <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">
                                <img src="../assets/images/thumbs/teacher-details-img.png" alt="employee Image"
                                    class="w-100 h-100 object-fit-cover">
                            </figure>
                            <h2 class="h6 text-primary-light mb-16 fw-semibold">
                                <span
                                    id="memberName"><?= htmlspecialchars($user['firstname'] . ' ' . $user['lastname']) ?></span>
                            </h2>
                            <p class="mb-0">State Code: <span class="text-primary-600 fw-semibold" id="memberState">
                                    <?= htmlspecialchars($user['state_code'] ?? 'State Code Not Found') ?></span>
                            </p>
                            <p class=" mb-0">Role: <span class="text-primary-light fw-semibold"
                                    id="memberRole"><?= $user['role'] ?></span>
                            </p>
                        </div>
                        <div class="">
                            <span class="h-100 w-1-px bg-neutral-200"></span>
                        </div>
                        <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">My CDS Group</h3>
                                <span
                                    class="bg-success-100 text-success-600 px-16 py-4 radius-4 fw-medium text-sm">Active</span>
                            </div>
                            <?php if ($cds): ?>
                            <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">:
                                        <?= date('h:i A', strtotime($cds['meeting_time'] ?? '')) ?></span>
                                </div>
                                <div class="d-flex gap-4">
                                    <span class="fw-semibold text-sm text-primary-light w-110-px">Meeting Venue</span>
                                    <span class="fw-normal text-sm text-secondary-light">:
                                        <?= htmlspecialchars($cds['meeting_venue'] ?? '') ?></span>
                                </div>
                                <div class="d-flex gap-4">
                                    <span class="fw-semibold text-sm text-primary-light w-110-px">Assigned Date</span>
                                    <span class="fw-normal text-sm text-secondary-light">:
                                        <?= date('d M Y', strtotime($cds['assigned_date'] ?? '')) ?></span>
                                </div>
                                <div class="d-flex gap-4">
                                    <span class="fw-semibold text-sm text-primary-light w-110-px">Executive
                                        Member</span>
                                    <span class="fw-normal text-sm text-secondary-light">:
                                        <?= $cds['is_executive'] ?></span>
                                </div>
                                <?php if($cds['is_executive'] == 'Yes'): ?>
                                <div class="d-flex gap-4">
                                    <span class="fw-semibold text-sm text-primary-light w-110-px">Position</span>
                                    <span class="fw-normal text-sm text-primary-600">:
                                        <?= htmlspecialchars($cds['executive_position'] ?? '') ?></span>
                                </div>
                                <?php endif; ?>
                                <div class="d-flex gap-4">
                                    <span class="fw-semibold text-sm text-primary-light w-110-px">Description</span>
                                    <span class="fw-normal text-sm text-primary-600">:
                                        <?= htmlspecialchars($cds['description']) ?></span>
                                </div>
                            </div>
                            <?php else: ?>
                            <div class="text-center py-5">
                                <span class="mt-4">
                                    <iconify-icon icon="mdi:account-group" width="80" class="text-secondary">
                                    </iconify-icon>
                                </span>

                                <h5 class="mt-4">
                                    No CDS Group Assigned
                                </h5>

                                <p class="text-secondary">
                                    You have not yet been assigned
                                    to any CDS group.
                                </p>

                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php endif; ?>


        <?php if(in_array($user['role'], ['Administrator', 'LGI'])): ?>
        <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-payroll-tab" data-bs-toggle="pill" data-bs-target="#pills-payroll" type="button"
                        role="tab" aria-controls="pills-payroll" aria-selected="false">
                        <span class="d-flex tab-icon line-height-1 text-md">
                            <i class="ri-money-dollar-box-line"></i>
                        </span>
                        CDS GROUPS
                    </button>
                </li>
            </ul>


            <div class="tab-content" id="pills-tabContent">
                <!-- Payroll tab start -->
                <div class="tab-pane fade show active" id="pills-payroll" role="tabpanel"
                    aria-labelledby="pills-payroll-tab" tabindex="0">
                    <div class="shadow-1 radius-12 bg-base h-100 overflow-hidden">
                        <div
                            class="card-header border-bottom bg-base py-10 px-20 d-flex align-items-center justify-content-between">
                            <h6 class="text-lg fw-semibold mb-0">Corps Members CDS Grouping </h6>
                        </div>
                        <div class="card-body p-0 dataTable-wrapper">
                            <div class="d-flex flex-wrap align-items-center gap-24 justify-content-between px-20 py-16">
                                <div class="d-flex flex-wrap align-items-center gap-16">
                                    <form class="navbar-search dt-search m-0">
                                        <input type="text" class="dt-input bg-transparent radius-4"
                                            aria-controls="dataTable" name="search" placeholder="Search...">
                                        <iconify-icon icon="ion:search-outline" class="icon"></iconify-icon>
                                    </form>
                                    <div class="">
                                        <select class="form-control form-select">
                                            <option value="Year 2025/2026">Year 2025/2026</option>
                                            <option value="Year 2026/2027">Year 2026/2027</option>
                                            <option value="Year 2027/2028">Year 2027/2028</option>
                                            <option value="Year 2028/2029">Year 2028/2029</option>
                                        </select>
                                    </div>
                                    <div class="dropdown">
                                        <button type="button"
                                            class="px-12 py-5-px 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 gap-8 text-secondary-light">
                                    <span class="">
                                        Rows per page:
                                    </span>
                                    <div class="dt-length">
                                        <select name="dataTable_length" aria-controls="dataTable"
                                            class="dt-input form-control form-select">
                                            <option value="5">5</option>
                                            <option value="10" selected>10</option>
                                            <option value="25">25</option>
                                            <option value="50">50</option>
                                            <option value="100">100</option>
                                        </select>
                                    </div>
                                </div>

                                <button type="button"
                                    class="apply-leave-btn btn btn-primary-600 d-flex align-items-center gap-6 py-8 text-sm"
                                    data-bs-toggle="modal" data-bs-target="#addGroupModal">
                                    <span class="d-flex text-sm">
                                        <i class="ri-add-line"></i>
                                    </span>
                                    Add CDS Group
                                </button>

                            </div>
                            <table class="table bordered-table mb-0 table-heading-dark-mode w-100 data-table"
                                id="dataTableTwo" data-page-length='10'>
                                <thead>
                                    <tr>
                                        <th scope="col">
                                            <div class="form-check style-check d-flex align-items-center">
                                                <input class="form-check-input" type="checkbox">
                                                <label class="form-check-label">
                                                    S/N
                                                </label>
                                            </div>
                                        </th>
                                        <th scope="col">State Code</th>
                                        <th scope="col">Name</th>
                                        <th scope="col">Batch</th>
                                        <th scope="col">Members</th>
                                        <th scope="col">Status</th>
                                        <th scope="col">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if(!empty($groups)): ?>
                                    <?php foreach ($groups as $index => $group): ?>
                                    <tr>
                                        <td>
                                            <div class="form-check style-check d-flex align-items-center">
                                                <input class="form-check-input" type="checkbox">
                                                <label class="form-check-label"><?= $index + 1 ?></label>
                                            </div>
                                        </td>
                                        <td><span
                                                class="text-primary-600"><?= htmlspecialchars($group['group_code']) ?></span>
                                        </td>
                                        <td><?= htmlspecialchars($group['group_name']) ?></td>
                                        <td><?= htmlspecialchars($group['meeting_day']) ?></td>
                                        <td><?= $group['total_members'] ?></td>
                                        <td>
                                            <span
                                                class="bg-success-100 text-success-600 px-20 py-4 radius-4 fw-medium text-sm">
                                                <?= $group['status'] ?>
                                            </span>
                                        </td>
                                        <td>
                                            <a href="../dashboard/cds_group_members.php?id=<?= $group['id'] ?>"
                                                class="btn btn-info">
                                                View Members
                                            </a>
                                            <button class="btn btn-sm btn-primary" data-bs-toggle="modal"
                                                data-bs-target="#editGroupModal<?= $group['id'] ?>">
                                                Edit
                                            </button>

                                            <button class="btn btn-sm btn-danger" data-bs-toggle="modal"
                                                data-bs-target="#deleteModal<?= $group['id'] ?>">
                                                Delete
                                            </button>
                                            <!-- Modal Delete Event start -->
                                            <div class="modal fade" id="deleteModal<?= $group['id'] ?>" tabindex="-1"
                                                aria-hidden="true">
                                                <div
                                                    class="modal-dialog modal-sm modal-dialog modal-dialog-centered max-w-340-px">
                                                    <div class="modal-content radius-16 bg-base">
                                                        <div class="modal-body pt-32 px-36 pb-24 text-center">
                                                            <span class="mb-16 fs-1 line-height-1 text-danger">
                                                                <iconify-icon icon="fluent:delete-24-regular"
                                                                    class="menu-icon"></iconify-icon>
                                                            </span>
                                                            <h6 class="text-lg fw-semibold text-primary-light mb-0">Are
                                                                your sure you want to Delete this group
                                                                <?= htmlspecialchars($group['group_name']) ?>
                                                            </h6>
                                                            <div
                                                                class="d-flex align-items-center justify-content-center gap-3 mt-24">
                                                                <button type="reset"
                                                                    class="flex-grow-1 border border-danger-600 bg-hover-danger-200 text-danger-600 text-md px-24 py-11 radius-8">
                                                                    Cancel
                                                                </button>
                                                                <form action="../actions/delete_cds_group.php"
                                                                    method="POST">

                                                                    <input type="hidden" name="group_id"
                                                                        id="delete_group_id"
                                                                        value="<?= $group['id'] ?>">

                                                                    <button type="submit"
                                                                        class="flex-grow-1 btn btn-primary-600 border border-primary-600 text-md px-16 py-12 radius-8">
                                                                        Delete CDS Group
                                                                    </button>

                                                                </form>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- Modal Delete Event end -->
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                    <?php foreach($groups as $group): ?>

                                    <div class="modal fade" id="editGroupModal<?= $group['id'] ?>" tabindex="-1">

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

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

                                                    <input type="hidden" name="group_id" value="<?= $group['id'] ?>">

                                                    <div class="modal-header">
                                                        <h5>Edit CDS Group</h5>
                                                    </div>

                                                    <div class="modal-body">

                                                        <div class="mb-16">
                                                            <label>Group Name</label>

                                                            <input type="text" name="group_name"
                                                                value="<?= htmlspecialchars($group['group_name']) ?>"
                                                                class="form-control">
                                                        </div>

                                                        <div class="mb-16">
                                                            <label>Meeting Day</label>

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

                                                                <option
                                                                    <?= $group['meeting_day']=='Monday'?'selected':'' ?>>
                                                                    Monday
                                                                </option>

                                                                <option
                                                                    <?= $group['meeting_day']=='Thursday'?'selected':'' ?>>
                                                                    Thursday
                                                                </option>

                                                            </select>

                                                        </div>

                                                        <div class="mb-16">
                                                            <label>Meeting Venue</label>

                                                            <input type="text" name="meeting_venue"
                                                                value="<?= htmlspecialchars($group['meeting_venue']) ?>"
                                                                class="form-control">
                                                        </div>

                                                        <div class="mb-16">
                                                            <label>Description</label>

                                                            <textarea name="description"
                                                                class="form-control"><?= htmlspecialchars($group['description']) ?></textarea>
                                                        </div>

                                                    </div>

                                                    <div class="modal-footer">

                                                        <button class="btn btn-primary">
                                                            Update Group
                                                        </button>

                                                    </div>

                                                </form>

                                            </div>
                                        </div>
                                    </div>

                                    <?php endforeach; ?>
                                    <?php else: ?>
                                    <tr>
                                        <td colspan="7" class="text-center py-5">
                                            <div class="d-flex flex-column align-items-center">
                                                <iconify-icon icon="mdi:account-group-outline" width="60"
                                                    class="text-secondary mb-3">
                                                </iconify-icon>

                                                <h6 class="mb-2">
                                                    No Corps Members Assigned
                                                </h6>
                                                <p class="text-secondary-light mb-0">
                                                    There are currently no corps members assigned to this CDS group.
                                                </p>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- Payroll tab end -->
            </div>
        </div>
        <?php endif; ?>
    </div>
</main>
<!-- Add Group modal start -->
<div class="modal fade" id="addGroupModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-sm 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">Add Member to Group
                </h6>
            </div>
            <div class="modal-body p-24">
                <form action="../actions/create_cds_group.php" method="POST">

                    <div class="mb-16">
                        <label class="form-label">Group Name</label>
                        <input type="text" name="group_name" class="form-control" required>
                    </div>

                    <div class="mb-16">
                        <label class="form-label">Group Code</label>
                        <input type="text" name="group_code" class="form-control" placeholder="CDS009" required>
                    </div>

                    <div class="mb-16">
                        <label class="form-label">Meeting Day</label>

                        <select name="meeting_day" class="form-select" required>

                            <option value="Monday">Monday</option>
                            <option value="Tuesday">Tuesday</option>
                            <option value="Wednesday">Wednesday</option>
                            <option value="Thursday" selected>Thursday</option>
                            <option value="Friday">Friday</option>
                            <option value="Saturday">Saturday</option>

                        </select>
                    </div>

                    <div class="mb-16">
                        <label class="form-label">Meeting Time</label>

                        <input type="time" name="meeting_time" class="form-control">
                    </div>

                    <div class="mb-16">
                        <label class="form-label">Meeting Venue</label>

                        <input type="text" name="meeting_venue" class="form-control">
                    </div>

                    <div class="mb-16">
                        <label class="form-label">Description</label>

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

                    <div class="mb-16">

                        <label class="form-label">
                            Add Corps Members
                        </label>

                        <select multiple name="members[]" class="form-select">

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

                            <option value="<?= $member['id'] ?>">
                                <?= $member['state_code'] ?>
                                -
                                <?= $member['fullname'] ?>
                            </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <button class="btn btn-primary-600 w-100">
                        Create CDS Group
                    </button>

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