File size: 6,460 Bytes
892cf37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Sahkey Imóveis — painel administrativo de demonstração (CRUD de anúncios) */

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);