<?php

$pageTitle = "Pending Clearance";

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

require_login();

$user = current_user();

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

/*
|--------------------------------------------------------------------------
| DETERMINE USER ROLE
|--------------------------------------------------------------------------
*/

$isCorpsMember = false;

if (
    isset($user['role']) &&
    strtolower($user['role']) == 'corps member'
) {

    $isCorpsMember = true;

}

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

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

    $successMessage = $_SESSION['success_message'];

    unset($_SESSION['success_message']);
}

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

    $errorMessage = $_SESSION['error_message'];

    unset($_SESSION['error_message']);
}

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

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

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

    try {

         /*
        |--------------------------------------------------------------------------
        | SUBMIT MONTHLY CLEARANCE
        |--------------------------------------------------------------------------
        */

        if ($action == "submit_clearance") {

            /*
            |--------------------------------------------------------------------------
            | GET CORPS MEMBER
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("

                SELECT *

                FROM corps_members

                WHERE user_id=?

                LIMIT 1

            ");

            $stmt->execute([

                $user['id']

            ]);

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

            if (!$corpsMember) {

                throw new Exception(

                    "Unable to locate Corps Member profile."

                );

            }

            $corpsMemberId = $corpsMember['id'];

            $month = date('n');

            $year = date('Y');

            /*
            |--------------------------------------------------------------------------
            | DUPLICATE CHECK
            |--------------------------------------------------------------------------
            */

            $check = $db->prepare("

                SELECT id

                FROM monthly_clearance

                WHERE

                corps_member_id=?

                AND clearance_month=?

                AND clearance_year=?

                LIMIT 1

            ");

            $check->execute([

                $corpsMemberId,

                $month,

                $year

            ]);

            if ($check->fetch()) {

                throw new Exception(

                    "You have already submitted clearance for this month."

                );

            }

            /*
            |--------------------------------------------------------------------------
            | VERIFY CDS ATTENDANCE
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("

                SELECT COUNT(*)

                FROM cds_attendance

                WHERE

                corps_member_id=?

                AND attendance_status='Present'

                AND MONTH(attendance_date)=?

                AND YEAR(attendance_date)=?

            ");

            $stmt->execute([

                $corpsMemberId,

                $month,

                $year

            ]);

            $cdsVerified =

                $stmt->fetchColumn() > 0

                ? "Yes"

                : "No";

            /*
            |--------------------------------------------------------------------------
            | VERIFY CAMP ACTIVITY
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("

                SELECT COUNT(*)

                FROM camp_activity_attendance ca

                INNER JOIN camp_activities a

                ON a.id=ca.activity_id

                WHERE

                ca.corps_member_id=?

                AND ca.attendance_status='Present'

                AND MONTH(a.activity_date)=?

                AND YEAR(a.activity_date)=?

            ");

            $stmt->execute([

                $corpsMemberId,

                $month,

                $year

            ]);

            $activityVerified =

                $stmt->fetchColumn() > 0

                ? "Yes"

                : "No";

            /*
            |--------------------------------------------------------------------------
            | INSERT CLEARANCE
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("

                INSERT INTO monthly_clearance

                (

                    corps_member_id,

                    clearance_month,

                    clearance_year,

                    cds_attendance_verified,

                    camp_activity_verified,

                    biometric_verified,

                    submitted_date,

                    status

                )

                VALUES

                (

                    ?,?,?,?,?,?,NOW(),'Pending'

                )

            ");

            $stmt->execute([

                $corpsMemberId,

                $month,

                $year,

                $cdsVerified,

                $activityVerified,

                "No"

            ]);

            $_SESSION['success_message'] =

                "Monthly Clearance submitted successfully.";

            header("Location: pending-clearance.php");

            exit;

        }

        /*
        |--------------------------------------------------------------------------
        | APPROVE CLEARANCE
        |--------------------------------------------------------------------------
        */

        if ($action == "approve") {

            $clearanceId = (int)$_POST['clearance_id'];

            $remarks = trim($_POST['remarks']);

             /*
            |--------------------------------------------------------------------------
            | GET CAMP OFFICIAL ID
            |--------------------------------------------------------------------------
            */

            $official = $db->prepare("

                SELECT id

                FROM camp_officials

                WHERE user_id=?

                LIMIT 1

            ");

            $official->execute([

                $user['id']

            ]);

            $officialId = $official->fetchColumn();

            if (!$officialId) {

                throw new Exception(

                    "Camp Official record not found."

                );

            }

            $stmt = $db->prepare("

                UPDATE monthly_clearance

                SET

                    status='Approved',

                    approval_date=NOW(),

                    approved_by=?,

                    remarks=?

                WHERE id=?

            ");

            $stmt->execute([

                $user['id'],

                $remarks,

                $clearanceId

            ]);

            $_SESSION['success_message'] =
                "Monthly clearance approved successfully.";

            header("Location: pending-clearance.php");

            exit;

        }

        /*
        |--------------------------------------------------------------------------
        | REJECT CLEARANCE
        |--------------------------------------------------------------------------
        */

        if ($action == "reject") {

            $clearanceId = (int)$_POST['clearance_id'];

            $remarks = trim($_POST['remarks']);

            if (empty($remarks)) {

                throw new Exception(
                    "Please provide a reason for rejection."
                );

            }

            $official = $db->prepare("

                SELECT id

                FROM camp_officials

                WHERE user_id=?

                LIMIT 1

            ");

            $official->execute([

                $user['id']

            ]);

            $officialId = $official->fetchColumn();

            $stmt = $db->prepare("

                UPDATE monthly_clearance

                SET

                    status='Rejected',

                    approval_date=NOW(),

                    approved_by=?,

                    remarks=?

                WHERE id=?

            ");

            $stmt->execute([

                $officialId,

                $remarks,

                $clearanceId

            ]);

            $_SESSION['success_message'] =
                "Monthly clearance rejected successfully.";

            header("Location: pending-clearance.php");

            exit;

        }

    }

    catch(Throwable $e){

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

        header("Location: pending-clearance.php");

        exit;

    }

}

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

$totalPending = $db->query("

SELECT COUNT(*)

FROM monthly_clearance

WHERE status='Pending'

")->fetchColumn();

$totalApproved = $db->query("

SELECT COUNT(*)

FROM monthly_clearance

WHERE status='Approved'

")->fetchColumn();

$totalRejected = $db->query("

SELECT COUNT(*)

FROM monthly_clearance

WHERE status='Rejected'

")->fetchColumn();

$totalRequests = $db->query("

SELECT COUNT(*)

FROM monthly_clearance

")->fetchColumn();

/*
|--------------------------------------------------------------------------
| FETCH CURRENT CORPS MEMBER CLEARANCE
|--------------------------------------------------------------------------
*/

$currentClearance = null;

if ($isCorpsMember) {

    $stmt = $db->prepare("

        SELECT

        mc.*

        FROM monthly_clearance mc

        INNER JOIN corps_members cm

        ON cm.id=mc.corps_member_id

        WHERE cm.user_id=?

        ORDER BY

        clearance_year DESC,

        clearance_month DESC

        LIMIT 1

    ");

    $stmt->execute([

        $user['id']

    ]);

    $currentClearance =

        $stmt->fetch(PDO::FETCH_ASSOC);

}

/*
|--------------------------------------------------------------------------
| FETCH PENDING CLEARANCES
|--------------------------------------------------------------------------
*/    

$clearanceStmt = $db->query("

SELECT

mc.*,

cm.state_code,

cm.firstname,

cm.lastname,

cm.call_up_number,

cg.group_name,

CONCAT(co.firstname,' ',co.lastname) AS approved_by_name

FROM monthly_clearance mc

INNER JOIN corps_members cm

ON cm.id = mc.corps_member_id

LEFT JOIN cds_group_members cgm

ON cgm.corps_member_id = cm.id

LEFT JOIN cds_groups cg

ON cg.id = cgm.cds_group_id

LEFT JOIN camp_officials co

ON co.id = mc.approved_by

WHERE mc.status='Pending'

ORDER BY

mc.clearance_year DESC,

mc.clearance_month DESC,

cm.firstname

");

$clearances = $clearanceStmt->fetchAll(PDO::FETCH_ASSOC);

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

?>

<style>
.clearance-sidebar {

    position: fixed;

    top: 0;

    right: -520px;

    width: 500px;

    height: 100vh;

    background: #fff;

    overflow-y: auto;

    z-index: 9999;

    transition: .35s;

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

}

.clearance-sidebar.active {

    right: 0;

}

.overlay {

    position: fixed;

    top: 0;

    left: 0;

    width: 100%;

    height: 100%;

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

    display: none;

    z-index: 9998;

}

.overlay.active {

    display: block;

}
</style>

<div class="overlay"></div>

<main class="dashboard-main">

    <?php require_once '../includes/topbar.php'; ?>

    <div class="dashboard-main-body">

        <div class="breadcrumb d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">

            <div>

                <h6 class="fw-semibold mb-0">

                    <?= $pageTitle ?>

                </h6>

                <p class="text-neutral-500">

                    <?= APP_SHORT_NAME ?>

                    >

                    Monthly Clearance

                    >

                    Pending Clearance

                </p>

            </div>

        </div>

        <?php if($successMessage): ?>

        <div class="alert alert-success">

            <?= $successMessage ?>

        </div>

        <?php endif; ?>

        <?php if($errorMessage): ?>

        <div class="alert alert-danger">

            <?= $errorMessage ?>

        </div>

        <?php endif; ?>


        <?php if(!$isCorpsMember): ?>
        <div class="row gy-4">

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

                <div class="card">

                    <div class="card-body text-center">

                        <h6>Pending</h6>

                        <h2><?= $totalPending ?></h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body text-center">

                        <h6>Approved</h6>

                        <h2><?= $totalApproved ?></h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body text-center">

                        <h6>Rejected</h6>

                        <h2><?= $totalRejected ?></h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body text-center">

                        <h6>Total Requests</h6>

                        <h2><?= $totalRequests ?></h2>

                    </div>

                </div>

            </div>

        </div>
        <?php endif; ?>

        <div class="card mt-24">

            <!-- ===================================================== -->
            <!-- CORPS MEMBER VIEW                                     -->
            <!-- ===================================================== -->

            <?php if($isCorpsMember): ?>

            <div class="row">

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

                    <div class="card">

                        <div class="card-header">

                            <h5 class="mb-0">

                                Monthly Clearance

                            </h5>

                        </div>

                        <div class="card-body">

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

                            <div class="alert alert-info">

                                You have not submitted your Monthly Clearance
                                for

                                <strong>

                                    <?= date('F Y') ?>

                                </strong>

                            </div>

                            <table class="table table-bordered">

                                <tr>

                                    <th width="35%">

                                        Clearance Month

                                    </th>

                                    <td>

                                        <?= date('F') ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Clearance Year

                                    </th>

                                    <td>

                                        <?= date('Y') ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Status

                                    </th>

                                    <td>

                                        <span class="badge bg-warning">

                                            Not Submitted

                                        </span>

                                    </td>

                                </tr>

                            </table>

                            <button class="btn btn-primary" id="openSubmitSidebar">

                                <i class="ri-send-plane-line"></i>

                                Submit Monthly Clearance

                            </button>

                            <?php else: ?>

                            <table class="table table-bordered">

                                <tr>

                                    <th width="35%">

                                        Month

                                    </th>

                                    <td>

                                        <?= date(
                                        'F',
                                        mktime(
                                            0,
                                            0,
                                            0,
                                            $currentClearance['clearance_month'],
                                            1
                                        )
                                    ) ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Year

                                    </th>

                                    <td>

                                        <?= $currentClearance['clearance_year'] ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        CDS Attendance

                                    </th>

                                    <td>

                                        <?= $currentClearance['cds_attendance_verified'] ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Camp Activities

                                    </th>

                                    <td>

                                        <?= $currentClearance['camp_activity_verified'] ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Biometric

                                    </th>

                                    <td>

                                        <?= $currentClearance['biometric_verified'] ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Status

                                    </th>

                                    <td>

                                        <?php

                                    if($currentClearance['status']=="Pending"){

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

                                    }elseif($currentClearance['status']=="Approved"){

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

                                    }else{

                                        echo '<span class="badge bg-danger">Rejected</span>';

                                    }

                                    ?>

                                    </td>

                                </tr>

                            </table>

                            <?php endif; ?>

                        </div>

                    </div>

                </div>

            </div>

            <?php else: ?>

            <!-- ADMIN TABLE STARTS HERE -->
            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>State Code</th>

                            <th>Corps Member</th>

                            <th>CDS Group</th>

                            <th>Month</th>

                            <th>Year</th>

                            <th>CDS</th>

                            <th>Camp Activity</th>

                            <th>Biometric</th>

                            <th>Status</th>

                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php
                            $count = 1;
                            foreach($clearances as $clearance):
                        ?>

                        <tr>

                            <td>
                                <?= $count++ ?>
                            </td>

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

                            <td>
                                <?= htmlspecialchars($clearance['firstname'].' '.$clearance['lastname']) ?>
                            </td>

                            <td>
                                <?= htmlspecialchars($clearance['group_name'] ?? 'Not Assigned') ?>
                            </td>

                            <td>
                                <?= date('F', mktime(0,0,0,$clearance['clearance_month'],1)) ?>
                            </td>

                            <td>
                                <?= $clearance['clearance_year'] ?>
                            </td>

                            <td>

                                <?php if($clearance['cds_attendance_verified']=="Yes"): ?>

                                <span class="badge bg-success">
                                    Yes
                                </span>

                                <?php else: ?>

                                <span class="badge bg-danger">
                                    No
                                </span>

                                <?php endif; ?>

                            </td>

                            <td>

                                <?php if($clearance['camp_activity_verified']=="Yes"): ?>

                                <span class="badge bg-success">
                                    Yes
                                </span>

                                <?php else: ?>

                                <span class="badge bg-danger">
                                    No
                                </span>

                                <?php endif; ?>

                            </td>

                            <td>

                                <?php if($clearance['biometric_verified']=="Yes"): ?>

                                <span class="badge bg-success">
                                    Yes
                                </span>

                                <?php else: ?>

                                <span class="badge bg-danger">
                                    No
                                </span>

                                <?php endif; ?>

                            </td>

                            <td>
                                <?php
                                if($clearance['status']=="Pending"){                                    
                                ?>

                                <span class="badge bg-warning">

                                    Pending

                                </span>
                                <?php 
                                } 
                                elseif($clearance['status']=="Approved"){
                                ?>

                                <span class="badge bg-success">
                                    Approved
                                </span>

                                <?php 
                                } 
                                else{
                                ?>

                                <span class="badge bg-danger">
                                    Rejected
                                </span>
                                <?php 
                                }
                                ?>

                            </td>

                            <td>

                                <div class="dropdown">

                                    <button class="btn btn-light" data-bs-toggle="dropdown">

                                        <i class="ri-more-2-fill"></i>

                                    </button>

                                    <ul class="dropdown-menu">

                                        <li>

                                            <button class="dropdown-item view-clearance-btn"
                                                data-id="<?= $clearance['id'] ?>"
                                                data-state="<?= htmlspecialchars($clearance['state_code']) ?>"
                                                data-name="<?= htmlspecialchars($clearance['firstname'].' '.$clearance['lastname']) ?>"
                                                data-callup="<?= htmlspecialchars($clearance['call_up_number']) ?>"
                                                data-group="<?= htmlspecialchars($clearance['group_name'] ?? 'Not Assigned') ?>"
                                                data-month="<?= date('F',mktime(0,0,0,$clearance['clearance_month'],1)) ?>"
                                                data-year="<?= $clearance['clearance_year'] ?>"
                                                data-cds="<?= $clearance['cds_attendance_verified'] ?>"
                                                data-camp="<?= $clearance['camp_activity_verified'] ?>"
                                                data-biometric="<?= $clearance['biometric_verified'] ?>"
                                                data-submitted="<?= $clearance['submitted_date'] ?>"
                                                data-status="<?= $clearance['status'] ?>"
                                                data-remarks="<?= htmlspecialchars($clearance['remarks']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#viewClearanceModal">

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

                                                View

                                            </button>

                                        </li>

                                        <?php if($clearance['status']=="Pending"): ?>

                                        <li>

                                            <button class="dropdown-item approve-btn" data-id="<?= $clearance['id'] ?>"
                                                data-name="<?= htmlspecialchars($clearance['firstname'].' '.$clearance['lastname']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#approveModal">

                                                <i class="ri-checkbox-circle-line text-success"></i>

                                                Approve

                                            </button>

                                        </li>

                                        <li>

                                            <button class="dropdown-item reject-btn" data-id="<?= $clearance['id'] ?>"
                                                data-name="<?= htmlspecialchars($clearance['firstname'].' '.$clearance['lastname']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#rejectModal">

                                                <i class="ri-close-circle-line text-danger"></i>

                                                Reject

                                            </button>

                                        </li>

                                        <?php endif; ?>

                                        <li>

                                            <a href="print-clearance.php?id=<?= $clearance['id'] ?>"
                                                class="dropdown-item" target="_blank">

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

                                                Print

                                            </a>

                                        </li>

                                    </ul>

                                </div>

                            </td>

                        </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>
            <?php endif; ?>

        </div>

    </div>

    <!-- ======================================================= -->
    <!-- SUBMIT CLEARANCE SIDEBAR -->
    <!-- ======================================================= -->

    <div class="clearance-sidebar" id="submitSidebar">

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

            <h5 class="mb-0">

                Submit Monthly Clearance

            </h5>

            <button class="btn btn-light" id="closeSubmitSidebar">

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

            </button>

        </div>

        <form method="POST">

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

            <div class="p-24">

                <div class="alert alert-info">

                    The system will automatically verify your

                    <strong>

                        CDS Attendance

                    </strong>

                    and

                    <strong>

                        Camp Activity Attendance

                    </strong>

                    before submitting your clearance.

                </div>

                <table class="table table-bordered">

                    <tr>

                        <th width="40%">

                            Month

                        </th>

                        <td>

                            <?= date('F') ?>

                        </td>

                    </tr>

                    <tr>

                        <th>

                            Year

                        </th>

                        <td>

                            <?= date('Y') ?>

                        </td>

                    </tr>

                    <tr>

                        <th>

                            Biometric

                        </th>

                        <td>

                            Will be marked automatically

                        </td>

                    </tr>

                </table>

                <div class="mt-24">

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

                        Submit Clearance

                    </button>

                </div>

            </div>

        </form>

    </div>

    <!-- ================= VIEW CLEARANCE ================= -->

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

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

            <div class="modal-content">

                <div class="modal-header">

                    <h5 class="modal-title">

                        Monthly Clearance Details

                    </h5>

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

                    </button>

                </div>

                <div class="modal-body">

                    <table class="table table-bordered">

                        <tr>

                            <th width="35%">

                                Corps Member

                            </th>

                            <td id="viewMember"></td>

                        </tr>

                        <tr>

                            <th>

                                State Code

                            </th>

                            <td id="viewStateCode"></td>

                        </tr>

                        <tr>

                            <th>

                                Call-Up Number

                            </th>

                            <td id="viewCallUp"></td>

                        </tr>

                        <tr>

                            <th>

                                CDS Group

                            </th>

                            <td id="viewGroup"></td>

                        </tr>

                        <tr>

                            <th>

                                Clearance Month

                            </th>

                            <td id="viewMonth"></td>

                        </tr>

                        <tr>

                            <th>

                                Clearance Year

                            </th>

                            <td id="viewYear"></td>

                        </tr>

                        <tr>

                            <th>

                                CDS Attendance

                            </th>

                            <td id="viewCDS"></td>

                        </tr>

                        <tr>

                            <th>

                                Camp Activities

                            </th>

                            <td id="viewCamp"></td>

                        </tr>

                        <tr>

                            <th>

                                Biometric

                            </th>

                            <td id="viewBiometric"></td>

                        </tr>

                        <tr>

                            <th>

                                Submitted Date

                            </th>

                            <td id="viewSubmitted"></td>

                        </tr>

                        <tr>

                            <th>

                                Status

                            </th>

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

                        </tr>

                        <tr>

                            <th>

                                Remarks

                            </th>

                            <td id="viewRemarks"></td>

                        </tr>

                    </table>

                </div>

            </div>

        </div>

    </div>

    <!-- ================= APPROVE CLEARANCE ================= -->

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

        <div class="modal-dialog">

            <div class="modal-content">

                <form method="POST">

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

                    <input type="hidden" id="approveClearanceId" name="clearance_id">

                    <div class="modal-header">

                        <h5 class="modal-title">

                            Approve Monthly Clearance

                        </h5>

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

                        </button>

                    </div>

                    <div class="modal-body">

                        <div class="alert alert-success">

                            You are about to approve clearance for

                            <strong id="approveMemberName"></strong>

                        </div>

                        <div class="mb-20">

                            <label class="form-label">

                                Remarks

                            </label>

                            <textarea name="remarks" class="form-control" rows="4"
                                placeholder="Optional remarks..."></textarea>

                        </div>

                    </div>

                    <div class="modal-footer">

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

                            Cancel

                        </button>

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

                            Approve Clearance

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

    <!-- ================= REJECT CLEARANCE ================= -->

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

        <div class="modal-dialog">

            <div class="modal-content">

                <form method="POST">

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

                    <input type="hidden" id="rejectClearanceId" name="clearance_id">

                    <div class="modal-header">

                        <h5 class="modal-title">

                            Reject Monthly Clearance

                        </h5>

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

                        </button>

                    </div>

                    <div class="modal-body">

                        <div class="alert alert-danger">

                            You are about to reject clearance for

                            <strong id="rejectMemberName"></strong>

                        </div>

                        <div class="mb-20">

                            <label class="form-label">

                                Reason for Rejection

                            </label>

                            <textarea name="remarks" class="form-control" rows="4" required
                                placeholder="Reason is required..."></textarea>

                        </div>

                    </div>

                    <div class="modal-footer">

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

                            Cancel

                        </button>

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

                            Reject Clearance

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

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

    <script>
    /* ==========================================================
        SUBMIT SIDEBAR
        ========================================================== */

    $('#openSubmitSidebar').click(function() {

        $('#submitSidebar').addClass('active');

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

    });

    $('#closeSubmitSidebar').click(function() {

        $('#submitSidebar').removeClass('active');

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

    });

    /* ==========================================================
       APPROVE SIDEBAR
    ========================================================== */

    $(document).on('click', '.approve-btn', function() {

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

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

        $('#approveSidebar').addClass('active');

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

    });

    $('#closeApproveSidebar').click(function() {

        $('#approveSidebar').removeClass('active');

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

    });

    /* ==========================================================
       REJECT SIDEBAR
    ========================================================== */

    $(document).on('click', '.reject-btn', function() {

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

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

        $('#rejectSidebar').addClass('active');

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

    });

    $('#closeRejectSidebar').click(function() {

        $('#rejectSidebar').removeClass('active');

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

    });

    /* ==========================================================
       CLOSE EVERYTHING
    ========================================================== */

    $('.overlay').click(function() {

        $('#submitSidebar').removeClass('active');

        $('#approveSidebar').removeClass('active');

        $('#rejectSidebar').removeClass('active');

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

    });

    /* ==========================================================
       VIEW MODAL
    ========================================================== */

    $(document).on('click', '.view-clearance-btn', function() {

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

        $('#viewStateCode').text($(this).data('state'));

        $('#viewCallUp').text($(this).data('callup'));

        $('#viewGroup').text($(this).data('group'));

        $('#viewMonth').text($(this).data('month'));

        $('#viewYear').text($(this).data('year'));

        $('#viewCDS').text($(this).data('cds'));

        $('#viewCamp').text($(this).data('camp'));

        $('#viewBiometric').text($(this).data('biometric'));

        $('#viewSubmitted').text($(this).data('submitted'));

        $('#viewApproved').text($(this).data('approved'));

        $('#viewApprovedBy').text($(this).data('approvedby'));

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

        $('#viewRemarks').text($(this).data('remarks'));

    });
    </script>

    <script>
    $(document).on('click', '.view-clearance-btn', function() {

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

        $('#viewStateCode').text($(this).data('state'));

        $('#viewCallUp').text($(this).data('callup'));

        $('#viewGroup').text($(this).data('group'));

        $('#viewMonth').text($(this).data('month'));

        $('#viewYear').text($(this).data('year'));

        $('#viewCDS').text($(this).data('cds'));

        $('#viewCamp').text($(this).data('camp'));

        $('#viewBiometric').text($(this).data('biometric'));

        $('#viewSubmitted').text($(this).data('submitted'));

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

        $('#viewRemarks').text($(this).data('remarks'));

    });

    $(document).on('click', '.approve-btn', function() {

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

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

    });

    $(document).on('click', '.reject-btn', function() {

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

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

    });
    </script>