<?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 
   fitness_test,blood_group,sname
    FROM medical_report 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>Print Medical Report</title>
    <link rel="stylesheet" href="styles.css"> <!-- Add your CSS file -->
</head>
<body>
    <div class="container fullname-section">
        <h5 align="center">View your Medical Report</h5>
        <hr></hr>
        <div class="row">
           
                 <!-- <div class="col-md-6">
                <h6>Fitness Test:</h6>
                <?php echo renderFile(htmlspecialchars($row['fitness_test'])); ?>
            </div> -->
            <div class="col-md-6">
                <h6>Blood Group:</h6>
                <?php echo renderFile(htmlspecialchars($row['blood_group'])); ?>
            </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-medical_report" 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>
