<?php
session_start();
include_once 'connect.php';

// Enable error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $step = isset($_POST['step']) ? intval($_POST['step']) : 1;

    switch ($step) {
        case 1:
            // Fetch form data
            $school_name = $_POST['school_name'];
            $school_gender = $_POST['school_gender'];
            $school_type = $_POST['school_type'];
            $emis_code = $_POST['emis_code'];
            $ddo_code = $_POST['ddo_code'];
            $village = $_POST['village'];
            $post_office = $_POST['post_office'];
            $district_id = $_POST['districts_dropdown'];
            $tehsil_id = $_POST['tehsil_dropdown'];
            $constituency_id = $_POST['constituency_dropdown'];
            $union_council_id = $_POST['union_concils_dropdown'];
            $contact_no = $_POST['contact_no'];
            $level = $_POST['level'];
            $email = $_POST['email'];
            $locality = $_POST['locality'];
            $distance = $_POST['distance'];
            $zone = $_POST['zone'];
            $status = $_POST['status'];

            // SQL query
            $sql = "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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            
            if ($stmt = $conn->prepare($sql)) {
                // Bind parameters
                $stmt->bind_param(
                    "sssssssiiiiisssiss", 
                    $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
                );
                
                // Execute the query
                if ($stmt->execute()) {
                    $_SESSION['success_message'] = "School profile saved successfully!";
                    header("Location: school_land.php?step=2"); // Redirect to next step
                    exit();
                } else {
                    $_SESSION['error_message'] = "Error saving school profile. SQL Error: " . $stmt->error;
                    header("Location: multi1.php?step=1"); // Redirect back to step 1
                    exit();
                }
            } else {
                $_SESSION['error_message'] = "Error preparing SQL statement: " . $conn->error;
                header("Location: multi1.php?step=1"); // Redirect back to step 1
                exit();
            }
            break;

        case 2:
            // Save data from Step 2 (School Land Record)
            // ...
            break;

        // Continue for other steps
    }

    $conn->close();
} else {
    $_SESSION['error_message'] = "Invalid request method.";
    header("Location: multi1.php?step=1");
    exit();
}
?>
