<?php
session_start();
include 'db.php';

if (!isset($_SESSION['user'])) {
    header("Location: admin.php");
    exit;
}

/* Pagination settings */
$limit = 10; // show 10 records per page
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $limit;

/* Total records */
$countQuery = mysqli_query($conn, "SELECT COUNT(*) AS total FROM application");
$countRow = mysqli_fetch_assoc($countQuery);
$totalRecords = $countRow['total'];
$totalPages = ceil($totalRecords / $limit);

/* Fetch paginated applications */
$query = "SELECT * FROM application ORDER BY id DESC LIMIT $limit OFFSET $offset";
$result = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Application List</title>

<link rel="icon" href="assets/img/logo.png" type="image/png">
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<style>
body {
    padding-top: 80px;
    font-family: "Noto Sans", sans-serif;
    background-color: #f8f9fa;
}
.table td, .table th {
    vertical-align: top;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 13px;
}
.nested-table {
    width: 100%;
    margin: 0;
    border-collapse: collapse;
    font-size: 12px;
}
.nested-table th, .nested-table td {
    border: 1px solid #dee2e6;
    padding: 4px;
    text-align: left;
}
.nested-table th {
    background-color: #f1f1f1;
}
.badge-missing {
    background-color: #dc3545 !important;
    color: #fff;
}
.table-responsive {
    overflow-x: auto;
}
</style>
</head>

<body class="container-fluid">

<?php include 'admheader.php'; ?>

<div class="container-fluid mt-4">
    <div class="card shadow-sm">
        <div class="card-header bg-dark text-white">
            <h5 class="mb-0">Application List</h5>
        </div>

        <div class="card-body table-responsive">
            <table class="table table-bordered table-striped table-hover align-middle w-100">
                <thead class="table-dark">
                    <tr>
                        <th style="width:3%;">#</th>
                        <th style="width:6%;">App No</th>
                        <th style="width:10%;">Name</th>
                        <th style="width:8%;">Mobile</th>
                        <th style="width:12%;">Email</th>
                        <th style="width:6%;">DOB</th>
                        <th style="width:5%;">Gender</th>
                        <th style="width:5%;">Category</th>
                        <th style="width:12%;">Address</th>
                        <th style="width:5%;">State</th>
                        <th style="width:5%;">Nationality</th>
                        <th style="width:25%;">Education</th>
                        <th style="width:7%;">Training Duration</th>
                        <th style="width:8%;">Training Period</th>
                        <th style="width:5%;">Mumbai Univ</th>
                        <th style="width:10%;">College Name</th>
                        <th style="width:8%;">HOD Name</th>
                        <th style="width:6%;">HOD Contact</th>
                        <th style="width:5%;">HOD Approval</th>
                        <th style="width:5%;">HOD Letter</th>
                        <th style="width:7%;">Applied On</th>
                        <th style="width:5%;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($result && mysqli_num_rows($result) > 0): ?>
                        <?php $i = $offset + 1; ?>
                        <?php while ($row = mysqli_fetch_assoc($result)): ?>
                            <tr>
                                <td><?= $i++; ?></td>
                                <td><?= htmlspecialchars($row['app_no']); ?></td>
                                <td><?= htmlspecialchars($row['name']); ?></td>
                                <td><?= htmlspecialchars($row['mobile']); ?></td>
                                <td><?= htmlspecialchars($row['email']); ?></td>
                                <td><?= htmlspecialchars($row['dob']); ?></td>
                                <td><?= htmlspecialchars($row['gender']); ?></td>
                                <td><?= htmlspecialchars($row['category']); ?></td>
                                <td><?= htmlspecialchars($row['address']); ?></td>
                                <td><?= htmlspecialchars($row['state']); ?></td>
                                <td><?= htmlspecialchars($row['nationality']); ?></td>

                                <!-- Nested Education Table -->
                                <td>
                                    <table class="nested-table">
                                        <thead>
                                            <tr>
                                                <th>Level</th>
                                                <th>Board/Degree/Course</th>
                                                <th>Year</th>
                                                <th>Subject</th>
                                                <th>Division</th>
                                                <th>Percentage</th>
                                                <th>Upload</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>12th</td>
                                                <td><?= htmlspecialchars($row['twelfth_board']); ?></td>
                                                <td><?= htmlspecialchars($row['twelfth_year']); ?></td>
                                                <td><?= htmlspecialchars($row['twelfth_subjects']); ?></td>
                                                <td><?= htmlspecialchars($row['twelfth_division']); ?></td>
                                                <td><?= htmlspecialchars($row['twelfth_percentage']); ?></td>
                                                <td>
                                                    <?php 
                                                    $twelfthFile = trim($row['twelfth_marksheet']);
                                                    if (!empty($twelfthFile) && file_exists('uploads/12th/' . $twelfthFile)):
                                                        $fileUrl = 'uploads/12th/' . rawurlencode($twelfthFile); ?>
                                                        <a href="<?= $fileUrl ?>" target="_blank">View</a>
                                                    <?php else: ?>
                                                        Not Uploaded
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Graduation</td>
                                                <td><?= htmlspecialchars($row['grad_degree']); ?> (<?= htmlspecialchars($row['grad_subject']); ?>)</td>
                                                <td><?= htmlspecialchars($row['grad_year']); ?></td>
                                                <td><?= htmlspecialchars($row['grad_subject']); ?></td>
                                                <td><?= htmlspecialchars($row['grad_division']); ?></td>
                                                <td><?= htmlspecialchars($row['grad_percentage']); ?></td>
                                                <td>
                                                    <?php 
                                                    $gradFile = trim($row['grad_marksheet']);
                                                    if (!empty($gradFile) && file_exists('uploads/graduation/' . $gradFile)):
                                                        $fileUrl = 'uploads/graduation/' . rawurlencode($gradFile); ?>
                                                        <a href="<?= $fileUrl ?>" target="_blank">View</a>
                                                    <?php else: ?>
                                                        Not Uploaded
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>PG</td>
                                                <td><?= htmlspecialchars($row['pg_course']); ?> (<?= htmlspecialchars($row['pg_subject']); ?>)</td>
                                                <td><?= htmlspecialchars($row['pg_year']); ?></td>
                                                <td><?= htmlspecialchars($row['pg_subject']); ?></td>
                                                <td><?= htmlspecialchars($row['pg_division']); ?></td>
                                                <td><?= htmlspecialchars($row['pg_percentage']); ?></td>
                                                <td>
                                                    <?php 
                                                    $pgFile = trim($row['pg_marksheet']);
                                                    if (!empty($pgFile) && file_exists('uploads/pg/' . $pgFile)):
                                                        $fileUrl = 'uploads/pg/' . rawurlencode($pgFile); ?>
                                                        <a href="<?= $fileUrl ?>" target="_blank">View</a>
                                                    <?php else: ?>
                                                        Not Uploaded
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>

                                <td><?= htmlspecialchars($row['training_duration']); ?></td>
                                <td>
                                    <?php
                                        $from = !empty($row['training_from']) ? htmlspecialchars($row['training_from']) : 'N/A';
                                        $to = !empty($row['training_to']) ? htmlspecialchars($row['training_to']) : 'N/A';
                                        echo $from . ' – ' . $to;
                                    ?>
                                </td>

                                <td>
                                    <span class="badge <?= $row['mumbai_univ'] === 'Yes' ? 'bg-success' : 'bg-secondary'; ?>">
                                        <?= $row['mumbai_univ']; ?>
                                    </span>
                                </td>
                                <td><?= htmlspecialchars($row['college_name']); ?></td>
                                <td><?= htmlspecialchars($row['hod_name']); ?></td>
                                <td><?= htmlspecialchars($row['hod_contact']); ?></td>
                                <td><?= htmlspecialchars($row['hod_approval']); ?></td>
                                <td>
                                    <?php 
                                    $hodFile = trim($row['recommendation_letter']);
                                    if(!empty($hodFile) && file_exists('uploads/recommendation/'.$hodFile)):
                                        $hodUrl = 'uploads/recommendation/'.rawurlencode($hodFile); ?>
                                        <a href="<?= $hodUrl ?>" target="_blank">View</a>
                                    <?php else: ?>
                                        <span class="badge badge-missing">Not Uploaded</span>
                                    <?php endif; ?>
                                </td>
                                <td><?= htmlspecialchars($row['created_at']); ?></td>
                                <td>
                                    <a href="applicationView.php?id=<?= $row['id']; ?>" class="btn btn-sm btn-primary">View</a>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="22" class="text-center">No applications found</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
        <div class="card-footer">
            <nav>
                <ul class="pagination justify-content-center mb-0">
                    <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a>
                    </li>

                    <?php for ($p = 1; $p <= $totalPages; $p++): ?>
                        <li class="page-item <?= ($page == $p) ? 'active' : '' ?>">
                            <a class="page-link" href="?page=<?= $p ?>"><?= $p ?></a>
                        </li>
                    <?php endfor; ?>

                    <li class="page-item <?= ($page >= $totalPages) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $page + 1 ?>">Next</a>
                    </li>
                </ul>
            </nav>
        </div>
        <?php endif; ?>

    </div>
</div>

<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>
