<?php
include 'connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect form data and store in session
    $_SESSION['building_type'] = $_POST['building_type'];
    $_SESSION['ownershiptype'] = $_POST['ownershiptype'];
    $_SESSION['rental_payee'] = $_POST['rental_payee'];
    $_SESSION['construction_year'] = $_POST['construction_year'];
    $_SESSION['building_condition'] = $_POST['building_condition'];
    $_SESSION['cons_material'] = $_POST['cons_material'];
    $_SESSION['boundry_wall'] = $_POST['boundry_wall'];
    
    // Handle room details
    if (isset($_POST['room_type']) && isset($_POST['total_rooms']) && isset($_POST['room_condition'])) {
        $rooms = array();
        $room_types = $_POST['room_type'];
        $total_rooms = $_POST['total_rooms'];
        $room_conditions = $_POST['room_condition'];
        
        for ($i = 0; $i < count($room_types); $i++) {
            $rooms[] = [
                'type' => $room_types[$i],
                'total' => $total_rooms[$i],
                'condition' => $room_conditions[$i]
            ];
        }
        $_SESSION['rooms'] = $rooms;
    }

    // Handle playground details
    $_SESSION['playground'] = isset($_POST['playground']) ? $_POST['playground'] : null;
    
    // Handle no building reason
    $_SESSION['nobuilding_reason'] = isset($_POST['nobuilding_reason']) ? $_POST['nobuilding_reason'] : null;

    // Save everything to the database
    $sql_profile = "INSERT INTO school_profile (school_name, school_gender, school_type, emis_code, ddo_code, village, post_office, district_id, tehsil_id, constituency_id, union_council_id, contact_no, level, email, locality, distance, zone, status)
            VALUES ('" . $_SESSION['school_name'] . "', '" . $_SESSION['school_gender'] . "', '" . $_SESSION['school_type'] . "', '" . $_SESSION['emis_code'] . "', '" . $_SESSION['ddo_code'] . "', '" . $_SESSION['village'] . "', '" . $_SESSION['post_office'] . "', '" . $_SESSION['district_id'] . "', '" . $_SESSION['tehsil_id'] . "', '" . $_SESSION['constituency_id'] . "', '" . $_SESSION['union_council_id'] . "', '" . $_SESSION['contact_no'] . "', '" . $_SESSION['level'] . "', '" . $_SESSION['email'] . "', '" . $_SESSION['locality'] . "', '" . $_SESSION['distance'] . "', '" . $_SESSION['zone'] . "', '" . $_SESSION['status'] . "')";

    $sql_land = "INSERT INTO school_land (emis_code, land_available, land_size, land_ownership)
            VALUES ('" . $_SESSION['emis_code'] . "', '" . $_SESSION['land_available'] . "', '" . $_SESSION['land_size'] . "', '" . $_SESSION['land_ownership'] . "')";

    $sql_building = "INSERT INTO building_details (emis_code, building_type, ownershiptype, rental_payee, construction_year, building_condition, cons_material, boundry_wall, playground, nobuilding_reason)
            VALUES ('" . $_SESSION['emis_code'] . "', '" . $_SESSION['building_type'] . "', '" . $_SESSION['ownershiptype'] . "', '" . $_SESSION['rental_payee'] . "', '" . $_SESSION['construction_year'] . "', '" . $_SESSION['building_condition'] . "', '" . $_SESSION['cons_material'] . "', '" . $_SESSION['boundry_wall'] . "', '" . $_SESSION['playground'] . "', '" . $_SESSION['nobuilding_reason'] . "')";

    // Execute SQL queries
    if ($conn->query($sql_profile) === TRUE && $conn->query($sql_land) === TRUE && $conn->query($sql_building) === TRUE) {
        echo "Record saved successfully!";
        // Clear session after saving
        session_unset();
        session_destroy();
    } else {
        echo "Error: " . $conn->error;
    }

    $conn->close();
}
?>
