<main class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH . '/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <div class="d-flex flex-column flex-md-row justify-content-between gap-3 mb-4">
                <div>
                    <h1 class="h3 fw-bold text-cfc-dark mb-1">Aulas Teóricas</h1>
                    <p class="text-muted mb-0">Cadastre os dias, horários, tempo de curso e matérias que os alunos vão visualizar.</p>
                </div>
                <a class="btn btn-outline-primary rounded-pill align-self-start" target="_blank" href="<?= e(url('theory')) ?>">Ver como aluno</a>
            </div>

            <div class="card border-0 shadow-soft rounded-4 mb-4">
                <div class="card-body p-4">
                    <h2 class="h5 fw-bold mb-3"><i class="bi bi-journal-plus text-warning"></i> Cadastrar nova aula teórica</h2>
                    <form method="post" action="<?= e(url('admin/theory/save')) ?>" class="row g-3">
                        <?= CSRF::input() ?>
                        <div class="col-md-4"><label class="form-label">Título</label><input name="title" class="form-control" value="Aula teórica" required></div>
                        <div class="col-md-2"><label class="form-label">Data</label><input type="date" name="lesson_date" class="form-control" value="<?= e(date('Y-m-d')) ?>" required></div>
                        <div class="col-md-2"><label class="form-label">Início</label><input type="time" name="start_time" class="form-control" value="08:00" required></div>
                        <div class="col-md-2"><label class="form-label">Fim</label><input type="time" name="end_time" class="form-control" value="10:00"></div>
                        <div class="col-md-2"><label class="form-label">Tempo do Curso</label><input name="course_duration" class="form-control" placeholder="Ex: 45h/aula"></div>
                        <div class="col-md-7"><label class="form-label">Matéria / conteúdo do dia</label><textarea name="subject" rows="3" class="form-control" placeholder="Ex: Legislação de trânsito, sinalização, direção Defensiva..."></textarea></div>
                        <div class="col-md-5"><label class="form-label">Observações</label><textarea name="notes" rows="3" class="form-control" placeholder="Ex: Trazer documento, chegar 15 minutos antes..."></textarea></div>
                        <div class="col-md-3"><label class="form-check mt-2"><input type="checkbox" class="form-check-input" name="is_active" value="1" checked> Visível para alunos</label></div>
                        <div class="col-12"><button class="btn btn-primary rounded-pill px-4">Salvar aula teórica</button></div>
                    </form>
                </div>
            </div>

            <div class="card border-0 shadow-soft rounded-4">
                <div class="card-body p-4">
                    <h2 class="h5 fw-bold mb-3">Aulas Cadastradas</h2>
                    <div class="table-responsive">
                        <table class="table align-middle">
                            <thead><tr><th>Data</th><th>Horário</th><th>Informações</th><th>Status</th><th>Editar</th><th class="text-end">Ações</th></tr></thead>
                            <tbody>
                            <?php foreach ($lessons as $lesson): ?>
                                <tr>
                                    <td><strong><?= e(date('d/m/Y', strtotime($lesson['lesson_date']))) ?></strong><small class="d-block text-muted"><?= e(weekday_pt_full($lesson['lesson_date'])) ?></small></td>
                                    <td><?= e(substr($lesson['start_time'], 0, 5)) ?><?= !empty($lesson['end_time']) ? ' - ' . e(substr($lesson['end_time'], 0, 5)) : '' ?><small class="d-block text-muted"><?= e($lesson['course_duration'] ?? '') ?></small></td>
                                    <td style="min-width:260px"><strong><?= e($lesson['title']) ?></strong><small class="d-block text-muted"><?= nl2br(e($lesson['subject'] ?? '')) ?></small><?php if (!empty($lesson['notes'])): ?><small class="d-block mt-1"><i class="bi bi-info-circle"></i> <?= nl2br(e($lesson['notes'])) ?></small><?php endif; ?></td>
                                    <td><span class="badge <?= !empty($lesson['is_active']) ? 'text-bg-success' : 'text-bg-secondary' ?>"><?= !empty($lesson['is_active']) ? 'visível' : 'oculta' ?></span></td>
                                    <td style="min-width:420px">
                                        <form method="post" action="<?= e(url('admin/theory/save')) ?>" class="row g-2">
                                            <?= CSRF::input() ?>
                                            <input type="hidden" name="id" value="<?= (int)$lesson['id'] ?>">
                                            <div class="col-md-6"><input name="title" class="form-control form-control-sm" value="<?= e($lesson['title']) ?>" required></div>
                                            <div class="col-md-3"><input type="date" name="lesson_date" class="form-control form-control-sm" value="<?= e($lesson['lesson_date']) ?>" required></div>
                                            <div class="col-md-3"><input name="course_duration" class="form-control form-control-sm" value="<?= e($lesson['course_duration'] ?? '') ?>" placeholder="45h/aula"></div>
                                            <div class="col-md-3"><input type="time" name="start_time" class="form-control form-control-sm" value="<?= e(substr($lesson['start_time'], 0, 5)) ?>" required></div>
                                            <div class="col-md-3"><input type="time" name="end_time" class="form-control form-control-sm" value="<?= e(!empty($lesson['end_time']) ? substr($lesson['end_time'], 0, 5) : '') ?>"></div>
                                            <div class="col-md-6"><label class="form-check small mt-1"><input type="checkbox" class="form-check-input" name="is_active" value="1" <?= !empty($lesson['is_active']) ? 'checked' : '' ?>> Visível</label></div>
                                            <div class="col-12"><textarea name="subject" rows="2" class="form-control form-control-sm" placeholder="Matéria do dia"><?= e($lesson['subject'] ?? '') ?></textarea></div>
                                            <div class="col-12"><textarea name="notes" rows="2" class="form-control form-control-sm" placeholder="Observações"><?= e($lesson['notes'] ?? '') ?></textarea></div>
                                            <div class="col-12"><button class="btn btn-sm btn-primary rounded-pill px-3"><i class="bi bi-save"></i> Salvar edição</button></div>
                                        </form>
                                    </td>
                                    <td class="text-end">
                                        <form method="post" action="<?= e(url('admin/theory/delete')) ?>" onsubmit="return confirm('Apagar está aula teórica?');">
                                            <?= CSRF::input() ?>
                                            <input type="hidden" name="lesson_id" value="<?= (int)$lesson['id'] ?>">
                                            <button class="btn btn-sm btn-outline-danger rounded-pill">Apagar</button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (!$lessons): ?><tr><td colspan="6" class="text-center text-muted py-4">Nenhuma aula teórica cadastrada.</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
