<?php
include('includes/dbconnection.php');

if (isset($_POST['edit_id'])) {
    $edit_id = intval($_POST['edit_id']);
    $stmt = $con->prepare("SELECT * FROM school_terms WHERE id = ?");
    $stmt->bind_param("i", $edit_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    $stmt->close();
?>
    <form id="updateEventForm">
        <input type="hidden" name="edit_id" value="<?php echo htmlentities($row['id']); ?>">
        <input type="hidden" name="session_id" value="<?php echo htmlentities($row['session_id']); ?>">
        <div class="form-group">
            <label>Session</label>
            <?php
                // Fetch the session name from school_sessions table
                $session_name = '';
                if (!empty($row['session_id'])) {
                    $session_stmt = $con->prepare("SELECT session_name FROM school_sessions WHERE id = ?");
                    $session_stmt->bind_param("i", $row['session_id']);
                    $session_stmt->execute();
                    $session_result = $session_stmt->get_result();
                    if ($session_row = $session_result->fetch_assoc()) {
                        $session_name = $session_row['session_name'];
                    }
                    $session_stmt->close();
                }
            ?>
            <input type="text" class="form-control" name="edit_session" value="<?php echo htmlentities($session_name); ?>" required readonly>
        </div>
        <div class="form-group">
            <label>Term</label>
            <input type="text" class="form-control" name="edit_term" value="<?php echo htmlentities($row['term_name']); ?>" required readonly>
        </div>
        <div class="form-group">
            <label>Start Date</label>
            <input type="date" class="form-control" name="edit_start_date" value="<?php echo htmlentities($row['start_date']); ?>" required>
        </div>
        <div class="form-group">
            <label>End Date</label>
            <input type="date" class="form-control" name="edit_end_date" value="<?php echo htmlentities($row['end_date']); ?>" required>
        </div>
        <button type="submit" class="btn btn-success">Update</button>
    </form>
<?php } ?>