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

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'lecturer') {
    header("Location: dashboard.php");
    exit();
}

$user_id = $_SESSION['user_id'];
$error = '';
$success = '';
$preselected_course_id = isset($_GET['course_id']) ? $_GET['course_id'] : '';

$stmt = $pdo->prepare("SELECT * FROM courses WHERE lecturer_id=?");
$stmt->execute([$user_id]);
$courses = $stmt->fetchAll();

if ($_POST) {

    $course_id = $_POST['course_id'];
    $module_name = trim($_POST['module_name']);

    if ($course_id == '' || $module_name == '') {
        $error = "All fields are required!";
    } else {

        $stmt = $pdo->prepare("SELECT id FROM courses WHERE id=? AND lecturer_id=?");
        $stmt->execute([$course_id, $user_id]);

        if ($stmt->rowCount() == 0) {
            $error = "Invalid course selected!";
        } else {

            $stmt = $pdo->prepare("INSERT INTO modules (course_id, module_name) VALUES (?, ?)");
            $stmt->execute([$course_id, $module_name]);
            $new_module_id = $pdo->lastInsertId();
            header("Location: add_unit.php?module_id=" . $new_module_id);
            exit();
        }
    }
}

include('../header/header.php');
?>

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card shadow-sm border-0">
                <div class="card-body p-5">
                    <h2 class="fw-bold mb-4 text-center">Add New Module</h2>
                    
                    <?php if($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php endif; ?>

                    <?php if($success): ?>
                        <div class="alert alert-success"><?php echo $success; ?></div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label">Select Course</label>
                            <select name="course_id" class="form-select" required>
                                <option value="">Select Course</option>
                                <?php foreach($courses as $c): ?>
                                <option value="<?= $c['id']; ?>" <?= ($c['id'] == $preselected_course_id) ? 'selected' : ''; ?>>
                                    <?= htmlspecialchars($c['course_name']); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Module Name</label>
                            <input type="text" name="module_name" class="form-control" placeholder="Enter module name" required>
                        </div>
                        <button type="submit" class="btn btn-primary w-100 py-2 fw-bold">Add Module & Add Unit</button>
                    </form>
                    <div class="text-center mt-3">
                        <a href="dashboard.php" class="text-decoration-none">Back to Dashboard</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>