<?php

$pageTitle = "Corps Members Report";

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

require_login();

$user = current_user();

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

$totalMembers = $db->query("

SELECT COUNT(*)

FROM corps_members

")->fetchColumn();

$maleMembers = $db->query("

SELECT COUNT(*)

FROM corps_members

WHERE gender='Male'

")->fetchColumn();

$femaleMembers = $db->query("

SELECT COUNT(*)

FROM corps_members

WHERE gender='Female'

")->fetchColumn();

$servingMembers = $db->query("

SELECT COUNT(*)

FROM corps_members

WHERE status='Serving'

")->fetchColumn();

/*
|--------------------------------------------------------------------------
| FETCH MEMBERS
|--------------------------------------------------------------------------
*/

$stmt = $db->query("

SELECT

cm.*,

p.platoon_name,

h.hostel_name,

cg.group_name

FROM corps_members cm

LEFT JOIN platoons p

ON p.id=cm.platoon_id

LEFT JOIN hostels h

ON h.id=cm.hostel_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

ORDER BY

cm.firstname,
cm.lastname

");

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

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

?>

<main class="dashboard-main">

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

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

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

            <div>

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

                    <?= $pageTitle ?>

                </h6>

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

                    <?= APP_SHORT_NAME ?>

                    >

                    Reports

                    >

                    Corps Members Report

                </p>

            </div>

            <div>

                <a href="export-corps-members.php" class="btn btn-success">

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

                    Export CSV

                </a>

                <a href="print-corps-members.php" target="_blank" class="btn btn-primary">

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

                    Print Report

                </a>

            </div>

        </div>

        <div class="row gy-4">
            <div class="col-lg-3">

                <div class="card">

                    <div class="card-body">

                        <h6>Total Members</h6>

                        <h2>

                            <?= $totalMembers ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>Male</h6>

                        <h2 class="text-primary">

                            <?= $maleMembers ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>Female</h6>

                        <h2 class="text-danger">

                            <?= $femaleMembers ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>Serving</h6>

                        <h2 class="text-success">

                            <?= $servingMembers ?>

                        </h2>

                    </div>

                </div>

            </div>

        </div>

        <div class="card mt-24">

            <div class="card-header">

                <h6>

                    Corps Members Report

                </h6>

            </div>

            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>Passport</th>

                            <th>State Code</th>

                            <th>Name</th>

                            <th>Gender</th>

                            <th>Platoon</th>

                            <th>Hostel</th>

                            <th>CDS</th>

                            <th>Status</th>

                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

$count=1;

foreach($members as $member):

?>

                        <tr>

                            <td>

                                <?= $count++ ?>

                            </td>

                            <td>

                                <?php

        if(!empty($member['passport']) && file_exists("../uploads/passports/".$member['passport'])){

        ?>

                                <img src="../uploads/passports/<?= htmlspecialchars($member['passport']) ?>" width="45"
                                    height="45" class="rounded-circle">

                                <?php

        }else{

        ?>

                                <img src="../assets/images/avatar.png" width="45" height="45" class="rounded-circle">

                                <?php

        }

        ?>

                            </td>

                            <td>

                                <?= htmlspecialchars($member['state_code']) ?>

                            </td>

                            <td>

                                <strong>

                                    <?= htmlspecialchars(

                $member['firstname']." ".$member['lastname']

            ) ?>

                                </strong>

                            </td>

                            <td>

                                <?= htmlspecialchars($member['gender']) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars($member['platoon_name'] ?? '-') ?>

                            </td>

                            <td>

                                <?= htmlspecialchars($member['hostel_name'] ?? '-') ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?php

        switch($member['status']){

            case "Serving":

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

                break;

            case "Completed":

                echo '<span class="badge bg-primary">Completed</span>';

                break;

            case "Suspended":

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

                break;

            default:

                echo '<span class="badge bg-danger">'.$member['status'].'</span>';

        }

        ?>

                            </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-member-btn" data-id="<?= $member['id'] ?>"
                                                data-name="<?= htmlspecialchars($member['firstname'].' '.$member['lastname']) ?>"
                                                data-state="<?= htmlspecialchars($member['state_code']) ?>"
                                                data-callup="<?= htmlspecialchars($member['call_up_number']) ?>"
                                                data-gender="<?= htmlspecialchars($member['gender']) ?>"
                                                data-phone="<?= htmlspecialchars($member['phone']) ?>"
                                                data-email="<?= htmlspecialchars($member['email']) ?>"
                                                data-platoon="<?= htmlspecialchars($member['platoon_name']) ?>"
                                                data-hostel="<?= htmlspecialchars($member['hostel_name']) ?>"
                                                data-cds="<?= htmlspecialchars($member['group_name']) ?>"
                                                data-status="<?= htmlspecialchars($member['status']) ?>"
                                                data-passport="<?= htmlspecialchars($member['passport']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#viewMemberModal">

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

                                                View

                                            </button>

                                        </li>

                                        <li>

                                            <a href="view-corps-member.php?id=<?= $member['id'] ?>"
                                                class="dropdown-item">

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

                                                Open Profile

                                            </a>

                                        </li>

                                        <li>

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

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

                                                Print Profile

                                            </a>

                                        </li>

                                    </ul>

                                </div>

                            </td>

                        </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

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

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

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

                <div class="modal-content">

                    <div class="modal-header">

                        <h5 class="modal-title">

                            Corps Member Details

                        </h5>

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

                        </button>

                    </div>

                    <div class="modal-body">

                        <div class="row">

                            <!-- Profile -->

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

                                <div class="text-center">

                                    <img id="viewPassport" src="../assets/images/avatar.png"
                                        class="rounded-circle border"
                                        style="width:170px;height:170px;object-fit:cover;">

                                    <h4 class="mt-3 mb-1" id="viewName">

                                    </h4>

                                    <span class="badge bg-success" id="viewStatus">

                                    </span>

                                </div>

                            </div>

                            <!-- Details -->

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

                                <table class="table table-bordered">

                                    <tr>

                                        <th width="35%">

                                            State Code

                                        </th>

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

                                    </tr>

                                    <tr>

                                        <th>

                                            Call-Up Number

                                        </th>

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

                                    </tr>

                                    <tr>

                                        <th>

                                            Gender

                                        </th>

                                        <td id="viewGender"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Phone Number

                                        </th>

                                        <td id="viewPhone"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Email Address

                                        </th>

                                        <td id="viewEmail"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Platoon

                                        </th>

                                        <td id="viewPlatoon"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Hostel

                                        </th>

                                        <td id="viewHostel"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            CDS Group

                                        </th>

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

                                    </tr>

                                </table>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

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

        <script>
        $(document).on(

            'click',

            '.view-member-btn',

            function() {

                $('#viewName').text(

                    $(this).data('name')

                );

                $('#viewStateCode').text(

                    $(this).data('state')

                );

                $('#viewCallUp').text(

                    $(this).data('callup')

                );

                $('#viewGender').text(

                    $(this).data('gender')

                );

                $('#viewPhone').text(

                    $(this).data('phone')

                );

                $('#viewEmail').text(

                    $(this).data('email')

                );

                $('#viewPlatoon').text(

                    $(this).data('platoon')

                );

                $('#viewHostel').text(

                    $(this).data('hostel')

                );

                $('#viewCDS').text(

                    $(this).data('cds')

                );

                $('#viewStatus')

                    .removeClass()

                    .addClass('badge');

                if ($(this).data('status') == 'Serving') {

                    $('#viewStatus')

                        .addClass('bg-success');

                } else if ($(this).data('status') == 'Completed') {

                    $('#viewStatus')

                        .addClass('bg-primary');

                } else if ($(this).data('status') == 'Suspended') {

                    $('#viewStatus')

                        .addClass('bg-warning');

                } else {

                    $('#viewStatus')

                        .addClass('bg-danger');

                }

                $('#viewStatus').text(

                    $(this).data('status')

                );

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

                if (passport != "") {

                    $('#viewPassport').attr(

                        'src',

                        '../uploads/passports/' + passport

                    );

                } else {

                    $('#viewPassport').attr(

                        'src',

                        '../assets/images/avatar.png'

                    );

                }

            });
        </script>