<?php $pageTitle = e($lesson['title']); ?>
<div class="mb-3">
    <a href="<?= base_url('student/course/' . e($course['slug'] ?? '')) ?>" class="text-secondary"><i class="fas fa-arrow-left me-1"></i> Back to Course</a>
</div>
<div class="d-flex align-items-center gap-2 mb-3 text-secondary small">
    <span><?= e($course['course_title'] ?? '') ?></span> <i class="fas fa-chevron-right"></i>
    <span><?= e($lesson['title']) ?></span>
</div>
<div class="lesson-content">
    <div class="d-flex justify-content-between align-items-start mb-3">
        <div>
            <h2><?= e($lesson['title']) ?></h2>
            <p class="text-secondary"><i class="fas fa-tag me-1"></i> <?= ucfirst(e($lesson['lesson_type'])) ?> Lesson
            <?php if ($lesson['duration_minutes']): ?> | <i class="fas fa-clock me-1"></i> <?= (int)$lesson['duration_minutes'] ?> min<?php endif; ?></p>
        </div>
        <button onclick="markComplete(<?= $lesson['id'] ?>)" class="btn-cix btn-cix-accent btn-cix-sm" id="completeBtn"><i class="fas fa-check"></i> Mark Complete</button>
    </div>
    <?php if ($lesson['lesson_type'] === 'video' && $lesson['video_url']): ?>
        <div class="ratio ratio-16x9 mb-4"><iframe src="<?= e($lesson['video_url']) ?>" allowfullscreen></iframe></div>
    <?php endif; ?>
    <?php if ($lesson['lesson_type'] === 'external_link' && $lesson['external_url']): ?>
        <div class="alert-cix alert-cix-info mb-3"><i class="fas fa-external-link-alt"></i> This lesson is an external resource: <a href="<?= e($lesson['external_url']) ?>" target="_blank"><?= e($lesson['external_url']) ?></a></div>
    <?php endif; ?>
    <div class="mb-4"><?= $lesson['content'] ?? '' ?></div>
    <?php if (!empty($files)): ?>
        <div class="card-cix mt-4">
            <h5><i class="fas fa-paperclip text-orange me-2"></i> Downloadable Files</h5>
            <?php foreach ($files as $file): ?>
            <div style="padding:0.5rem 0;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
                <div class="d-flex align-items-center gap-2"><i class="fas fa-file text-secondary"></i> <?= e($file['file_name']) ?></div>
                <a href="<?= asset_url('uploads/' . $file['file_path']) ?>" class="btn-cix btn-cix-sm btn-cix-outline" download><i class="fas fa-download"></i></a>
            </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
<script>
function markComplete(lessonId) {
    fetch('<?= base_url('api/mark-lesson-complete/') ?>' + lessonId, {method:'POST', headers:{'Content-Type':'application/json','X-CSRF-Token':'<?= csrf_token() ?>'}})
    .then(r => r.json()).then(d => { if(d.success) { document.getElementById('completeBtn').innerHTML = '<i class="fas fa-check-double"></i> Completed'; document.getElementById('completeBtn').disabled = true; }});
}
</script>
