<?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
     $blood_group = $_FILES['blood_group']['name'];
 
     // destination of the file on the server
     $destination = 'uploads/' . $blood_group;
 
     // get the file extension
     $extension = pathinfo($blood_group, PATHINFO_EXTENSION);
     $file = $_FILES['blood_group']['tmp_name'];
     $size = $_FILES['blood_group']['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)){
                    // Insert files into database
                    $query = "INSERT INTO medical_report (blood_group,sname) VALUES ('$blood_group','$sname')";
                    if (mysqli_query($db, $query)) {
                        echo "<script>alert('Successfully submitted')</script>";

                        // Update session progress
                        $_SESSION['progress']['Medical_report'] = true;
                        
                        // Calculate progress percentage
                        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 progress in the database
                        $query = "UPDATE student_table SET progress_percentage = '$progress_percentage' WHERE sname = '$sname'";
                        mysqli_query($db, $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 Your Medical Report</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
<h3 align="center">Upload Your Medical Report </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=Medical_report" enctype="multipart/form-data"  >
           
            <!-- <label for="">Upload Your Fitness Test</label>
            <input type="file" name="fitness_test" class="form-control" required> <br> -->
            <label for="">Upload Your Blood Group</label>
            <input type="file" name="blood_group" class="form-control" required> <br>
            <button type="submit" name="submit_Medical_report" 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-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 class="col-md-6">
                        
                        </div>
                    </div>

</body>
</html>




