<?php
session_start(); // Ensure session is started

// Check if the user is logged in by verifying the session variable
if (!isset($_SESSION['school_name']) || !isset($_SESSION['emis_code']) || !isset($_SESSION['cate_id']) || !isset($_SESSION['level_id'])) {
    // Redirect to login page if the session is not set
    header("Location: login.php");
    exit();
}

// Retrieve session variables
$school_name = $_SESSION['school_name'] ?? '';
$emis_code = $_SESSION['emis_code'];
$cate_id = $_SESSION['cate_id'] ?? '';
$level_id = $_SESSION['level_id'] ?? '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['school_building_submit'])) {
    // Collect form data and store in session
    $_SESSION['building_select'] = $_POST['building_select'];
    $_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'];
    $_SESSION['nobuilding_reason'] = $_POST['nobuilding_reason'];
    
    // Collect room details
    $room_types = $_POST['room_type'] ?? [];
    $total_rooms = $_POST['total_rooms'] ?? [];
    $room_conditions = $_POST['room_condition'] ?? [];
    
    $_SESSION['rooms'] = [];
    for ($i = 0; $i < count($room_types); $i++) {
        $_SESSION['rooms'][] = [
            'room_type' => $room_types[$i],
            'total_rooms' => $total_rooms[$i],
            'room_condition' => $room_conditions[$i],
        ];
    }

    // Redirect to the next step
    header("Location: school_facilities.php"); // Move to Step 4
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>School Building</title>
    <style>
        #formbuilding1, #formbuilding2 {
            display: none;
        }
        
        .add-room-btn-building {
            margin: 10px 0;
            background-color: #007bff;
            width: 20%;
            padding: 5px;
            border-radius: 4px;
            color: white;
            border: none;
        }
        .remove-room-btn-building {
            background-color: #dc3545;
            cursor: pointer;
            width: 60%;
            padding: 2px;
            border-radius: 4px;
            color: white;
            border: none;
        }
    </style>
</head>
<body>

<div align="center">
    <form method="POST">

        <br><br>
        <label for="building_select">Building</label>
        <select id="building_select" name="building_select" onchange="showFormbuilding(this.value)" style="width:200px">
            <option value="">Select Yes/No.............</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
        </select>
        <br><br>

        <div id="formbuilding1">
            <table align="center" border="1" width="80%">
                <tr>
                    <td><label>Ownership status</label></td>
                    <td>
                        <select id="ownershiptype" name="ownershiptype" style="width:200px">
                            <option value="">Select ownership.....</option>
                            <option value="Govt.">Government</option>
                            <option value="Rental">Rental</option>
                            <option value="Without Rent">Without Rent</option>
                            <option value="Donated">Donated</option>
                            <option value="Masjid">Masjid</option>
                            <option value="In Other school building">In Other school building</option>
                        </select>
                    </td>
                    <td><label>If rental</label></td>
                    <td>
                        <input id="rental_payee" name="rental_payee" size="25" placeholder="who pays">
                    </td>
                </tr>
                <tr>
                    <td><label>Construction year:</label></td>
                    <td>
                        <input id="construction_year" name="construction_year" type="number" style="width:194px" min="1900" max="2050" placeholder="Initial construction year">
                    </td>
                    <td><label>Building Condition</label></td>
                    <td>
                        <select name="building_condition[]" style="width:200px">
                            <option value="">Select condition...</option>
                            <option value="Satisfactory">Satisfactory</option>
                            <option value="Need Repair">Need Repair</option>
                            <option value="Need construction">Need Construction</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><label>Construction Material</label></td>
                    <td>
                        <select id="cons_material" name="cons_material" style="width:200px">
                            <option value="">Constructed with ....</option>
                            <option value="Cemented">Cemented</option>
                            <option value="Mud/wood">Mud/wood</option>
                            <option value="Pre Febricated">Pre Febricated</option>
                            <option value="Shelter">Shelter</option>
                            <option value="Wood/Iron sheet">Wood/Iron sheet</option>
                            <option value="Mixed">Mixed</option>
                        </select>
                    </td>
                    <td><label>Boundary Wall</label></td>
                    <td>
                        <select name="boundry_wall[]" style="width:200px">
                            <option value="">Boundary wall present? ....</option>
                            <option value="yes">Yes</option>
                            <option value="no">No</option>
                        </select>
                    </td>
                </tr>
            </table>
            <br>
            <h2>Rooms Detail</h2>
            <table id="roomsTable" align="center" border="1" width="80%">
                <tr align="center" style="font-weight:bold">
                    <td><label for="room_type">Room Type</label></td>
                    <td><label for="total_rooms">Total</label></td>
                    <td><label for="room_condition">Condition</label></td>
                    <td><label>Actions</label></td>
                </tr>
            </table>
            <table border="1" width="80%">
                <tr>
                    <td colspan="4" align="center">
                        <button type="button" class="add-room-btn-building" onclick="addRoomDetailRow()">Add Room</button>
                    </td>
                </tr>
            </table>
        </div>

        <div id="formbuilding2">
            <input name="nobuilding_reason" type="text" id="nobuilding_reason" placeholder="Reason why there is no building" size="140">
        </div>
        
        <br><br>
        <input type="submit" name="school_building_submit" value="Next">
        
    </form>

    <script>
        function showFormbuilding(value) {
            document.getElementById('formbuilding1').style.display = value === 'yes' ? 'block' : 'none';
            document.getElementById('formbuilding2').style.display = value === 'no' ? 'block' : 'none';
        }

        function addRoomDetailRow() {
            const table = document.getElementById('roomsTable');
            const newRow = document.createElement('tr');
            
            newRow.innerHTML = `
                <td>
                    <select name="room_type[]">
                        <option value="">Select room type...</option>
                        <option value="Heads Office">Heads Office</option>
                        <option value="Staff Room">Staff Room</option>
                        <option value="Clerk Office">Clerk Office</option>
                        <option value="Library">Library</option>
                        <option value="Labs">Labs</option>
                        <option value="Class Room">Class Room</option>
                        <option value="Hall">Hall</option>
                        <option value="Store">Store</option>
                        <option value="Kitchen">Kitchen</option>
                        <option value="Residential Room">Residential Room</option>
                        <option value="Hostel">Hostel</option>
                        <option value="Canteen">Canteen</option>
                        <option value="Security Room">Security Room</option>
                        <option value="Staff Washrooms">Staff Washrooms</option>
                        <option value="Students Washrooms">Students Washrooms</option>
                    </select>
                </td>
                <td>
                    <input name="total_rooms[]" type="number" size="10" min="0" placeholder="Total">
                </td>
                <td>
                    <select name="room_condition[]">
                        <option value="">Select condition...</option>
                        <option value="Satisfactory">Satisfactory</option>
                        <option value="Need Repair">Need Repair</option>
                        <option value="Need Construction">Need Construction</option>
                    </select>
                </td>
                <td align="center">
                    <button type="button" class="remove-room-btn-building" onclick="removeRoomDetailRow(this)">Remove</button>
                </td>
            `;
            table.appendChild(newRow);
        }

        function removeRoomDetailRow(button) {
            const row = button.parentElement.parentElement;
            row.remove();
        }
    </script>
</div>

</body>
</html>
