<?php
session_start();
include "../../db.php";
date_default_timezone_set('Asia/Kolkata');

if (!isset($_SESSION['uid']) || !isset($_SESSION['uref']) || !isset($_SESSION['utype'])) {
    header("Location: logout.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "includes/meta.php"; ?>
    <?php include "includes/css.php"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet">
    <link rel="stylesheet" href="vendor/croppie/croppie.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <style>
    .panel-heading {
        cursor: pointer;
    }

    .document-img {
        max-width: 150px;
        margin: 5px;
        text-align: center;
    }

    .document-img img {
        width: 100%;
    }

    .action-btn {
        margin-right: 5px;
    }

    .load-more-btn {
        margin: 20px auto;
        display: block;
    }
    </style>
</head>

<body>
    <?php include "includes/header2.php"; ?>

    <div class="container mt-5">
        <h1 class="h3 text-center" style="margin-top: 80px;">Online Applications</h1>
        <hr>

        <!-- Filter -->
        <div class="row mb-3">
            <div class="col-md-4 d-flex align-items-center">
                <label>Status: </label> &nbsp;
                <select id="statusFilter" class="form-control">
                    <option value="">All</option>
                    <option value="Pending">Pending</option>
                    <option value="Approved">Approved</option>
                    <option value="Rejected">Rejected</option>
                    <option value="Completed">Completed</option>
                </select>
            </div>
        </div>

        <div id="admissionAccordion">
            <!-- AJAX content will load here -->
        </div>

        <button id="loadMoreBtn" class="btn btn-info load-more-btn" data-offset="0">Load More</button>
    </div>

    <?php include "includes/ft.php"; ?>

    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <script>
    var limit = 10;

    function loadAdmissions(status = '', offset = 0, append = true) {
        $.ajax({
            url: 'fetch_admissions.php',
            type: 'GET',
            data: {
                status: status,
                offset: offset,
                limit: limit
            },
            success: function(data) {
                if (append) {
                    $('#admissionAccordion').append(data);
                } else {
                    $('#admissionAccordion').html(data);
                }

                // Hide Load More if no more data
                if (data.trim() == '' || data.trim() == '<p class="text-center">No records found</p>') {
                    $('#loadMoreBtn').hide();
                } else {
                    $('#loadMoreBtn').show();
                    $('#loadMoreBtn').data('offset', offset + limit);
                }

                // Ensure only one panel open at a time
                $('.panel-collapse').on('show.bs.collapse', function() {
                    $('.panel-collapse.in').collapse('hide');
                });

                // AJAX update for action status
                $('.updateStatus').off('click').on('click', function(e) {
                    e.preventDefault();
                    var id = $(this).data('id');
                    var status = $(this).data('status');

                    $.ajax({
                        url: 'update_admission_status.php',
                        type: 'POST',
                        data: {
                            id: id,
                            status: status
                        },
                        success: function(response) {
                            if (response.trim() == 'success') {
                                $('#statusText' + id).text(status);
                                alert('Status updated to ' + status);
                            } else {
                                alert('Error updating status');
                            }
                        }
                    });
                });
            }
        });
    }

    // Initial load
    loadAdmissions($('#statusFilter').val(), 0, false);

    // On filter change
    $('#statusFilter').change(function() {
        var status = $(this).val();
        $('#loadMoreBtn').data('offset', 0); // reset offset
        loadAdmissions(status, 0, false);
    });

    // Load More button
    $('#loadMoreBtn').click(function() {
        var status = $('#statusFilter').val();
        var offset = $(this).data('offset');
        loadAdmissions(status, offset, true);
    });
    </script>
</body>

</html>