<?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 
    Guarantor_name, rel_student, readress, rephone, person_email, 
    occupation, job_title, employer_name,  office_phone , office_email, declarer_name, declarer_names, 
    Guarantor_pic, Guarantor_signature,sname
    FROM guarantor_form 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>Guarantor Form</title>
    <link rel="stylesheet" href="styles.css"> <!-- Add your CSS file -->
</head>
<body>
    <div class="container fullname-section">
        <h4 align="center">Guarantor Form</h4>
        <hr></hr>
        <div class="row">
           
            <div class="col-md-6">
                <h6>Guarantor Name:</h6>
                <p><?php echo htmlspecialchars($row['Guarantor_name']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Relationship with the Student</h6>
                <p><?php echo htmlspecialchars($row['rel_student']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Office Email:</h6>
                <p><?php echo htmlspecialchars($row['office_email']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Residential Address:</h6>
                <p><?php echo htmlspecialchars($row['readress']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Residential Phone No(s):</h6>
                <p><?php echo htmlspecialchars($row['rephone']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Personal E-mail:</h6>
                <p><?php echo htmlspecialchars($row['person_email']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Occupation:</h6>
                <p><?php echo htmlspecialchars($row['occupation']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Job Title:</h6>
                <p><?php echo htmlspecialchars($row['job_title']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Employer’s Name & Address:</h6>
                <p><?php echo htmlspecialchars($row['employer_name']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Office Phone No(s)::</h6>
                <p><?php echo htmlspecialchars($row['office_phone']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Guarantor Name</h6>
                <p><?php echo htmlspecialchars($row['declarer_name']); ?></p>
            </div>
            <div class="col-md-6">
                <h6>Student Name:</h6>
                <p><?php echo htmlspecialchars($row['declarer_names']); ?></p>
            </div>
           <div class="col-md-6">
                <h6>Guarantor Signature:</h6>
                <?php echo renderFile(htmlspecialchars($row['Guarantor_signature'])); ?>
            </div>
            <div class="col-md-6">
                <h6>Guarantor Picture:</h6>
                <?php echo renderFile(htmlspecialchars($row['Guarantor_pic'])); ?>
            </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-guarantor_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>
