<?php
include 'config.php';
if($_SESSION['user_role'] != 'SuperAdmin') exit();

// HANDLE DELETE
if(isset($_GET['delete'])){
    $id = (int)$_GET['delete'];
    $conn->query("DELETE FROM announcements WHERE id = $id");
    header("Location: broadcast.php?msg=deleted");
    exit();
}

// HANDLE NEW MESSAGE
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title'];
    $msg = $_POST['message'];

    $stmt = $conn->prepare("INSERT INTO announcements (title, message) VALUES (?, ?)");
    $stmt->bind_param("ss", $title, $msg);
    
    if($stmt->execute()){
        header("Location: broadcast.php?success=1");
    } else {
        echo "Error: " . $conn->error;
    }
}
?>