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

// Fetch days from a predefined list (assuming days table or static array)
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

// Fetch subjects from subjects table
$subject_query = "SELECT subject_id, subject_name FROM subject";
$subject_result = mysqli_query($conn, $subject_query);

// Fetch classes from classes table
$class_query = "SELECT class_id, class_name FROM classes";
$class_result = mysqli_query($conn, $class_query);

// Fetch teachers from teachers table
$teacher_query = "SELECT teacher_id, name FROM teachers";
$teacher_result = mysqli_query($conn, $teacher_query);

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $teacher_id = $_POST['teacher_id'];
    $day = $_POST['day'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];
    $subject_id = $_POST['subject_id'];
    $class_id = $_POST['class_id'];

    // Insert into database or perform necessary actions
    // Example: Insert query
    $insert_query = "INSERT INTO teacher_schedules (teacher_id, day, start_time, end_time, subject_id, class_id) VALUES ('$teacher_id', '$day', '$start_time', '$end_time', '$subject_id', '$class_id')";
    mysqli_query($conn, $insert_query);
    // Redirect or display success message
    header('Location: view_schedule.php');
    exit;
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Schedule</title>
    <link rel="stylesheet" href="#">
    <style type="text/css">
        /* Global Reset and Defaults */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
    line-height: 1.6;
    background-color: #f0f0f0;
    padding: 20px;
}

.container {
    max-width: 800px;
    margin: 0 auto;
    background-color: #fff;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
    margin-bottom: 20px;
}

form {
    max-width: 600px;
    margin: 0 auto;
}

label {
    display: block;
    margin-bottom: 10px;
}

input[type="text"],
input[type="email"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: 10px;
    margin-bottom: 15px;
    border: 1px solid #ccc;
    border-radius: 3px;
    font-size: 16px;
    box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}

input[type="submit"],
button {
    display: inline-block;
    padding: 10px 20px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-size: 16px;
}

input[type="submit"]:hover,
button:hover {
    background-color: #0056b3;
}

.error {
    color: red;
    font-style: italic;
}

.success {
    color: green;
    font-weight: bold;
}

/* Additional Specific Styles */

/* Adjust as per your specific design and requirements */

    </style>
</head>

<body>
    <div class="container">
        <h2>Add Schedule</h2>
        <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
            <label for="teacher_id">Teacher:</label>
            <select id="teacher_id" name="teacher_id" required>
                <?php while ($row = mysqli_fetch_assoc($teacher_result)) : ?>
                    <option value="<?php echo $row['teacher_id']; ?>"><?php echo $row['name']; ?></option>
                <?php endwhile; ?>
            </select><br>

            <label for="day">Day:</label>
            <select id="day" name="day" required>
                <?php foreach ($days as $day) : ?>
                    <option value="<?php echo $day; ?>"><?php echo $day; ?></option>
                <?php endforeach; ?>
            </select><br>

            <label for="start_time">Start Time:</label>
            <input type="time" id="start_time" name="start_time" required><br>

            <label for="end_time">End Time:</label>
            <input type="time" id="end_time" name="end_time" required><br>

            <label for="subject_id">Subject:</label>
            <select id="subject_id" name="subject_id" required>
                <?php while ($row = mysqli_fetch_assoc($subject_result)) : ?>
                    <option value="<?php echo $row['subject_id']; ?>"><?php echo $row['subject_name']; ?></option>
                <?php endwhile; ?>
            </select><br>

            <label for="class_id">Class to Teach:</label>
            <select id="class_id" name="class_id" required>
                <?php while ($row = mysqli_fetch_assoc($class_result)) : ?>
                    <option value="<?php echo $row['class_id']; ?>"><?php echo $row['class_name']; ?></option>
                <?php endwhile; ?>
            </select><br>

            <input type="submit" value="Add Schedule">
        </form>
    </div>
</body>

</html>

<?php
// Close database connection
mysqli_close($conn);
?>
