<?php
// Include database connection code
include("includes/db.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect form data
    $schoolName = mysqli_real_escape_string($conn, $_POST['school_name']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $establishmentYear = mysqli_real_escape_string($conn, $_POST['establishment_year']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $website = mysqli_real_escape_string($conn, $_POST['website']);
    $schoolCode = mysqli_real_escape_string($conn, $_POST['school_code']);
    $taxpin = mysqli_real_escape_string($conn, $_POST['taxpin']);

    // Upload logo file
    $logoPath = "";
    if ($_FILES["logo"]["error"] == 0) {
        $targetDir = "uploads/";
        $targetFile = $targetDir . basename($_FILES["logo"]["name"]);
        move_uploaded_file($_FILES["logo"]["tmp_name"], $targetFile);
        $logoPath = $targetFile;
    }

    // SQL query to insert school details into the database
    $query = "INSERT INTO schools (school_name, address, establishment_year, phone, email, website, logo, school_code, neb_code, ctevt_code, pan)
              VALUES ('$schoolName', '$address', '$establishmentYear', '$phone', '$email', '$website', '$logoPath', '$schoolCode','','', '$taxpin')";

    if ($conn->query($query) === TRUE) {
        echo "School details added successfully!";
         header("Location: index.php?success=1");
    } else {
        echo "Error: " . $query . "<br>" . $conn->error;
    }

    // Close the database connection
    $conn->close();
}
?>
