<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="mb-4"><h1 class="h3 fw-bold text-cfc-dark mb-1">E-books dos alunos</h1><p class="text-muted mb-0">Libere para um aluno específico ou para todos os alunos de um grupo teórico.</p></div>
<div class="row g-4 mb-4"><div class="col-lg-5"><div class="card border-0 shadow-soft rounded-4 h-100"><div class="card-body p-4"><h2 class="h5 fw-bold mb-3"><i class="bi bi-upload"></i> Adicionar E-book</h2><form method="post" action="<?= e(url('admin/ebooks/save')) ?>" enctype="multipart/form-data" class="row g-3"><?= CSRF::input() ?><div class="col-12"><label class="form-label">Título</label><input name="title" class="form-control" required></div><div class="col-12"><label class="form-label">Descrição</label><textarea name="description" rows="3" class="form-control"></textarea></div><div class="col-12"><label class="form-label">Arquivo</label><input type="file" name="ebook_file" class="form-control" required accept=".pdf,.epub,.doc,.docx,image/*"></div><div class="col-12"><label class="form-label">Imagem da capa</label><input type="file" name="cover_image" class="form-control" accept="image/jpeg,image/png,image/webp"></div><div class="col-12"><button class="btn btn-success rounded-pill px-4">Salvar e-book</button></div></form></div></div></div>
<div class="col-lg-7"><div class="card border-0 shadow-soft rounded-4 h-100"><div class="card-body p-4"><h2 class="h5 fw-bold mb-3"><i class="bi bi-unlock"></i> Liberar e-book</h2><form method="post" action="<?= e(url('admin/ebooks/grant')) ?>" class="row g-3 recipient-form"><?= CSRF::input() ?><input type="hidden" name="recipient_type"><input type="hidden" name="recipient_id"><div class="col-md-6"><label class="form-label">E-book</label><select name="ebook_id" class="form-select" required><option value="">Selecione</option><?php foreach($ebooks as $eBook): ?><option value="<?= (int)$eBook['id'] ?>"><?= e($eBook['title']) ?></option><?php endforeach; ?></select></div><div class="col-md-6"><label class="form-label">Aluno ou grupo</label><select class="form-select recipient-picker" required><option value="">Selecione</option><optgroup label="Alunos"><?php foreach($students as $p): ?><option value="student:<?= (int)$p['id'] ?>"><?= e($p['name']) ?></option><?php endforeach; ?></optgroup><optgroup label="Grupos teóricos"><?php foreach($groups as $g): if(empty($g['is_active']))continue; ?><option value="group:<?= (int)$g['id'] ?>"><?= e($g['name']) ?></option><?php endforeach; ?></optgroup></select></div><div class="col-12"><button class="btn btn-primary rounded-pill px-4">Liberar conteúdo</button></div></form></div></div></div></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">Materiais cadastrados</h2><div class="table-responsive"><table class="table align-middle"><thead><tr><th>Capa</th><th>Título</th><th>Liberados</th><th>Status</th><th></th></tr></thead><tbody><?php foreach($ebooks as $eBook): ?><tr><td><?php if(!empty($eBook['cover_path'])): ?><img class="content-cover-thumb" src="<?= e(url('media/ebook-cover',['id'=>(int)$eBook['id']])) ?>" alt="Capa"><?php else: ?><div class="content-cover-placeholder"><i class="bi bi-book"></i></div><?php endif; ?></td><td><strong><?= e($eBook['title']) ?></strong><small class="d-block text-muted"><?= e($eBook['original_name']??'') ?></small></td><td><span class="badge text-bg-info"><?= (int)$eBook['total_released'] ?> liberação(ões)</span></td><td><?= !empty($eBook['is_active'])?'<span class="badge text-bg-success">Ativo</span>':'<span class="badge text-bg-secondary">Inativo</span>' ?></td><td class="text-end"><div class="d-flex gap-2 justify-content-end"><form method="post" action="<?= e(url('admin/ebooks/toggle')) ?>"><?= CSRF::input() ?><input type="hidden" name="ebook_id" value="<?= (int)$eBook['id'] ?>"><button class="btn btn-sm btn-outline-primary rounded-pill"><?= !empty($eBook['is_active'])?'Desativar':'Ativar' ?></button></form><form method="post" action="<?= e(url('admin/ebooks/delete')) ?>" onsubmit="return confirm('Apagar este e-book?');"><?= CSRF::input() ?><input type="hidden" name="ebook_id" value="<?= (int)$eBook['id'] ?>"><button class="btn btn-sm btn-outline-danger rounded-pill">Apagar</button></form></div></td></tr><?php endforeach; ?><?php if(empty($ebooks)): ?><tr><td colspan="5" class="text-center text-muted py-4">Nenhum e-book cadastrado.</td></tr><?php endif; ?></tbody></table></div></div></div>
<div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><h2 class="h5 fw-bold mb-3">Liberações atuais</h2><div class="table-responsive"><table class="table align-middle"><thead><tr><th>E-book</th><th>Destinatário</th><th>Tipo</th><th>Liberado em</th><th></th></tr></thead><tbody><?php foreach($accessList as $a): ?><tr><td><strong><?= e($a['ebook_title']) ?></strong></td><td><?= e($a['recipient_name']) ?><small class="d-block text-muted"><?= e($a['recipient_detail']??'') ?></small></td><td><span class="badge text-bg-light border"><?= e($a['recipient_type']==='group'?'Grupo teórico':'Aluno') ?></span></td><td><?= e(date('d/m/Y H:i',strtotime($a['granted_at']))) ?></td><td class="text-end"><form method="post" action="<?= e(url('admin/ebooks/revoke')) ?>" onsubmit="return confirm('Remover esta liberação?');"><?= CSRF::input() ?><input type="hidden" name="ebook_id" value="<?= (int)$a['ebook_id'] ?>"><input type="hidden" name="recipient_type" value="<?= e($a['recipient_type']) ?>"><input type="hidden" name="recipient_id" value="<?= (int)$a['recipient_id'] ?>"><button class="btn btn-sm btn-outline-danger rounded-pill">Remover</button></form></td></tr><?php endforeach; ?><?php if(empty($accessList)): ?><tr><td colspan="5" class="text-center text-muted py-4">Nenhuma liberação.</td></tr><?php endif; ?></tbody></table></div></div></div>
</div></div></main><script>document.querySelectorAll('.recipient-form').forEach(f=>f.addEventListener('submit',e=>{const v=f.querySelector('.recipient-picker').value.split(':');if(v.length!==2){e.preventDefault();return}f.querySelector('[name=recipient_type]').value=v[0];f.querySelector('[name=recipient_id]').value=v[1]}));</script>
