<?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: 'Prompt', sans-serif;
        margin: 0;
        background: #eef2f7;
        color: #333;
    }
    header {
        background: #047857;
        color: #fff;
        padding: 18px 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        box-shadow: 0 3px 10px rgba(0,0,0,0.15);
    }
    header h1 {
        margin: 0;
        font-size: 22px;
    }
    header a {
        color: #fff;
        text-decoration: none;
        background: #dc2626;
        padding: 8px 15px;
        border-radius: 6px;
        transition: background 0.3s;
    }
    header a:hover {
        background: #b91c1c;
    }

    .container {
        max-width: 1100px;
        margin: 30px auto;
        padding: 20px;
        background: #fff;
        border-radius: 12px;
        box-shadow: 0 5px 20px rgba(0,0,0,0.08);
    }
    h2 {
        text-align: center;
        margin-bottom: 15px;
        color: #065f46;
    }
    p.welcome {
        text-align: center;
        font-size: 16px;
        margin-bottom: 25px;
        color: #444;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        border-radius: 10px;
        overflow: hidden;
    }
    thead {
        background: #047857;
        color: #fff;
    }
    th, td {
        padding: 12px 15px;
        text-align: left;
    }
    tr:nth-child(even) {
        background: #f9fafb;
    }
    tr:hover {
        background: #ecfdf5;
    }
    .status {
        font-weight: bold;
        padding: 4px 8px;
        border-radius: 5px;
    }
    .status.active { background: #d1fae5; color: #065f46; }
    .status.inactive { background: #fee2e2; color: #b91c1c; }

    .btn {
        padding: 6px 12px;
        border-radius: 6px;
        text-decoration: none;
        color: #fff;
        font-size: 14px;
        transition: background 0.3s;
    }
    .edit { background: #2563eb; }
    .edit:hover { background: #1e3a8a; }
    .delete { background: #dc2626; }
    .delete:hover { background: #991b1b; }

    @media (max-width: 768px) {
        table, thead, tbody, th, td, tr {
            display: block;
            width: 100%;
        }
        thead { display: none; }
        tr {
            margin-bottom: 15px;
            background: #f9fafb;
            border-radius: 8px;
            padding: 12px;
        }
        td {
            padding: 8px;
            display: flex;
            justify-content: space-between;
            border: none;
        }
        td::before {
            content: attr(data-label);
            font-weight: bold;
            color: #555;
        }
    }
</style>
</head>
<body>
<header>
    <h1>📚 แดชบอร์ดอาจารย์</h1>
    <a href="../admin/logout.php">ออกจากระบบ</a>
</header>

<div class="container">
    <h2>ข่าวทั้งหมด</h2>
    <p class="welcome">ยินดีต้อนรับ, <?php echo htmlspecialchars($_SESSION['username']); ?> 👋</p>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>หัวข้อข่าว</th>
                <th>หมวดข่าว</th>
                <th>สถานะ</th>
                <th>วันที่สร้าง</th>
                <th>จัดการ</th>
            </tr>
        </thead>
        <tbody>
        <?php while($row = $result->fetch_assoc()): ?>
            <tr>
                <td data-label="ID"><?php echo $row['id']; ?></td>
                <td data-label="หัวข้อข่าว"><?php echo htmlspecialchars($row['title']); ?></td>
                <td data-label="หมวดข่าว"><?php echo htmlspecialchars($row['category_name']); ?></td>
                <td data-label="สถานะ">
                    <span class="status <?php echo $row['status']=='active'?'active':'inactive'; ?>">
                        <?php echo $row['status']; ?>
                    </span>
                </td>
                <td data-label="วันที่สร้าง"><?php echo $row['created_at']; ?></td>
                <td data-label="จัดการ">
                    <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; ?>
        </tbody>
    </table>
</div>
</body>
</html>
