<?php
session_start();

include_once 'connect.php'; // Connect to the database

// Debugging: Print EMIS code to confirm it's set
if (isset($_SESSION['emis_code'])) {
    echo "EMIS Code: " . htmlspecialchars($_SESSION['emis_code']);
} else {
    die("Error: EMIS Code is not set in the session.");
}

// Check if all required session variables are set
$required_sessions = [
    'emis_code', 'building_select', 'ownership_type', 'rental_payee', 
    'construction_year', 'building_condition', 'construction_material', 
    'boundary_wall', 'no_building_reason', 'school_name', 'school_gender', 
    'school_type', 'ddo_code', 'village', 'post_office', 'district', 
    'tehsil', 'constituency', 'union_council', 'contact_no', 'level', 
    'email', 'locality', 'distance', 'zone', 'status', 'land_select', 
    'kanal', 'marla', 'land_type', 'ownership_status', 'land_document'
];

foreach ($required_sessions as $session) {
    if (!isset($_SESSION[$session])) {
        die("Error: Missing session variable: $session");
    }
}

// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['final_submit'])) {
    // Collect EMIS code to link tables
    $emis_code = $_SESSION['emis_code'];

    // Insert into school_profile table
    $query1 = "INSERT INTO school_profile (school_name, school_gender, school_type, emis_code, ddo_code, village, post_office, district, tehsil, constituency, union_council, contact_no, level, email, locality, distance, zone, status) 
               VALUES ('" . $_SESSION['school_name'] . "', '" . $_SESSION['school_gender'] . "', '" . $_SESSION['school_type'] . "', '$emis_code', '" . $_SESSION['ddo_code'] . "', '" . $_SESSION['village'] . "', 
               '" . $_SESSION['post_office'] . "', '" . $_SESSION['district'] . "', '" . $_SESSION['tehsil'] . "', '" . $_SESSION['constituency'] . "', '" . $_SESSION['union_council'] . "', '" . $_SESSION['contact_no'] . "', 
               '" . $_SESSION['level'] . "', '" . $_SESSION['email'] . "', '" . $_SESSION['locality'] . "', '" . $_SESSION['distance'] . "', '" . $_SESSION['zone'] . "', '" . $_SESSION['status'] . "')";

    if (!$conn->query($query1)) {
        die("Error inserting into school_profile: " . $conn->error);
    }

    // Insert into school_land table
    $query2 = "INSERT INTO school_land (emis_code, land_select, kanal, marla, land_type, ownership_status, land_document) 
               VALUES ('$emis_code', '" . $_SESSION['land_select'] . "', '" . $_SESSION['kanal'] . "', '" . $_SESSION['marla'] . "', 
               '" . $_SESSION['land_type'] . "', '" . $_SESSION['ownership_status'] . "', '" . $_SESSION['land_document'] . "')";

    if (!$conn->query($query2)) {
        die("Error inserting into school_land: " . $conn->error);
    }

    // Insert into building_details table
    $query3 = "INSERT INTO building_details (emis_code, building_select, ownership_type, rental_payee, construction_year, building_condition, construction_material, boundary_wall, no_building_reason) 
               VALUES ('$emis_code', '" . $_SESSION['building_select'] . "', '" . $_SESSION['ownership_type'] . "', '" . $_SESSION['rental_payee'] . "', 
               '" . $_SESSION['construction_year'] . "', '" . $_SESSION['building_condition'] . "', '" . $_SESSION['construction_material'] . "', 
               '" . $_SESSION['boundary_wall'] . "', '" . $_SESSION['no_building_reason'] . "')";

    if (!$conn->query($query3)) {
        die("Error inserting into building_details: " . $conn->error);
    }

    // Insert room details into the rooms table
    if (isset($_SESSION['room_type']) && isset($_SESSION['total_rooms']) && isset($_SESSION['room_condition'])) {
        $room_types = $_SESSION['room_type'];
        $total_rooms = $_SESSION['total_rooms'];
        $room_conditions = $_SESSION['room_condition'];

        for ($i = 0; $i < count($room_types); $i++) {
            // Ensure all room details are set
            if (!empty($room_types[$i]) && !empty($total_rooms[$i]) && !empty($room_conditions[$i])) {
                $query4 = "INSERT INTO room_details (emis_code, room_type, total_rooms, room_condition) 
                           VALUES ('$emis_code', '" . $conn->real_escape_string($room_types[$i]) . "', '" . (int)$total_rooms[$i] . "', '" . $conn->real_escape_string($room_conditions[$i]) . "')";

                if (!$conn->query($query4)) {
                    die("Error inserting into room_details: " . $conn->error);
                }
            }
        }
    }

    // Clear session data and redirect to a success page
    session_unset();
    session_destroy();
    header('Location: success.php');
    exit;
}
?>
