<?php
include(__DIR__ . '/../config/db.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $article_id = intval($_POST['article_id']);
    $author_name = htmlspecialchars(trim($_POST['author_name']));
    $comment = htmlspecialchars(trim($_POST['comment']));

    if($article_id > 0 && !empty($author_name) && !empty($comment)){
        $stmt = $conn->prepare("INSERT INTO comments (article_id, author_name, comment, is_approved) VALUES (?, ?, ?, 0)");
        $stmt->bind_param("iss", $article_id, $author_name, $comment);
        $stmt->execute();
        $stmt->close();
    }

    header("Location: article.php?id=".$article_id);
    exit();
}
