// components/CourseDetail.tsx 'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import { ChevronRight, Clock, Users, Calendar, User, ExternalLink } from 'lucide-react'; import { educationService, CourseApplicationData } from '../../services/educationService'; import { fileUploadService } from '../../services/fileUploadService'; interface CourseDetailProps { courseId?: string; } const CourseDetail: React.FC = ({ courseId }) => { const [mounted, setMounted] = useState(false); const [course, setCourse] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [submitting, setSubmitting] = useState(false); const [submitSuccess, setSubmitSuccess] = useState(false); // Application form data const [formData, setFormData] = useState({ fullName: '', email: '', phone: '', qualification: '', experience: '', coverLetter: '', resume: null as File | null }); useEffect(() => { setMounted(true); // Get course ID from URL params if not provided as prop const urlParams = new URLSearchParams(window.location.search); const id = courseId || urlParams.get('id'); if (id) { loadCourseDetail(parseInt(id)); } else { setError('Course ID not provided'); setLoading(false); } }, [courseId]); const loadCourseDetail = async (id: number) => { try { setLoading(true); setError(null); const courseData = await educationService.getCourseById(id); if (courseData) { setCourse(courseData); } else { setError('Course not found'); } } catch (err) { setError('Failed to load course details'); console.error('Error loading course:', err); } finally { setLoading(false); } }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleFileChange = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { setFormData(prev => ({ ...prev, resume: e.target.files![0] })); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!course) { alert('Course not found.'); return; } try { setSubmitting(true); setError(null); let resumeUrl = ''; // Upload resume if provided if (formData.resume) { try { const uploadResponse = await fileUploadService.uploadFile(formData.resume); resumeUrl = uploadResponse.url; } catch (uploadError) { console.warn('Resume upload failed, submitting application without resume:', uploadError); resumeUrl = ''; } } // Prepare application data const applicationData: CourseApplicationData = { courseId: parseInt(course.id), fullName: formData.fullName, email: formData.email, phone: formData.phone, qualification: formData.qualification, experience: formData.experience || undefined, coverLetter: formData.coverLetter || undefined, resumeUrl: resumeUrl || undefined }; // Submit application const success = await educationService.submitApplication(applicationData); if (success) { setSubmitSuccess(true); // Reset form setFormData({ fullName: '', email: '', phone: '', qualification: '', experience: '', coverLetter: '', resume: null }); // Clear file input const fileInput = document.getElementById('resume') as HTMLInputElement; if (fileInput) { fileInput.value = ''; } } else { setError('Failed to submit application. Please try again.'); } } catch (err) { console.error('Error submitting application:', err); setError('Failed to submit application. Please try again.'); } finally { setSubmitting(false); } }; if (!mounted) { return null; } if (loading) { return (

Loading course details...

); } if (error || !course) { return (

⚠️ {error || 'Course not found'}

← Back to Education & Training
); } return (
{/* Header Section with Background Pattern */}
{/* Breadcrumb */} {/* Course Title and Meta */}

{course.title}

Duration: {course.duration}
No of seats: {course.seats}
{/* Course Info Bar */}
Instructor: {course.instructor}
{course.startDate && (
Start Date: {new Date(course.startDate).toLocaleDateString()}
)}
{/* Main Content - Full Width */}
{/* Left Column - Course Information */}
{/* Overview Section */}

Overview

{course.description}

{course.objectives && course.objectives.length > 0 && ( <>

Learning Objectives

The main objectives of the program can be summarised as follows:

    {course.objectives.map((objective: string, index: number) => (
  • {objective}
  • ))}
)}
{/* Eligibility Criteria Section */} {course.eligibility && course.eligibility.length > 0 && (

Eligibility Criteria

{course.eligibility.map((criteria: string, index: number) => ( ))}
Sr. No. Eligibility Requirements
{index + 1} {criteria}
)} {/* How to Apply Section */}

How to Apply

Use the application form on the right to apply for this course, or visit our admissions website:

traumasurg@cmcvellore.ac.in
); }; export default CourseDetail;