<?php
session_start();
require_once("../config.php");

if (!isset($_SESSION['admin_login'])) {
    header("Location: ../index.php");
    exit();
}

// ดึงข่าวทั้งหมด
$result = $conn->query("SELECT * FROM news ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
</head>
<body>

<h1>Admin Dashboard</h1>
<a href="add_news.php">➕ เพิ่มข่าว</a> |
<a href="logout.php">Logout</a>

<hr>

<h2>รายการข่าว</h2>

<table border="1" cellpadding="5">
    <tr>
        <th>ID</th>
        <th>หัวข้อ</th>
        <th>จัดการ</th>
    </tr>

    <?php while ($row = $result->fetch_assoc()): ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= htmlspecialchars($row['title']) ?></td>
        <td>
            <a href="delete_news.php?id=<?= $row['id'] ?>"
               onclick="return confirm('ลบข่าวนี้?')">
               ลบ
            </a>
        </td>
    </tr>
    <?php endwhile; ?>
</table>

</body>
</html>