<?php
require_once 'config.php';
require_once 'includes/db.php';

$db = DB();
$id = intval($_GET['id'] ?? 0);

if (!$id) {
    header('Location: all-courses.php');
    exit;
}

    $isLiveSchema = false;
    try {
        $db->fetchOne("SELECT category_id FROM courses LIMIT 1");
        $isLiveSchema = true;
    } catch(Exception $e) {}

    if ($isLiveSchema) {
        $course = $db->fetchOne("
            SELECT c.*, cat.name as cat_name, sub.name as sub_name 
            FROM courses c 
            LEFT JOIN course_categories cat ON c.category_id = cat.id
            LEFT JOIN course_sub_categories sub ON c.sub_category_id = sub.id
            WHERE c.id = ? AND c.is_active = 1
        ", [$id]);
    } else {
        $course = $db->fetchOne("
            SELECT c.*, c.short_name as cat_name, c.category as sub_name 
            FROM courses c 
            WHERE c.id = ? AND c.is_active = 1
        ", [$id]);
    }

if (!$course) {
    header('Location: all-courses.php');
    exit;
}

// Fetch fee for college
$fee = $db->fetchOne("SELECT total_fee FROM course_fees WHERE course_id = ? AND user_type='college'", [$id]);
$totalFee = $fee ? floatval($fee['total_fee']) : 0;

// Visual metadata
$cat = $course['cat_name'] ?? 'General';
$bannerImg = 'https://images.unsplash.com/photo-1523240795612-9a054b0db644?w=1600';
if (stripos($cat, 'Computer') !== false) $bannerImg = 'https://images.unsplash.com/photo-1517694712202-14dd9538aa97?w=1600';
if (stripos($cat, 'Automobile') !== false) $bannerImg = 'https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?w=1600';
if (stripos($cat, 'Aviation') !== false) $bannerImg = 'https://images.unsplash.com/photo-1436491865332-7a61a109cc05?w=1600';
if (stripos($cat, 'Medical') !== false || stripos($cat, 'Ayurveda') !== false || stripos($cat, 'Health') !== false) $bannerImg = 'https://images.unsplash.com/photo-1512069772995-ec65ed45afd6?w=1600';
if (stripos($cat, 'Civil') !== false) $bannerImg = 'https://images.unsplash.com/photo-1503387762-592deb58ef4e?w=1600';

if (!empty($course['image'])) {
    $bannerImg = $course['image'];
}
$courseName = $course['course_name'];
$pageTitle = htmlspecialchars($courseName) . ' Course Details, Duration & Fee structure | IBVTE';
$pageDesc  = 'Get details about ' . htmlspecialchars($courseName) . ' in the ' . htmlspecialchars($cat) . ' department at IBVTE. View eligibility, course duration, total fees, and syllabus details.';
include 'includes/header.php';
?>

<style>
    .detail-hero {
        height: 450px;
        background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.6)), url('<?= $bannerImg ?>');
        background-size: cover;
        background-position: center;
        display: flex;
        align-items: center;
        color: white;
        position: relative;
    }
    .glass-card {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        border-radius: 20px;
        border: 1px solid rgba(255, 255, 255, 0.2);
    }
    .sticky-sidebar {
        top: 100px;
    }
    .benefit-item {
        transition: all 0.3s ease;
        border-left: 4px solid var(--primary-color);
    }
    .benefit-item:hover {
        background: #f8f9fa;
        transform: translateX(5px);
    }
    .syllabus-rich-text ul {
        padding-left: 20px;
        margin-bottom: 0;
    }
    .syllabus-rich-text li {
        margin-bottom: 10px;
    }
    .syllabus-rich-text h3, .syllabus-rich-text h4 {
        font-weight: 700;
        margin-bottom: 15px;
    }
</style>

<div class="detail-hero">
    <div class="container">
        <div class="row">
            <div class="col-lg-8">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-2 small text-white-50">
                        <li class="breadcrumb-item"><a href="index.php" class="text-white-50">Home</a></li>
                        <li class="breadcrumb-item"><a href="all-courses.php" class="text-white-50">Our Departments</a></li>
                        <li class="breadcrumb-item active text-white" aria-current="page"><?= htmlspecialchars($cat) ?></li>
                    </ol>
                </nav>
                <div class="mb-3">
                    <span class="badge bg-light text-dark px-3 py-2 rounded-pill shadow-sm fw-bold">
                        <?= htmlspecialchars($course['course_code']) ?>
                    </span>
                    <span class="badge bg-primary px-3 py-2 rounded-pill shadow-sm fw-bold ms-2">
                        <?= htmlspecialchars($course['course_type']) ?>
                    </span>
                </div>
                <h1 class="display-4 fw-bold mb-3"><?= htmlspecialchars($course['course_name']) ?></h1>
                
                <div class="d-flex gap-3 flex-wrap align-items-center mt-4">
                    <span class="badge bg-info px-3 py-2 rounded-pill shadow-sm text-dark fs-6">
                        <i class="fas fa-clock me-1"></i> <?= htmlspecialchars($course['duration_text'] ?? $course['duration'] ?? '') ?>
                    </span>
                    <span class="badge bg-dark px-3 py-2 rounded-pill shadow-sm text-white fs-6">
                        <i class="fas fa-graduation-cap me-1"></i> <?= htmlspecialchars($course['eligibility'] ?: '10th/12th Pass') ?>
                    </span>
                    <span class="badge bg-success px-3 py-2 rounded-pill shadow-sm text-white fs-6">
                        <i class="fas fa-rupee-sign me-1"></i> Fee: ₹<?= number_format($totalFee) ?>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>

<section class="py-5 bg-light mt-n5 position-relative">
    <div class="container">
        <div class="row g-4">
            <!-- Main Content -->
            <div class="col-lg-8">
                <div class="card glass-card border-0 shadow-sm p-4 mb-4">
                    <h3 class="fw-bold mb-4 border-bottom pb-2"><i class="fas fa-info-circle text-primary me-2"></i>Course Highlights</h3>
                    <div class="row g-4 mb-4">
                        <div class="col-md-6 col-lg-3 text-center">
                            <div class="p-3 bg-light rounded-4 h-100 border">
                                <i class="fas fa-clock fa-2x text-primary mb-2"></i>
                                <h6 class="fw-bold text-dark mb-1">Duration</h6>
                                <span class="text-muted small"><?= htmlspecialchars($course['duration_text'] ?? $course['duration'] ?? 'N/A') ?></span>
                            </div>
                        </div>
                        <div class="col-md-6 col-lg-3 text-center">
                            <div class="p-3 bg-light rounded-4 h-100 border">
                                <i class="fas fa-graduation-cap fa-2x text-success mb-2"></i>
                                <h6 class="fw-bold text-dark mb-1">Eligibility</h6>
                                <span class="text-muted small"><?= htmlspecialchars($course['eligibility'] ?: '10th/12th Pass') ?></span>
                            </div>
                        </div>
                        <div class="col-md-6 col-lg-3 text-center">
                            <div class="p-3 bg-light rounded-4 h-100 border">
                                <i class="fas fa-rupee-sign fa-2x text-warning mb-2"></i>
                                <h6 class="fw-bold text-dark mb-1">Total Fee</h6>
                                <span class="text-muted small">₹<?= number_format($totalFee) ?></span>
                            </div>
                        </div>
                        <div class="col-md-6 col-lg-3 text-center">
                            <div class="p-3 bg-light rounded-4 h-100 border">
                                <i class="fas fa-chalkboard-teacher fa-2x text-info mb-2"></i>
                                <h6 class="fw-bold text-dark mb-1">Mode</h6>
                                <span class="text-muted small"><?= htmlspecialchars(ucfirst($course['mode'] ?? 'Offline')) ?></span>
                            </div>
                        </div>
                    </div>

                    <h3 class="fw-bold mb-4 border-bottom pb-2 mt-5"><i class="fas fa-book-open text-primary me-2"></i>Course Overview</h3>
                    <div class="course-description mb-4 text-muted" style="line-height: 1.8;">
                        <?= !empty($course['description']) ? nl2br(htmlspecialchars($course['description'])) : 'This comprehensive program is designed to provide students with industry-relevant skills and practical knowledge in '.htmlspecialchars($cat).'. Our curriculum focuses on both theoretical foundations and hands-on experience to ensure our graduates are career-ready.' ?>
                    </div>
                    
                    <div class="row g-3 mb-4 mt-4">
                        <div class="col-md-6">
                            <div class="benefit-item p-3 rounded mb-2 bg-white shadow-sm border-0 border-start border-4 border-primary">
                                <i class="fas fa-check-circle text-success me-2"></i> Modern Curriculum
                            </div>
                            <div class="benefit-item p-3 rounded mb-2 bg-white shadow-sm border-0 border-start border-4 border-primary">
                                <i class="fas fa-check-circle text-success me-2"></i> Expert Faculty
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="benefit-item p-3 rounded mb-2 bg-white shadow-sm border-0 border-start border-4 border-primary">
                                <i class="fas fa-check-circle text-success me-2"></i> Industry Internship
                            </div>
                            <div class="benefit-item p-3 rounded mb-2 bg-white shadow-sm border-0 border-start border-4 border-primary">
                                <i class="fas fa-check-circle text-success me-2"></i> Placement Assistance
                            </div>
                        </div>
                    </div>
                </div>

                <?php if (!empty($course['syllabus_text']) || !empty($course['syllabus_pdf'])): ?>
                <div class="card glass-card border-0 shadow-sm p-4 mb-4">
                    <div class="d-flex justify-content-between align-items-center border-bottom pb-2 mb-4">
                        <h3 class="fw-bold mb-0"><i class="fas fa-file-alt text-primary me-2"></i>Detailed Syllabus</h3>
                        <?php if (!empty($course['syllabus_pdf'])): ?>
                            <a href="<?= $course['syllabus_pdf'] ?>" target="_blank" class="btn btn-outline-danger btn-sm rounded-pill px-3 fw-bold">
                                <i class="fas fa-file-pdf me-1"></i> Download PDF
                            </a>
                        <?php endif; ?>
                    </div>
                    
                    <?php if (!empty($course['syllabus_text'])): ?>
                    <div class="card bg-light border-0 rounded-4 p-4">
                        <div class="syllabus-rich-text">
                            <?= $course['syllabus_text'] ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">
                <div class="sticky-sidebar">
                    <div class="card glass-card border-0 shadow-sm p-4 mb-4">
                        <h4 class="fw-bold mb-3">Quick Inquiry</h4>
                        <p class="text-muted small mb-4">Fill out the form below and our career counselor will contact you.</p>
                        <form action="api/inquiry.php" method="POST">
                            <input type="hidden" name="course_id" value="<?= $id ?>">
                            <div class="mb-3">
                                <input type="text" name="name" class="form-control" placeholder="Your Name" required>
                            </div>
                            <div class="mb-3">
                                <input type="email" name="email" class="form-control" placeholder="Email ID" required>
                            </div>
                            <div class="mb-3">
                                <input type="tel" name="phone" class="form-control" placeholder="Phone Number" required>
                            </div>
                            <button type="submit" class="btn btn-primary w-100 rounded-pill py-2 fw-bold shadow-sm">
                                Submit Inquiry <i class="fas fa-paper-plane ms-2"></i>
                            </button>
                        </form>
                    </div>

                    <div class="card glass-card border-0 shadow-sm p-4 text-center">
                        <div class="mb-3">
                            <i class="fas fa-headset fa-3x text-primary"></i>
                        </div>
                        <h5 class="fw-bold">Need Help?</h5>
                        <p class="small text-muted mb-4">Talk to our career counselor for personalized guidance regarding this course.</p>
                        <a href="tel:+919627769817" class="btn btn-outline-dark w-100 rounded-pill py-2 fw-bold">
                            <i class="fas fa-phone-alt me-2"></i> +91 96277 69817
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<?php include 'includes/footer.php'; ?>
