<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}
require 'dbconn.php';

// Get and sanitize POST data
$id            = isset($_POST['id']) ? (int)$_POST['id'] : 0;
$circularTitle = trim($_POST['news_title']);
$circularDate  = trim($_POST['news_date']);
$description   = trim($_POST['desc']);

// Validate
if (empty($id) || empty($circularTitle) || empty($circularDate) || empty($description)) {
    die("All fields are required.");
}

// Prepare and execute update query
$query = "UPDATE circulars_tb SET circular_title = ?, circular_date = ?, description = ? WHERE id = ?";
$stmt  = $mysqli->prepare($query);
$stmt->bind_param("sssi", $circularTitle, $circularDate, $description, $id);

if ($stmt->execute()) {
    // Redirect after success
    header("Location: manage_circulars.php?updated=1");
    exit;
} else {
    echo "Error updating circular: " . $stmt->error;
}
?>
