<?php
include 'db_connection.php';

$message = "";
$error = "";

/* ---------------- FETCH DATA ---------------- */
$classes = $conn->query("SELECT class_id, class_name FROM classes ORDER BY class_name ASC");
$subjects = $conn->query("SELECT subject_id, subject_name FROM subject ORDER BY subject_name ASC");

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

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

    if ($class_id > 0 && $subject_id > 0) {

        // Check for duplicate assignment
        $check = $conn->prepare("
            SELECT id FROM class_subject 
            WHERE class_id = ? AND subject_id = ?
        ");
        $check->bind_param("ii", $class_id, $subject_id);
        $check->execute();
        $result = $check->get_result();

        if ($result->num_rows > 0) {
            $error = "⚠️ This subject is already assigned to the selected class.";
        } else {

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

            if ($stmt->execute()) {
                $message = "✅ Subject successfully assigned to class.";
            } else {
                $error = "❌ Failed to assign subject. Please try again.";
            }
        }
    } else {
        $error = "⚠️ Please select both class and subject.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Panel | Assign Subject</title>

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

        /* HEADER */
        .header {
            background: #1e3a8a;
            color: #fff;
            padding: 15px 20px;
            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 {
            width: 50%;
            margin: 40px auto;
            background: #fff;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

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

        button {
            background: #1e3a8a;
            color: #fff;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background: #163172;
        }

        .message {
            padding: 10px;
            background: #d1fae5;
            color: #065f46;
            border-radius: 5px;
            margin-bottom: 15px;
        }

        .error {
            padding: 10px;
            background: #fee2e2;
            color: #991b1b;
            border-radius: 5px;
            margin-bottom: 15px;
        }
    </style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <h1>School Admin Panel</h1>
    <div class="nav">
        <a href="admin_dashboard.php">Dashboard</a>
        <a href="add_class.php">Classes</a>
        <a href="add_subject.php">Subjects</a>
        <a href="assign_subject.php">Assign Subject</a>
        <a href="logout.php">Logout</a>
    </div>
</div>

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

    <h2>Assign Subject to Class</h2>

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

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

    <form method="POST">

        <label>Select Class</label>
        <select name="class_id" required>
            <option value="">-- Choose Class --</option>
            <?php while($c = $classes->fetch_assoc()): ?>
                <option value="<?= $c['class_id'] ?>">
                    <?= htmlspecialchars($c['class_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="<?= $s['subject_id'] ?>">
                    <?= htmlspecialchars($s['subject_name']) ?>
                </option>
            <?php endwhile; ?>
        </select>

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

    </form>

</div>

</body>
</html>