<?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') {

    $schoolname = mysqli_real_escape_string($db, $_POST['schoolname']);
    $years = mysqli_real_escape_string($db, $_POST['years']);
    $examination_Type_one = mysqli_real_escape_string($db, $_POST['examination_Type_one']);
    
    // First sitting file
    $fwaec = $_FILES['fwaec']['name'];
    $fwaec_destination = 'uploads/' . $fwaec;
    $fwaec_extension = pathinfo($fwaec, PATHINFO_EXTENSION);
    $fwaec_size = $_FILES['fwaec']['size'];
    
    // Optional fields
    $sname_optional = !empty($_POST['sname']) ? mysqli_real_escape_string($db, $_POST['sname']) : null;
    $examination_Type_two = !empty($_POST['examination_Type_two']) ? mysqli_real_escape_string($db, $_POST['examination_Type_two']) : null;
    $year_optional = !empty($_POST['year']) ? mysqli_real_escape_string($db, $_POST['year']) : null;
    $swaec = $_FILES['swaec']['name'] ? $_FILES['swaec']['name'] : null;
    $swaec_destination = $swaec ? 'uploads/' . $swaec : null;
    $swaec_extension = $swaec ? pathinfo($swaec, PATHINFO_EXTENSION) : null;
    $swaec_size = $swaec ? $_FILES['swaec']['size'] : null;

    // Validate file extension and size for first sitting
    if(!in_array($fwaec_extension, ['png', 'jpg', 'jpeg', 'doc', 'pdf', 'docx'])){
        echo "<script>alert('Your file extension must be .png, .jpeg, .jpg, .doc, .pdf, or .docx')</script>";
    } elseif($fwaec_size > 1000000){  // Check if file is larger than 1MB (1000000 bytes)
        echo "<script>alert('File too large')</script>";
    } else {
        if(move_uploaded_file($_FILES['fwaec']['tmp_name'], $fwaec_destination)){

            // Validate and upload second sitting file if provided
            if ($swaec) {
                if (!in_array($swaec_extension, ['png', 'jpg', 'jpeg', 'doc', 'pdf', 'docx'])) {
                    echo "<script>alert('Your file extension must be .png, .jpeg, .jpg, .doc, .pdf, or .docx for the second sitting')</script>";
                    $swaec = null; // Reset if invalid
                } elseif ($swaec_size > 1000000) {
                    echo "<script>alert('File too large for the second sitting')</script>";
                    $swaec = null; // Reset if too large
                } else {
                    move_uploaded_file($_FILES['swaec']['tmp_name'], $swaec_destination);
                }
            }

            // Insert into database with optional fields
            $query = "INSERT INTO waec (schoolname, years, fwaec, school_name, examination_Type_one, examination_Type_two, year, swaec)
                      VALUES ('$schoolname', '$years', '$fwaec', '$school_name,', '$examination_Type_one', 
                              '$examination_Type_two', '$year_optional', '$swaec')";
            if(mysqli_query($db, $query)){
                echo "<script>alert('Successfully submitted')</script>";

                // Update session progress and calculate progress percentage
                $_SESSION['progress']['waec_result'] = true;
                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_query = "UPDATE student_table SET progress_percentage = '$progress_percentage' WHERE sname = '$sname'";
                mysqli_query($db, $update_query);

                // Redirect to dashboard
                header("Location: index.php?env=dashboard");
                exit;
            } else {
                echo "<script>alert('Submission failed')</script>";
            }
        }
    }
}
ob_end_flush();
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Waec/Olevel Result</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
<h3 align="center">UPLOAD YOUR O'LEVEL 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=waec_result" enctype="multipart/form-data">
        <input type="text" name="schoolname" class="form-control" placeholder="School Name " required>  <br>
        <label> Examination Type </label> <select class="form-control" name="examination_Type_one" required> 
                   <option  value="select">Select</option>
                    <option  value="Waec">WAEC</option>
                    <option   value="Neco">NECO</option>
                    <option   value="Gce">GCE</option>
                    <option   value="Nabteb">NABTEB</option>
                   </select></label> <br>
                   <input type="text" name="years" class="form-control" placeholder="Examination Year " required> <br>
                   <label for="">First Sitting</label>
            <input type="file" name="fwaec" class="form-control" required> <br>
           <hr></hr>
           <b><p>Optional</p></b>
            <input type="text" name="school_name" class="form-control" placeholder="School Name " >  <br>
            <label> Examination Type</label> <select class="form-control" name="examination_Type_two" > 
                   <option value="select">Select</option>
                    <option value="Waec">WAEC</option>
                    <option  value="Neco">NECO</option>
                    <option  value="Gce">GCE</option>
                    <option  value="Nabteb">NABTEB</option>
                   </select></label> <br>
                   <input type="text" name="year" class="form-control" placeholder="Examination Year " > <br>                  
            <label for="">Second Sitting</label>
            <input type="file" name="swaec" class="form-control" > <br>
            <button type="submit" name="submit_waec" 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-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 class="col-md-6">
                        
                        </div>
                    </div>


</body>
</html>