<?php
session_start();
include "../../db.php";

$statusFilter = isset($_GET['status']) ? $_GET['status'] : '';
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 10;

$sql = "SELECT * FROM student_admissions WHERE admin_id='".$_SESSION['uid']."'";
if($statusFilter != '') {
    $sql .= " AND admission_status='$statusFilter'";
}
$sql .= " ORDER BY id DESC LIMIT $offset, $limit";

$run = mysqli_query($con, $sql);
$statuses = ['Pending', 'Approved', 'Rejected', 'Completed'];
$c = $offset+1;

if(mysqli_num_rows($run)==0){
    echo '<p class="text-center">No records found</p>';
    exit;
}

while($rows = mysqli_fetch_assoc($run)) {
    $panelId = "collapseStudent".$rows['id'];
    $currentStatus = isset($rows['admission_status']) ? $rows['admission_status'] : 'Pending';
    ?>
<div class="panel panel-default mb-3">
    <div class="panel-heading">
        <div class="row">
            <div class="col-md-8">
                <strong>Student Name:</strong> <?php echo $rows['stu_name']; ?><br>
                <strong>Class:</strong> <?php echo $rows['admission_class']; ?> |
                Status: <strong id="statusText<?php echo $rows['id']; ?>"><?php echo $currentStatus; ?></strong> <br>
                <strong>Guardian Phone:</strong> <?php echo $rows['guardian_phone']; ?>
            </div>
            <div class="col-md-4 text-right d-flex align-items-center">
                <!-- Status select -->
                <select class="form-control input-sm updateStatusSelect mr-2" data-id="<?php echo $rows['id']; ?>">
                    <?php foreach ($statuses as $st): ?>
                    <option value="<?php echo $st; ?>" <?php echo ($rows['admission_status']==$st)?'selected':''; ?>>
                        <?php echo $st; ?>
                    </option>
                    <?php endforeach; ?>
                </select>
                <!-- Collapse button -->
                <button class="btn btn-info btn-sm viewDetailsBtn" type="button" data-target="#<?php echo $panelId; ?>">
                    View Details
                </button>

            </div>
        </div>
    </div>

    <div id="<?php echo $panelId; ?>" class="panel-collapse collapse">
        <div class="panel-body">
            <div class="row">
                <!-- Student Information -->
                <h5 class="col-12 mb-3">Student Information</h5>
                <div class="col-lg-6">
                    <div><strong>Name:</strong> <?php echo $rows['stu_name']; ?></div>
                    <div><strong>DOB:</strong> <?php echo $rows['stu_dob']; ?></div>
                    <div><strong>Gender:</strong> <?php echo $rows['stu_gender']; ?></div>
                    <div><strong>Blood Group:</strong> <?php echo $rows['stu_blood']; ?></div>
                    <div><strong>Nationality:</strong> <?php echo $rows['stu_nationality']; ?></div>
                    <div><strong>Religion:</strong> <?php echo $rows['stu_religion']; ?></div>
                </div>
                <div class="col-lg-6">
                    <div><strong>Aadhar Number:</strong> <?php echo $rows['stu_aadhar']; ?></div>
                    <?php if(!empty($rows['stu_photo'])): ?>
                    <div class="mt-2"><strong>Photo:</strong><br>
                        <img src="../upload/admission/<?php echo $rows['stu_photo']; ?>" class="img-thumbnail"
                            style="max-width:150px;">
                    </div>
                    <?php endif; ?>
                </div>

                <!-- Guardian Information -->
                <h5 class="col-12 mt-4 mb-3">Guardian Information</h5>
                <div class="col-lg-6">
                    <div><strong>Father:</strong>
                        <?php echo $rows['father_name'] . " (" . $rows['father_occupation'] . ")"; ?></div>
                    <div><strong>Mother:</strong>
                        <?php echo $rows['mother_name'] . " (" . $rows['mother_occupation'] . ")"; ?></div>
                </div>
                <div class="col-lg-6">
                    <div><strong>Guardian Phone:</strong> <?php echo $rows['guardian_phone']; ?></div>
                    <div><strong>Guardian Email:</strong> <?php echo $rows['guardian_email']; ?></div>
                </div>

                <!-- Address Information -->
                <h5 class="col-12 mt-4 mb-3">Address Information</h5>
                <div class="col-lg-12">
                    <div><strong>Permanent Address:</strong> <?php echo $rows['permanent_address']; ?></div>
                    <div><strong>Present Address:</strong> <?php echo $rows['present_address']; ?></div>
                </div>
                <div class="col-lg-4"><strong>District:</strong> <?php echo $rows['district']; ?></div>
                <div class="col-lg-4"><strong>State:</strong> <?php echo $rows['state']; ?></div>
                <div class="col-lg-4"><strong>Pincode:</strong> <?php echo $rows['pincode']; ?></div>
                <div class="col-lg-12 mt-2"><strong>Emergency Contact:</strong>
                    <?php echo $rows['emergency_contact']; ?></div>

                <!-- Admission Details -->
                <h5 class="col-12 mt-4 mb-3">Admission Details</h5>
                <div class="col-lg-6"><strong>Class:</strong> <?php echo $rows['admission_class']; ?></div>
                <div class="col-lg-6"><strong>Previous School:</strong> <?php echo $rows['previous_school']; ?></div>
                <div class="col-lg-6 mt-2"><strong>Hostel:</strong> <?php echo $rows['hostel']; ?></div>

                <!-- Medical Information -->
                <h5 class="col-12 mt-4 mb-3">Medical Information</h5>
                <div class="col-lg-6"><strong>Medical Condition:</strong> <?php echo $rows['medical_condition']; ?>
                    <?php if(!empty($rows['medical_condition_details'])) echo "(".$rows['medical_condition_details'].")"; ?>
                </div>
                <div class="col-lg-6"><strong>Allergies:</strong> <?php echo $rows['allergies']; ?></div>

                <!-- Documents -->
                <h5 class="col-12 mt-4 mb-3">Submitted Documents</h5>
                <div class="col-12 d-flex flex-wrap">
                    <?php
                    $docs = ['birth_certificate'=>'Birth Certificate','transfer_certificate'=>'Transfer Certificate','guardian_id'=>'Guardian ID','report_card'=>'Report Card','student_aadhar'=>'Student Aadhar','signature'=>'Signature','stu_photo'=>'Photo'];
                    foreach($docs as $field=>$label){
                        if(!empty($rows[$field])){
                            echo "<div class='document-img m-2 text-center'><img src='../upload/admission/{$rows[$field]}' class='img-thumbnail' style='max-width:150px;'><br>$label</div>";
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php $c++; } ?>


<script>
$(document).ready(function() {
    $('.viewDetailsBtn').click(function() {
        var target = $(this).data('target');
        var $panel = $(target);

        if ($panel.hasClass('in')) {
            // Close if already open
            $panel.collapse('hide');
        } else {
            // Close other panels
            $('.panel-collapse.in').collapse('hide');
            // Open this panel
            $panel.collapse('show');
        }
    });

    // Update status via select
    $('.updateStatusSelect').change(function() {
        var id = $(this).data('id');
        var status = $(this).val();

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