<?php

$pageTitle = "Hostels Report";

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

require_login();

$user = current_user();

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

$totalHostels = $db->query("

SELECT COUNT(*)

FROM hostels

")->fetchColumn();

$totalCapacity = $db->query("

SELECT IFNULL(SUM(capacity),0)

FROM hostels

")->fetchColumn();

$totalOccupants = $db->query("

SELECT COUNT(*)

FROM corps_members

WHERE hostel_id IS NOT NULL

")->fetchColumn();

$totalAvailable =

$totalCapacity - $totalOccupants;

/*
|--------------------------------------------------------------------------
| FETCH HOSTELS
|--------------------------------------------------------------------------
*/

$stmt = $db->query("

SELECT

h.*,

COUNT(cm.id) AS occupants

FROM hostels h

LEFT JOIN corps_members cm

ON cm.hostel_id=h.id

GROUP BY h.id

ORDER BY hostel_name

");

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

                    >

                    Hostels Report

                </p>

            </div>

            <div>

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

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

                    Export CSV

                </a>

                <a href="print-hostels.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-3">

                <div class="card">

                    <div class="card-body">

                        <h6>Total Hostels</h6>

                        <h2>

                            <?= $totalHostels ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>Total Capacity</h6>

                        <h2 class="text-primary">

                            <?= $totalCapacity ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>Occupants</h6>

                        <h2 class="text-success">

                            <?= $totalOccupants ?>

                        </h2>

                    </div>

                </div>

            </div>

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

                <div class="card">

                    <div class="card-body">

                        <h6>Available Spaces</h6>

                        <h2 class="text-warning">

                            <?= $totalAvailable ?>

                        </h2>

                    </div>

                </div>

            </div>

        </div>

        <div class="card">

            <div class="card-header">

                <h6>

                    Hostels Report

                </h6>

            </div>

            <div class="card-body">

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

                    <thead>

                        <tr>

                            <th>#</th>

                            <th>Hostel</th>

                            <th>Code</th>

                            <th>Gender</th>

                            <th>Capacity</th>

                            <th>Occupants</th>

                            <th>Available</th>

                            <th>Status</th>

                            <th>Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

$count=1;

foreach($hostels as $hostel):

$available=

$hostel['capacity']

-

$hostel['occupants'];

?>

                        <tr>

                            <td>

                                <?= $count++ ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= htmlspecialchars($hostel['hostel_code']) ?>

                            </td>

                            <td>

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

                            </td>

                            <td>

                                <?= $hostel['capacity'] ?>

                            </td>

                            <td>

                                <?= $hostel['occupants'] ?>

                            </td>

                            <td>

                                <?= $available ?>

                            </td>

                            <td>

                                <?php

if($hostel['status']=="Available"){

?>

                                <span class="badge bg-success">

                                    Available

                                </span>

                                <?php

}
elseif($hostel['status']=="Full"){

?>

                                <span class="badge bg-warning">

                                    Full

                                </span>

                                <?php

}
else{

?>

                                <span class="badge bg-danger">

                                    Maintenance

                                </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-hostel-report-btn"
                                                data-name="<?= htmlspecialchars($hostel['hostel_name']) ?>"
                                                data-code="<?= htmlspecialchars($hostel['hostel_code']) ?>"
                                                data-gender="<?= htmlspecialchars($hostel['gender']) ?>"
                                                data-capacity="<?= $hostel['capacity'] ?>"
                                                data-occupants="<?= $hostel['occupants'] ?>"
                                                data-available="<?= $available ?>"
                                                data-status="<?= $hostel['status'] ?>"
                                                data-description="<?= htmlspecialchars($hostel['description']) ?>"
                                                data-bs-toggle="modal" data-bs-target="#viewHostelReportModal">

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

                                                View

                                            </button>

                                        </li>

                                        <li>

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

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

                                                View Occupants

                                            </a>

                                        </li>

                                        <li>

                                            <a href="print-hostel.php?id=<?= $hostel['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>

        <!-- ===================================================== -->
        <!-- VIEW HOSTEL REPORT MODAL -->
        <!-- ===================================================== -->

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

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

                <div class="modal-content">

                    <div class="modal-header">

                        <h5 class="modal-title">

                            Hostel Report Details

                        </h5>

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

                        </button>

                    </div>

                    <div class="modal-body">

                        <div class="row">

                            <div class="col-md-12">

                                <table class="table table-bordered">

                                    <tr>

                                        <th width="35%">

                                            Hostel Name

                                        </th>

                                        <td id="viewHostelName"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Hostel Code

                                        </th>

                                        <td id="viewHostelCode"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Gender

                                        </th>

                                        <td id="viewHostelGender"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Capacity

                                        </th>

                                        <td id="viewHostelCapacity"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Occupants

                                        </th>

                                        <td id="viewHostelOccupants"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Available Spaces

                                        </th>

                                        <td id="viewHostelAvailable"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Occupancy Rate

                                        </th>

                                        <td id="viewOccupancyRate"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Status

                                        </th>

                                        <td id="viewHostelStatus"></td>

                                    </tr>

                                    <tr>

                                        <th>

                                            Description

                                        </th>

                                        <td id="viewHostelDescription"></td>

                                    </tr>

                                </table>

                            </div>

                        </div>

                    </div>

                    <div class="modal-footer">

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

                            Close

                        </button>

                    </div>

                </div>

            </div>

        </div>

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

        <script>
        $(document).on(

            'click',

            '.view-hostel-report-btn',

            function() {

                $('#viewHostelName').text(

                    $(this).data('name')

                );

                $('#viewHostelCode').text(

                    $(this).data('code')

                );

                $('#viewHostelGender').text(

                    $(this).data('gender')

                );

                $('#viewHostelCapacity').text(

                    $(this).data('capacity')

                );

                $('#viewHostelOccupants').text(

                    $(this).data('occupants')

                );

                $('#viewHostelAvailable').text(

                    $(this).data('available')

                );

                $('#viewHostelDescription').text(

                    $(this).data('description')

                );

                let capacity = parseInt($(this).data('capacity'));

                let occupants = parseInt($(this).data('occupants'));

                let percentage = 0;

                if (capacity > 0) {

                    percentage = ((occupants / capacity) * 100).toFixed(1);

                }

                $('#viewOccupancyRate').html(

                    percentage + '%'

                );

                let badge = '';

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

                if (status == 'Available') {

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

                } else if (status == 'Full') {

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

                } else {

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

                }

                $('#viewHostelStatus').html(

                    badge

                );

            });
        </script>