<?php 
require 'db.php'; 
$pageTitle = "ข่าวประชาสัมพันธ์";
$headerTitle = "ข่าวสารจากทางวัด";
$headerSubtitle = "แจ้งกำหนดการและกิจกรรมต่างๆ";
include 'header.php'; 
?>

<div class="container">
    <div class="card reveal">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; padding-bottom: 15px; border-bottom: 2px solid #f0f0f0;">
            <h2 style="margin:0; border:none; padding:0; color:var(--primary);">ประกาศล่าสุด</h2>
            <a href="add_news.php" class="btn">+ เพิ่มข่าวประกาศ</a>
        </div>

        <?php
        $sql = "SELECT * FROM news ORDER BY created_at DESC";
        $result = $conn->query($sql);
        
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $th_month = array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
                $date = date_create($row['created_at']);
                $thai_date = date_format($date,"j") . " " . $th_month[date_format($date,"n")] . " " . (date_format($date,"Y")+543);

                echo "<div class='news-item'>";
                echo "<h3>" . htmlspecialchars($row['title']) . "</h3>";
                echo "<p style='color:#555;'>" . nl2br(htmlspecialchars($row['content'])) . "</p>";
                echo "<small>📅 " . $thai_date . "</small>";
                echo "</div>";
            }
        } else {
            echo "<p style='text-align:center; color:#999;'>ยังไม่มีข่าวประกาศ</p>";
        }
        ?>
    </div>
</div>

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