| |
|
|
| const API_BASE = "/api/anuncios"; |
| const WHATSAPP_NUMERO = "5541998112201"; |
|
|
| function apiUrl(path, params = {}) { |
| const url = new URL(API_BASE + path, window.location.origin); |
| Object.entries(params).forEach(([k, v]) => { |
| if (v !== null && v !== undefined && v !== "") url.searchParams.set(k, v); |
| }); |
| return url.toString(); |
| } |
|
|
| async function apiGet(path, params = {}) { |
| const res = await fetch(apiUrl(path, params)); |
| if (!res.ok) { |
| const detalhe = await res.json().catch(() => ({})); |
| throw new Error(detalhe.detail || `Erro ${res.status} ao consultar a API`); |
| } |
| return res.json(); |
| } |
|
|
| async function apiSend(method, path, body) { |
| const res = await fetch(API_BASE + path, { |
| method, |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify(body), |
| }); |
| if (!res.ok) { |
| const detalhe = await res.json().catch(() => ({})); |
| throw new Error(detalhe.detail ? JSON.stringify(detalhe.detail) : `Erro ${res.status}`); |
| } |
| return method === "DELETE" ? null : res.json(); |
| } |
|
|
| function formatMoney(valor, tipoNegocio) { |
| const n = Number(valor) || 0; |
| const formatted = n.toLocaleString("pt-BR", { style: "currency", currency: "BRL", maximumFractionDigits: 0 }); |
| if (tipoNegocio === "Locação") return `${formatted}<small> /mês</small>`; |
| return formatted; |
| } |
|
|
| function escapeHtml(str) { |
| const div = document.createElement("div"); |
| div.textContent = str ?? ""; |
| return div.innerHTML; |
| } |
|
|
| function statusClass(status) { |
| const map = { |
| "Disponível": "status-disponivel", |
| "Reservado": "status-reservado", |
| "Inativo": "status-inativo", |
| "Vendido": "status-vendido", |
| "Alugado": "status-alugado", |
| }; |
| return map[status] || "status-disponivel"; |
| } |
|
|
| function iconCama() { |
| return `<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M2 18v-6a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v6"/><path d="M2 18v2"/><path d="M22 18v2"/><path d="M4 10V7a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v3"/></svg>`; |
| } |
| function iconBanheiro() { |
| return `<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 12h16v3a4 4 0 0 1-4 4H8a4 4 0 0 1-4-4v-3Z"/><path d="M4 12V5a2 2 0 0 1 2-2c1.1 0 2 .9 2 2"/><path d="M8 19v2M16 19v2"/></svg>`; |
| } |
| function iconVaga() { |
| return `<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="7" width="18" height="12" rx="2"/><path d="M7 7V5a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v2"/></svg>`; |
| } |
| function iconArea() { |
| return `<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="1"/><path d="M9 3v4M15 3v4M9 21v-4M15 21v-4M3 9h4M3 15h4M21 9h-4M21 15h-4"/></svg>`; |
| } |
|
|
| function cardImovel(a) { |
| const specs = []; |
| if (a.quartos) specs.push(`<span>${iconCama()} ${a.quartos}</span>`); |
| if (a.banheiros) specs.push(`<span>${iconBanheiro()} ${a.banheiros}</span>`); |
| if (a.vagas_garagem) specs.push(`<span>${iconVaga()} ${a.vagas_garagem}</span>`); |
| const area = a.area_util || a.area_total; |
| if (area) specs.push(`<span>${iconArea()} ${area}m²</span>`); |
|
|
| return ` |
| <a class="card" href="imovel.html?id=${a.id}"> |
| <div class="card-media"> |
| <span class="tag ${a.tipo_negocio === 'Venda' ? 'tag-venda' : 'tag-locacao'}">${a.tipo_negocio}</span> |
| ${a.destaque ? '<span class="tag-destaque">Destaque</span>' : ''} |
| <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M3 21h18M5 21V9l7-6 7 6v12M9 21v-6h6v6"/></svg> |
| </div> |
| <div class="card-body"> |
| <span class="status-pill ${statusClass(a.status)}">${a.status}</span> |
| <div class="card-price">${formatMoney(a.preco, a.tipo_negocio)}</div> |
| <h3 class="card-title">${escapeHtml(a.titulo)}</h3> |
| <div class="card-loc">📍 ${escapeHtml(a.bairro)}, ${escapeHtml(a.cidade)}</div> |
| <div class="card-specs">${specs.join("")}</div> |
| </div> |
| </a>`; |
| } |
|
|
| function skeletonCards(n = 6) { |
| return Array.from({ length: n }).map(() => ` |
| <div class="card" style="opacity:.5"> |
| <div class="card-media"></div> |
| <div class="card-body"> |
| <div style="height:14px;background:#e5ded0;border-radius:4px;width:60%"></div> |
| <div style="height:18px;background:#e5ded0;border-radius:4px;width:90%;margin-top:8px"></div> |
| </div> |
| </div>`).join(""); |
| } |
|
|
| function whatsappLink(mensagem) { |
| return `https://wa.me/${WHATSAPP_NUMERO}?text=${encodeURIComponent(mensagem)}`; |
| } |
|
|
| function toast(msg, isError = false) { |
| let el = document.querySelector(".toast"); |
| if (!el) { |
| el = document.createElement("div"); |
| el.className = "toast"; |
| document.body.appendChild(el); |
| } |
| el.textContent = msg; |
| el.classList.toggle("error", isError); |
| el.classList.add("show"); |
| clearTimeout(el._t); |
| el._t = setTimeout(() => el.classList.remove("show"), 3200); |
| } |
|
|