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

    $sname = mysqli_real_escape_string($db, $_POST['sname']);
    $othernames = mysqli_real_escape_string($db, $_POST['othernames']);
    $nationality = mysqli_real_escape_string($db, $_POST['nationality']);
    $state_of_origin = mysqli_real_escape_string($db, $_POST['state_of_origin']);
    $lga = mysqli_real_escape_string($db, $_POST['lga']);
    $dob = mysqli_real_escape_string($db, $_POST['dob']);
    $gender = mysqli_real_escape_string($db, $_POST['gender']);
    $religion = mysqli_real_escape_string($db, $_POST['religion']);
    $phoneno = mysqli_real_escape_string($db, $_POST['phoneno']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $addresss = mysqli_real_escape_string($db, $_POST['addresss']);
    $school = mysqli_real_escape_string($db, $_POST['school']);
    $department = mysqli_real_escape_string($db, $_POST['department']);
    $sessionse = mysqli_real_escape_string($db, $_POST['sessionse']);
    $mode = mysqli_real_escape_string($db, $_POST['mode']);
    $sign = mysqli_real_escape_string($db, $_POST['sign']);
    $health_disability = mysqli_real_escape_string($db, $_POST['health_disability']);
    $disability_details = mysqli_real_escape_string($db, $_POST['disability_details']);
    
    //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'])){
        echo "<script>alert('Your file extension must be .png, .jpeg, .jpg')</script>";
    } elseif($size > 1000000){  // Check if file is larger than 100KB
        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'])){
        echo "<script>alert('Your file extension must be .png, .jpeg, .jpg')</script>";
    } elseif($size > 1000000){  // Check if file is larger than 100KB
        echo "<script>alert('File too large')</script>";
    } else {
        if(move_uploaded_file($file, $destination)){
            // SQL query to insert data into the database
            $query = "INSERT INTO bio_data (sname, othernames, nationality, state_of_origin, lga, dob, gender, religion,phoneno, email, addresss, department,school,sessionse, mode, passport,sign,health_disability,disability_details)
            VALUES('$sname', '$othernames', '$nationality', '$state_of_origin', '$lga', '$dob', '$gender', '$religion',  '$phoneno', '$email', '$addresss','$school', '$department',  '$sessionse','$mode', '$passport','$sign','$health_disability','$disability_details')";
            
            // Execute the query
            if(mysqli_query($db, $query)){

                // Update the session progress
                    $_SESSION['progress']['bio_data'] = 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>";
            }
        }
    }
}
}
}


ob_end_flush();
?>




<?php ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
<h3 align="center">BIODATA FORM</h3>
<hr/>
<div class="container">
    <div class="row">
        <form method="POST" action="index.php?env=bio_data" enctype="multipart/form-data">
            <div class="row form-group">
                <div class="col-md-6">
                    <input type="text" name="sname" class="form-control" placeholder="Surname" required>
                </div>
                <div class="col-md-6">
                    <input type="text" name="othernames" class="form-control" placeholder="Other Names" required>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6">
                    <input type="text" name="nationality" class="form-control" placeholder="Nationality" required>
                </div>
                <div class="col-md-6">
                    <input type="text" name="state_of_origin" class="form-control" placeholder="State of Origin" required>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6">
                    <input type="text" name="lga" class="form-control" placeholder="Local Govt. Area" required>
                </div>
                <div class="col-md-6">
                    <input type="date" name="dob" class="form-control" required>
                </div>  
            </div>
            <div class="row form-group">
                <div class="col-md-6">
                    <label for="male">Sex:</label><br>     
                    <label for="male">Male</label>
                    <input type="radio" name="gender" id="male" value="male" checked>
                    <label for="female">Female</label>
                    <input type="radio" name="gender" id="female" value="female">
                </div>
                <div class="col-md-6">
                    <input type="text" name="religion" class="form-control" placeholder="Religion" required>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6">
                    
                <label> MODE OF ENTRY </label> <select class="form-control" name="mode" > 
                   <option name="mode" value="select">Select</option>
                    <option name="mode" value="Caps">CAPS</option>
                    <option  name="mode" value="TRANSFER">TRANSFER</option>
                   </select>
                </div>
                <div class="col-md-6">
                    <input type="text" name="phoneno" class="form-control" placeholder="Phone No" required>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6">
                    <input type="email" name="email" class="form-control" placeholder="Email" required>
                </div>
                <div class="col-md-6">
                    <input type="text" name="addresss" class="form-control" placeholder="Address" required>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-6">
                    <input type="text" name="school" class="form-control" placeholder="School" required>
                </div>
                <div class="col-md-6">
                    <input type="text" name="department" class="form-control" placeholder="Department" required>
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-12">
                    <input type="text" name="sessionse" class="form-control" placeholder="Session" required>
                </div>
            </div>
           
            <div class="row form-group">
                    <div class="col-md-6">
                        <label for="health">Do you have any Health/Physical Disability:</label><br>
                        <label for="yes">Yes</label>
                        <input type="radio" name="health_disability" id="yes" value="yes">
                        <label for="no">No</label>
                        <input type="radio" name="health_disability" id="no" value="no">
                    </div>
                    <div class="col-md-6">
                        <label for="disability_details">If Yes, give details:</label>
                        <input type="text" name="disability_details" class="form-control">
                    </div>
                </div>

            <div class="row form-group">
                <div class="col-md-6">
                    <label for="">Passport</label>
                    <input type="file" name="passport" class="form-control" required> <br>
                </div>
                <div class="col-md-6">
                <label for="">Upload Your Signature</label>
                <input type="file" name="sign" class="form-control" required>
                <p align="center">Use black ink  </p> <br>
               
              </div>
            </div>
            <button type="submit" name="bio_data" class="btn btn-success btn-block">Submit</button>
        </form>
    </div>
</div>

<div class="row" style="margin-top:50px;">
                        <div class="col-md-6">
                            <a href="index.php?env=Upload_biodata_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>

