<?php include app()->basePath('app/Views/partials/header.php'); ?>

<section class="panel">
    <p class="eyebrow">Conteudo do curso</p>
    <h1><?= e($course['title']) ?></h1>
    <p>Organize o curso em modulos, aulas, video-aulas, aulas em texto, PDFs e materiais de apoio.</p>

    <div class="security-note">
        <strong>Proteccao de videos:</strong>
        os ficheiros ficam fora da pasta publica e so passam por rotas autenticadas. O player nao mostra botao de download, mas nenhuma plataforma web consegue impedir 100% gravações de ecrã ou extensoes abusivas.
    </div>

    <div class="content-layout">
        <div>
            <section class="sub-panel">
                <h2>Modulos e aulas</h2>
                <?php if (!$sections): ?>
                    <p>Ainda nao existem modulos. Crie o primeiro para adicionar aulas.</p>
                <?php endif; ?>

                <div class="curriculum">
                    <?php foreach ($sections as $section): ?>
                        <article class="curriculum-section">
                            <div class="section-head">
                                <div>
                                    <strong><?= e((string) $section['position']) ?>. <?= e($section['title']) ?></strong>
                                    <?php if (!empty($section['description'])): ?>
                                        <span><?= e($section['description']) ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="curriculum-actions">
                                    <span class="badge"><?= count($section['lessons']) ?> aulas</span>
                                    <form method="post" action="/manage/courses/content/sections/move">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                        <input type="hidden" name="section_id" value="<?= e((string) $section['id']) ?>">
                                        <input type="hidden" name="direction" value="up">
                                        <button class="mini-button" type="submit">Subir</button>
                                    </form>
                                    <form method="post" action="/manage/courses/content/sections/move">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                        <input type="hidden" name="section_id" value="<?= e((string) $section['id']) ?>">
                                        <input type="hidden" name="direction" value="down">
                                        <button class="mini-button" type="submit">Descer</button>
                                    </form>
                                </div>
                            </div>

                            <details class="curriculum-edit">
                                <summary>Editar modulo</summary>
                                <form method="post" action="/manage/courses/content/sections/update" class="form compact-form">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                    <input type="hidden" name="section_id" value="<?= e((string) $section['id']) ?>">
                                    <label>
                                        Titulo
                                        <input name="title" value="<?= e($section['title']) ?>" required>
                                    </label>
                                    <label>
                                        Descricao
                                        <textarea name="description" rows="2"><?= e($section['description'] ?? '') ?></textarea>
                                    </label>
                                    <div class="actions">
                                        <button class="mini-button good" type="submit">Guardar modulo</button>
                                    </div>
                                </form>
                                <form method="post" action="/manage/courses/content/sections/delete" class="compact-form">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                    <input type="hidden" name="section_id" value="<?= e((string) $section['id']) ?>">
                                    <button class="mini-button danger" type="submit">Apagar modulo e aulas</button>
                                </form>
                            </details>

                            <?php foreach ($section['lessons'] as $lesson): ?>
                                <div class="lesson-row">
                                    <div>
                                        <strong><?= e((string) $lesson['position']) ?>. <?= e($lesson['title']) ?></strong>
                                        <span><?= e(lesson_type_label($lesson['type'])) ?><?= !empty($lesson['is_preview']) ? ' · pre-visualizacao' : '' ?></span>
                                        <?php if ($lesson['materials']): ?>
                                            <small><?= count($lesson['materials']) ?> material(is) de apoio</small>
                                        <?php endif; ?>
                                    </div>
                                    <div class="curriculum-actions">
                                        <form method="post" action="/manage/courses/content/lessons/move">
                                            <?= csrf_field() ?>
                                            <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                            <input type="hidden" name="lesson_id" value="<?= e((string) $lesson['id']) ?>">
                                            <input type="hidden" name="direction" value="up">
                                            <button class="mini-button" type="submit">Subir</button>
                                        </form>
                                        <form method="post" action="/manage/courses/content/lessons/move">
                                            <?= csrf_field() ?>
                                            <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                            <input type="hidden" name="lesson_id" value="<?= e((string) $lesson['id']) ?>">
                                            <input type="hidden" name="direction" value="down">
                                            <button class="mini-button" type="submit">Descer</button>
                                        </form>
                                        <a class="mini-button" href="/learn/preview?lesson_id=<?= e((string) $lesson['id']) ?>">Prever</a>
                                    </div>
                                </div>
                                <details class="curriculum-edit lesson-edit">
                                    <summary>Editar aula</summary>
                                    <form method="post" action="/manage/courses/content/lessons/update" class="form compact-form">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                        <input type="hidden" name="lesson_id" value="<?= e((string) $lesson['id']) ?>">
                                        <label>
                                            Titulo da aula
                                            <input name="title" value="<?= e($lesson['title']) ?>" required>
                                        </label>
                                        <label>
                                            Descricao curta
                                            <textarea name="description" rows="2"><?= e($lesson['description'] ?? '') ?></textarea>
                                        </label>
                                        <div class="grid two">
                                            <label>
                                                Duracao em segundos
                                                <input type="number" name="duration_seconds" min="0" value="<?= e((string) ($lesson['duration_seconds'] ?? 0)) ?>">
                                            </label>
                                            <label class="check-row">
                                                <input type="checkbox" name="is_preview" value="1" <?= !empty($lesson['is_preview']) ? 'checked' : '' ?>>
                                                Aula gratuita
                                            </label>
                                        </div>
                                        <div class="actions">
                                            <button class="mini-button good" type="submit">Guardar aula</button>
                                        </div>
                                    </form>
                                    <form method="post" action="/manage/courses/content/lessons/delete" class="compact-form">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                        <input type="hidden" name="lesson_id" value="<?= e((string) $lesson['id']) ?>">
                                        <button class="mini-button danger" type="submit">Apagar aula</button>
                                    </form>
                                </details>
                            <?php endforeach; ?>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>
        </div>

        <aside class="side-stack">
            <section class="sub-panel">
                <h2>Novo modulo</h2>
                <form method="post" action="/manage/courses/content/sections/store" class="form">
                    <?= csrf_field() ?>
                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                    <label>
                        Titulo do modulo
                        <input name="title" placeholder="Ex: Introducao" required>
                    </label>
                    <label>
                        Descricao
                        <textarea name="description" rows="3"></textarea>
                    </label>
                    <button class="button" type="submit">Criar modulo</button>
                </form>
            </section>

            <section class="sub-panel">
                <h2>Nova aula</h2>
                <form method="post" action="/manage/courses/content/lessons/store" class="form" enctype="multipart/form-data">
                    <?= csrf_field() ?>
                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                    <label>
                        Modulo
                        <select name="section_id" required>
                            <option value="">Escolha</option>
                            <?php foreach ($sections as $section): ?>
                                <option value="<?= e((string) $section['id']) ?>"><?= e($section['title']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                    <label>
                        Titulo da aula
                        <input name="title" required>
                    </label>
                    <label>
                        Tipo de aula
                        <select name="type">
                            <option value="video">Video-aula</option>
                            <option value="text">Aula em texto</option>
                            <option value="pdf">Aula em PDF</option>
                        </select>
                    </label>
                    <label>
                        Ficheiro da aula
                        <input type="file" name="lesson_file" accept="video/mp4,video/webm,video/ogg,application/pdf">
                    </label>
                    <label>
                        Conteudo textual
                        <textarea name="text_content" rows="4" placeholder="Use este campo para aulas em texto."></textarea>
                    </label>
                    <label>
                        Materiais de apoio
                        <input type="file" name="materials[]" multiple accept="application/pdf,image/jpeg,image/png,text/plain">
                    </label>
                    <div class="grid two">
                        <label>
                            Duracao em segundos
                            <input type="number" name="duration_seconds" min="0" value="0">
                        </label>
                        <label class="check-row">
                            <input type="checkbox" name="is_preview" value="1">
                            Aula gratuita
                        </label>
                    </div>
                    <label>
                        Descricao curta
                        <textarea name="description" rows="3"></textarea>
                    </label>
                    <button class="button" type="submit">Criar aula</button>
                </form>
            </section>
        </aside>
    </div>
</section>

<?php include app()->basePath('app/Views/partials/footer.php'); ?>
