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

/* ===============================
   AUTH CHECK
================================ */
if (!isset($_SESSION['teacher_id']) || $_SESSION['role'] !== 'teacher') {
    header("Location: teacher_login.php");
    exit();
}

$teacher_id = $_SESSION['teacher_id'];

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

$id = intval($_GET['id']);

/* ===============================
   FETCH HOMEWORK
================================ */
$stmt = $conn->prepare("
    SELECT h.*, c.class_name, s.subject_name
    FROM homework h
    JOIN classes c ON h.class_id = c.class_id
    JOIN subject s ON h.subject_id = s.subject_id
    WHERE h.homework_id = ? AND h.teacher_id = ?
");

$stmt->bind_param("ii", $id, $teacher_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("Homework not found or access denied.");
}

$hw = $result->fetch_assoc();
$stmt->close();

$conn->close();
?>

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

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

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

/* SIDEBAR */
.sidebar {
    width:240px;
    height:100vh;
    position:fixed;
    background:#2c3e50;
    padding-top:20px;
}

.sidebar a {
    display:block;
    padding:12px 20px;
    color:#ecf0f1;
    text-decoration:none;
    transition:0.3s;
}

.sidebar a:hover {
    background:#34495e;
}

.sidebar a.active {
    background:#3498db;
    font-weight:bold;
}

/* MAIN */
.main {
    margin-left:240px;
    padding:25px;
}

/* CARD */
.card-box {
    background:#fff;
    padding:25px;
    border-radius:10px;
    box-shadow:0 2px 10px rgba(0,0,0,0.05);
    max-width:700px;
}
</style>
</head>

<body>

<!-- SIDEBAR -->
<div class="sidebar">
    <a href="teacher_dashboard.php">Dashboard</a>
    <a href="feedback_grading.php">Feedback & Grading</a>
    <a href="attendance.php">Attendance</a>
    <a href="add_homework.php" class="active">Homework</a>
    <a href="class_timetable.php">Timetable</a>
    <a href="profile.php">Profile</a>
    <a href="logout.php">Logout</a>
</div>

<!-- MAIN CONTENT -->
<div class="main">

<div class="card-box">

    <h4 class="mb-3">Edit Homework</h4>

    <form action="update_homework.php" method="POST" enctype="multipart/form-data">

        <input type="hidden" name="id" value="<?= $hw['homework_id'] ?>">

        <!-- CLASS (READ ONLY) -->
        <div class="mb-3">
            <label class="form-label">Class</label>
            <input type="text" class="form-control" value="<?= htmlspecialchars($hw['class_name']) ?>" readonly>
        </div>

        <!-- SUBJECT (READ ONLY) -->
        <div class="mb-3">
            <label class="form-label">Subject</label>
            <input type="text" class="form-control" value="<?= htmlspecialchars($hw['subject_name']) ?>" readonly>
        </div>

        <!-- TITLE -->
        <div class="mb-3">
            <label class="form-label">Title</label>
            <input type="text" name="title" class="form-control" value="<?= htmlspecialchars($hw['title']) ?>" required>
        </div>

        <!-- DESCRIPTION -->
        <div class="mb-3">
            <label class="form-label">Description</label>
            <textarea name="description" class="form-control" rows="4" required><?= htmlspecialchars($hw['description']) ?></textarea>
        </div>

        <!-- DATE -->
        <div class="mb-3">
            <label class="form-label">Due Date</label>
            <input type="date" name="due_date" class="form-control" value="<?= $hw['due_date'] ?>" required>
        </div>

        <!-- CURRENT FILE -->
        <?php if (!empty($hw['file_path'])): ?>
        <div class="mb-3">
            <label class="form-label">Current File</label><br>
            <a href="<?= $hw['file_path'] ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                View File
            </a>
        </div>
        <?php endif; ?>

        <!-- NEW FILE -->
        <div class="mb-3">
            <label class="form-label">Replace File (optional)</label>
            <input type="file" name="file" class="form-control" accept=".pdf,.doc,.docx">
        </div>

        <button class="btn btn-success">Update Homework</button>
        <a href="add_homework.php" class="btn btn-secondary">Back</a>

    </form>

</div>

</div>

</body>
</html>