<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">E-books / PDFs dos Instrutores</h1>
                    <p class="text-muted mb-0">Envie materiais para todos os instrutores ou para um profissional específico.</p>
                </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"><i class="bi bi-upload"></i> Adicionar Material</h2>
                    <form method="post" action="<?= e(url('admin/professional-materials/save')) ?>" enctype="multipart/form-data" class="row g-3">
                        <?= CSRF::input() ?>
                        <div class="col-md-4"><label class="form-label">Título</label><input name="title" class="form-control" required placeholder="Ex: Manual do Instrutor"></div>
                        <div class="col-md-4"><label class="form-label">Liberar para</label><select name="professional_id" class="form-select"><option value="0">Todos os Instrutores</option><?php foreach ($professionals as $p): ?><option value="<?= (int)$p['id'] ?>"><?= e($p['name']) ?> - <?= e($p['type']) ?></option><?php endforeach; ?></select></div>
                        <div class="col-md-4"><label class="form-label">Arquivo</label><input type="file" name="material_file" class="form-control" required accept=".pdf,.epub,.doc,.docx,image/*"></div>
                        <div class="col-md-6"><label class="form-label">Imagem da capa</label><input type="file" name="cover_image" class="form-control" accept="image/jpeg,image/png,image/webp"><small class="text-muted">Opcional. JPG, PNG ou WEBP, até 5 MB.</small></div>
                        <div class="col-12"><label class="form-label">Descrição</label><textarea name="description" rows="2" class="form-control" placeholder="Descrição opcional"></textarea></div>
                        <div class="col-12"><button class="btn btn-success rounded-pill px-4"><i class="bi bi-check2-circle"></i> Salvar material</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">Materiais Cadastrados</h2>
                    <div class="table-responsive"><table class="table align-middle"><thead><tr><th>Capa</th><th>Título</th><th>Arquivo</th><th>Instrutor</th><th>Status</th><th class="text-end">Ações</th></tr></thead><tbody>
                        <?php foreach ($materials as $m): ?>
                            <tr>
                                <td style="min-width:150px">
                                    <?php if (!empty($m['cover_path'])): ?><img class="content-cover-thumb mb-2" src="<?= e(url('media/professional-material-cover', ['id'=>(int)$m['id']])) ?>" alt="Capa"><?php else: ?><div class="content-cover-placeholder mb-2"><i class="bi bi-file-earmark-text"></i></div><?php endif; ?>
                                    <form method="post" action="<?= e(url('admin/professional-materials/cover')) ?>" enctype="multipart/form-data" class="d-flex flex-column gap-1">
                                        <?= CSRF::input() ?><input type="hidden" name="material_id" value="<?= (int)$m['id'] ?>">
                                        <input type="file" name="cover_image" class="form-control form-control-sm" accept="image/jpeg,image/png,image/webp" required>
                                        <button class="btn btn-sm btn-outline-primary">Salvar capa</button>
                                    </form>
                                </td>
                                <td><strong><?= e($m['title']) ?></strong><?php if (!empty($m['description'])): ?><small class="d-block text-muted"><?= nl2br(e($m['description'])) ?></small><?php endif; ?></td>
                                <td><small class="text-muted"><?= e($m['original_name'] ?? '') ?></small><small class="d-block text-muted"><?= number_format(((int)($m['file_size'] ?? 0))/1024, 1, ',', '.') ?> KB</small></td>
                                <td><?= !empty($m['professional_id']) ? e($m['professional_name'] ?? 'Instrutor Removido') : '<span class="badge text-bg-info">Todos</span>' ?></td>
                                <td><?= !empty($m['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 flex-wrap gap-2 justify-content-end">
                                    <form method="post" action="<?= e(url('admin/professional-materials/toggle')) ?>"><?= CSRF::input() ?><input type="hidden" name="material_id" value="<?= (int)$m['id'] ?>"><button class="btn btn-sm btn-outline-primary rounded-pill"><?= !empty($m['is_active']) ? 'Desativar' : 'Ativar' ?></button></form>
                                    <form method="post" action="<?= e(url('admin/professional-materials/delete')) ?>" onsubmit="return confirm('Apagar este material dos instrutores?');"><?= CSRF::input() ?><input type="hidden" name="material_id" value="<?= (int)$m['id'] ?>"><button class="btn btn-sm btn-outline-danger rounded-pill">Apagar</button></form>
                                </div></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (!$materials): ?><tr><td colspan="6" class="text-center text-muted py-4">Nenhum material cadastrado.</td></tr><?php endif; ?>
                    </tbody></table></div>
                </div>
            </div>
        </div>
    </div>
</main>
