<?php

$pageTitle = "Platoons Report";

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

require_login();

$user = current_user();

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

$totalPlatoons = $db->query("

SELECT COUNT(*)

FROM platoons

")->fetchColumn();

$totalMembers = $db->query("

SELECT COUNT(*)

FROM corps_members

WHERE platoon_id IS NOT NULL

")->fetchColumn();

/*
|--------------------------------------------------------------------------
| FETCH PLATOONS
|--------------------------------------------------------------------------
*/

$stmt = $db->query("

SELECT

p.*,

COUNT(cm.id) AS total_members

FROM platoons p

LEFT JOIN corps_members cm

ON cm.platoon_id = p.id

GROUP BY p.id

ORDER BY p.platoon_name

");

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

                    >

                    Platoons Report

                </p>

            </div>

            <div>

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

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

                    Export CSV

                </a>

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

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

                    Print Report

                </a>

            </div>

        </div>

        <div class="row gy-4 mb-24">

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

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Total Platoons

                        </h6>

                        <h2 class="fw-bold">

                            <?= $totalPlatoons ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>

                            Assigned Corps Members

                        </h6>

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

                            <?= $totalMembers ?>

                        </h2>

                    </div>

                </div>

            </div>

        </div>

        <div class="card">

            <div class="card-header">

                <h6>

                    Platoons Report

                </h6>

            </div>

            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>Platoon</th>

                            <th>Code</th>

                            <th>Description</th>

                            <th>Total Members</th>

                            <th>Status</th>

                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

$count=1;

foreach($platoons as $platoon):

?>

                        <tr>

                            <td>

                                <?= $count++ ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= htmlspecialchars($platoon['platoon_code']) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars($platoon['description']) ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?php

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

?>

                                <span class="badge bg-success">

                                    Active

                                </span>

                                <?php

}else{

?>

                                <span class="badge bg-danger">

                                    Inactive

                                </span>

                                <?php

}

?>

                            </td>

                            <td>

                                <div class="dropdown">

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

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

                                    </button>

                                    <ul class="dropdown-menu">

                                        <li>

                                            <button class="dropdown-item view-platoon-btn"
                                                data-name="<?= htmlspecialchars($platoon['platoon_name']) ?>"
                                                data-code="<?= htmlspecialchars($platoon['platoon_code']) ?>"
                                                data-description="<?= htmlspecialchars($platoon['description']) ?>"
                                                data-members="<?= $platoon['total_members'] ?>"
                                                data-status="<?= $platoon['status'] ?>" data-bs-toggle="modal"
                                                data-bs-target="#viewPlatoonModal">

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

                                                View

                                            </button>

                                        </li>

                                        <li>

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

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

                                                View Members

                                            </a>

                                        </li>

                                        <li>

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

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

                                                Print

                                            </a>

                                        </li>

                                    </ul>

                                </div>

                            </td>

                        </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

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

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

                <div class="modal-content">

                    <div class="modal-header">

                        <h5>

                            Platoon 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%">

                                    Platoon Name

                                </th>

                                <td id="viewPlatoonName"></td>

                            </tr>

                            <tr>

                                <th>

                                    Platoon Code

                                </th>

                                <td id="viewPlatoonCode"></td>

                            </tr>

                            <tr>

                                <th>

                                    Description

                                </th>

                                <td id="viewPlatoonDescription"></td>

                            </tr>

                            <tr>

                                <th>

                                    Members

                                </th>

                                <td id="viewPlatoonMembers"></td>

                            </tr>

                            <tr>

                                <th>

                                    Status

                                </th>

                                <td id="viewPlatoonStatus"></td>

                            </tr>

                        </table>

                    </div>

                </div>

            </div>

        </div>

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

        <script>
        $(document).on(

            'click',

            '.view-platoon-btn',

            function() {

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

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

                $('#viewPlatoonDescription').text($(this).data('description'));

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

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

                if (status == "Active") {

                    $('#viewPlatoonStatus').html(

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

                    );

                } else {

                    $('#viewPlatoonStatus').html(

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

                    );

                }

            });
        </script>