<?php 
ob_start();
include("inc/db.php");
if(!isset($_SESSION['sname'])){
    header("location:validation/index.php");
    exit();
}


$sname = $_SESSION["sname"];
$query = "SELECT sname FROM student_table WHERE sname = '$sname'";
$query_result = $db->query($query);


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

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

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

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

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

    // get the file extension
    $extension = pathinfo($jamb_letter, PATHINFO_EXTENSION);
    $file = $_FILES['jamb_letter']['tmp_name'];
    $size = $_FILES['jamb_letter']['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 jamb (jamb,jamb_letter,sname)
                      VALUES('$jamb','$jamb_letter','$sname')";
            if(mysqli_query($db, $query)){
                echo "<script>alert('Successfully submitted')</script>";

                 // Update the session progress
                 $_SESSION['progress']['jamb'] = true;
                    // Recalculate progress
                    function calculate_progress() {
                        $total_forms = 6;
                        $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 Jamb Admission Letter/ Jamb Result </h3>
<hr>
<div class="container">
<div class="row">
    <div class="col-md-2"></div>
    
    <div class="col-md-8">
    <div class="form-group">
        <form method="post" action="index.php?env=Jamb" enctype="multipart/form-data"  >
            <label for="">Upload Your Jamb Admission Letter</label>
            <input type="file" name="jamb_letter" class="form-control" required> <br>
            <label for="">Upload Your Jamb Result</label>
            <input type="file" name="jamb" 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=upload-jamb_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 class="col-md-6">
                        
                        </div>
                    </div>


</body>
</html>
