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

$teacher_id = $_SESSION['teacher_id'];

// Delete Logic
if (isset($_GET['delete_id'])) {
    $del_id = $_GET['delete_id'];
    $pdo->prepare("DELETE FROM class_notices WHERE id = ? AND teacher_id = ?")->execute([$del_id, $teacher_id]);
    header("Location: class_notices.php?msg=deleted");
    exit;
}

// Fetch Class Notices
$stmt = $pdo->prepare("SELECT n.*, c.title as course_name FROM class_notices n 
                       JOIN courses c ON n.course_id = c.id 
                       WHERE n.teacher_id = ? ORDER BY n.created_at DESC");
$stmt->execute([$teacher_id]);
$notices = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<style>
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    .main-content { background-color: var(--bg-soft); min-height: 100vh; padding: 30px; transition: 0.4s; }
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 6px solid var(--theme-green); }
    .notice-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; padding: 25px; margin-bottom: 20px; box-shadow: 0 5px 15px rgba(0,0,0,0.02); transition: 0.3s; position: relative; border-left: 5px solid var(--theme-gold); }
    .notice-card:hover { transform: translateY(-3px); box-shadow: 0 8px 25px rgba(0,0,0,0.05); }
    
    .course-tag { font-size: 10px; font-weight: 800; text-transform: uppercase; color: var(--theme-green); background: rgba(6, 78, 59, 0.05); padding: 4px 10px; border-radius: 50px; margin-bottom: 10px; display: inline-block; }
    
    .btn-theme { background: var(--theme-green); color: #fff; border-radius: 50px; padding: 10px 25px; font-weight: 700; text-decoration: none; display: inline-flex; align-items: center; transition: 0.3s; border: none; }
    .btn-theme:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }

    #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px; border: 1px solid #e2e8f0; background: #fff; }

    @media (max-width: 991px) {
        .main-content { padding: 15px !important; margin-left: 0; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .btn-theme { width: 100%; justify-content: center; }
    }
</style>

<div class="main-content">
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-lg-none me-3 shadow-sm" id="sidebarToggle"><i class="fas fa-bars text-dark"></i></button>
            <h4 class="mb-0 fw-bold text-dark"><i class="fas fa-bullhorn me-2" style="color: var(--theme-green);"></i> Class Notices</h4>
        </div>
        <a href="add_notice.php" class="btn-theme shadow-sm"><i class="fas fa-paper-plane me-2"></i> Post Notice</a>
    </div>

    <div class="row">
        <?php if(!empty($notices)): foreach($notices as $note): ?>
        <div class="col-md-6 col-xl-4 fade-up">
            <div class="notice-card">
                <span class="course-tag"><?= htmlspecialchars($note['course_name']) ?></span>
                <h6 class="fw-bold text-dark mb-2"><?= htmlspecialchars($note['title']) ?></h6>
                <p class="text-muted small mb-3"><?= nl2br(htmlspecialchars($note['content'])) ?></p>
                <div class="d-flex justify-content-between align-items-center mt-3 pt-3 border-top">
                    <small class="text-muted fw-bold"><i class="far fa-calendar-alt me-1"></i> <?= date('d M, Y', strtotime($note['created_at'])) ?></small>
                    <a href="?delete_id=<?= $note['id'] ?>" class="text-danger small fw-bold text-decoration-none" onclick="return confirm('Delete this notice?')">
                        <i class="fas fa-trash-alt me-1"></i> Delete
                    </a>
                </div>
            </div>
        </div>
        <?php endforeach; else: ?>
        <div class="col-12 text-center py-5">
            <i class="fas fa-comment-slash fa-4x text-muted opacity-25 mb-3"></i>
            <h5 class="fw-bold text-muted">No notices posted yet for your classes.</h5>
        </div>
        <?php endif; ?>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        if (toggleBtn) {
            toggleBtn.addEventListener('click', function(e) {
                e.preventDefault();
                const sidebar = document.getElementById('sidebarMenu');
                if (sidebar) { sidebar.classList.toggle('mobile-show'); }
            });
        }
    });
</script>

<?php include 'includes/footer.php'; ?>