<?php
include 'db_connection.php';
include 'header.php';

// =========================
// FETCH TEACHERS (ONCE ONLY)
// =========================
$teachers = [];

$sql = "
    SELECT 
        t.teacher_id,
        u.name
    FROM teachers t
    JOIN users u ON t.user_id = u.user_id
";

$result = $conn->query($sql);

if (!$result) {
    die("Error fetching teachers: " . $conn->error);
}

while ($row = $result->fetch_assoc()) {
    $teachers[] = $row;
}


// =========================
// HANDLE FORM SUBMISSION
// =========================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $class_name = $_POST['class_name'];
    $teacher_id = $_POST['teacher_id'];
    $section = $_POST['section'];

    $stmt = $conn->prepare("
        INSERT INTO classes (class_name, teacher_id, section)
        VALUES (?, ?, ?)
    ");

    if (!$stmt) {
        die("Prepare failed: " . $conn->error);
    }

    $stmt->bind_param("sis", $class_name, $teacher_id, $section);

    if ($stmt->execute()) {
        $stmt->close();
        header("Location: add_class.php?success=1");
        exit();
    } else {
        die("Error inserting class: " . $stmt->error);
    }
}
?>

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

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

        .container {
            max-width: 700px;
            margin: 40px auto;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
        }

        h2 {
            text-align: center;
        }

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

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

        .btn {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background: #28a745;
            border: none;
            color: white;
            font-size: 16px;
            cursor: pointer;
        }

        .btn:hover {
            background: #218838;
        }

        .success {
            text-align: center;
            color: green;
        }
    </style>
</head>

<body>

<div class="container">

    <h2>Add Class</h2>

    <?php if (isset($_GET['success'])): ?>
        <p class="success">Class added successfully!</p>
    <?php endif; ?>

    <form method="POST">

        <label>Class Name:</label>
        <input type="text" name="class_name" required>

        <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>

        <label>Section:</label>
        <select name="section" required>
            <option value="Primary">Primary</option>
            <option value="Lower">Lower Secondary</option>
            <option value="Secondary">Upper Secondary</option>
        </select>

        <button type="submit" class="btn">Add Class</button>

    </form>

</div>

</body>
</html>