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

$teacher_id = $_GET['id'];
$query = "SELECT t.teacher_id, t.name, u.email, t.subject 
          FROM teachers t 
          INNER JOIN users u ON t.user_id = u.user_id 
          WHERE t.teacher_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$result = $stmt->get_result();
$teacher = $result->fetch_assoc();
$stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Teacher</title>
    <link rel="stylesheet" href="#">
    <style type="text/css">
        body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
}

h2 {
    text-align: center;
    color: #333;
}

form {
    max-width: 600px;
    margin: 20px auto;
    padding: 20px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
    color: #555;
}

input[type="text"],
input[type="email"],
select {
    width: 100%;
    padding: 8px;
    margin-bottom: 10px;
    border: 1px solid #ddd;
    border-radius: 3px;
}

button {
    display: block;
    width: 100%;
    padding: 10px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 3px;
    cursor: pointer;
}

button:hover {
    background-color: #0056b3;
}

    </style>
</head>
<body>
    <div class="container">
        <h2>Edit Teacher</h2>
        <form action="process_edit_teacher.php" method="post">
            <input type="hidden" name="teacher_id" value="<?php echo htmlspecialchars($teacher['teacher_id']); ?>">

            <label for="name">Name:</label>
            <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($teacher['name']); ?>" required>

            <label for="email">Email:</label>
            <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($teacher['email']); ?>" required>

            <label for="subject">Subject:</label>
            <select id="subject" name="subject" required>
                <?php
                include('db_connection.php');
                $query = "SELECT subject_name FROM subject";
                $result = mysqli_query($conn, $query);
                while ($row = mysqli_fetch_assoc($result)) {
                    $selected = ($row['subject_name'] == $teacher['subject']) ? 'selected' : '';
                    echo '<option value="' . htmlspecialchars($row['subject_name']) . '" ' . $selected . '>' . htmlspecialchars($row['subject_name']) . '</option>';
                }
                mysqli_close($conn);
                ?>
            </select>

            <button type="submit">Update Teacher</button>
        </form>
    </div>
</body>
</html>
