<?php 
ob_start();
include("inc/db.php");
$sname = $_SESSION['sname']; // Assuming the student's name is stored in the session

// Handle form submission logic here
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // name of the uploaded file
    $passport = $_FILES['passport']['name'];

    // destination of the file on the server
    $destination = 'uploads/' . $passport;

    // get the file extension
    $extension = pathinfo($passport, PATHINFO_EXTENSION);
    $file = $_FILES['passport']['tmp_name'];
    $size = $_FILES['passport']['size'];

    // Validate file extension and size
    if(!in_array($extension, ['png', 'jpg', 'jpeg', 'doc', 'pdf', 'docx'])){
        echo "<script>alert('Your file extension must be .png, .jpeg, .jpg, .doc, .pdf, or .docx')</script>";
    } elseif($size > 1000000){  // Check if file is larger than 1MB (1000000 bytes)
        echo "<script>alert('File too large')</script>";
    } else {
        if(move_uploaded_file($file, $destination)){
            // name of the uploaded file
    $sign = $_FILES['sign']['name'];

    // destination of the file on the server
    $destination = 'uploads/' . $sign;

    // get the file extension
    $extension = pathinfo($sign, PATHINFO_EXTENSION);
    $file = $_FILES['sign']['tmp_name'];
    $size = $_FILES['sign']['size'];

    // Validate file extension and size
    if(!in_array($extension, ['png', 'jpg', 'jpeg', 'doc', 'pdf', 'docx'])){
        echo "<script>alert('Your file extension must be .png, .jpeg, .jpg, .doc, .pdf, or .docx')</script>";
    } elseif($size > 1000000){  // Check if file is larger than 1MB (1000000 bytes)
        echo "<script>alert('File too large')</script>";
    } else {
        if(move_uploaded_file($file, $destination)){
            $query = "INSERT INTO passpprt_sign (passport,sign)
                      VALUES('$passport','$sign')";
            if(mysqli_query($db, $query)){
                echo "<script>alert('Successfully submitted')</script>";

                 // Update the session progress
                 $_SESSION['progress']['passport'] = true;
                    // Recalculate progress
                    function calculate_progress() {
                        $total_forms = 8;
                        $completed_forms = 0;
                        foreach ($_SESSION['progress'] as $form => $status) {
                            if ($status) $completed_forms++;
                        }
                        return ($completed_forms / $total_forms) * 100;
                    }

                    $progress_percentage = calculate_progress();

                    // Update the progress percentage in the database
                    $query = "UPDATE student_table SET progress_percentage = '$progress_percentage' WHERE sname = '$sname'";
                    mysqli_query($db, $query);

                 // Redirect back to the dashboard
                 header("Location: index.php?env=dashboard");
                 exit;
            } else {
                echo "<script>alert('Submission failed')</script>";
            }
        }
    }
}}}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Your Jamb Admission Letter/ Jamb Result</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
<h3 align="center">Upload Your Passport And  Signature </h3>
<hr>
<div class="container">
<div class="row">
    <div class="col-md-2"></div>
    
    <div class="col-md-8">
    <form method="post" action="index.php?env=passport" enctype="multipart/form-data" >
    <div class="form-group">
        
            <label for="">Upload Your Passport</label>
            <input type="file" name="passport" class="form-control" required> <br>
            <label for="">Upload Your Signature</label>
            <input type="file" name="sign" class="form-control" required> <br>
            <button type="submit" name="submit_jamb" class="btn btn-success btn-block">Submit</button>
        </form>
    </div>
    </div>
    <div class="col-md-2"></div>
</div>
</div>
  
<div class="row" style="margin-top:50px;">
                        <div class="col-md-6">
                            <a href="index.php?env=dashboard" 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 class="col-md-6">
                        
                        </div>
                    </div>


</body>
</html>
