
<?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') {
    // Capture form data
   
    $Guarantor_name = mysqli_real_escape_string($db, $_POST['Guarantor_name']);
    $rel_student = mysqli_real_escape_string($db, $_POST['rel_student']);
    $readress = mysqli_real_escape_string($db, $_POST['readress']);
    $rephone = mysqli_real_escape_string($db, $_POST['rephone']);
    $person_email = mysqli_real_escape_string($db, $_POST['person_email']);
    $occupation = mysqli_real_escape_string($db, $_POST['occupation']);
    $job_title = mysqli_real_escape_string($db, $_POST['job_title']);  
    $employer_name = mysqli_real_escape_string($db, $_POST['employer_name']); 
    $office_phone = mysqli_real_escape_string($db, $_POST['office_phone']);
    $office_email = mysqli_real_escape_string($db, $_POST['office_email']);
    $declarer_name = mysqli_real_escape_string($db, $_POST['declarer_name']);
    $declarer_names = mysqli_real_escape_string($db, $_POST['declarer_names']);
  

    // Upload Guarantor picture
    $Guarantor_pic = $_FILES['Guarantor_pic']['name'];
    $Guarantor_pic_destination = 'uploads/' . $Guarantor_pic;
    $Guarantor_pic_extension = pathinfo($Guarantor_pic, PATHINFO_EXTENSION);
    $Guarantor_pic_tmp = $_FILES['Guarantor_pic']['tmp_name'];

    if (!in_array($Guarantor_pic_extension, ['png', 'jpg', 'jpeg'])) {
        echo "<script>alert('Your file extension must be .png, .jpeg, or .jpg')</script>";
    } elseif ($_FILES['Guarantor_pic']['size'] > 1000000) {
        echo "<script>alert('File too large')</script>";
    } else {
        if (move_uploaded_file($Guarantor_pic_tmp, $Guarantor_pic_destination)) {
            // Upload Guarantor signature
            $Guarantor_signature = $_FILES['Guarantor_signature']['name'];
            $Guarantor_signature_destination = 'uploads/' . $Guarantor_signature;
            $Guarantor_signature_extension = pathinfo($Guarantor_signature, PATHINFO_EXTENSION);
            $Guarantor_signature_tmp = $_FILES['Guarantor_signature']['tmp_name'];

            if (!in_array($Guarantor_signature_extension, ['png', 'jpg', 'jpeg'])) {
                echo "<script>alert('Your signature file extension must be .png, .jpeg, or .jpg')</script>";
            } elseif ($_FILES['Guarantor_signature']['size'] > 1000000) {
                echo "<script>alert('Signature file too large')</script>";
            } else {
                if (move_uploaded_file($Guarantor_signature_tmp, $Guarantor_signature_destination)) {
                    // Insert data into the database
                    $query = "INSERT INTO guarantor_form ( Guarantor_name, rel_student, readress, rephone, person_email, occupation, job_title, employer_name, office_phone, office_email, Guarantor_pic, Guarantor_signature, declarer_name, declarer_names,sname) 
                    VALUES( '$Guarantor_name', '$rel_student', '$readress', '$rephone', '$person_email', '$occupation', '$job_title', '$employer_name', '$office_phone', '$office_email', '$Guarantor_pic', '$Guarantor_signature', '$declarer_name', '$declarer_names','$sname')";

                    if (mysqli_query($db, $query)) {
                        
                        // Update the session progress
                    $_SESSION['progress']['guarantor_form'] = 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();
?>
<!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">GUARANTOR FORM</h3>
<hr>
<div class="container">
        <div class="row">
            <form method="post" action="index.php?env=Guarantor_Form" enctype="multipart/form-data">
                 <!-- <p style="font-size:13px;"><b>STUDENT’S DATA:</b></p>
                 <div class="row form-group">
                    <div class="col-md-6">
                        <Label>Surname</Label>
                        <input type="text" name="surname" class="form-control" placeholder="surname" required>
                    </div>
                    <div class="col-md-6">
                    <Label>First Name</Label>
                        <input type="text" name="fname" class="form-control" placeholder="First Name" required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-md-6">
                    <Label>Other Names</Label>
                        <input type="text" name="othname" class="form-control" placeholder="Other Names" required>
                    </div>
                    <div class="col-md-6">
                    <Label>Permanent Address </Label>
                        <input type="text" name="paddress" class="form-control" placeholder="Permanent Address " required>
                    </div>
                </div> -->
                <!-- <HR></HR> -->
                <p style="font-size:13px;"><b>GUARANTOR’S DATA:</b></p>
                <div class="row form-group">
                    <div class="col-md-6">
                        <Label>Guarantor’s Name: </Label>
                        <input type="text" name="Guarantor_name" class="form-control" placeholder="Guarantor’s Name: " required>
                    </div>
                    <div class="col-md-6">
                    <Label>Relationship with the Student</Label>
                        <input type="text" name="rel_student" class="form-control" placeholder="Relationship with the Student" required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-md-6">
                    <Label>Residential Address:</Label>
                        <input type="text" name="readress" class="form-control" placeholder="Residential Address:" required>
                    </div>
                    <div class="col-md-6">
                    <Label>Residential Phone No(s): </Label>
                        <input type="text" name="rephone" class="form-control" placeholder="Residential Phone No(s): " required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-md-6">
                        <Label>Personal E-mail: </Label>
                        <input type="text" name="person_email" class="form-control" placeholder="Personal E-mail: " required>
                    </div>
                    <div class="col-md-6">
                    <Label>Occupation:</Label>
                        <input type="text" name="occupation" class="form-control" placeholder="Occupation:" required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-md-6">
                    <Label>Job Title:</Label>
                        <input type="text" name="job_title" class="form-control" placeholder="Job Title:" required>
                    </div>
                    <div class="col-md-6">
                    <Label>Employer’s Name & Address: </Label>
                        <input type="text" name="employer_name" class="form-control" placeholder="Employer’s Name and Address: " required>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-md-6">
                    <Label>Office Phone No(s):</Label>
                        <input type="text" name="office_phone" class="form-control" placeholder="Office Phone No(s):" required>
                    </div>
                    <div class="col-md-6">
                    <Label>Office E-mail: </Label>
                        <input type="email" name="office_email" class="form-control" placeholder="Office E-mail: " required>
                    </div>
                </div>
              
              
                <p style="font-size:13px;"><u><b>UNDERTAKEN</b></u></p>
                <p style="font-size:13px;">I, <input type="text" name="declarer_name" class="form-control" placeholder="Enter Guarantor Name" required> hereby undertake to ensure 
                that the student, by name <input type="text" name="declarer_names" class="form-control" placeholder="Enter Student Name" required>will maintain good behaviour throughout his/her
                stay at the Polytechnic.<br> <br>
                I confirm that he/she is of good character and is fit and proper to attend Enville Polytechnic,
                Opebi, Ikeja, Lagos. <br> <br>
                Accordingly, I hereby agree to stand as a guarantor for him/her and promise to provide for
                his/her financial needs, including tuition fees, throughout his/her stay at this Polytechnic. <br> <br>
                I make these declarations with full knowledge of the obligations and responsibilities of a
                Guarantor.</p>

<HR></HR>
<div class="row form-group">
                    <div class="col-md-12">
                    <Label>Guarantor's Picture</Label>
                        <input type="file" name="Guarantor_pic" class="form-control" placeholder="Office Phone No(s):" required>
                    </div></div>
                    <div class="row form-group">
                    <div class="col-md-12">
                    <Label>Guarantor’s Signature (Use black ink)</Label>
                        <input type="file" name="Guarantor_signature" class="form-control" placeholder="Office Phone No(s):" required>
                     
                    </div></div>
<button type="submit" name="Guarantor" 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-guarantor_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>
