<?php
include 'db_connection.php';

/* ==============================
   FETCH DATA
============================== */
$teachers = $conn->query("SELECT teacher_id, name FROM teachers");
$subjects = $conn->query("SELECT subject_id, subject_name FROM subject");

$message = "";
$messageType = "";

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

    $teacher_id = intval($_POST['teacher_id']);
    $subject_id = intval($_POST['subject_id']);

    // Check if assignment already exists
    $check = $conn->prepare("
        SELECT 1 FROM teacher_subject 
        WHERE teacher_id = ? AND subject_id = ?
        LIMIT 1
    ");
    $check->bind_param("ii", $teacher_id, $subject_id);
    $check->execute();
    $result = $check->get_result();

    if ($result->num_rows === 0) {

        $stmt = $conn->prepare("
            INSERT INTO teacher_subject (teacher_id, subject_id)
            VALUES (?, ?)
        ");
        $stmt->bind_param("ii", $teacher_id, $subject_id);

        if ($stmt->execute()) {
            $message = "Subject successfully assigned to teacher.";
            $messageType = "success";
        } else {
            $message = "Error occurred while assigning subject.";
            $messageType = "error";
        }

    } else {
        $message = "This teacher already has this subject assigned.";
        $messageType = "error";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Teacher Subject Assignment</title>

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

        /* HEADER */
        .header {
            background: #1f2937;
            color: white;
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 20px;
            margin: 0;
        }

        .nav a {
            color: #fff;
            margin-left: 15px;
            text-decoration: none;
            font-size: 14px;
        }

        .nav a:hover {
            text-decoration: underline;
        }

        /* CONTAINER */
        .container {
            max-width: 600px;
            margin: 40px auto;
            background: #fff;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        h2 {
            margin-bottom: 20px;
            color: #333;
        }

        label {
            font-weight: bold;
        }

        select, button {
            width: 100%;
            padding: 10px;
            margin-top: 5px;
            margin-bottom: 20px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }

        button {
            background: #2563eb;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background: #1d4ed8;
        }

        /* MESSAGES */
        .message {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 5px;
        }

        .success {
            background: #dcfce7;
            color: #166534;
        }

        .error {
            background: #fee2e2;
            color: #991b1b;
        }
    </style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <h1>School Management System</h1>

    <div class="nav">
        <a href="admin_dashboard.php">Dashboard</a>
        <a href="manage_teachers.php">Teachers</a>
        <a href="add_subject.php">Subjects</a>
        <a href="logout.php">Logout</a>
    </div>
</div>

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

    <h2>Assign Subject to Teacher</h2>

    <?php if ($message): ?>
        <div class="message <?php echo $messageType; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="POST">

        <label>Select Teacher</label>
        <select name="teacher_id" required>
            <option value="">-- Choose Teacher --</option>
            <?php while ($t = $teachers->fetch_assoc()): ?>
                <option value="<?php echo $t['teacher_id']; ?>">
                    <?php echo htmlspecialchars($t['name']); ?>
                </option>
            <?php endwhile; ?>
        </select>

        <label>Select Subject</label>
        <select name="subject_id" required>
            <option value="">-- Choose Subject --</option>
            <?php while ($s = $subjects->fetch_assoc()): ?>
                <option value="<?php echo $s['subject_id']; ?>">
                    <?php echo htmlspecialchars($s['subject_name']); ?>
                </option>
            <?php endwhile; ?>
        </select>

        <button type="submit">Assign Subject</button>

    </form>
</div>

</body>
</html>