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

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

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

$course = $db->fetchOne("SELECT * FROM courses WHERE id = ? AND is_active = 1", [$id]);
if (!$course) {
    header('Location: all-courses.php');
    exit;
}

$subItems = $db->fetchAll("SELECT * FROM course_sub_items WHERE course_id = ? AND is_active = 1 ORDER BY duration_value", [$id]);

// If sub_id is provided, find that specific sub-item to use as primary info
$primarySub = null;
if ($sub_id) {
    foreach ($subItems as $sub) {
        if ($sub['id'] == $sub_id) {
            $primarySub = $sub;
            break;
        }
    }
}
// Fallback to first sub-item if sub_id not provided or not found
if (!$primarySub && !empty($subItems)) {
    $primarySub = $subItems[0];
}

// Visual metadata
$cat = $course['category'];
$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) $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'];
}

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);
    }
    .sub-course-table thead {
        background: var(--primary-color);
        color: white;
    }
    .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 Courses</a></li>
                        <li class="breadcrumb-item active text-white" aria-current="page"><?= htmlspecialchars($course['category']) ?></li>
                    </ol>
                </nav>
                <h1 class="display-4 fw-bold mb-3"><?= htmlspecialchars($primarySub['sub_name'] ?? $course['course_name']) ?></h1>
                <div class="d-flex gap-3 flex-wrap align-items-center">
                    <span class="badge bg-primary px-3 py-2 rounded-pill shadow-sm">
                        <i class="fas fa-tag me-1"></i> <?= htmlspecialchars($course['category']) ?>
                    </span>
                    <span class="badge bg-info px-3 py-2 rounded-pill shadow-sm text-dark">
                        <i class="fas fa-clock me-1"></i> <?= $primarySub['duration_label'] ?? $course['duration'] ?>
                    </span>
                    <span class="badge bg-light px-3 py-2 rounded-pill shadow-sm text-dark">
                        <i class="fas fa-graduation-cap me-1"></i> <?= htmlspecialchars($course['eligibility'] ?: '10th/12th Pass') ?>
                    </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 Overview</h3>
                    <div class="course-description mb-4">
                        <?= !empty($course['description']) ? nl2br(htmlspecialchars($course['description'])) : 'This comprehensive program is designed to provide students with industry-relevant skills and practical knowledge in '.htmlspecialchars($course['category']).'. Our curriculum focuses on both theoretical foundations and hands-on experience to ensure our graduates are career-ready.' ?>
                    </div>

                    <?php 
                    // Use syllabus from primarySub
                    $syllabusContent = $primarySub['syllabus'] ?? '';
                    ?>

                    <?php if (!empty($syllabusContent)): ?>
                    <div class="syllabus-container mt-5">
                        <h4 class="fw-bold mb-3 text-primary"><i class="fas fa-file-invoice me-2"></i>Detailed Syllabus</h4>
                        <div class="card bg-light border-0 rounded-4 p-4">
                            <div class="syllabus-rich-text">
                                <?= $syllabusContent ?>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <div class="row g-3 mb-4">
                        <div class="col-md-6">
                            <div class="benefit-item p-3 rounded mb-2">
                                <i class="fas fa-check-circle text-success me-2"></i> Modern Curriculum
                            </div>
                            <div class="benefit-item p-3 rounded mb-2">
                                <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">
                                <i class="fas fa-check-circle text-success me-2"></i> Industry Internship
                            </div>
                            <div class="benefit-item p-3 rounded mb-2">
                                <i class="fas fa-check-circle text-success me-2"></i> 100% Placement Support
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Sub-Courses Table (Now Variants) -->
                <?php if (!empty($subItems)): ?>
                <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-layer-group text-primary me-2"></i>Other Available Durations</h3>
                    <p class="text-muted small mb-4">Explore other duration options available for this program.</p>
                    <div class="table-responsive">
                        <table class="table table-hover sub-course-table align-middle">
                            <thead>
                                <tr>
                                    <th class="ps-3 py-3 rounded-start">Duration Variant</th>
                                    <th class="py-3">Fee</th>
                                    <th class="py-3 rounded-end text-end pe-3">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($subItems as $sub): ?>
                                <tr class="<?= $sub['id'] == $primarySub['id'] ? 'table-primary' : '' ?>">
                                    <td class="ps-3 fw-bold">
                                        <?= htmlspecialchars($sub['sub_name']) ?>
                                        <?php if ($sub['id'] == $primarySub['id']): ?>
                                            <span class="badge bg-primary ms-2 small">Selected</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><span class="badge bg-light text-success border px-3">₹<?= number_format($sub['total_fee'], 0) ?></span></td>
                                    <td class="text-end pe-3">
                                        <?php if ($sub['id'] != $primarySub['id']): ?>
                                            <a href="?id=<?= $id ?>&sub_id=<?= $sub['id'] ?>" class="btn btn-sm btn-outline-primary rounded-pill px-3">View This</a>
                                        <?php else: ?>
                                            <a href="contact-us.php" class="btn btn-sm btn-primary rounded-pill px-3">Enquire Now</a>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </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>
                        <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">
                                Get More Information <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-gradient"></i>
                        </div>
                        <h5 class="fw-bold">Need Help?</h5>
                        <p class="small text-muted">Talk to our career counselor for personalized guidance.</p>
                        <a href="tel:+919627769817" class="btn btn-outline-dark w-100 rounded-pill py-2">
                            <i class="fas fa-phone-alt me-2"></i> +91 96277 69817
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

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