<?php
$pageTitle = "Dashboard";
require_once '../includes/auth.php';

require_login();

$user = current_user();


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

$role = strtolower($user['role']);
$chartTitle = '';
$chartLablels = [];
$chartSeries = [];

$card1Title = '';
$card1Value = 0;
$card2Title = '';
$card2Value = 0;
$card3Title = '';
$card3Value = 0;
$card4Title = '';
$card4Value = 0;

$tableTitle = '';
$rightPanelTitle = '';
$tableData = [];
$rightPanelData = [];


switch ($role) {

    /*
    |--------------------------------------------------------------------------
    | ADMINISTRATOR
    |--------------------------------------------------------------------------
    */
    case 'administrator':

        // Dashboard Cards
        $card1Title = 'Total Corps Members';
        $card1Value = $db->query("
            SELECT COUNT(*) FROM corps_members
        ")->fetchColumn();

        $card2Title = 'Camp Officers';
        $card2Value = $db->query("
            SELECT COUNT(*)
            FROM users u
            JOIN roles r ON u.role_id = r.id
            WHERE LOWER(r.role_name) = 'Camp Officer'
        ")->fetchColumn();

        $card3Title = 'LG Inspectors';
        $card3Value = $db->query("
            SELECT COUNT(*)
            FROM users u
            JOIN roles r ON u.role_id = r.id
            WHERE LOWER(r.role_name) = 'LGI'
        ")->fetchColumn();

        $card4Title = 'Pending Clearance';
        $card4Value = $db->query("
            SELECT COUNT(*)
            FROM monthly_clearance
            WHERE status='Pending'
        ")->fetchColumn();

        // Chart
        $chartTitle = 'System Overview';

        $chartLabels = [
            'Corps Members',
            'Camp Officers',
            'LGI',
            'Pending Clearance'
        ];

        $chartSeries = [
            (int)$card1Value,
            (int)$card2Value,
            (int)$card3Value,
            (int)$card4Value
        ];


        $tableTitle = 'Recent Corps Members';

        $tableData = $db->query("
            SELECT
                state_code,
                CONCAT(firstname, ' ', lastname) AS fullname,
                batch,
                stream,
                status
            FROM corps_members
            ORDER BY id DESC
            LIMIT 10
        ")->fetchAll();


        $rightPanelTitle = 'System Activity';

        $rightPanelData = [
            [
                'title' => 'New Corps Registration',
                'date' => date('d M Y'),
                'status' => 'Completed'
            ],
            [
                'title' => 'CDS Attendance Submitted',
                'date' => date('d M Y'),
                'status' => 'Completed'
            ],
            [
                'title' => 'Monthly Clearance Pending',
                'date' => date('d M Y'),
                'status' => 'Pending'
            ]
        ];      

        break;


    /*
    |--------------------------------------------------------------------------
    | CAMP OFFICER
    |--------------------------------------------------------------------------
    */
    case 'camp officer':

        $card1Title = 'Registered Members';
        $card1Value = $db->query("
            SELECT COUNT(*) FROM corps_members
        ")->fetchColumn();

        $card2Title = 'Hostels';
        $card2Value = $db->query("
            SELECT COUNT(*) FROM hostels
        ")->fetchColumn();

        $card3Title = 'Platoons';
        $card3Value = $db->query("
            SELECT COUNT(*) FROM platoons
        ")->fetchColumn();

        $card4Title = 'Camp Activities';
        $card4Value = $db->query("
            SELECT COUNT(*) FROM camp_activities
        ")->fetchColumn();

        $chartTitle = 'Camp Statistics';

        $chartLabels = [
            'Members',
            'Hostels',
            'Platoons',
            'Activities'
        ];

        $chartSeries = [
            (int)$card1Value,
            (int)$card2Value,
            (int)$card3Value,
            (int)$card4Value
        ];

        $tableTitle = 'Camp Registration Queue';

        $tableData = $db->query("
            SELECT
                cm.state_code,
                CONCAT(cm.firstname,' ',cm.lastname) AS fullname,
                p.platoon_name AS platoon,
                h.hostel_name AS hostel,
                cm.status
            FROM corps_members cm
            LEFT JOIN platoons p ON cm.platoon_id = p.id
            LEFT JOIN hostels h ON cm.hostel_id = h.id
            ORDER BY cm.id DESC
            LIMIT 10
        ")->fetchAll();

        $rightPanelTitle = 'Camp Activities';

        $rightPanelData = [
            [
                'title' => 'Swearing-In Ceremony',
                'date' => '20 Jul 2026',
                'status' => 'Upcoming'
            ],
            [
                'title' => 'SAED Orientation',
                'date' => '21 Jul 2026',
                'status' => 'Upcoming'
            ],
            [
                'title' => 'Camp Inspection',
                'date' => '22 Jul 2026',
                'status' => 'Upcoming'
            ]
        ];

        break;


    /*
    |--------------------------------------------------------------------------
    | LGI
    |--------------------------------------------------------------------------
    */
    case 'lgi':

        $card1Title = 'Pending Clearance';
        $card1Value = $db->query("
            SELECT COUNT(*)
            FROM monthly_clearance
            WHERE status='Pending'
        ")->fetchColumn();

        $card2Title = 'Approved Clearance';
        $card2Value = $db->query("
            SELECT COUNT(*)
            FROM monthly_clearance
            WHERE status='Approved'
        ")->fetchColumn();

        $card3Title = 'Rejected Clearance';
        $card3Value = $db->query("
            SELECT COUNT(*)
            FROM monthly_clearance
            WHERE status='Rejected'
        ")->fetchColumn();

        $card4Title = 'CDS Groups';
        $card4Value = $db->query("
            SELECT COUNT(*) FROM cds_groups
        ")->fetchColumn();

        $chartTitle = 'Clearance Overview';

        $chartLabels = [
            'Pending',
            'Approved',
            'Rejected',
            'CDS Groups'
        ];

        $chartSeries = [
            (int)$card1Value,
            (int)$card2Value,
            (int)$card3Value,
            (int)$card4Value
        ];

        $tableTitle = 'Pending Monthly Clearance';

        $tableData = $db->query("
            SELECT
                cm.state_code,
                CONCAT(cm.firstname,' ',cm.lastname) AS fullname,
                CONCAT(mc.clearance_month, '/', mc.clearance_year) AS month,
                mc.status
            FROM monthly_clearance mc
            JOIN corps_members cm ON mc.corps_member_id = cm.id
            ORDER BY mc.id DESC
            LIMIT 10
        ")->fetchAll();

        $rightPanelTitle = 'Clearance Queue';

        $rightPanelData = [
            [
                'title' => 'July Clearance',
                'date' => '01 Jul 2026',
                'status' => 'Pending'
            ],
            [
                'title' => 'August Clearance',
                'date' => '01 Aug 2026',
                'status' => 'Approved'
            ]
        ];

        break;


    /*
    |--------------------------------------------------------------------------
    | CORPS MEMBER
    |--------------------------------------------------------------------------
    */
    default:

        $corpsMemberId = $user['id'];

        $card1Title = 'My CDS Attendance';
        $card1Value = $db->query("
            SELECT COUNT(*)
            FROM cds_attendance
            WHERE attendance_status='Present'
        ")->fetchColumn();

        $card2Title = 'Clearance Status';
        $card2Value = $db->query("
            SELECT COUNT(*)
            FROM monthly_clearance
            WHERE status='Approved'
        ")->fetchColumn();

        $card3Title = 'PPA Status';
        $card3Value = $db->query("
            SELECT COUNT(*)
            FROM corps_postings
        ")->fetchColumn();

        $card4Title = 'Uploaded Documents';
        $card4Value = $db->query("
            SELECT COUNT(*)
            FROM documents
        ")->fetchColumn();

        $chartTitle = 'My Dashboard';

        $chartLabels = [
            'Present',
            'Approved Clearance',
            'PPA',
            'Documents'
        ];

        $chartSeries = [
            (int)$card1Value,
            (int)$card2Value,
            (int)$card3Value,
            (int)$card4Value
        ];

        $tableTitle = 'My Attendance History';

        $stmt = $db->prepare("
            SELECT
                ca.attendance_date,
                cg.group_name AS cds_group,
                ca.attendance_status
            FROM cds_attendance ca
            JOIN cds_groups cg
                ON ca.cds_group_id = cg.id
            JOIN corps_members cm
                ON ca.corps_member_id = cm.id
            WHERE cm.user_id = ?
            ORDER BY ca.attendance_date DESC
            LIMIT 10
        ");

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

        $rightPanelTitle = 'My Clearance History';

        $stmt = $db->prepare("
            SELECT
                CONCAT(mc.clearance_month,'/',mc.clearance_year) AS month,
                mc.created_at,
                mc.status
            FROM monthly_clearance mc
            JOIN corps_members cm
                ON mc.corps_member_id = cm.id
            WHERE cm.user_id = ?
            ORDER BY mc.id DESC
            LIMIT 10
        ");

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

        $rightPanelData = $stmt->fetchAll();

        break;
}

$announcements = $db->query("
    SELECT
        a.*,
        CONCAT(u.firstname, ' ', u.lastname) AS created_by_name
        FROM announcements a
        LEFT JOIN users u ON a.created_by = u.id
        WHERE LOWER(a.target_role) = 'all'
        OR LOWER(a.target_role) = LOWER('{$user['role']}')
        ORDER BY a.created_at DESC
        LIMIT 5
        ")->fetchAll();

$eventsStmt = $db->prepare("
    SELECT e.*, CONCAT(u.firstname, ' ', u.lastname) AS organizer
    FROM events e
    LEFT JOIN users u ON e.organizer_id = u.id
    WHERE LOWER(e.target_role) = 'all'
    OR LOWER(e.target_role) = LOWER(?)
    ORDER BY e.start_time ASC
    LIMIT 5
    ");
$eventsStmt->execute([$user['role']]);
$events = $eventsStmt->fetchAll();

?>
<!-- Dashboard Content Goes Here -->
<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>
        <div class="mt-24">
            <div class="row gy-4">
                <!-- Dashboard widgets start -->
                <div class="col-xxl-7">
                    <div class="card radius-12 border-0 h-100">
                        <div class="card-body p-24">
                            <div class="row g-3">
                                <div class="col-md-5">
                                    <div
                                        class="radius-8 overflow-hidden position-relative z-1 h-100 py-32 px-20 text-center d-flex justify-content-center align-items-center">
                                        <img src="../assets/images/bg/edit-profile-bg.png" alt="BG Image"
                                            class="position-absolute start-0 top-0 w-100 h-100 object-fit-cover z-n1">
                                        <div class="">
                                            <span class="mb-12">
                                                <img src="../assets/images/thumbs/edit-profile-img.png" alt="BG Image"
                                                    class="rounded-circle object-fit-cover">
                                            </span>

                                            <h6 class="text-white"><?= $user['firstname'].' '.$user['lastname'] ?></h6>
                                            <span class="text-white text-lg d-block"><?= $user['role'] ?></span>

                                            <?php if($user['role'] == 'Corps Member'): ?>
                                            <span class="text-white text-lg d-block">
                                                <?= htmlspecialchars($user['state_code'] ?? 'State Code Not Found') ?>
                                            </span>
                                            <?php endif; ?>
                                            <?php if($user['role'] == 'Camp Officer'): ?>
                                            <span class="text-white text-lg d-block">
                                                Orientation Camp Officer
                                            </span>
                                            <?php endif; ?>

                                            <?php if($user['role'] == 'LGI'): ?>
                                            <span class="text-white text-lg d-block">
                                                Local Government Inspector
                                            </span>
                                            <?php endif; ?>
                                            <div class="mt-12">
                                                <a href="edit-teacher.html"
                                                    class="px-20 py-8 text-white bg-white bg-opacity-10 radius-6 fw-medium text-lg">Edit
                                                    Profile</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-7">
                                    <?php if($user['role'] == 'Administrator'): ?>
                                    <div class="row g-3">
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-purple-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-purple-300 bg-purple-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon1.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card1Title ?></span>
                                                <h5 class="text-primary-light"><?= number_format($card1Value) ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-success-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-success-300 bg-success-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon2.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card2Title ?></span>
                                                <h5 class="text-primary-light"><?= number_format($card2Value) ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-primary-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-primary-300 bg-primary-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon3.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card3Title ?></span>
                                                <h5 class="text-primary-light"><?= number_format($card3Value) ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-danger-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-danger-300 bg-danger-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon4.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card4Title ?></span>
                                                <h5 class="text-primary-light"><?= number_format($card4Value) ?></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endif; ?>
                                    <?php if($user['role'] == 'Camp Officer'): ?>
                                    <div class="row g-3">
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-purple-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-purple-300 bg-purple-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon1.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card1Title ?></span>
                                                <h5 class="text-primary-light"><?= $card1Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-success-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-success-300 bg-success-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon2.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card2Title ?></span>
                                                <h5 class="text-primary-light"><?= $card2Value ?> </h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-primary-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-primary-300 bg-primary-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon3.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card3Title ?></span>
                                                <h5 class="text-primary-light"><?= $card3Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-danger-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-danger-300 bg-danger-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon4.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card4Title ?></span>
                                                <h5 class="text-primary-light"><?= $card4Value ?></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endif; ?>
                                    <?php if($user['role'] == 'LGI'): ?>
                                    <div class="row g-3">
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-purple-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-purple-300 bg-purple-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon1.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card1Title ?></span>
                                                <h5 class="text-primary-light"><?= $card1Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-success-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-success-300 bg-success-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon2.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card2Title ?></span>
                                                <h5 class="text-primary-light"><?= $card2Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-primary-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-primary-300 bg-primary-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon3.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card3Title ?></span>
                                                <h5 class="text-primary-light"><?= $card3Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-danger-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-danger-300 bg-danger-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon4.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card4Title ?></span>
                                                <h5 class="text-primary-light"><?= $card4Value ?></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endif; ?>
                                    <?php if($user['role'] == 'Corps Member'): ?>
                                    <div class="row g-3">
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-purple-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-purple-300 bg-purple-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon1.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card1Title ?></span>
                                                <h5 class="text-primary-light"><?= $card1Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-success-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-success-300 bg-success-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon2.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card2Title ?></span>
                                                <h5 class="text-primary-light"><?= $card2Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-primary-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-primary-300 bg-primary-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon3.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card3Title ?></span>
                                                <h5 class="text-primary-light"><?= $card3Value ?></h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-6 col-xs-6">
                                            <div class="radius-8 py-24 px-16 text-center bg-danger-100">
                                                <span
                                                    class="w-48-px h-48-px d-inline-flex justify-content-center align-items-center rounded-circle border border-danger-300 bg-danger-200">
                                                    <img src="../assets/images/icons/teacher-widget-icon4.png"
                                                        alt="User Icon">
                                                </span>
                                                <span
                                                    class="text-secondary-light fw-medium d-block mt-12"><?= $card4Title ?></span>
                                                <h5 class="text-primary-light"><?= $card4Value ?></h5>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Dashboard widgets end -->

                <!-- User activity Start -->
                <div class="col-xxl-5">
                    <div class="card radius-12 border-0 h-100">
                        <div
                            class="d-flex align-items-center flex-wrap gap-2 justify-content-between py-12 px-20 border-bottom border-neutral-200">
                            <h6 class="mb-2 fw-bold text-lg"><?= $chartTitle ?></h6>
                            <select class="form-select bg-base form-select-sm w-auto radius-8">
                                <option>Yearly</option>
                                <option>Monthly</option>
                                <option>Weekly</option>
                                <option>Today</option>
                            </select>
                        </div>
                        <div class="card-body py-24 d-flex flex-column justify-content-center">
                            <div
                                class="d-flex align-items-center gap-20 flex-sm-nowrap flex-wrap justify-content-around">
                                <div class="d-flex flex-column gap-12">
                                    <?php 
                                    $colors = ['bg-primary-600', 'bg-warning-600', 'bg-success', 'bg-purple'];
                                    foreach ($chartLabels as $index => $label): 
                                    ?>
                                    <div class="d-flex align-items-center gap-12">
                                        <div class="">
                                            <span
                                                class="w-20-px h-20-px bg-primary-600<?= $colors[$index] ?> rounded-circle position-relative">
                                                <span
                                                    class="w-10-px h-10-px bg-white rounded-circle position-absolute top-50 start-50 translate-middle"></span>
                                            </span>
                                        </div>
                                        <div class="">
                                            <h6 class="mb-0"><?= number_format($chartSeries[$index]) ?></h6>
                                            <p class="text-secondary-light text-sm mb-0"><?= $label ?></p>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                                <div class="text-center">

                                    <?php if(array_sum($chartSeries) > 0): ?>

                                    <div id="userOverviewDonutChart" class="apexcharts-tooltip-z-none">
                                    </div>

                                    <?php else: ?>

                                    <div class="py-5">
                                        <iconify-icon icon="mdi:chart-donut" width="80" class="text-secondary">
                                        </iconify-icon>

                                        <h6 class="mt-3 text-secondary">
                                            No chart data available
                                        </h6>
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
                <!-- User activity End -->

                <!-- Notice Board start -->
                <div class="col-xxl-4 col-lg-6">
                    <div class="card h-100">
                        <div class="card-body p-0">
                            <div
                                class="d-flex flex-wrap align-items-center justify-content-between px-20 py-16 border-bottom border-neutral-200">
                                <h6 class="text-lg mb-0">Notice Board</h6>
                                <?php if($user['role'] == 'Administrator'): ?>
                                <div class="dropdown">
                                    <button type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                        <iconify-icon icon="entypo:dots-three-vertical"
                                            class="icon text-secondary-light"></iconify-icon>
                                    </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">
                                                <iconify-icon icon="hugeicons:view" class="icon text-lg line-height-1">
                                                </iconify-icon>
                                                View
                                            </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">
                                                <iconify-icon icon="lucide:edit" class="icon text-lg line-height-1">
                                                </iconify-icon>
                                                Edit
                                            </button>
                                        </li>
                                        <li>
                                            <button type="button"
                                                class="delete-item dropdown-item px-16 py-8 rounded text-secondary-light bg-hover-danger-100 text-hover-danger-600 d-flex align-items-center gap-10"
                                                data-bs-toggle="modal" data-bs-target="#exampleModalDelete">
                                                <iconify-icon icon="fluent:delete-24-regular"
                                                    class="icon text-lg line-height-1"></iconify-icon>
                                                Delete
                                            </button>
                                        </li>
                                    </ul>
                                </div>
                                <?php endif; ?>
                            </div>
                            <div class="ps-20 pt-20 pb-20">
                                <div class="pe-20 d-flex flex-column gap-20 max-h-462-px overflow-y-auto scroll-sm">

                                    <?php if (!empty($announcements)): ?>
                                    <?php foreach($announcements as $notice): ?>

                                    <div class="d-flex align-items-start gap-16">

                                        <img src="../assets/images/thumbs/avatar-img1.png"
                                            class="w-40-px h-40-px rounded-circle object-fit-cover flex-shrink-0">

                                        <div>

                                            <h6 class="mb-4 text-lg">
                                                <?= htmlspecialchars($notice['created_by_name'] ?? '') ?>
                                            </h6>
                                            <p class="text-secondary-light text-sm mb-0">
                                                <?= htmlspecialchars($notice['title'] ?? '') ?>
                                            </p>

                                            <p class="text-secondary-light text-sm mb-0">
                                                <?= htmlspecialchars($notice['announcement'] ?? '') ?>
                                            </p>

                                            <span class="text-secondary-light text-sm mb-0 mt-4">
                                                <?= date('d M Y', strtotime($notice['created_at'] ?? '')) ?>
                                            </span>

                                        </div>

                                    </div>

                                    <?php endforeach; ?>
                                    <?php else: ?>
                                    <div class="text-center py-40">
                                        <i class="ri-notification-off-line text-secondary-light"
                                            style="font-size:60px;"></i>

                                        <h6 class="mt-16 text-secondary-light">
                                            No Notices Available
                                        </h6>

                                        <p class="text-sm text-secondary-light">
                                            There are currently no announcements.
                                        </p>
                                    </div>
                                    <?php endif; ?>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Notice Board End -->

                <!-- Upcoming Events start -->
                <div class="col-xxl-4 col-lg-6">
                    <div class="card h-100">
                        <div class="card-body p-0">
                            <div
                                class="d-flex flex-wrap align-items-center justify-content-between px-20 py-16 border-bottom border-neutral-200">
                                <h6 class="text-lg mb-0">Upcoming Events</h6>
                            </div>
                            <div class="ps-20 pt-20 pb-20">
                                <div class="pe-20 d-flex flex-column gap-20 overflow-y-auto max-h-460-px scroll-sm">

                                    <?php if (!empty($events)): ?>

                                    <?php foreach($events as $event): ?>

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

                                        <div class="ps-10 border-start-width-3-px border-primary-600">

                                            <div class="d-flex align-items-end gap-6">
                                                <h6 class="text-lg fw-normal mb-0">
                                                    <?= date('h:i A', strtotime($event['start_time'] ?? '')) ?>
                                                    -
                                                    <?= date('h:i A', strtotime($event['end_time'] ?? '')) ?>
                                                </h6>
                                            </div>

                                            <p class="text-secondary-light mt-4 mb-2 text-sm">
                                                <?= htmlspecialchars($event['title'] ?? '') ?>
                                            </p>

                                            <p class="text-xs text-secondary-light mb-0">
                                                Organized by
                                                <span class="text-primary-600">
                                                    <?= htmlspecialchars($event['organizer'] ?? 'NYSC Management') ?>
                                                </span>
                                            </p>

                                        </div>

                                        <div>
                                            <a href="event-details.php?id=<?= $event['id'] ?>"
                                                class="py-6 px-16 radius-4 bg-neutral-100 text-secondary-light fw-semibold bg-hover-primary-600 hover-text-white">
                                                View
                                            </a>
                                        </div>

                                    </div>

                                    <?php endforeach; ?>

                                    <?php else: ?>

                                    <div
                                        class="d-flex flex-column align-items-center justify-content-center py-40 text-center">

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

                                        <h6 class="mt-16 mb-8 text-secondary-light">
                                            No Upcoming Events
                                        </h6>

                                        <p class="text-sm text-secondary-light mb-0">
                                            There are currently no scheduled NYSC activities or events.
                                        </p>

                                    </div>

                                    <?php endif; ?>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Upcoming Events End -->

                <!-- Calendar start -->
                <div class="col-xxl-4 col-lg-6">
                    <div class="card h-100">
                        <div class="card-body p-0">
                            <div
                                class="d-flex flex-wrap align-items-center justify-content-between px-20 py-16 border-bottom border-neutral-200">
                                <h6 class="text-lg mb-0">Calendar</h6>
                            </div>
                            <div class="p-20">
                                <div class="calendar style-big">
                                    <div class="calendar__header">
                                        <button type="button" class="calendar__arrow left">
                                            <i class="ri-arrow-left-s-line"></i>
                                        </button>
                                        <p class="display text-md text-secondary-light fw-semibold mb-0">""</p>
                                        <button type="button" class="calendar__arrow right">
                                            <i class="ri-arrow-right-s-line"></i>
                                        </button>
                                    </div>

                                    <div class="calendar__week week">
                                        <div class="calendar__week-text">Su</div>
                                        <div class="calendar__week-text">Mo</div>
                                        <div class="calendar__week-text">Tu</div>
                                        <div class="calendar__week-text">We</div>
                                        <div class="calendar__week-text">Th</div>
                                        <div class="calendar__week-text">Fr</div>
                                        <div class="calendar__week-text">Sa</div>
                                    </div>
                                    <div class="days"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Calendar end -->

                <!-- Recent Enrolled Courses Start -->
                <div class="col-xxl-8">
                    <div class="card radius-12 border-0">
                        <div
                            class="d-flex align-items-center flex-wrap gap-2 justify-content-between py-12 px-20 border-bottom border-neutral-200">
                            <h6 class="mb-2 fw-bold text-lg"><?= $tableTitle ?></h6>
                            <div class="dropdown">
                                <button type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                    <iconify-icon icon="entypo:dots-three-vertical" class="icon text-secondary-light">
                                    </iconify-icon>
                                </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">
                                            <iconify-icon icon="hugeicons:view" class="icon text-lg line-height-1">
                                            </iconify-icon>
                                            View
                                        </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">
                                            <iconify-icon icon="lucide:edit" class="icon text-lg line-height-1">
                                            </iconify-icon>
                                            Edit
                                        </button>
                                    </li>
                                    <li>
                                        <button type="button"
                                            class="delete-item dropdown-item px-16 py-8 rounded text-secondary-light bg-hover-danger-100 text-hover-danger-600 d-flex align-items-center gap-10"
                                            data-bs-toggle="modal" data-bs-target="#exampleModalDelete">
                                            <iconify-icon icon="fluent:delete-24-regular"
                                                class="icon text-lg line-height-1"></iconify-icon>
                                            Delete
                                        </button>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <div class="card-body p-0">
                            <div class="table-responsive scroll-sm">
                                <table class="table bordered-table mb-0 data-table" id="dataTable"
                                    data-page-length='10'>
                                    <thead>
                                        <tr>
                                            <?php if($user['role'] == 'Administrator'): ?>
                                            <th scope="col">State Code</th>
                                            <th scope="col">Name</th>
                                            <th scope="col">Stream</th>
                                            <th scope="col">-</th>
                                            <th scope="col">-</th>
                                            <th scope="col">Status</th>
                                            <?php elseif($user['role'] == 'Camp Officer'): ?>
                                            <th scope="col">State Code</th>
                                            <th scope="col">Name</th>
                                            <th scope="col">Platoon</th>
                                            <th scope="col">Hostel</th>
                                            <th scope="col">-</th>
                                            <th scope="col">Status</th>
                                            <?php elseif($user['role'] == 'LGI'): ?>
                                            <th scope="col">State Code</th>
                                            <th scope="col">Name</th>
                                            <th scope="col">Month</th>
                                            <th scope="col">-</th>
                                            <th scope="col">-</th>
                                            <th scope="col">Status</th>
                                            <?php else: ?>
                                            <th scope="col">State Code</th>
                                            <th scope="col">CDS Group</th>
                                            <th scope="col">-</th>
                                            <th scope="col">-</th>
                                            <th scope="col">-</th>
                                            <th scope="col">Status</th>
                                            <?php endif; ?>
                                        </tr>
                                    </thead>
                                    <tbody>

                                        <?php if(!empty($tableData)): ?>

                                        <?php foreach($tableData as $row): ?>

                                        <?php if($role == 'administrator'): ?>

                                        <tr>
                                            <td class="py-10-px">
                                                <span class="text-primary-600">
                                                    <?= htmlspecialchars($row['state_code'] ?? '') ?>
                                                </span>
                                            </td>

                                            <td class="py-10-px">
                                                <div class="d-flex align-items-center">
                                                    <img src="../assets/images/thumbs/avatar-img2.png"
                                                        class="flex-shrink-0 me-12 radius-8" width="40">

                                                    <div>
                                                        <h6 class="text-md mb-0 fw-medium">
                                                            <?= htmlspecialchars($row['fullname'] ?? '') ?>
                                                        </h6>

                                                        <span>
                                                            Batch:
                                                            <span class="fw-semibold">
                                                                <?= htmlspecialchars($row['batch'] ?? '') ?>
                                                            </span>
                                                        </span>
                                                    </div>
                                                </div>
                                            </td>

                                            <td><?= htmlspecialchars($row['stream'] ?? '') ?></td>
                                            <td>-</td>
                                            <td>-</td>

                                            <td>
                                                <span
                                                    class="bg-success-100 text-success-600 px-24 py-4 radius-4 fw-medium text-sm">
                                                    <?= htmlspecialchars($row['status'] ?? '') ?>
                                                </span>
                                            </td>
                                        </tr>

                                        <?php elseif($role == 'camp officer'): ?>

                                        <tr>
                                            <td><?= htmlspecialchars($row['state_code'] ?? '') ?></td>

                                            <td>
                                                <?= htmlspecialchars($row['fullname'] ?? '') ?>
                                            </td>

                                            <td>
                                                <?= htmlspecialchars($row['platoon'] ?? '') ?>
                                            </td>

                                            <td>
                                                <?= htmlspecialchars($row['hostel'] ?? '') ?>
                                            </td>

                                            <td>-</td>

                                            <td>
                                                <span
                                                    class="bg-primary-100 text-primary-600 px-24 py-4 radius-4 fw-medium text-sm">
                                                    <?= htmlspecialchars($row['status'] ?? '') ?>
                                                </span>
                                            </td>
                                        </tr>

                                        <?php elseif($role == 'lgi'): ?>

                                        <tr>
                                            <td><?= htmlspecialchars($row['state_code'] ?? '') ?></td>

                                            <td>
                                                <?= htmlspecialchars($row['fullname'] ?? '') ?>
                                            </td>

                                            <td><?= htmlspecialchars($row['month'] ?? '') ?></td>

                                            <td>-</td>
                                            <td>-</td>

                                            <td>
                                                <?php
                                                    $badge = 'warning';

                                                    if($row['status'] == 'Approved'){
                                                        $badge = 'success';
                                                    }

                                                    if($row['status'] == 'Rejected'){
                                                        $badge = 'danger';
                                                    }
                                                    ?>

                                                <span
                                                    class="bg-<?= $badge ?>-100 text-<?= $badge ?>-600 px-24 py-4 radius-4 fw-medium text-sm">
                                                    <?= htmlspecialchars($row['status'] ?? '') ?>
                                                </span>
                                            </td>
                                        </tr>

                                        <?php else: ?>

                                        <tr>

                                            <td>
                                                <?= date('d M Y', strtotime($row['attendance_date'] ?? '')) ?>
                                            </td>

                                            <td>
                                                <?= htmlspecialchars($row['cds_group'] ?? '') ?>
                                            </td>

                                            <td>-</td>
                                            <td>-</td>
                                            <td>-</td>

                                            <td>

                                                <?php
                                                $badge = $row['attendance_status'] == 'Present'
                                                    ? 'success'
                                                    : 'danger';
                                                ?>

                                                <span
                                                    class="bg-<?= $badge ?>-100 text-<?= $badge ?>-600 px-24 py-4 radius-4 fw-medium text-sm">
                                                    <?= htmlspecialchars($row['attendance_status'] ?? '') ?>
                                                </span>

                                            </td>

                                        </tr>

                                        <?php endif; ?>

                                        <?php endforeach; ?>

                                        <?php else: ?>

                                        <div class="text-center py-5">
                                            No records available
                                        </div>

                                        <?php endif; ?>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Recent Enrolled Courses End -->

                <!-- Leave Status start -->
                <div class="col-xxl-4">
                    <div class="card radius-12 border-0 h-100">
                        <div
                            class="d-flex align-items-center flex-wrap gap-2 justify-content-between py-12 px-20 border-bottom border-neutral-200">
                            <h6 class="mb-2 fw-bold text-lg"><?= $rightPanelTitle ?></h6>
                            <select class="form-select bg-base form-select-sm w-auto radius-8">
                                <option>Yearly</option>
                                <option>Monthly</option>
                                <option>Weekly</option>
                                <option>Today</option>
                            </select>
                        </div>
                        <div class="card-body pe-0">
                            <div class="d-flex flex-column max-h-494-px overflow-y-auto scroll-sm pe-20">

                                <?php if(!empty($rightPanelData)): ?>

                                <?php foreach($rightPanelData as $item): ?>

                                <?php
                                    $badge = 'warning';

                                    if(
                                        strtolower($item['status']) == 'approved' ||
                                        strtolower($item['status']) == 'completed' ||
                                        strtolower($item['status']) == 'upcoming'
                                    ){
                                        $badge = 'success';
                                    }

                                    if(strtolower($item['status']) == 'rejected'){
                                        $badge = 'danger';
                                    }
                                ?>

                                <div
                                    class="d-flex align-items-center justify-content-between gap-3 py-10 border-bottom">

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

                                        <h6 class="text-lg mb-0 fw-medium">
                                            <?= htmlspecialchars($item['title'] ?? $item['month'] ?? '') ?>
                                        </h6>

                                        <span class="text-sm text-secondary-light fw-medium">
                                            Date:
                                            <?= !empty($item['date'] ?? $item['created_at'])
                                                ? date('d M Y', strtotime($item['date'] ?? $item['created_at']))
                                                : 'N/A'
                                            ?>
                                        </span>

                                    </div>

                                    <div>
                                        <span
                                            class="bg-<?= $badge ?>-100 text-<?= $badge ?>-600 px-24 py-4 radius-4 fw-medium text-sm">
                                            <?= htmlspecialchars($item['status'] ?? '') ?>
                                        </span>
                                    </div>

                                </div>

                                <?php endforeach; ?>

                                <?php else: ?>

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

                                    <i class="ri-folder-open-line text-secondary-light" style="font-size:70px;"></i>

                                    <h6 class="mt-3">
                                        No Records Available
                                    </h6>

                                    <p class="text-secondary-light">
                                        There is currently no information to display.
                                    </p>

                                </div>

                                <?php endif; ?>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- Leave Status End -->

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