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

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

$result = $conn->query("SELECT * FROM articles ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title>หน้าแรกนักเรียน</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f4f4f9;
            color: #333;
        }
        h2 {
            color: #2c3e50;
        }
        a {
            color: #2980b9;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
        .logout {
            float: right;
            margin-top: -40px;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        li {
            background: #fff;
            margin-bottom: 10px;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0px 2px 5px rgba(0,0,0,0.1);
        }
        li a {
            font-weight: bold;
        }
        .welcome {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="welcome">
        <h2>ข่าวสารสำหรับนักเรียน</h2>
        <p>สวัสดี, <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong></p>
        <a href="../admin/logout.php">ออกจากระบบ</a>
    </div>

    <ul>
        <?php while($row = $result->fetch_assoc()): ?>
            <li>
                <?php echo htmlspecialchars($row['title']); ?> 
                - <a href="article.php?id=<?php echo $row['id']; ?>">อ่านต่อ</a>
            </li>
        <?php endwhile; ?>
    </ul>
</body>
</html>