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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Basic sanitization
    $edit_id = intval($_POST['edit_id']);
    $edit_session = trim($_POST['edit_session']); // Might not be used
    $edit_term = trim($_POST['edit_term']);
    $start_date = trim($_POST['edit_start_date']);
    $end_date = trim($_POST['edit_end_date']);
    $session_id = intval($_POST['session_id']);

    // Check if required fields are not empty
    if (!$edit_id || !$edit_term || !$start_date || !$end_date || !$session_id) {
        echo "missing_fields";
        exit;
    }

    // Prepare and execute the update
    $stmt = $con->prepare("UPDATE school_terms SET session_id = ?, term_name = ?, start_date = ?, end_date = ? WHERE id = ?");
    $stmt->bind_param("isssi", $session_id, $edit_term, $start_date, $end_date, $edit_id);

    if ($stmt->execute()) {
        echo "success";
    } else {
        // Debug only: show error details (remove in production)
        echo "error";
    }

    $stmt->close();
}
