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

// Check if the student is logged in
if (!isset($_SESSION['student_id'])) {
    header("Location: login.php");
    exit();
}

$student_id = $_SESSION['student_id'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Send Message</title>
    <style type="text/css"></style>
</head>
<body>
    <h2>Send a Message</h2>
    <form action="send_message.php" method="post">
        <label for="message">Message:</label>
        <textarea id="message" name="message" placeholder="Type your message here..." required></textarea>
        
        <label for="receiver_id">Select Receiver:</label>
        <select id="receiver_id" name="receiver_id" required>
            <option value="">Select Receiver</option>
            <?php
            // Fetch all students to populate the receiver dropdown
            $sql_students = "SELECT student_id, name FROM students WHERE student_id != ?";
            if ($stmt_students = $conn->prepare($sql_students)) {
                $stmt_students->bind_param("i", $student_id);
                $stmt_students->execute();
                $result_students = $stmt_students->get_result();
                
                if ($result_students->num_rows > 0) {
                    while ($row = $result_students->fetch_assoc()) {
                        echo '<option value="' . htmlspecialchars($row['student_id']) . '">' . htmlspecialchars($row['name']) . '</option>';
                    }
                } else {
                    echo '<option value="">No students available</option>';
                }
                
                $stmt_students->close();
            } else {
                echo '<option value="">Error fetching students</option>';
            }
            ?>
        </select>
        
        <button type="submit">Send</button>
    </form>
</body>
</html>
