<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Function to sanitize form data
    function sanitizeInput($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    // Process and store form data in the database
    $conn = new mysqli("localhost", "communi1_user", "PASSword6116", "communi1_cba");

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Form data for School Information
    $schoolName = sanitizeInput($_POST["school_name"]);
    $state = sanitizeInput($_POST["state"]);
    $lga = sanitizeInput($_POST["lga"]);
    $location = sanitizeInput($_POST["location"]);

    // Form data for Other Details
    $principalName = sanitizeInput($_POST["principal_name"]);
    $principalPhoneNumber = sanitizeInput($_POST["principal_phone"]);
    $coordinatorName = sanitizeInput($_POST["coordinator_name"]);
    $coordinatorPhoneNumber = sanitizeInput($_POST["coordinator_phone"]);

    // Form data for Applicant 1
    $applicant1Name = sanitizeInput($_POST["applicant1_name"]);
    $applicant1PhoneNumber = sanitizeInput($_POST["applicant1_phone"]);
    $applicant1ParentName = sanitizeInput($_POST["applicant1_parent_name"]);
    $applicant1ParentPhoneNumber = sanitizeInput($_POST["applicant1_parent_phone"]);

    // Form data for Applicant 2
    $applicant2Name = sanitizeInput($_POST["applicant2_name"]);
    $applicant2PhoneNumber = sanitizeInput($_POST["applicant2_phone"]);
    $applicant2ParentName = sanitizeInput($_POST["applicant2_parent_name"]);
    $applicant2ParentPhoneNumber = sanitizeInput($_POST["applicant2_parent_phone"]);

    // Form data for Applicant 3
    $applicant3Name = sanitizeInput($_POST["applicant3_name"]);
    $applicant3PhoneNumber = sanitizeInput($_POST["applicant3_phone"]);
    $applicant3ParentName = sanitizeInput($_POST["applicant3_parent_name"]);
    $applicant3ParentPhoneNumber = sanitizeInput($_POST["applicant3_parent_phone"]);

    // Upload signature files
    $targetDir = "uploads/";
    $principalSignatureFile = $targetDir . basename($_FILES["principal_signature"]["name"]);
    $coordinatorSignatureFile = $targetDir . basename($_FILES["coordinator_signature"]["name"]);
    $applicant1SignatureFile = $targetDir . basename($_FILES["applicant1_signature"]["name"]);
    $applicant2SignatureFile = $targetDir . basename($_FILES["applicant2_signature"]["name"]);
    $applicant3SignatureFile = $targetDir . basename($_FILES["applicant3_signature"]["name"]);

    move_uploaded_file($_FILES["principal_signature"]["tmp_name"], $principalSignatureFile);
    move_uploaded_file($_FILES["coordinator_signature"]["tmp_name"], $coordinatorSignatureFile);
    move_uploaded_file($_FILES["applicant1_signature"]["tmp_name"], $applicant1SignatureFile);
    move_uploaded_file($_FILES["applicant2_signature"]["tmp_name"], $applicant2SignatureFile);
    move_uploaded_file($_FILES["applicant3_signature"]["tmp_name"], $applicant3SignatureFile);

    // Store data in the database
    $sql = "INSERT INTO form_data (school_name, state, lga, location, principal_name, principal_phone, coordinator_name, coordinator_phone, 
            applicant1_name, applicant1_phone, applicant1_parent_name, applicant1_parent_phone, 
            applicant2_name, applicant2_phone, applicant2_parent_name, applicant2_parent_phone, 
            applicant3_name, applicant3_phone, applicant3_parent_name, applicant3_parent_phone,
            principal_signature, coordinator_signature, applicant1_signature, applicant2_signature, applicant3_signature)
            VALUES ('$schoolName', '$state', '$lga', '$location', '$principalName', '$principalPhoneNumber', '$coordinatorName', '$coordinatorPhoneNumber',
            '$applicant1Name', '$applicant1PhoneNumber', '$applicant1ParentName', '$applicant1ParentPhoneNumber',
            '$applicant2Name', '$applicant2PhoneNumber', '$applicant2ParentName', '$applicant2ParentPhoneNumber',
            '$applicant3Name', '$applicant3PhoneNumber', '$applicant3ParentName', '$applicant3ParentPhoneNumber',
            '$principalSignatureFile', '$coordinatorSignatureFile', '$applicant1SignatureFile', '$applicant2SignatureFile', '$applicant3SignatureFile')";

    if ($conn->query($sql) === TRUE) {
        // Send email notification
        $to = "info@mymail.com";
        $subject = "New Form Submission";
        $message = "A new form has been submitted. Check the database for details.";
        $headers = "From: webmaster@example.com"; // Change this to your email or leave it as is

        mail($to, $subject, $message, $headers);

        echo "Form submitted successfully.";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
} else {
    header("Location: index.php"); // Redirect to the form page if accessed directly
}
?>
