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

/* ===============================
   CHECK LOGIN
================================ */
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$applicant_id = $_SESSION['user_id'];

/* ===============================
   FETCH USER APPLICATION
================================ */
$stmt = $conn->prepare("
    SELECT * FROM application
    WHERE applicant_id = ?
    ORDER BY id DESC
    LIMIT 1
");
$stmt->bind_param("i", $applicant_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("No application found for your account.");
}

$app = $result->fetch_assoc();
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Summer Training Programme – 2026</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 { font-family: "Noto Sans", sans-serif; margin: 20px; background: #f8f9fa; }
h2, h3 { text-align: center; margin-bottom: 20px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #444; padding: 8px; vertical-align: top; }
th { background: #f2f2f2; width: 30%; }
img { max-width: 150px; }
.print-btn { margin: 20px auto; display: block; padding: 10px 20px; font-size: 16px; cursor: pointer; }
@media print { .print-btn { display: none; } }
</style>

<script>
function printPage() {
    window.print();
}
</script>
</head>
<body>

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

<h2>Winter Training – 2026-2027</h2>
<h3>Application Form</h3>

<p style="text-align:center;"><strong>Application No: <?= htmlspecialchars($app['app_no'] ?? '-') ?></strong></p>

<table class="table table-bordered">

<tr><th>Mumbai University</th><td><?= htmlspecialchars($app['mumbai_univ']) ?></td></tr>
<tr><th>Training Duration</th><td><?= htmlspecialchars($app['training_duration']) ?></td></tr>

<tr><th>Name</th><td><?= htmlspecialchars($app['name']) ?></td></tr>
<tr><th>Guardian Name</th><td><?= htmlspecialchars($app['guardian_name']) ?></td></tr>
<tr><th>Photograph</th>
    <td>
        <?php if (!empty($app['photo'])): ?>
            <img src="uploads/photo/<?= htmlspecialchars($app['photo']) ?>" alt="Photo">
        <?php else: ?>Not Uploaded<?php endif; ?>
    </td>
</tr>
<tr><th>Address</th><td><?= nl2br(htmlspecialchars($app['address'])) ?></td></tr>
<tr><th>State/UT</th><td><?= htmlspecialchars($app['state']) ?></td></tr>
<tr><th>Mobile</th><td><?= htmlspecialchars($app['mobile']) ?></td></tr>
<tr><th>Email</th><td><?= htmlspecialchars($app['email']) ?></td></tr>
<tr><th>DOB</th><td><?= htmlspecialchars($app['dob']) ?></td></tr>
<tr><th>Gender</th><td><?= htmlspecialchars($app['gender']) ?></td></tr>
<tr><th>Category</th><td><?= htmlspecialchars($app['category']) ?></td></tr>
<tr><th>Nationality</th><td><?= htmlspecialchars($app['nationality']) ?></td></tr>

<tr><th colspan="2">Academic Record – 12th</th></tr>
<tr><th>Board</th><td><?= htmlspecialchars($app['twelfth_board']) ?></td></tr>
<tr><th>Year</th><td><?= htmlspecialchars($app['twelfth_year']) ?></td></tr>
<tr><th>Subjects</th><td><?= nl2br(htmlspecialchars($app['twelfth_subjects'])) ?></td></tr>
<tr><th>Division</th><td><?= htmlspecialchars($app['twelfth_division']) ?></td></tr>
<tr><th>Percentage</th><td><?= htmlspecialchars($app['twelfth_percentage']) ?></td></tr>
<tr><th>12th Marksheet</th>
    <td>
        <?php if (!empty($app['twelfth_marksheet'])): ?>
            <a href="uploads/12th/<?= htmlspecialchars($app['twelfth_marksheet']) ?>" target="_blank">View</a>
        <?php else: ?>Not Uploaded<?php endif; ?>
    </td>
</tr>

<tr><th colspan="2">Graduation</th></tr>
<tr><th>Degree</th><td><?= htmlspecialchars($app['grad_degree']) ?></td></tr>
<tr><th>Subject</th><td><?= htmlspecialchars($app['grad_subject']) ?></td></tr>
<tr><th>University</th><td><?= htmlspecialchars($app['grad_university']) ?></td></tr>
<tr><th>Year</th><td><?= htmlspecialchars($app['grad_year']) ?></td></tr>
<tr><th>Division</th><td><?= htmlspecialchars($app['grad_division']) ?></td></tr>
<tr><th>Percentage</th><td><?= htmlspecialchars($app['grad_percentage']) ?></td></tr>
<tr><th>Graduation Degree File</th>
    <td>
        <?php if (!empty($app['grad_marksheet'])): ?>
            <a href="uploads/graduation/<?= htmlspecialchars($app['grad_marksheet']) ?>" target="_blank">View</a>
        <?php else: ?>Not Uploaded<?php endif; ?>
    </td>
</tr>

<tr><th colspan="2">Post Graduation</th></tr>
<tr><th>Course</th><td><?= htmlspecialchars($app['pg_course']) ?></td></tr>
<tr><th>Year</th><td><?= htmlspecialchars($app['pg_year']) ?></td></tr>
<tr><th>Subject</th><td><?= htmlspecialchars($app['pg_subject']) ?></td></tr>
<tr><th>University</th><td><?= htmlspecialchars($app['pg_university']) ?></td></tr>
<tr><th>Division</th><td><?= htmlspecialchars($app['pg_division']) ?></td></tr>
<tr><th>Percentage</th><td><?= htmlspecialchars($app['pg_percentage']) ?></td></tr>
<tr><th>PG Marksheet</th>
    <td>
        <?php if (!empty($app['pg_marksheet'])): ?>
            <a href="uploads/pg/<?= htmlspecialchars($app['pg_marksheet']) ?>" target="_blank">View</a>
        <?php else: ?>Not Uploaded<?php endif; ?>
    </td>
</tr>

<tr><th>College Name</th><td><?= htmlspecialchars($app['college_name']) ?></td></tr>
<tr><th>College Address</th><td><?= nl2br(htmlspecialchars($app['college_address'])) ?></td></tr>
<tr><th>HOD Name</th><td><?= htmlspecialchars($app['hod_name']) ?></td></tr>
<tr><th>HOD Contact</th><td><?= htmlspecialchars($app['hod_contact']) ?></td></tr>
<tr><th>Training From</th><td><?= htmlspecialchars($app['training_from']) ?></td></tr>
<tr><th>Training To</th><td><?= htmlspecialchars($app['training_to']) ?></td></tr>
<tr><th>COVID Status</th><td><?= htmlspecialchars($app['covid_status']) ?></td></tr>
<tr><th>Forwarded by HOD</th><td><?= htmlspecialchars($app['hod_approval']) ?></td></tr>
<tr><th>Recommendation Letter</th>
    <td>
        <?php if (!empty($app['recommendation_letter'])): ?>
            <a href="uploads/recommendation/<?= htmlspecialchars($app['recommendation_letter']) ?>" target="_blank">View</a>
        <?php else: ?>Not Uploaded<?php endif; ?>
    </td>
</tr>

</table>

<button class="print-btn btn btn-primary" onclick="printPage()">🖨️ Print Application</button>

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