<?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 
   schoolname,years,fwaec,year,swaec,examination_Type_one,examination_Type_two,school_name,sname
    FROM waec 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>Waec Result</title>
    <link rel="stylesheet" href="styles.css"> <!-- Add your CSS file -->
</head>
<body>
    <div class="container fullname-section">
        <h4 align="center">
        View your O'level Result</h4>
        <hr></hr>
        <div class="row">
        <div class="col-md-6">
            
                <h6>School Name:</h6>
                <p><?php echo htmlspecialchars($row['schoolname']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Examination Type:</h6>
                <p><?php echo htmlspecialchars($row['examination_Type_one']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Examination Year:</h6>
                <p><?php echo htmlspecialchars($row['years']); ?></p>
            </div>
            <div class="col-md-6">
    <h6>First Sitting:</h6>
    <?php 
    $fwaecPath = htmlspecialchars($row['fwaec']); 
    if (!empty($fwaecPath) && file_exists("uploads/$fwaecPath")) { 
        echo "<img src='uploads/$fwaecPath' alt='First Sitting Result' style='max-width: 100%; height: auto; border: 1px solid #ccc; padding: 5px;'>"; 
    } else { 
        echo "<p>No image available for the first sitting.</p>"; 
    } 
    ?>
</div>
            
            <div class="col-md-6">
                <h6>School Name:</h6>
                <p><?php echo htmlspecialchars($row['school_name']); ?></p>
            </div>
           
            <div class="col-md-6">
                <h6>Examination Type:</h6>
                <p><?php echo htmlspecialchars($row['examination_Type_two']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Examination Year:</h6>
                <p><?php echo htmlspecialchars($row['year']); ?></p>
            </div>
                
            <div class="col-md-6">
    <h6>Second Sitting:</h6>
    <?php 
    $swaecPath = htmlspecialchars($row['swaec']); 
    if (!empty($swaecPath) && file_exists("uploads/$swaecPath")) { 
        echo "<img src='uploads/$swaecPath' alt='Second Sitting Result' style='max-width: 100%; height: auto; border: 1px solid #ccc; padding: 5px;'>"; 
    } else { 
        echo "<p>No image available for the second sitting.</p>"; 
    } 
    ?>
</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-waec_result" 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>
