<?php 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$teacher_id = $_SESSION['teacher_id'];
$courses = $pdo->prepare("SELECT id, title FROM courses WHERE teacher_id = ? OR 1=1 AND status='active'");
$courses->execute([$teacher_id]);
$course_list = $courses->fetchAll(PDO::FETCH_ASSOC);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $pdo->prepare("INSERT INTO class_notices (course_id, teacher_id, title, content) VALUES (?, ?, ?, ?)")
        ->execute([$_POST['course_id'], $teacher_id, $_POST['title'], $_POST['content']]);
    header("Location: class_notices.php?msg=success");
    exit;
}
?>

<div class="main-content">
    <div class="dashboard-header d-flex justify-content-between align-items-center">
        <div class="d-flex align-items-center"><button class="btn btn-light d-lg-none me-3" id="sidebarToggle"><i class="fas fa-bars"></i></button><h4 class="mb-0 fw-bold">Post New Notice</h4></div>
        <a href="class_notices.php" class="btn btn-light border rounded-pill px-4 fw-bold">Cancel</a>
    </div>

    <div class="row justify-content-center">
        <div class="col-xl-7">
            <div class="form-card" style="border-top-color: var(--theme-gold);">
                <form method="POST">
                    <div class="mb-4">
                        <label class="form-label">Target Course</label>
                        <select name="course_id" class="form-select" required>
                            <option value="">Select Course</option>
                            <?php foreach($course_list as $c): ?><option value="<?= $c['id'] ?>"><?= $c['title'] ?></option><?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-4"><label class="form-label">Notice Title</label><input type="text" name="title" class="form-control" placeholder="e.g. Class Rescheduled" required></div>
                    <div class="mb-4"><label class="form-label">Notice Content</label><textarea name="content" class="form-control" rows="6" placeholder="Write your message here..." required></textarea></div>
                    <div class="text-center mt-4"><button type="submit" class="btn-submit" style="background:var(--theme-green)">Post Notice Now</button></div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include 'includes/footer.php'; ?>