<?php include("inc/db.php");?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hostel Application</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>

    <h3 align="center">SCREENING FORM</h3>
    <hr/>
    <div class="container">
        
        <div class="row">
            <form method="POST" action="index.php?env=Screening_form" enctype="multipart/form-data">
                <p style="font-size:13px;"><b>SECTION A</b></p>
                <p style="font-size:13px;"><b>PERSONAL DATA:</b></p>
                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="surname" 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">
                        <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="date" name="dob" class="form-control" required>
                    </div>
                </div>
                
                <div class="row form-group">
                    <div class="col-md-12">
                        <input type="text" name="residential_address" class="form-control" placeholder="Residential Address" 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" required>
                        <label for="no">No</label>
                        <input type="radio" name="health_disability" id="no" value="no" checked required>
                    </div>
                    <div class="col-md-6">
                        <label for="disability_details">If Yes, give details:</label>
                        <input type="text" name="disability_details" class="form-control" required>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-12">
                        <label for="sponsor_name">Sponsor's Name and Address:</label>
                        <input type="text" name="sponsor_name" class="form-control" placeholder="Sponsor's Name and Address" required><br>
                        <p><b>PERSONAL CONTACT DETAILS</b></p>
                    </div>
                </div>
                
                <p style="font-size:13px;"><b>PARTICULARS OF PARENT/GUARDIAN</b></p>
                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="parent_address" class="form-control" placeholder="Contact Address" required>
                    </div>
                    <div class="col-md-6">
                        <input type="tel" name="parent_phone" class="form-control" placeholder="Phone No" required>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="parent_email" class="form-control" placeholder="Parent's Email" required>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="nationality" class="form-control" placeholder="Nationality" required>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="state_of_origin" class="form-control" placeholder="State of Origin" required>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="city" class="form-control" placeholder="City/Town" required>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-12">
                        <input type="text" name="lga" class="form-control" placeholder="Local Govt. Area" required>
                    </div>
                </div>

                <p style="font-size:13px;"><b>Section B</b></p>
                <p style="font-size:13px;"><b>EDUCATIONAL BACKGROUND</b></p>
                <div class="row form-group">
                    <div class="col-md-12">
                        <textarea name="education_background" class="form-control" placeholder="List of schools attended with dates" required></textarea>
                    </div>
                </div>

                <h5 style="font-size:13px;"><b>ENTRY REQUIREMENTS:</b></h5>
                <div class="row form-group">
                    <div class="col-md-12">
                        <p>Five [5] O'Level credits at NECO, WAEC, GCE, TCC ll or their equivalent in not more than two sittings, including English Language and Mathematics, and any other three subjects related to the course of study.</p>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-12">
                        <label for="first_sitting">FIRST SITTING:</label>
                        <textarea name="first_sitting" class="form-control" placeholder="First Sitting Details" required></textarea>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-12">
                        <label for="second_sitting">SECOND SITTING:</label>
                        <textarea name="second_sitting" class="form-control" placeholder="Second Sitting Details" required></textarea>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="jamb_score" class="form-control" placeholder="JAMB Score" required>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="exam_year" class="form-control" placeholder="Exam Year" required>
                    </div>
                </div>

                <p style="font-size:13px;"><b>REFEREES DETAILS</b></p>
                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="referee_name" class="form-control" placeholder="Referee Name" required>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="referee_occupation" class="form-control" placeholder="Occupation" required>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-12">
                        <textarea name="referee_address" class="form-control" placeholder="Referee Address" required></textarea>
                    </div>
                </div>

                <div class="row form-group">
                    <div class="col-md-6">
                        <input type="text" name="referee_phone" class="form-control" placeholder="Phone No(s)" required>
                    </div>
                    <div class="col-md-6">
                        <input type="email" name="referee_email" class="form-control" placeholder="Email" required>
                    </div>
                </div>
                <div class="row form-group">
                <div class="col-md-12">
                    <label for="passport">Upload Passport Photograph:</label>
                    <input type="file" name="passport" class="form-control" accept="image/*" required>
                </div>
            </div>


                <p style="font-size:13px;"><b>DECLARATION</b></p>
                <p style="font-size:13px;">I, <input type="text" name="declarer_name" class="form-control" placeholder="Enter Your Full Name" required> hereby declare that <b>ALL</b> information provided in this form is correct to the best of my knowledge.</p>

                <button type="submit" name="screening" 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=dashboard" 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>

    <!-- PHP processing code -->
<?php
if (isset($_POST['screening'])) {
    // Sanitize and validate inputs
    $surname = mysqli_real_escape_string($db, $_POST['surname']);
    $othernames = mysqli_real_escape_string($db, $_POST['othernames']);
    $gender = mysqli_real_escape_string($db, $_POST['gender']);
    $dob = mysqli_real_escape_string($db, $_POST['dob']);
    $residential_address = mysqli_real_escape_string($db, $_POST['residential_address']);
    $health_disability = mysqli_real_escape_string($db, $_POST['health_disability']);
    $disability_details = mysqli_real_escape_string($db, $_POST['disability_details']);
    $sponsor_name = mysqli_real_escape_string($db, $_POST['sponsor_name']);
    $parent_address = mysqli_real_escape_string($db, $_POST['parent_address']);
    $parent_phone = mysqli_real_escape_string($db, $_POST['parent_phone']);
    $parent_email = mysqli_real_escape_string($db, $_POST['parent_email']);
    $nationality = mysqli_real_escape_string($db, $_POST['nationality']);
    $state_of_origin = mysqli_real_escape_string($db, $_POST['state_of_origin']);
    $city = mysqli_real_escape_string($db, $_POST['city']);
    $lga = mysqli_real_escape_string($db, $_POST['lga']);
    $education_background = mysqli_real_escape_string($db, $_POST['education_background']);
    $first_sitting = mysqli_real_escape_string($db, $_POST['first_sitting']);
    $second_sitting = mysqli_real_escape_string($db, $_POST['second_sitting']);
    $jamb_score = mysqli_real_escape_string($db, $_POST['jamb_score']);
    $exam_year = mysqli_real_escape_string($db, $_POST['exam_year']);
    $referee_name = mysqli_real_escape_string($db, $_POST['referee_name']);
    $referee_occupation = mysqli_real_escape_string($db, $_POST['referee_occupation']);
    $referee_address = mysqli_real_escape_string($db, $_POST['referee_address']);
    $referee_phone = mysqli_real_escape_string($db, $_POST['referee_phone']);
    $referee_email = mysqli_real_escape_string($db, $_POST['referee_email']);
    $declarer_name = mysqli_real_escape_string($db, $_POST['declarer_name']);

    // Handle file upload for the passport
    $passport = $_FILES['passport']['name'];
    $file = $_FILES['passport']['tmp_name'];
    $size = $_FILES['passport']['size'];

    // Directory to store uploaded files
    $destination = 'uploads/' . $passport;
    $extension = pathinfo($passport, PATHINFO_EXTENSION);
    
    // Validate file type and size
    if (!in_array($extension, ['png', 'jpg', 'jpeg'])) {
        echo "Your file extension must be .png, .jpeg, or .jpg.";
    } elseif ($size > 1000000) { // Change size limit to 1MB (1000000 bytes)
        echo "File is too large.";
    } else {
        // Move uploaded file to the server
        if (move_uploaded_file($file, $destination)) {
            // Insert into the database
            $query = "INSERT INTO screening_form 
                (surname, othernames, gender, dob, residential_address, health_disability, disability_details, sponsor_name, parent_address, parent_phone, parent_email, nationality, state_of_origin, city, lga, education_background, first_sitting, second_sitting, jamb_score, exam_year, referee_name, referee_occupation, referee_address, referee_phone, referee_email, declarer_name, passport) 
                VALUES ('$surname', '$othernames', '$gender', '$dob', '$residential_address', '$health_disability', '$disability_details', '$sponsor_name', '$parent_address', '$parent_phone', '$parent_email', '$nationality', '$state_of_origin', '$city', '$lga', '$education_background', '$first_sitting', '$second_sitting', '$jamb_score', '$exam_year', '$referee_name', '$referee_occupation', '$referee_address', '$referee_phone', '$referee_email', '$declarer_name', '$passport')";

            if (mysqli_query($db, $query)) {
                echo "<script>alert('Successfully Submitted');</script>";
            } else {
                echo "<script>alert('Submission Failed');</script>";
            }
        } else {
            echo "Failed to upload file.";
        }
    }
}
?>

