| |
|
|
| const form = document.getElementById("form-anuncio"); |
| const btnCancelar = document.getElementById("btn-cancelar"); |
| const btnSalvar = document.getElementById("btn-salvar"); |
|
|
| function lerFormulario() { |
| return { |
| tipo_negocio: document.getElementById("a-tipo-negocio").value, |
| tipo_imovel: document.getElementById("a-tipo-imovel").value, |
| titulo: document.getElementById("a-titulo").value, |
| descricao: document.getElementById("a-descricao").value, |
| endereco: document.getElementById("a-endereco").value, |
| bairro: document.getElementById("a-bairro").value, |
| cidade: document.getElementById("a-cidade").value, |
| estado: document.getElementById("a-estado").value, |
| cep: document.getElementById("a-cep").value, |
| preco: Number(document.getElementById("a-preco").value || 0), |
| condominio: Number(document.getElementById("a-condominio").value || 0), |
| iptu: Number(document.getElementById("a-iptu").value || 0), |
| area_util: Number(document.getElementById("a-area-util").value || 0), |
| area_total: Number(document.getElementById("a-area-total").value || 0), |
| quartos: Number(document.getElementById("a-quartos").value || 0), |
| suites: Number(document.getElementById("a-suites").value || 0), |
| banheiros: Number(document.getElementById("a-banheiros").value || 0), |
| vagas_garagem: Number(document.getElementById("a-vagas").value || 0), |
| status: document.getElementById("a-status").value, |
| destaque: document.getElementById("a-destaque").checked, |
| corretor_responsavel: document.getElementById("a-corretor").value, |
| telefone_contato: document.getElementById("a-telefone").value, |
| fotos: "", |
| }; |
| } |
|
|
| function preencherFormulario(a) { |
| document.getElementById("a-id").value = a.id; |
| document.getElementById("a-tipo-negocio").value = a.tipo_negocio; |
| document.getElementById("a-tipo-imovel").value = a.tipo_imovel; |
| document.getElementById("a-titulo").value = a.titulo; |
| document.getElementById("a-descricao").value = a.descricao || ""; |
| document.getElementById("a-endereco").value = a.endereco || ""; |
| document.getElementById("a-bairro").value = a.bairro || ""; |
| document.getElementById("a-cidade").value = a.cidade; |
| document.getElementById("a-estado").value = a.estado; |
| document.getElementById("a-cep").value = a.cep || ""; |
| document.getElementById("a-preco").value = a.preco; |
| document.getElementById("a-condominio").value = a.condominio || 0; |
| document.getElementById("a-iptu").value = a.iptu || 0; |
| document.getElementById("a-area-util").value = a.area_util || 0; |
| document.getElementById("a-area-total").value = a.area_total || 0; |
| document.getElementById("a-quartos").value = a.quartos || 0; |
| document.getElementById("a-suites").value = a.suites || 0; |
| document.getElementById("a-banheiros").value = a.banheiros || 0; |
| document.getElementById("a-vagas").value = a.vagas_garagem || 0; |
| document.getElementById("a-status").value = a.status; |
| document.getElementById("a-destaque").checked = !!a.destaque; |
| document.getElementById("a-corretor").value = a.corretor_responsavel || ""; |
| document.getElementById("a-telefone").value = a.telefone_contato || ""; |
|
|
| btnSalvar.textContent = `Atualizar anúncio #${a.id}`; |
| btnCancelar.style.display = ""; |
| form.scrollIntoView({ behavior: "smooth" }); |
| } |
|
|
| function limparFormulario() { |
| form.reset(); |
| document.getElementById("a-id").value = ""; |
| document.getElementById("a-estado").value = "PR"; |
| btnSalvar.textContent = "Salvar anúncio"; |
| btnCancelar.style.display = "none"; |
| } |
|
|
| async function carregarLista() { |
| const tbody = document.getElementById("admin-tbody"); |
| tbody.innerHTML = `<tr><td colspan="7" class="muted">Carregando anúncios…</td></tr>`; |
| try { |
| const data = await apiGet("/", { limit: 200, ordenar_por: "mais_recentes" }); |
| document.getElementById("admin-total").textContent = `${data.total} anúncio(s) na base`; |
| if (!data.resultados.length) { |
| tbody.innerHTML = `<tr><td colspan="7" class="muted">Nenhum anúncio cadastrado ainda.</td></tr>`; |
| return; |
| } |
| tbody.innerHTML = data.resultados.map((a) => ` |
| <tr> |
| <td class="badge-mono">#${a.id}</td> |
| <td>${escapeHtml(a.titulo)}</td> |
| <td>${a.tipo_negocio}</td> |
| <td>${escapeHtml(a.cidade)}</td> |
| <td class="badge-mono">${formatMoney(a.preco, a.tipo_negocio).replace(/<[^>]+>/g, "")}</td> |
| <td><span class="status-pill ${statusClass(a.status)}">${a.status}</span></td> |
| <td> |
| <button class="icon-btn" data-acao="editar" data-id="${a.id}">Editar</button> |
| <button class="icon-btn" data-acao="ver" data-id="${a.id}">Ver</button> |
| <button class="icon-btn danger" data-acao="excluir" data-id="${a.id}">Excluir</button> |
| </td> |
| </tr> |
| `).join(""); |
| } catch (err) { |
| tbody.innerHTML = `<tr><td colspan="7">Erro ao carregar: ${escapeHtml(err.message)}</td></tr>`; |
| } |
| } |
|
|
| document.getElementById("admin-tbody").addEventListener("click", async (e) => { |
| const btn = e.target.closest("button[data-acao]"); |
| if (!btn) return; |
| const id = btn.dataset.id; |
|
|
| if (btn.dataset.acao === "ver") { |
| window.open(`imovel.html?id=${id}`, "_blank"); |
| return; |
| } |
| if (btn.dataset.acao === "editar") { |
| try { |
| const a = await apiGet(`/${id}`); |
| preencherFormulario(a); |
| } catch (err) { |
| toast(err.message, true); |
| } |
| return; |
| } |
| if (btn.dataset.acao === "excluir") { |
| if (!confirm(`Excluir o anúncio #${id}? Essa ação não pode ser desfeita.`)) return; |
| try { |
| await apiSend("DELETE", `/${id}`); |
| toast(`Anúncio #${id} removido.`); |
| carregarLista(); |
| } catch (err) { |
| toast(err.message, true); |
| } |
| } |
| }); |
|
|
| form.addEventListener("submit", async (e) => { |
| e.preventDefault(); |
| const id = document.getElementById("a-id").value; |
| const payload = lerFormulario(); |
|
|
| btnSalvar.disabled = true; |
| try { |
| if (id) { |
| await apiSend("PUT", `/${id}`, payload); |
| toast(`Anúncio #${id} atualizado.`); |
| } else { |
| const criado = await apiSend("POST", "/", payload); |
| toast(`Anúncio #${criado.id} criado.`); |
| } |
| limparFormulario(); |
| carregarLista(); |
| } catch (err) { |
| toast(err.message, true); |
| } finally { |
| btnSalvar.disabled = false; |
| } |
| }); |
|
|
| btnCancelar.addEventListener("click", limparFormulario); |
|
|
| document.addEventListener("DOMContentLoaded", carregarLista); |
|
|