<?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>
<link href="https://fonts.googleapis.com/css2?family=Prompt:wght@400;600&display=swap" rel="stylesheet">
<style>
*{margin:0;padding:0;box-sizing:border-box;font-family:'Prompt',sans-serif;}
body{background:#f0f3f9;color:#333;}

/* Header */
.header{
    background:linear-gradient(90deg,#36d1dc,#5b86e5);
    color:#fff;padding:20px;
    border-bottom-left-radius:25px;border-bottom-right-radius:25px;
    box-shadow:0 4px 12px rgba(0,0,0,0.2);
    text-align:center;margin-bottom:30px;
}
.header h2{font-size:26px;margin-bottom:8px;}
.header p{font-size:16px;}
.header .logout{
    display:inline-block;margin-top:12px;
    background:#e74c3c;color:#fff;
    padding:8px 16px;border-radius:25px;text-decoration:none;
    transition:0.3s;
}
.header .logout:hover{background:#c0392b;}

/* News Grid */
.container{max-width:1100px;margin:0 auto;padding:20px;}
.grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(280px,1fr));
    gap:20px;
}
.card{
    background:#fff;border-radius:12px;padding:18px;
    box-shadow:0 4px 12px rgba(0,0,0,0.08);
    transition:transform 0.25s,box-shadow 0.25s;
}
.card:hover{
    transform:translateY(-5px);
    box-shadow:0 6px 16px rgba(0,0,0,0.15);
}
.card h3{font-size:18px;margin-bottom:12px;color:#2c3e50;}
.card a.readmore{
    display:inline-block;
    background:#2980b9;color:#fff;
    padding:6px 14px;border-radius:20px;
    font-size:14px;text-decoration:none;
    transition:0.3s;
}
.card a.readmore:hover{background:#1f6391;}
</style>
</head>
<body>

<div class="header">
    <h2>ข่าวสารสำหรับนักเรียน</h2>
    <p>สวัสดี 👋 <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong></p>
    <a class="logout" href="../admin/logout.php">ออกจากระบบ</a>
</div>

<div class="container">
    <div class="grid">
        <?php while($row = $result->fetch_assoc()): ?>
        <div class="card">
            <h3><?php echo htmlspecialchars($row['title']); ?></h3>
            <a class="readmore" href="article.php?id=<?php echo $row['id']; ?>">อ่านต่อ</a>
        </div>
        <?php endwhile; ?>
    </div>
</div>

</body>
</html>
