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

$message = $_POST['message'] ?? '';

// Check if message already exists
$query = "SELECT id FROM president_messages LIMIT 1";
$result = $mysqli->query($query);

if ($result && $result->num_rows > 0) {
    // UPDATE
    $row = $result->fetch_assoc();
    $id = $row['id'];

    $stmt = $mysqli->prepare("UPDATE president_messages SET message = ? WHERE id = ?");
    $stmt->bind_param("si", $message, $id);
    $stmt->execute();
    $stmt->close();

} else {
    // INSERT
    $stmt = $mysqli->prepare("INSERT INTO president_messages (message) VALUES (?)");
    $stmt->bind_param("s", $message);
    $stmt->execute();
    $stmt->close();
}

$mysqli->close();

// Redirect after insert/update
header("Location: manage_pmsg.php");
exit;
