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

$teacher_id = $_SESSION['teacher_id'];
$msg = ""; $msgType = "";

// Fetch assigned courses
$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") {
    $course_id = $_POST['course_id'];
    $title = trim($_POST['title']);
    $desc = trim($_POST['description']);
    $deadline = $_POST['deadline'];
    $db_path = "";

    if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
        $upload_dir = '../uploads/assignments/';
        if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true);
        $db_path = 'uploads/assignments/' . time() . '_' . $_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], '../' . $db_path);
    }

    try {
        $sql = "INSERT INTO assignments (course_id, teacher_id, title, description, deadline, file_path) VALUES (?, ?, ?, ?, ?, ?)";
        $pdo->prepare($sql)->execute([$course_id, $teacher_id, $title, $desc, $deadline, $db_path]);
        header("Location: assignments.php?msg=success");
        exit;
    } catch (PDOException $e) { $msg = "Error: " . $e->getMessage(); $msgType = "danger"; }
}
?>

<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; }
    .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); }
    .form-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; padding: 35px; border-top: 6px solid var(--theme-green); box-shadow: 0 10px 40px rgba(0,0,0,0.03); }
    .form-label { font-weight: 700; color: #64748b; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
    .form-control, .form-select { border-radius: 10px; padding: 12px 18px; border: 2px solid #f1f5f9; background: var(--bg-soft); font-size: 14px; }
    .btn-submit { background: var(--theme-green); color: #fff; padding: 14px 45px; border-radius: 50px; font-weight: 800; border: none; transition: 0.3s; }
    #sidebarToggle { width: 42px !important; height: 42px !important; display: inline-flex !important; align-items: center; justify-content: center; border-radius: 8px; border: 1px solid #e2e8f0; background: #fff; }
</style>

<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">New Assignment</h4>
        </div>
        <a href="assignments.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-8">
            <div class="form-card">
                <form method="POST" enctype="multipart/form-data">
                    <div class="row g-4">
                        <div class="col-12"><label class="form-label">Assignment Title *</label><input type="text" name="title" class="form-control" required></div>
                        <div class="col-md-6">
                            <label class="form-label">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="col-md-6"><label class="form-label">Deadline *</label><input type="datetime-local" name="deadline" class="form-control" required></div>
                        <div class="col-12"><label class="form-label">Instruction File (Optional)</label><input type="file" name="file" class="form-control"></div>
                        <div class="col-12"><label class="form-label">Description</label><textarea name="description" class="form-control" rows="4"></textarea></div>
                    </div>
                    <div class="text-center mt-5"><button type="submit" class="btn-submit shadow">Create Assignment</button></div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include 'includes/footer.php'; ?>