<?php
// get_course_details.php
include(__DIR__ . "/../includes/db.php");

if (!isset($_POST['id'])) { echo "<div class='alert alert-warning'>Invalid request.</div>"; exit; }

$course_id = (int)$_POST['id'];
$q = $conn->prepare("SELECT * FROM courses WHERE id=? LIMIT 1");
$q->bind_param("i",$course_id); $q->execute();
$course = $q->get_result()->fetch_assoc();
$q->close();
if (!$course) { echo "<div class='alert alert-danger'>Course not found.</div>"; exit; }

$status     = $course['status'] ?? 'Inactive';
$badgeClass = strtolower($status)==='active' ? 'success' : 'secondary';
$imageFile  = $course['image'] ?? '';
$imageUrl   = $imageFile ? ("../uploads/courses/" . $imageFile) : "../assets/no-image.png";

/* Parent modules */
$mods = [];
$m = $conn->prepare("SELECT title FROM course_modules WHERE course_id=? ORDER BY sort_order, id");
$m->bind_param("i",$course_id); $m->execute();
$r = $m->get_result(); while($row=$r->fetch_assoc()){ $mods[]=$row['title']; } $m->close();

/* Children + their modules */
$children = [];
$c = $conn->prepare("
  SELECT ch.child_id, co.course_name
  FROM course_children ch
  JOIN courses co ON co.id = ch.child_id
  WHERE ch.parent_id=?
  ORDER BY ch.sort_order, ch.child_id
");
$c->bind_param("i",$course_id); $c->execute();
$cr = $c->get_result();
while($row=$cr->fetch_assoc()){
    $cm = $conn->prepare("SELECT title FROM course_modules WHERE course_id=? ORDER BY sort_order, id");
    $cm->bind_param("i",$row['child_id']); $cm->execute();
    $rr = $cm->get_result(); $childMods=[];
    while($x=$rr->fetch_assoc()){ $childMods[]=$x['title']; }
    $cm->close();
    $children[] = ['name'=>$row['course_name'], 'mods'=>$childMods];
}
$c->close();

/* Render */
echo "<div class='card shadow-sm border-0 rounded-3'>";
echo "<div class='card-header bg-primary text-white d-flex align-items-center'>";
echo "<i class='bi bi-info-circle-fill fs-5 me-2'></i><h5 class='mb-0'>Course Overview</h5></div>";

echo "<div class='card-body'><div class='row g-4'>";

echo "<div class='col-md-6'>";
//echo "<div class='mb-3'><img src='".htmlspecialchars($imageUrl)."' class='img-fluid rounded' style='max-height:260px;object-fit:cover'></div>";
echo "<ul class='list-group list-group-flush'>";
echo "<li class='list-group-item'><strong>Status:</strong> <span class='badge bg-{$badgeClass}'>".htmlspecialchars($status)."</span></li>";
echo "<li class='list-group-item'><strong>🆔 Code:</strong> ".htmlspecialchars($course['code'] ?? '')."</li>";
echo "<li class='list-group-item'><strong>📘 Course Name:</strong> ".htmlspecialchars($course['course_name'] ?? '')."</li>";
echo "<li class='list-group-item'><strong>⏳ Duration:</strong> ".htmlspecialchars($course['duration'] ?? '')."</li>";
echo "</ul>";

echo "<div class='mt-4'><h6 class='text-decoration-underline'>📝 Course Description</h6>";
$desc = trim((string)($course['course_description'] ?? ''));
echo "<p class='text-muted'>".($desc!==''?nl2br(htmlspecialchars($desc)):'No description provided.')."</p></div>";
echo "</div>";

echo "<div class='col-md-6'>";

echo "<h6 class='text-decoration-underline'>📚 Modules (Main Course)</h6>";
if ($mods){
    echo "<div class='list-group mb-3'>";
    foreach($mods as $i=>$mod){
        echo "<div class='list-group-item d-flex justify-content-between align-items-center'>";
        echo "<span><i class='bi bi-check-circle-fill text-success me-2'></i>".htmlspecialchars($mod)."</span>";
        echo "<span class='badge rounded-pill bg-light text-dark'>".($i+1)."</span>";
        echo "</div>";
    }
    echo "</div>";
} else {
    echo "<div class='text-muted mb-3'>No modules listed.</div>";
}

if ($children){
    echo "<h6 class='text-decoration-underline mt-2'>📦 Bundle Includes</h6>";
    echo "<div class='list-group'>";
    foreach($children as $c){
        echo "<div class='list-group-item'>";
        echo "<div class='fw-semibold'>".htmlspecialchars($c['name'])."</div>";
        if (!empty($c['mods'])){
            echo "<div class='small text-muted mt-1'>Modules:</div><ul class='small mt-1 mb-0'>";
            foreach($c['mods'] as $mTitle){ echo "<li>".htmlspecialchars($mTitle)."</li>"; }
            echo "</ul>";
        } else {
            echo "<div class='small text-muted mt-1'>No modules listed.</div>";
        }
        echo "</div>";
    }
    echo "</div>";
} else {
    echo "<div class='text-muted'>No sub-courses selected.</div>";
}

echo "</div></div></div></div>";
