<?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 {
        margin: 0;
        padding: 0;
        font-family: "Poppins", sans-serif;
        background: linear-gradient(to right, #00c6ff, #0072ff);
        min-height: 100vh;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        padding: 30px;
        color: #333;
    }
    .container {
        width: 100%;
        max-width: 1100px;
        background: #fff;
        padding: 35px;
        border-radius: 20px;
        box-shadow: 0 10px 25px rgba(0,0,0,0.15);
        animation: fadeIn 0.6s ease-in-out;
    }
    h2 {
        text-align: center;
        font-size: 28px;
        margin-bottom: 10px;
        color: #0072ff;
    }
    p {
        text-align: center;
        color: #555;
        margin-bottom: 25px;
    }
    .top-links {
        text-align: center;
        margin-bottom: 25px;
    }
    .top-links a {
        display: inline-block;
        padding: 10px 20px;
        margin: 0 8px;
        background: linear-gradient(135deg, #00c6ff, #0072ff);
        color: #fff;
        border-radius: 8px;
        text-decoration: none;
        font-weight: bold;
        transition: 0.3s;
        box-shadow: 0 5px 15px rgba(0,114,255,0.3);
    }
    .top-links a:hover {
        transform: translateY(-3px);
        box-shadow: 0 8px 20px rgba(0,114,255,0.5);
    }

    h3 {
        margin-top: 20px;
        color: #0072ff;
        border-left: 6px solid #00c6ff;
        padding-left: 10px;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 15px;
        overflow: hidden;
        border-radius: 12px;
    }
    th, td {
        padding: 14px;
        text-align: left;
    }
    th {
        background: #0072ff;
        color: #fff;
        font-weight: 600;
    }
    tr:nth-child(even) {
        background: #f8f9fa;
    }
    tr:hover {
        background: #eaf4ff;
        transition: 0.2s;
    }
    .btn {
        display: inline-block;
        padding: 7px 15px;
        border-radius: 8px;
        color: #fff;
        text-decoration: none;
        font-size: 14px;
        font-weight: bold;
        transition: all 0.2s;
    }
    .edit {
        background: linear-gradient(135deg, #28a745, #5cd85c);
        box-shadow: 0 4px 10px rgba(40,167,69,0.3);
    }
    .edit:hover {
        transform: scale(1.05);
        background: linear-gradient(135deg, #218838, #4caf50);
    }
    .delete {
        background: linear-gradient(135deg, #dc3545, #ff6b6b);
        box-shadow: 0 4px 10px rgba(220,53,69,0.3);
    }
    .delete:hover {
        transform: scale(1.05);
        background: linear-gradient(135deg, #c82333, #e63946);
    }

    @media (max-width: 768px) {
        table, th, td {
            font-size: 13px;
        }
        .top-links a {
            display: block;
            margin: 10px auto;
        }
    }

    @keyframes fadeIn {
        from {opacity: 0; transform: translateY(20px);}
        to {opacity: 1; transform: translateY(0);}
    }
</style>
</head>
<body>
<div class="container">
    <h2>📚 แดชบอร์ดอาจารย์</h2>
    <p>สวัสดี, <?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>
