<?php
// Start session
// session_start();

// Include database connection
include("inc/db.php");

// Ensure the session variable 'sname' is set
if (!isset($_SESSION["sname"])) {
    die("Session 'sname' is not set. Please log in to access this page.");
}

$sname = $_SESSION["sname"];

// Prepare the SQL query with a parameterized statement to prevent SQL injection
$query = $db->prepare("SELECT 
    sname, othernames, nationality, state_of_origin, lga, dob, gender, 
    religion, phoneno, email, addresss, department, school, sessionse, 
    passport, mode, health_disability, disability_details 
    FROM bio_data WHERE sname = ?");
$query->bind_param("s", $sname);

// Execute the query
$query->execute();
$result = $query->get_result();

// Check if a record is found
if ($result->num_rows === 1) {
    $row = $result->fetch_assoc(); // Fetch the row as an associative array
} else {
    die("No record found for the provided session name.");
}
// Function to determine file type and return HTML accordingly
function renderFile($filePath) {
    $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));

    // Check file type
    if (in_array($fileExtension, ['jpg', 'jpeg', 'png', 'gif'])) {
        // Render as image
        return "<img src='uploads/$filePath' alt='File' style='max-width: 100px; max-height: 100px;'>";
    } elseif (in_array($fileExtension, ['pdf'])) {
        // Render as PDF link
        return "<a href='uploads/$filePath' target='_blank'>View PDF</a>";
    } elseif (in_array($fileExtension, ['doc', 'docx'])) {
        // Render as Document link
        return "<a href='uploads/$filePath' target='_blank'>Download Document</a>";
    } else {
        // Unsupported file type
        return "Unsupported file type: $fileExtension";
    }
}
?>

<!-- HTML Section -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Bio-data</title>
    <link rel="stylesheet" href="styles.css"> <!-- Add your CSS file -->
</head>
<body>
    <div class="container fullname-section">
        <h4 align="center">Student Bio-data</h4>
        <hr>
        <div class="row">
            <div class="col-md-6">
                <h6>Full Name:</h6>
                <p><?php echo htmlspecialchars($row['sname'] . ' ' . $row['othernames']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Nationality:</h6>
                <p><?php echo htmlspecialchars($row['nationality']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>State of Origin:</h6>
                <p><?php echo htmlspecialchars($row['state_of_origin']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Department:</h6>
                <p><?php echo htmlspecialchars($row['department']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Local Government Area:</h6>
                <p><?php echo htmlspecialchars($row['lga']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Date of Birth:</h6>
                <p><?php echo htmlspecialchars($row['dob']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Gender:</h6>
                <p><?php echo htmlspecialchars($row['gender']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Religion:</h6>
                <p><?php echo htmlspecialchars($row['religion']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Phone Number:</h6>
                <p><?php echo htmlspecialchars($row['phoneno']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Email:</h6>
                <p><?php echo htmlspecialchars($row['email']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Address:</h6>
                <p><?php echo htmlspecialchars($row['addresss']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>School:</h6>
                <p><?php echo htmlspecialchars($row['school']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Session:</h6>
                <p><?php echo htmlspecialchars($row['sessionse']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Mode of Study:</h6>
                <p><?php echo htmlspecialchars($row['mode']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Health Disability:</h6>
                <p><?php echo htmlspecialchars($row['health_disability']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Disability Details:</h6>
                <p><?php echo htmlspecialchars($row['disability_details']); ?></p>
            </div>
            <!-- <div class="col-md-6">
                <h6>Passport:</h6>
                <img src="uploads/<?php echo htmlspecialchars($row['passport']); ?>" alt="Image" style="max-width: 100px; max-height: 100px;">
            </div> -->
        </div>
    </div>
    <div class="row" style="margin-top:50px;">
						<div class="col-md-6">
                            <button type="submit" onclick="window.print()" class="btn btn-primary btn-block"><i class="fa fa-print"></i> Print</button>
                        </div>
                        <div class="col-md-6">
                            <a href="index.php?env=Upload_biodata_form" style="color:#ffffff;text-decoration:none;"> 
                                <button type="button" class="btn btn-primary btn-block"><i class="fa fa-arrow-left"></i> Go back</button>
                            </a>
                        </div>
                       
                    </div>
</body>
</html>
