<?php
session_start();
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] != 'admin' && $_SESSION['role'] != 'teacher')) {
    header("Location: ../login.php");
    exit();
}

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

// ดึงข่าวทั้งหมด พร้อมชื่อหมวด
$sql = "SELECT a.id, a.title, a.slug, a.status, a.created_at, c.name AS category_name 
        FROM articles a
        JOIN categories c ON a.category_id = c.id
        ORDER BY a.created_at DESC";
$articles = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>จัดการข่าว</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<style>
    * {margin:0;padding:0;box-sizing:border-box;font-family:'Poppins',sans-serif;}
    body {
        background:#f0f2f5;
        padding:20px;
    }
    .container {
        max-width:1200px;
        margin:auto;
        background:#fff;
        padding:30px;
        border-radius:15px;
        box-shadow:0 8px 20px rgba(0,0,0,0.1);
    }
    h2 {
        text-align:center;
        color:#333;
        font-size:28px;
        margin-bottom:15px;
    }
    p {
        text-align:center;
        margin-bottom:20px;
        color:#555;
    }
    p a {
        color:#dc3545;
        text-decoration:none;
        font-weight:600;
    }
    p a:hover {
        text-decoration:underline;
    }
    .top-links {
        text-align:center;
        margin-bottom:25px;
    }
    .btn-primary {
        background:#007bff;
        color:#fff;
        padding:10px 20px;
        border-radius:8px;
        text-decoration:none;
        font-weight:600;
        transition:0.3s;
    }
    .btn-primary:hover {
        background:#0056b3;
    }
    table {
        width:100%;
        border-collapse:collapse;
        margin-top:10px;
    }
    th, td {
        padding:14px;
        border-bottom:1px solid #ddd;
        text-align:left;
        font-size:15px;
    }
    th {
        background:#007bff;
        color:#fff;
        font-size:16px;
    }
    tr:hover {
        background:#f9f9f9;
    }
    .status-badge {
        padding:6px 12px;
        border-radius:20px;
        font-size:13px;
        font-weight:600;
        text-transform:capitalize;
    }
    .draft {
        background:#ffeeba;
        color:#856404;
    }
    .published {
        background:#c3e6cb;
        color:#155724;
    }
    .actions a {
        display:inline-block;
        padding:8px 12px;
        border-radius:6px;
        color:#fff;
        font-size:14px;
        margin-right:5px;
        text-decoration:none;
        transition:0.3s;
    }
    .edit {
        background:#28a745;
    }
    .edit:hover {
        background:#218838;
    }
    .delete {
        background:#dc3545;
    }
    .delete:hover {
        background:#c82333;
    }
    @media(max-width:768px){
        table,th,td {
            font-size:13px;
        }
        .btn-primary { padding:8px 15px; }
    }
</style>
</head>
<body>
<div class="container">
    <h2>📑 จัดการข่าว</h2>
    <p>
        สวัสดี, <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong> |
        <a href="../admin/logout.php">ออกจากระบบ</a>
    </p>

    <div class="top-links">
        <a href="add_article.php" class="btn-primary">➕ เพิ่มข่าวใหม่</a>
    </div>

    <table>
        <tr>
            <th>ID</th>
            <th>หัวข้อข่าว</th>
            <th>หมวดข่าว</th>
            <th>สถานะ</th>
            <th>วันที่สร้าง</th>
            <th>จัดการ</th>
        </tr>
        <?php while($row = $articles->fetch_assoc()): ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo htmlspecialchars($row['title']); ?></td>
            <td><?php echo htmlspecialchars($row['category_name']); ?></td>
            <td>
                <span class="status-badge <?php echo $row['status']; ?>">
                    <?php echo $row['status']; ?>
                </span>
            </td>
            <td><?php echo $row['created_at']; ?></td>
            <td class="actions">
                <a class="edit" href="edit_article.php?id=<?php echo $row['id']; ?>">✏️ แก้ไข</a>
                <a class="delete" href="delete_article.php?id=<?php echo $row['id']; ?>" onclick="return confirm('ยืนยันลบข่าวนี้?')">🗑️ ลบ</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </table>
</div>
</body>
</html>
