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

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

// ดึงข่าวทั้งหมด
$result = $conn->query("SELECT a.*, c.name AS category_name FROM articles a JOIN categories c ON a.category_id=c.id ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>แดชบอร์ดอาจารย์</title>
<style>
    body {
        font-family: Arial, sans-serif;
        background: #f0f2f5;
        margin: 0;
        padding: 20px;
    }
    .container {
        max-width: 1000px;
        margin: auto;
        background: #fff;
        padding: 30px;
        border-radius: 10px;
        box-shadow: 0 0 15px rgba(0,0,0,0.1);
    }
    h2 {
        text-align: center;
        color: #333;
    }
    .top-links {
        margin-bottom: 20px;
        text-align: center;
    }
    .top-links a {
        display: inline-block;
        margin: 0 10px;
        padding: 8px 15px;
        background: #007BFF;
        color: #fff;
        text-decoration: none;
        border-radius: 5px;
        transition: background 0.3s;
    }
    .top-links a:hover {
        background: #0056b3;
    }
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 15px;
    }
    th, td {
        border: 1px solid #ddd;
        padding: 12px;
        text-align: left;
    }
    th {
        background: #007BFF;
        color: #fff;
    }
    tr:nth-child(even) {
        background: #f9f9f9;
    }
    .btn {
        padding: 6px 12px;
        border-radius: 5px;
        color: #fff;
        text-decoration: none;
        margin-right: 5px;
    }
    .edit {
        background: #28a745;
    }
    .edit:hover {
        background: #218838;
    }
    .delete {
        background: #dc3545;
    }
    .delete:hover {
        background: #c82333;
    }
    @media (max-width: 768px) {
        table, th, td {
            font-size: 14px;
        }
        .top-links a {
            padding: 6px 10px;
            margin: 5px 3px;
        }
    }
</style>
</head>
<body>
<div class="container">
    <h2>แดชบอร์ดอาจารย์</h2>
    <p style="text-align:center;">สวัสดี, <?php echo htmlspecialchars($_SESSION['username']); ?></p>
    <div class="top-links">
        <a href="../admin/logout.php">ออกจากระบบ</a>
        
    </div>

    <h3>ข่าวทั้งหมด</h3>
    <table>
        <tr>
            <th>ID</th>
            <th>หัวข้อข่าว</th>
            <th>หมวดข่าว</th>
            <th>สถานะ</th>
            <th>วันที่สร้าง</th>
            <th>จัดการ</th>
        </tr>
        <?php while($row = $result->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><?php echo $row['status']; ?></td>
            <td><?php echo $row['created_at']; ?></td>
            <td>
                <a class="btn edit" href="edit_article.php?id=<?php echo $row['id']; ?>">แก้ไข</a>
                <a class="btn delete" href="delete_article.php?id=<?php echo $row['id']; ?>" onclick="return confirm('ยืนยันลบข่าวนี้?')">ลบ</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </table>
</div>
</body>
</html>
