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

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

/* =========================
   GET HOMEWORK ID
========================= */
if (!isset($_GET['id'])) {
    header("Location: manage_homework.php");
    exit();
}

$homework_id = (int) $_GET['id'];
$message = "";

/* =========================
   FETCH HOMEWORK DATA
========================= */
$stmt = $conn->prepare("SELECT * FROM homework WHERE homework_id = ?");
$stmt->bind_param("i", $homework_id);
$stmt->execute();
$result = $stmt->get_result();
$homework = $result->fetch_assoc();

if (!$homework) {
    die("Homework not found.");
}

/* =========================
   UPDATE HOMEWORK
========================= */
if (isset($_POST['update_homework'])) {

    $subject_id = $_POST['subject_id'];
    $teacher_id = $_POST['teacher_id'];
    $class_id = $_POST['class_id'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    $due_date = $_POST['due_date'];

    $sql = "UPDATE homework 
            SET subject_id=?, teacher_id=?, class_id=?, title=?, description=?, due_date=? 
            WHERE homework_id=?";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iiisssi", $subject_id, $teacher_id, $class_id, $title, $description, $due_date, $homework_id);

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

/* =========================
   FETCH DROPDOWN DATA
========================= */
$subjects = $conn->query("SELECT * FROM subject")->fetch_all(MYSQLI_ASSOC);
$teachers = $conn->query("SELECT * FROM teachers")->fetch_all(MYSQLI_ASSOC);
$classes = $conn->query("SELECT * FROM classes")->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Homework</title>

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

.header { background:#1e293b; color:white; padding:15px; }
.container {
    max-width:800px;
    margin:30px auto;
    background:white;
    padding:25px;
    border-radius:12px;
}

input, select, textarea {
    width:100%;
    padding:10px;
    margin-top:5px;
    margin-bottom:15px;
}

button {
    padding:10px;
    background:#2563eb;
    color:white;
    border:none;
    border-radius:6px;
}

.back-btn {
    display:inline-block;
    margin-bottom:15px;
    background:#111827;
    color:white;
    padding:10px;
    text-decoration:none;
}
</style>
</head>

<body>

<div class="header">
    <h2>AGID TECH</h2>
</div>

<div class="container">

<a href="manage_homework.php" class="back-btn">← Back</a>

<h2>Edit Homework</h2>

<?php if ($message): ?>
<p><?php echo $message; ?></p>
<?php endif; ?>

<form method="POST">

<label>Subject</label>
<select name="subject_id">
<?php foreach ($subjects as $s): ?>
<option value="<?= $s['subject_id'] ?>" <?= $s['subject_id']==$homework['subject_id']?'selected':'' ?>>
<?= $s['subject_name'] ?>
</option>
<?php endforeach; ?>
</select>

<label>Teacher</label>
<select name="teacher_id">
<?php foreach ($teachers as $t): ?>
<option value="<?= $t['teacher_id'] ?>" <?= $t['teacher_id']==$homework['teacher_id']?'selected':'' ?>>
<?= $t['name'] ?>
</option>
<?php endforeach; ?>
</select>

<label>Class</label>
<select name="class_id">
<?php foreach ($classes as $c): ?>
<option value="<?= $c['class_id'] ?>" <?= $c['class_id']==$homework['class_id']?'selected':'' ?>>
<?= $c['class_name'] ?>
</option>
<?php endforeach; ?>
</select>

<label>Title</label>
<input type="text" name="title" value="<?= $homework['title'] ?>" required>

<label>Description</label>
<textarea name="description"><?= $homework['description'] ?></textarea>

<label>Due Date</label>
<input type="date" name="due_date" value="<?= $homework['due_date'] ?>" required>

<button name="update_homework">Update Homework</button>

</form>

</div>

</body>
</html>