<?php
session_start();
require_once '../db_connect.php';

if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit; }

$success = ""; $error = "";
$courses = $pdo->query("SELECT id, title FROM courses WHERE status='active'")->fetchAll(PDO::FETCH_ASSOC);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = trim($_POST['title']);
    $content = trim($_POST['content']);
    $course_id = (int)$_POST['course_id'];
    $is_urgent = isset($_POST['is_urgent']) ? 1 : 0;
    $file_path = "";

    if (isset($_FILES['notice_file']) && $_FILES['notice_file']['error'] == 0) {
        $upload_dir = '../uploads/notices/';
        if (!file_exists($upload_dir)) mkdir($upload_dir, 0777, true);
        $file_name = time() . "_" . $_FILES['notice_file']['name'];
        if (move_uploaded_file($_FILES['notice_file']['tmp_name'], $upload_dir . $file_name)) {
            $file_path = 'uploads/notices/' . $file_name;
        }
    }

    if (!empty($title)) {
        $stmt = $pdo->prepare("INSERT INTO notices (course_id, title, content, is_urgent, file_path) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$course_id, $title, $content, $is_urgent, $file_path]);
        $success = "Notice published successfully!";
    }
}

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

<style>
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg: #f8fafc; }
    .main-content { background-color: var(--bg); min-height: 100vh; padding-bottom: 50px; }
    
    /* Mobile Toggle Styling */
    #sidebarToggle { 
        display: none; 
        width: 40px; 
        height: 40px; 
        align-items: center; 
        justify-content: center; 
        border-radius: 10px; 
        background: #fff; 
        border: 1px solid #ddd; 
        cursor: pointer; 
        color: var(--theme-green);
    }

    @media (max-width: 991.98px) {
        #sidebarToggle { display: inline-flex; margin-right: 15px; }
        .page-title-box { text-align: center; flex-grow: 1; }
    }

    .form-card { background: #fff; border-radius: 20px; border: none; box-shadow: 0 10px 40px rgba(0,0,0,0.04); padding: 35px; }
    .form-label { font-weight: 700; color: var(--theme-dark); margin-bottom: 8px; font-size: 14px; }
    .form-control, .form-select { border-radius: 12px; padding: 12px 15px; border: 2px solid #f1f5f9; background: #f8fafc; }
    .form-control:focus { border-color: var(--theme-green); background: #fff; box-shadow: none; }
</style>

<div class="main-content p-3 p-md-4">
    <div class="container-fluid">
        
        <div class="d-flex align-items-center mb-4 pb-3 border-bottom">
            <div id="sidebarToggle" class="shadow-sm">
                <i class="fas fa-bars"></i>
            </div>
            <div class="page-title-box">
                <h4 class="fw-bold mb-0 text-success">
                    <i class="fas fa-plus-circle me-2"></i> Add New Notice
                </h4>
            </div>
        </div>
        
        <div class="row justify-content-center">
            <div class="col-lg-10">
                <div class="form-card fade-up">
                    <?php if($success): ?> 
                        <div class="alert alert-success border-0 rounded-4 shadow-sm fw-bold mb-4">
                            <i class="fas fa-check-circle me-2"></i> <?= $success ?>
                        </div> 
                    <?php endif; ?>

                    <form method="POST" enctype="multipart/form-data">
                        <div class="row g-4">
                            <div class="col-md-8">
                                <label class="form-label">Notice Title *</label>
                                <input type="text" name="title" class="form-control" required placeholder="Enter Headline">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Target Audience</label>
                                <select name="course_id" class="form-select">
                                    <option value="0">All Students (General)</option>
                                    <?php foreach($courses as $c): ?>
                                        <option value="<?= $c['id'] ?>"><?= htmlspecialchars($c['title']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label">Description / Content</label>
                                <textarea name="content" class="form-control" rows="6" required placeholder="Write notice details here..."></textarea>
                            </div>
                            <div class="col-md-8">
                                <label class="form-label">Attachment (Optional: PDF or Image)</label>
                                <div class="p-3 bg-light rounded-3 border-2 border-dashed">
                                    <input type="file" name="notice_file" class="form-control border-0 bg-transparent shadow-none">
                                </div>
                            </div>
                            <div class="col-md-4 d-flex align-items-end">
                                <div class="form-check form-switch bg-light p-3 rounded-4 border w-100 shadow-sm">
                                    <input class="form-check-input ms-0 me-3" type="checkbox" name="is_urgent" id="u">
                                    <label class="form-check-label fw-bold text-danger" for="u">Mark as Urgent</label>
                                </div>
                            </div>
                            <div class="col-12 pt-4">
                                <button type="submit" class="btn btn-success px-5 py-3 fw-bold rounded-3 shadow w-100 w-md-auto">
                                    <i class="fas fa-paper-plane me-2"></i> PUBLISH NOTICE
                                </button>
                                <a href="manage_notices.php" class="btn btn-light px-4 py-3 fw-bold rounded-3 ms-md-2 mt-2 mt-md-0 shadow-sm border">
                                    Cancel
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // Sidebar Toggle Functionality
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        const sidebar = document.getElementById('sidebarMenu'); // নিশ্চিত করুন আপনার সাইডবারের আইডি এটাই
        
        if (toggleBtn && sidebar) {
            toggleBtn.addEventListener('click', function() {
                sidebar.classList.toggle('active');
            });
        }
    });
</script>

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