(() => { const onIndex = document.getElementById('formulario') !== null; const onProgresso = document.getElementById('botao-download') !== null; async function carregarTemplates() { try { const resp = await fetch('/templates'); const data = await resp.json(); const sel = document.getElementById('template'); if (!sel) return; (data.templates || []).forEach(t => { const o = document.createElement('option'); o.value = t; o.textContent = t; sel.appendChild(o); }); } catch (e) { console.error('Erro ao carregar templates', e); } } function reiniciarPaginaApos3s() { setTimeout(() => { window.location.reload(); }, 3000); } async function enviarConversao() { const form = document.getElementById('formulario'); const btn = document.getElementById('botao-converter'); const fd = new FormData(form); btn.disabled = true; btn.textContent = "Convertendo..."; try { const resp = await fetch('/processar', { method: 'POST', body: fd }); if (!resp.ok) { let errMsg = "Falha no processamento"; try { const data = await resp.json(); if (data.detail) errMsg = data.detail; } catch { const txt = await resp.text(); if (txt) errMsg = txt; } throw new Error(errMsg); } const blob = await resp.blob(); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "agiliza_site.zip"; document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 1000); btn.textContent = "Pronto"; alert("Conversão concluída. O download iniciou!"); reiniciarPaginaApos3s(); } catch (e) { alert(`Erro ao converter o projeto:\n${e.message}`); console.error(e); btn.disabled = false; btn.textContent = "Converter projeto"; } } function monitorarProgresso() { const msg = document.getElementById('mensagem'); const btn = document.getElementById('botao-download'); const tick = () => { const url = sessionStorage.getItem('downloadUrl'); if (url) { msg.textContent = 'Conversão concluída!'; btn.classList.remove('oculto'); btn.href = url; } else { msg.textContent = 'Aguarde, convertendo projeto…'; setTimeout(tick, 700); } }; tick(); } async function criarPastas() { const btn = document.getElementById("botao-criar-pastas"); if (!btn) return; btn.addEventListener("click", async () => { const original = btn.textContent; btn.disabled = true; btn.textContent = "Criando..."; try { const resp = await fetch("/criar_pastas", { method: "POST" }); if (!resp.ok) throw new Error("Erro HTTP " + resp.status); const blob = await resp.blob(); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "estrutura_pastas.zip"; document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url); } catch (err) { alert("Falha ao criar pastas. Verifique o backend."); console.error(err); } finally { btn.textContent = original; btn.disabled = false; } }); } if (onIndex) { carregarTemplates(); document.getElementById('botao-converter')?.addEventListener('click', enviarConversao); criarPastas(); } else if (onProgresso) { monitorarProgresso(); } })();