<?php
session_start();
include 'connect.php'; // Include database connection

// Check if user is logged in
if (!isset($_SESSION['school_name']) || !isset($_SESSION['emis_code'])) {
    header("Location: login.php");
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $emis_code = $_SESSION['emis_code'];

    // Save school profile data
    $sql = "INSERT INTO school_profile (emis_code, school_name, school_gender, school_type, ddo_code, village, post_office, district, tehsil, constituency, union_council, contact_no, level, email, locality, distance, zone, status) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssssssssssssssssss", $emis_code, $_SESSION['school_name'], $school_gender, $_SESSION['school_type'], $_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']);
    $stmt->execute();

    // Save school land data
    $sql_land = "INSERT INTO school_land (emis_code, land_select, kanal, marla, land_type, ownership_status, land_document, noland_reason) 
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    
    $stmt_land = $conn->prepare($sql_land);
    $stmt_land->bind_param("ssiiisss", $emis_code, $_SESSION['land_select'], $_SESSION['kanal'], $_SESSION['marla'], $_SESSION['land_type'], $_SESSION['ownership_status'], $_SESSION['land_document'], $_SESSION['noland_reason']);
    $stmt_land->execute();

    // Success message and clear session
    echo "Data has been successfully saved!";
    session_destroy(); // Clear the session
}
?>
<html>
<form method="POST">
<h2>Review and Submit</h2>
    <p>Please review the information and submit your data.</p>
        <!-- Ensure this button submits the form -->
    <button type="submit" id="submitForm">Submit</button>	
	</form>
</html>