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

// Optional: Restrict access
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

/* =========================
   FETCH DATA
========================= */

// Subjects
$subjects = [];
$result = $conn->query("SELECT subject_id, subject_name FROM subject");
if ($result->num_rows > 0) {
    $subjects = $result->fetch_all(MYSQLI_ASSOC);
}

// Teachers
$teachers = [];
$result = $conn->query("SELECT teacher_id, name FROM teachers");
if ($result->num_rows > 0) {
    $teachers = $result->fetch_all(MYSQLI_ASSOC);
}

// Classes
$classes = [];
$result = $conn->query("SELECT class_id, class_name FROM classes");
if ($result->num_rows > 0) {
    $classes = $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>Add Homework</title>

<style>
/* =========================
   BASE
========================= */
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;
}

.logo h2 {
    margin: 0;
    font-size: 20px;
}

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

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

.nav a:hover { background: rgba(255,255,255,0.1); }
.nav a.active { background: #2563eb; }
.nav a.logout { background: #ef4444; }

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

/* =========================
   BUTTON
========================= */
.back-btn {
    display: inline-block;
    margin-bottom: 15px;
    background: #111827;
    color: white;
    padding: 10px 15px;
    border-radius: 8px;
    text-decoration: none;
}

.back-btn:hover { background: #000; }

/* =========================
   FORM
========================= */
h2 {
    margin-bottom: 20px;
    color: #1e293b;
}

label {
    font-weight: bold;
    margin-top: 10px;
    display: block;
}

input, textarea, select {
    width: 100%;
    padding: 10px;
    margin-top: 5px;
    border: 1px solid #ccc;
    border-radius: 8px;
}

input:focus, textarea:focus, select:focus {
    border-color: #2563eb;
    outline: none;
}

textarea {
    resize: vertical;
}

.submit-btn {
    margin-top: 15px;
    background: #2563eb;
    color: white;
    border: none;
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
}

.submit-btn:hover {
    background: #1d4ed8;
}
</style>
</head>

<body>

<!-- ================= HEADER ================= -->
<div class="header">
    <div class="header-container">
        <div class="logo">
            <h2>EduConnect Admin</h2>
        </div>
        <div class="nav">
            <a href="admin_dashboard.php">Dashboard</a>
            <a href="add_homework.php" class="active">Add Homework</a>
            <a href="manage_homework.php">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">← Back to Dashboard</a>

<h2>Add Homework</h2>

<form action="process_add_homework.php" method="POST">

    <!-- SUBJECT -->
    <label>Subject</label>
    <select name="subject_id" required>
        <option value="">Select Subject</option>
        <?php foreach ($subjects as $subject): ?>
            <option value="<?= $subject['subject_id']; ?>">
                <?= htmlspecialchars($subject['subject_name']); ?>
            </option>
        <?php endforeach; ?>
    </select>

    <!-- TEACHER -->
    <label>Teacher</label>
    <select name="teacher_id" required>
        <option value="">Select Teacher</option>
        <?php foreach ($teachers as $teacher): ?>
            <option value="<?= $teacher['teacher_id']; ?>">
                <?= htmlspecialchars($teacher['name']); ?>
            </option>
        <?php endforeach; ?>
    </select>

    <!-- CLASS -->
    <label>Class</label>
    <select name="class_id" required>
        <option value="">Select Class</option>
        <?php foreach ($classes as $class): ?>
            <option value="<?= $class['class_id']; ?>">
                <?= htmlspecialchars($class['class_name']); ?>
            </option>
        <?php endforeach; ?>
    </select>

    <!-- TITLE -->
    <label>Title</label>
    <input type="text" name="title" required>

    <!-- DESCRIPTION -->
    <label>Description</label>
    <textarea name="description" rows="4"></textarea>

    <!-- DATE -->
    <label>Due Date</label>
    <input type="date" name="due_date" required>

    <!-- SUBMIT -->
    <button type="submit" class="submit-btn">Add Homework</button>

</form>

</div>

</body>
</html>