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

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

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

// Handle course deletion
if (isset($_GET['delete_course'])) {
    $course_id = $_GET['delete_course'];
    $stmt = $pdo->prepare("DELETE FROM courses WHERE id = ?");
    $stmt->execute([$course_id]);
    $success = "Course deleted successfully.";
}

// Fetch all courses with lecturer names
$stmt = $pdo->query("SELECT c.*, u.username as lecturer_name FROM courses c LEFT JOIN users u ON c.lecturer_id = u.id ORDER BY c.created_at DESC");
$courses = $stmt->fetchAll();
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">Manage Courses</h2>
        <a href="add_course.php" class="btn btn-primary btn-sm"><i class="fas fa-plus me-2"></i>New Course</a>
    </div>

    <?php if(isset($success)): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo $success; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <div class="card shadow-sm border-0">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Course Name</th>
                            <th>Lecturer</th>
                            <th>Description</th>
                            <th>Date Created</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($courses as $course): ?>
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold"><?php echo htmlspecialchars($course['course_name']); ?></div>
                                    <small class="text-muted">ID: #<?php echo $course['id']; ?></small>
                                </td>
                                <td><?php echo htmlspecialchars($course['lecturer_name'] ?? 'Unassigned'); ?></td>
                                <td>
                                    <div class="text-truncate" style="max-width: 250px;">
                                        <?php echo htmlspecialchars($course['description']); ?>
                                    </div>
                                </td>
                                <td><?php echo date('M d, Y', strtotime($course['created_at'])); ?></td>
                                <td class="text-end pe-4">
                                    <div class="btn-group">
                                        <a href="view_course.php?id=<?php echo $course['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i></a>
                                        <a href="manage_courses.php?delete_course=<?php echo $course['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this course?')"><i class="fas fa-trash"></i></a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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