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

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

$teacher_id = $_SESSION['teacher_id'];

/* FETCH CLASSES */
$stmt = $conn->prepare("SELECT class_id, class_name FROM classes WHERE teacher_id=?");
$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$classes = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

/* FETCH SUBJECTS */
$stmt = $conn->prepare("
    SELECT s.subject_id, s.subject_name 
    FROM teacher_subject ts 
    JOIN subject s ON ts.subject_id = s.subject_id
    WHERE ts.teacher_id=?
");
$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$subjects = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

/* FETCH HOMEWORK LIST */
$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.teacher_id = ?
    ORDER BY h.created_at DESC
");
$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$homeworks = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Homework Management</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background:#f4f6f9; }
.sidebar { width:240px; position:fixed; height:100%; background:#2c3e50; }
.sidebar a { color:#fff; display:block; padding:12px; text-decoration:none; }
.sidebar a:hover { background:#34495e; }
.main { margin-left:240px; padding:20px; }
.card-box { background:#fff; padding:20px; border-radius:10px; margin-bottom:20px; }
</style>
</head>
<body>

<div class="sidebar">
    <a href="teacher_dashboard.php">Dashboard</a>
    <a href="feedback_grading.php">Feedback</a>
    <a href="attendance.php">Attendance</a>
    <a href="add_homework.php" class="fw-bold">Homework</a>
    <a href="logout.php">Logout</a>
</div>

<div class="main">

<!-- ADD HOMEWORK -->
<div class="card-box">
<h4>Add Homework</h4>

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

<select name="class_id" class="form-control mb-2" required>
<option value="">Select Class</option>
<?php foreach($classes as $c): ?>
<option value="<?= $c['class_id'] ?>"><?= $c['class_name'] ?></option>
<?php endforeach; ?>
</select>

<select name="subject_id" class="form-control mb-2" required>
<option value="">Select Subject</option>
<?php foreach($subjects as $s): ?>
<option value="<?= $s['subject_id'] ?>"><?= $s['subject_name'] ?></option>
<?php endforeach; ?>
</select>

<input type="text" name="title" class="form-control mb-2" placeholder="Title" required>

<textarea name="description" class="form-control mb-2" placeholder="Description" required></textarea>

<input type="date" name="due_date" class="form-control mb-2" required>

<input type="file" name="file" class="form-control mb-2" accept=".pdf,.doc,.docx">

<button class="btn btn-primary">Submit</button>
</form>
</div>

<!-- HOMEWORK LIST -->
<div class="card-box">
<h4>Assigned Homework</h4>

<table class="table table-bordered">
<tr>
<th>Title</th><th>Class</th><th>Subject</th><th>File</th><th>Actions</th>
</tr>

<?php foreach($homeworks as $h): ?>
<tr>
<td><?= htmlspecialchars($h['title']) ?></td>
<td><?= $h['class_name'] ?></td>
<td><?= $h['subject_name'] ?></td>

<td>
<?php if($h['file_path']): ?>
<a href="<?= $h['file_path'] ?>" target="_blank">View</a>
<?php endif; ?>
</td>

<td>
<a href="edit_homework.php?id=<?= $h['homework_id'] ?>" class="btn btn-sm btn-warning">Edit</a>
<a href="delete_homework.php?id=<?= $h['homework_id'] ?>" class="btn btn-sm btn-danger"
onclick="return confirm('Delete this homework?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>

</table>
</div>

</div>
</body>
</html>