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

/* =========================
   SECURITY CHECK
========================= */
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$message = "";

// DELETE HOMEWORK (SAFE)
if (isset($_GET['delete'])) {
    $id = (int) $_GET['delete'];

    if ($id > 0) {
        $stmt = $conn->prepare("DELETE FROM homework WHERE homework_id = ?");
        $stmt->bind_param("i", $id);

        if ($stmt->execute()) {
            header("Location: manage_homework.php?success=deleted");
            exit();
        } else {
            $message = "Error deleting homework.";
        }
    }
}

/* =========================
   FETCH HOMEWORK
========================= */
$sql = "
SELECT h.*, 
       s.subject_name, 
       t.name AS teacher_name, 
       c.class_name
FROM homework h
JOIN subject s ON h.subject_id = s.subject_id
JOIN teachers t ON h.teacher_id = t.teacher_id
JOIN classes c ON h.class_id = c.class_id
ORDER BY h.homework_id DESC
";

$result = $conn->query($sql);
$homeworks = $result->fetch_all(MYSQLI_ASSOC);
?>

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

<style>
body {
    font-family: Arial, sans-serif;
    background: #f4f6f9;
    margin: 0;
}

/* HEADER */
.header {
    background: #1e293b;
    color: white;
    padding: 15px 20px;
}

.header-container {
    max-width: 1100px;
    margin: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav {
    display: flex;
    gap: 15px;
}

.nav a {
    color: white;
    text-decoration: none;
    padding: 8px 12px;
    border-radius: 6px;
}

.nav a.active { background: #2563eb; }
.nav a.logout { background: #ef4444; }

/* CONTAINER */
.container {
    max-width: 1100px;
    margin: 30px auto;
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.08);
}

/* BUTTONS */
.back-btn {
    background: #111827;
    color: white;
    padding: 10px 15px;
    text-decoration: none;
    border-radius: 8px;
}

.add-btn {
    background: #10b981;
    color: white;
    padding: 10px 15px;
    text-decoration: none;
    border-radius: 8px;
    float: right;
}

/* TABLE */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

th, td {
    padding: 12px;
    border-bottom: 1px solid #eee;
    text-align: left;
}

th {
    background: #f9fafb;
}

tr:hover {
    background: #f1f5f9;
}

/* ACTION BUTTONS */
.action-btn {
    padding: 5px 10px;
    border-radius: 6px;
    text-decoration: none;
    font-size: 13px;
}

.edit {
    background: #f59e0b;
    color: white;
}

.delete {
    background: #ef4444;
    color: white;
}

/* MESSAGE */
.message {
    padding: 10px;
    background: #ecfdf5;
    border: 1px solid #10b981;
    color: #065f46;
    border-radius: 8px;
    margin-bottom: 15px;
}
</style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <div class="header-container">
        <h2>AGID TECH</h2>
        <div class="nav">
            <a href="admin_dashboard.php">Dashboard</a>
            <a href="add_homework.php">Add Homework</a>
            <a href="manage_homework.php" class="active">Manage Homework</a>
            <a href="logout.php" class="logout">Logout</a>
        </div>
    </div>
</div>

<!-- MAIN -->
<div class="container">

<a href="admin_dashboard.php" class="back-btn">← Dashboard</a>
<a href="add_homework.php" class="add-btn">+ Add Homework</a>

<h2>Manage Homework</h2>

<?php if ($message): ?>
    <div class="message"><?php echo $message; ?></div>
<?php endif; ?>

<?php if (!empty($homeworks)): ?>
<table>
    <thead>
        <tr>
            <th>Title</th>
            <th>Subject</th>
            <th>Teacher</th>
            <th>Class</th>
            <th>Due Date</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($homeworks as $hw): ?>
        <tr>
            <td><?php echo htmlspecialchars($hw['title']); ?></td>
            <td><?php echo htmlspecialchars($hw['subject_name']); ?></td>
            <td><?php echo htmlspecialchars($hw['teacher_name']); ?></td>
            <td><?php echo htmlspecialchars($hw['class_name']); ?></td>
            <td><?php echo $hw['due_date']; ?></td>
            <td>
                <a class="action-btn edit" href="edit_homework.php?id=<?php echo $hw['homework_id']; ?>">Edit</a>
                <a class="action-btn delete"
                   onclick="return confirm('Delete this homework?')"
                   href="?delete=<?php echo $hw['homework_id']; ?>">
                   Delete
                </a>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php else: ?>
    <p>No homework found.</p>
<?php endif; ?>

</div>

</body>
</html>