<?php
include "./db.php";
include "x.php"; 

if (isset($_POST['submit'])) {
    // Function to handle file upload
    function uploadFile($fileInput, $folder) {
        if (isset($_FILES[$fileInput]) && $_FILES[$fileInput]['error'] == 0) {
            $ext = pathinfo($_FILES[$fileInput]['name'], PATHINFO_EXTENSION);
            $filename = uniqid() . "." . $ext;
            $target = $folder . $filename;

            if (move_uploaded_file($_FILES[$fileInput]['tmp_name'], $target)) {
                return $filename;
            }
        }
        return null;
    }

    $uploadDir = "./login/upload/admission/";

    // Upload files
    $stu_photo         = uploadFile("stu_photo", $uploadDir);
    $birth_certificate = uploadFile("birth_certificate", $uploadDir);
    $transfer_certificate = uploadFile("transfer_certificate", $uploadDir);
    $guardian_id       = uploadFile("guardian_id", $uploadDir);
    $report_card       = uploadFile("report_card", $uploadDir);
    $student_aadhar    = uploadFile("student_aadhar", $uploadDir);
    $signature         = uploadFile("signature", $uploadDir);

    // Get form data
    $stu_name       = mysqli_real_escape_string($con, $_POST['stu_name']);
    $stu_dob        = mysqli_real_escape_string($con, $_POST['stu_dob']);
    $stu_blood      = mysqli_real_escape_string($con, $_POST['stu_blood']);
    $stu_nationality = isset($_POST['stu_nationality_other']) && $_POST['stu_nationality_other'] != "" 
                        ? $_POST['stu_nationality_other'] 
                        : $_POST['stu_nationality'];
    $stu_religion   = mysqli_real_escape_string($con, $_POST['stu_religion']);
    $stu_gender     = mysqli_real_escape_string($con, $_POST['stu_gender']);
    $stu_aadhar     = mysqli_real_escape_string($con, $_POST['stu_aadhar']);
    $father_name    = mysqli_real_escape_string($con, $_POST['father_name']);
    $father_occupation = mysqli_real_escape_string($con, $_POST['father_occupation']);
    $mother_name    = mysqli_real_escape_string($con, $_POST['mother_name']);
    $mother_occupation = mysqli_real_escape_string($con, $_POST['mother_occupation']);
    $guardian_phone = mysqli_real_escape_string($con, $_POST['guardian_phone']);
    $guardian_email = mysqli_real_escape_string($con, $_POST['guardian_email']);
    $permanent_address = mysqli_real_escape_string($con, $_POST['permanent_address']);
    $present_address = mysqli_real_escape_string($con, $_POST['present_address']);
    $district       = mysqli_real_escape_string($con, $_POST['district']);
    $state          = mysqli_real_escape_string($con, $_POST['state']);
    $pincode        = mysqli_real_escape_string($con, $_POST['pincode']);
    $emergency_contact = mysqli_real_escape_string($con, $_POST['emergency_contact']);
    $admission_class = mysqli_real_escape_string($con, $_POST['admission_class']);
    $previous_school = mysqli_real_escape_string($con, $_POST['previous_school']);
    $hostel         = mysqli_real_escape_string($con, $_POST['hostel']);
    $medical_condition = mysqli_real_escape_string($con, $_POST['medical_condition']);
    $medical_condition_details = mysqli_real_escape_string($con, $_POST['medical_condition_details']);
    $allergies      = mysqli_real_escape_string($con, $_POST['allergies']);

    // Insert query
    $query = "INSERT INTO student_admissions (
        stu_name, stu_dob, stu_blood, stu_nationality, stu_religion, stu_gender, stu_aadhar, stu_photo,
        father_name, father_occupation, mother_name, mother_occupation, guardian_phone, guardian_email,
        permanent_address, present_address, district, state, pincode, emergency_contact, admission_class,
        previous_school, hostel, medical_condition, medical_condition_details, allergies,
        birth_certificate, transfer_certificate, guardian_id, report_card, student_aadhar, signature, admin_id, admission_status
    ) VALUES (
        '$stu_name', '$stu_dob', '$stu_blood', '$stu_nationality', '$stu_religion', '$stu_gender', '$stu_aadhar', '$stu_photo',
        '$father_name', '$father_occupation', '$mother_name', '$mother_occupation', '$guardian_phone', '$guardian_email',
        '$permanent_address', '$present_address', '$district', '$state', '$pincode', '$emergency_contact', '$admission_class',
        '$previous_school', '$hostel', '$medical_condition', '$medical_condition_details', '$allergies',
        '$birth_certificate', '$transfer_certificate', '$guardian_id', '$report_card', '$student_aadhar', '$signature', '$x', 'Pending'
    )";

    if (mysqli_query($con, $query)) {
        echo "<script>alert('Application submitted successfully'); window.location.href='application-form.php';</script>";
    } else {
        echo "Error: " . mysqli_error($con);
    }
}
?>