106 lines
3.7 KiB
JavaScript
106 lines
3.7 KiB
JavaScript
document.getElementById('uploadForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const fileEl = document.getElementById('csvfile');
|
|
if (!fileEl.files.length) return;
|
|
const fd = new FormData();
|
|
fd.append('csvfile', fileEl.files[0]);
|
|
const resEl = document.getElementById('uploadResult');
|
|
resEl.innerText = 'Enviando...';
|
|
try {
|
|
const resp = await fetch('/upload', { method: 'POST', body: fd });
|
|
const data = await resp.json();
|
|
if (data.jobId) {
|
|
// show progress bar
|
|
document.getElementById('progressWrap').style.display = 'block';
|
|
pollJob(data.jobId);
|
|
resEl.innerText = `Consultando viabilidade...`;
|
|
} else if (data.error) {
|
|
resEl.innerText = 'Erro: ' + data.error;
|
|
}
|
|
} catch (e) {
|
|
resEl.innerText = 'Erro no upload';
|
|
}
|
|
});
|
|
|
|
async function pollJob(jobId) {
|
|
const resEl = document.getElementById('uploadResult');
|
|
const bar = document.getElementById('progressBar');
|
|
try {
|
|
const resp = await fetch(`/status/${jobId}`);
|
|
const j = await resp.json();
|
|
if (j.total && j.total > 0) {
|
|
const pct = Math.round((j.processed / j.total) * 100);
|
|
bar.style.width = pct + '%';
|
|
bar.innerText = `${pct}%`;
|
|
}
|
|
if (j.status === 'done') {
|
|
bar.style.width = '100%';
|
|
bar.innerText = '100%';
|
|
resEl.innerHTML = `Concluído. <a href="${j.download}">Baixar CSV processado</a>`;
|
|
return;
|
|
}
|
|
if (j.status === 'error') {
|
|
resEl.innerText = 'Erro no processamento: ' + j.error;
|
|
return;
|
|
}
|
|
// ainda processando
|
|
setTimeout(() => pollJob(jobId), 1000);
|
|
} catch (e) {
|
|
resEl.innerText = 'Erro ao consultar status do job';
|
|
}
|
|
}
|
|
|
|
document.getElementById('btnConsultaCep').addEventListener('click', async () => {
|
|
const cep = document.getElementById('cep').value;
|
|
const numero = document.getElementById('numero').value;
|
|
const el = document.getElementById('consultaResult');
|
|
el.innerText = 'Consultando...';
|
|
try {
|
|
const resp = await fetch(`/consulta-cep?cep=${encodeURIComponent(cep)}&numero=${encodeURIComponent(numero)}`);
|
|
const data = await resp.json();
|
|
if (data.distancia) {
|
|
el.innerText = `Endereço: ${data.endereco}\nLat: ${data.latitude} Lon: ${data.longitude}\nDistância: ${data.distancia}`;
|
|
} else if (data.error) {
|
|
el.innerText = 'Erro: ' + data.error;
|
|
}
|
|
} catch (e) {
|
|
el.innerText = 'Erro na consulta';
|
|
}
|
|
});
|
|
|
|
// baixar modelo
|
|
// botão que inicia download de todos os modelos individualmente
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
const btn = document.getElementById("card__button-download");
|
|
if (!btn) return;
|
|
|
|
btn.addEventListener("click", async (e) => {
|
|
e.preventDefault();
|
|
try {
|
|
const resp = await fetch("/download-models/list");
|
|
if (!resp.ok) throw new Error("Não foi possível obter lista de modelos");
|
|
const data = await resp.json();
|
|
const files = data.files || [];
|
|
|
|
if (!files.length) {
|
|
alert("Nenhum arquivo de modelo disponível");
|
|
return;
|
|
}
|
|
|
|
// Para cada arquivo, cria um <a> e clica nele para iniciar download individual
|
|
files.forEach((fname) => {
|
|
const url = "/download-model/" + encodeURIComponent(fname);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = fname;
|
|
// necessário anexar no DOM para funcionar em alguns navegadores
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
});
|
|
} catch (err) {
|
|
console.error("Erro ao baixar modelos:", err);
|
|
alert("Erro ao iniciar downloads: " + (err.message || err));
|
|
}
|
|
});
|
|
}); |