"use client" import { useEffect, useState } from "react" import { useRouter } from "next/navigation" import { Edit, Plus, Trash2 } from "lucide-react" type Course = { id: string title: string subject: string description: string difficulty: string mentor: string video_url: string students: number } const API_BASE = "http://127.0.0.1:5000" export default function AdminCoursesPage() { const router = useRouter() const [ready, setReady] = useState(false) const [courses, setCourses] = useState([]) const [loading, setLoading] = useState(true) const [showAdd, setShowAdd] = useState(false) const [editing, setEditing] = useState(null) const getToken = () => localStorage.getItem("admin_token") const headers = () => { const token = getToken() return token ? { "Content-Type": "application/json", Authorization: `Bearer ${token}` } : { "Content-Type": "application/json" } } const ensureAuth = async () => { const token = getToken() if (!token) { router.push("/admin/login") return false } const resp = await fetch(`${API_BASE}/api/admin/test`, { headers: headers() }) if (!resp.ok) { localStorage.removeItem("admin_token") router.push("/admin/login") return false } return true } const fetchCourses = async () => { setLoading(true) try { const resp = await fetch(`${API_BASE}/api/admin/courses`, { headers: headers() }) if (resp.ok) { const data = await resp.json() setCourses(Array.isArray(data) ? data : []) } else { setCourses([]) } } catch { setCourses([]) } finally { setLoading(false) } } const saveCourse = async (payload: Partial, courseId?: string) => { const url = courseId ? `${API_BASE}/api/admin/courses/${courseId}` : `${API_BASE}/api/admin/courses` const method = courseId ? "PUT" : "POST" const resp = await fetch(url, { method, headers: headers(), body: JSON.stringify(payload), }) if (resp.ok) { setShowAdd(false) setEditing(null) await fetchCourses() } else { const err = await resp.json().catch(() => ({ error: "Operation failed" })) alert(err.error || "Operation failed") } } const deleteCourse = async (courseId: string) => { if (!confirm("Delete this course and related modules/lessons?")) return const resp = await fetch(`${API_BASE}/api/admin/courses/${courseId}`, { method: "DELETE", headers: headers(), }) if (resp.ok) { await fetchCourses() } else { alert("Failed to delete course") } } useEffect(() => { const init = async () => { const ok = await ensureAuth() if (!ok) return setReady(true) await fetchCourses() } init() }, []) if (!ready) { return (

Loading course management...

) } return (

Course Management

Manage real courses from database records.

{loading ? ( ) : courses.length === 0 ? ( ) : ( courses.map((course) => ( )) )}
Title Subject Difficulty Mentor Students Actions
Loading courses...
No courses found.
{course.title} {course.subject} {course.difficulty} {course.mentor} {Number(course.students || 0).toLocaleString()}
{(showAdd || editing) && ( { setShowAdd(false) setEditing(null) }} onSubmit={(payload) => saveCourse(payload, editing?.id)} /> )}
) } function CourseFormModal({ title, initialData, onClose, onSubmit, }: { title: string initialData?: Partial onClose: () => void onSubmit: (payload: Partial) => Promise }) { const [form, setForm] = useState>({ title: initialData?.title || "", subject: initialData?.subject || "", description: initialData?.description || "", difficulty: initialData?.difficulty || "Beginner", mentor: initialData?.mentor || "", video_url: initialData?.video_url || "", }) const [saving, setSaving] = useState(false) const submit = async (e: React.FormEvent) => { e.preventDefault() setSaving(true) await onSubmit(form) setSaving(false) } return (

{title}

setForm({ ...form, title: e.target.value })} className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm dark:border-gray-700 dark:bg-gray-800" /> setForm({ ...form, subject: e.target.value })} className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm dark:border-gray-700 dark:bg-gray-800" />