<?php 
require 'db.php';
$alert = "";

if (isset($_POST['save'])) {
    $title = trim($_POST['title']);
    $content = trim($_POST['content']);

    if (!empty($title) && !empty($content)) {
        $stmt = $conn->prepare("INSERT INTO news (title, content, created_at) VALUES (?, ?, NOW())");
        $stmt->bind_param("ss", $title, $content);
        if ($stmt->execute()) {
            echo "<script>alert('บันทึกสำเร็จ'); window.location.href='news.php';</script>";
            exit;
        } else {
            $alert = "<div style='color:red;'>Error: " . $conn->error . "</div>";
        }
    }
}
$pageTitle = "เพิ่มข่าวประกาศ"; $headerTitle = "ระบบจัดการข่าวสาร"; 
include 'header.php'; 
?>

<div class="container" style="max-width: 800px;">
    <div class="card reveal">
        <h2 class="section-title">เพิ่มข่าวใหม่</h2>
        <?php echo $alert; ?>
        <form method="post">
            <label style="font-weight:bold; color:var(--primary);">หัวข้อข่าว</label>
            <input type="text" name="title" class="form-control" required>
            
            <label style="font-weight:bold; color:var(--primary);">รายละเอียด</label>
            <textarea name="content" rows="6" class="form-control" required></textarea>
            
            <button type="submit" name="save" class="btn">บันทึกข่าว</button>
            <a href="news.php" style="margin-left:15px; color:#666;">ยกเลิก</a>
        </form>
    </div>
</div>

<?php include 'footer.php'; ?>