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

$teacher_id = $_SESSION['teacher_id'];
$id = $_GET['id'] ?? null;

$stmt = $pdo->prepare("SELECT * FROM assignments WHERE id = ? AND teacher_id = ?");
$stmt->execute([$id, $teacher_id]);
$data = $stmt->fetch();

if (!$data) { die("Assignment not found!"); }

$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 = $data['file_path'];

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

    $pdo->prepare("UPDATE assignments SET course_id=?, title=?, description=?, deadline=?, file_path=? WHERE id=?")
        ->execute([$course_id, $title, $desc, $deadline, $db_path, $id]);
    header("Location: assignments.php?msg=updated");
    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">Edit Assignment</h4>
        </div>
        <a href="assignments.php" class="btn btn-light border rounded-pill px-4 fw-bold">Back</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">Title</label><input type="text" name="title" class="form-control" value="<?= $data['title'] ?>" required></div>
                        <div class="col-md-6">
                            <label class="form-label">Course</label>
                            <select name="course_id" class="form-select" required>
                                <?php foreach($course_list as $c): ?>
                                    <option value="<?= $c['id'] ?>" <?= $data['course_id'] == $c['id'] ? 'selected' : '' ?>><?= $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" value="<?= date('Y-m-d\TH:i', strtotime($data['deadline'])) ?>" required></div>
                        <div class="col-12"><label class="form-label">Change 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"><?= $data['description'] ?></textarea></div>
                    </div>
                    <div class="text-center mt-5"><button type="submit" class="btn-submit shadow">Update Assignment</button></div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include 'includes/footer.php'; ?>