<?php
include("connection.php");

if (isset($_POST['add'])) {
    $user_notice = $_POST['user_notice'];
    $currentDateTime = date('Y-m-d H:i:s');
    // Prepare the insert statement
    $sql = "INSERT INTO user_notice (user_notice, date_time) VALUES ('$user_notice', '$currentDateTime')";
    $data = mysqli_query($conn, $sql);
    
    if ($data) {
        header("Location: add_user_notice.php?msg=1");
        exit(); // Ensure that no further code is executed after the header redirection
    } else {
        header("Location: add_user_notice.php?msg=2");
        exit(); // Ensure that no further code is executed after the header redirection
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $itemId = $_POST['item_id'];
    
    if (isset($_POST['activate'])) {
        $status = 1;
    } elseif (isset($_POST['block'])) {
        $status = 0;
    }
    
    $updateQuery = "UPDATE user_notice SET status = $status WHERE id = $itemId";
    mysqli_query($conn, $updateQuery);
    
    // Redirect back to the view table page
    header('Location: add_user_notice.php');
    exit();
}
?>