<?php 
include 'connect.php'; 
include 'header.php'; 
?>

<div class="container">
    <h2 style="color: #4a148c; border-left: 5px solid #ffd54f; padding-left: 15px; margin: 30px 0;">ข่าวสารและกิจกรรมงานบุญ</h2>
    
    <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(450px, 1fr)); gap: 25px;">
        <?php
        $sql = "SELECT * FROM news ORDER BY id DESC";
        $result = $conn->query($sql);
        
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo "<div class='news-card' style='background: white; border: 1px solid #eee; padding: 20px; border-radius: 15px; box-shadow: 0 4px 10px rgba(0,0,0,0.05); transition: 0.3s; display: flex; flex-direction: column;'>";
                
                // --- แสดงรูปภาพประกอบข่าว ---
                if (!empty($row["image"])) {
                    echo "<div style='width: 100%; height: 250px; overflow: hidden; border-radius: 10px; margin-bottom: 15px;'>";
                    echo "<img src='uploads/" . $row["image"] . "' style='width: 100%; height: 100%; object-fit: cover;' alt='ข่าววัดดับภัย'>";
                    echo "</div>";
                }
                
                echo "<h3 style='color: #8e24aa; margin-top: 0;'>" . $row["title"] . "</h3>";
                echo "<p style='color: #555; line-height: 1.6; flex-grow: 1;'>" . nl2br($row["detail"]) . "</p>";
                
                echo "<hr style='border: 0; border-top: 1px solid #eee; margin: 15px 0;'>";
                
                // ตรวจสอบชื่อคอลัมน์วันที่
                $date_column = isset($row["created_at"]) ? $row["created_at"] : (isset($row["date_at"]) ? $row["date_at"] : "ไม่ระบุวันที่");
                
                echo "<div style='display: flex; justify-content: space-between; align-items: center;'>";
                    echo "<small style='color: #999;'>📅 " . $date_column . "</small>";
                    
                    // --- ส่วนที่เพิ่มใหม่: ปุ่มจัดการสำหรับ Admin เท่านั้น ---
                    if(isset($_SESSION['username']) && $_SESSION['username'] === 'admin'){
                        echo "<div>";
                            echo "<a href='edit_news.php?id=" . $row['id'] . "' style='text-decoration: none; background: #fff3e0; color: #f57c00; padding: 5px 12px; border-radius: 15px; font-size: 0.85rem; font-weight: bold; margin-right: 5px;'>✏️ แก้ไข</a>";
                            echo "<a href='delete_news.php?id=" . $row['id'] . "' onclick='return confirm(\"ยืนยันการลบข่าวสารนี้หรือไม่?\")' style='text-decoration: none; background: #ffebee; color: #d32f2f; padding: 5px 12px; border-radius: 15px; font-size: 0.85rem; font-weight: bold;'>🗑️ ลบ</a>";
                        echo "</div>";
                    }
                echo "</div>";
                
                echo "</div>";
            }
        } else {
            echo "<div style='grid-column: 1/-1; text-align: center; padding: 50px; background: white; border-radius: 15px;'>";
            echo "<p style='color: #999;'>ยังไม่มีข่าวสารอัปเดตในขณะนี้</p>";
            echo "</div>";
        }
        ?>
    </div>
</div>

<style>
    .news-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 25px rgba(0,0,0,0.1) !important;
    }
    /* ปรับแต่งปุ่มเวลาเอาเมาส์วาง */
    .news-card a:hover {
        opacity: 0.8;
    }
</style>

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