<?php
session_start();
include 'db_connection.php';

/* ================= AUTH ================= */
if (
    !isset($_SESSION['student_id']) ||
    !isset($_SESSION['class_id']) ||
    $_SESSION['role'] !== 'student'
) {
    header("Location: student_login.php?error=Unauthorized");
    exit();
}

$class_id = $_SESSION['class_id'];

/* ================= FETCH HOMEWORK ================= */
$stmt = $conn->prepare("
    SELECT homework_id, title, description, due_date 
    FROM homework 
    WHERE class_id = ?
    ORDER BY due_date ASC
");
$stmt->bind_param("i", $class_id);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Homework</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body {
    background: linear-gradient(135deg, #eef2f7, #dbe9f4);
    font-family: 'Segoe UI', sans-serif;
}

/* HEADER */
.header {
    background: linear-gradient(to right, #4facfe, #00f2fe);
    color: white;
    padding: 20px;
    text-align: center;
    border-radius: 0 0 15px 15px;
}

/* CARD */
.homework-card {
    border-radius: 15px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.homework-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}

/* BADGES */
.badge-due {
    background: #28a745;
}

.badge-overdue {
    background: #dc3545;
}

/* BUTTONS */
.btn-custom {
    border-radius: 25px;
    padding: 8px 15px;
}

.empty-state {
    text-align: center;
    margin-top: 50px;
    color: #777;
}
</style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <h2><i class="fas fa-book-open"></i> Homework</h2>
    <p>Stay on track with your assignments</p>
</div>

<div class="container mt-4">

<?php if ($result->num_rows > 0): ?>
    <div class="row g-4">

    <?php while ($row = $result->fetch_assoc()): 
        $due_date = date("d M Y", strtotime($row['due_date']));
        $is_overdue = strtotime($row['due_date']) < time();
    ?>

        <div class="col-md-6 col-lg-4">
            <div class="card homework-card shadow-sm p-3">

                <h5 class="fw-bold"><?php echo htmlspecialchars($row['title']); ?></h5>

                <p class="text-muted">
                    <?php echo nl2br(htmlspecialchars($row['description'])); ?>
                </p>

                <div class="d-flex justify-content-between align-items-center mb-2">
                    <span class="badge <?php echo $is_overdue ? 'badge-overdue' : 'badge-due'; ?>">
                        <?php echo $is_overdue ? 'Overdue' : 'Due Soon'; ?>
                    </span>

                    <small class="text-muted">
                        <i class="fas fa-calendar"></i> <?php echo $due_date; ?>
                    </small>
                </div>

                <div class="d-flex justify-content-between mt-3">
                    <a href="submit_homework.php?homework_id=<?php echo $row['homework_id']; ?>" 
                       class="btn btn-primary btn-sm btn-custom">
                        <i class="fas fa-upload"></i> Submit
                    </a>

                    <a href="view_feedback.php?homework_id=<?php echo $row['homework_id']; ?>" 
                       class="btn btn-outline-secondary btn-sm btn-custom">
                        <i class="fas fa-comment"></i> Feedback
                    </a>
                </div>

            </div>
        </div>

    <?php endwhile; ?>

    </div>

<?php else: ?>

    <div class="empty-state">
        <i class="fas fa-book fa-3x mb-3"></i>
        <h4>No Homework Available</h4>
        <p>You’re all caught up 🎉</p>
    </div>

<?php endif; ?>

</div>

</body>
</html>

<?php
$stmt->close();
$conn->close();
?>