const form = document.querySelector("form");
let list_preview = "[";
let postname = "";
let authtor = "";

form.addEventListener("submit", (e) => {
  e.preventDefault();
  const foromdata = new FormData(form);
  for (intem of foromdata) {
    console.log(intem[0], intem[1]);
  }
  fetch("get_res", {
    method: "POST",
    body: foromdata,
  })
    .then((res) => res.json())
    .then((res) => {
      document.getElementById("header").value = "";
      document.getElementById("text").value = "";
      document.getElementById("img").value = "";
      document.getElementById("log").value += res["text"];
      list_preview += res["list_preview"];

      document.getElementById("post_namee").value = res["postname"];
      document.getElementById("authtorr").value = res["authtor"];

      document.getElementById("to_list").value = list_preview + "]";
      console.log(list_preview);
      foromdata.append("tolist", list_preview + "]");
      fetch("getlist", {
        method: "POST",
        body: foromdata,
      })
        .then((res) => res.json())
        .then((res) => {
          console.log(list_preview);
        });
    });
});

function go_to_preview() {
  try {
    const foromdata = new FormData(form);
    foromdata.append("tolist", list_preview + "]");
    fetch("get_res/preview", {
      method: "POST",
      body: foromdata,
    });
  } catch (error) {
    console.log(error);
  }
}

function clear_all() {
  list_preview = "[";
  document.getElementById("header").value = "";
  document.getElementById("text").value = "";
  document.getElementById("log").value = "";
  document.getElementById("post_name").value = "";
  document.getElementById("post_name").value = "";
  document.getElementById("desc").value = "";
  document.getElementById("img").value = "";
  document.getElementById("authtor").value = "";
}
