window.addEventListener("load", () => {
  const btn = document.getElementById("apiCallButton")

  btn.onclick = makeApiCall
})

function makeApiCall() {
  const myHeaders = new Headers()
  myHeaders.append("Content-Type", "application/x-www-form-urlencoded")
  myHeaders.append(
    "Cookie",
    "PVEAuthCookie=PVE:API@pve:65491655::wnT/6TOAZX9i3pzmbRi7IrewNqfBlrwbBbSwANQFffykEZN3N7L2nQzKMunVurZo5veWCOUgA4LAUpAsbhoqFeveOnqYhBv3E9gMpB+yiyET4BRecT0ZKqSvSCeCZCu8oslSBXYBWDqUet4wxnSIogFTtmFFXt6WaA07YT79Of8cUD65OXQF0l3crA2IZ7CasAGWq48wLn2yYn6C1Z/5efPW1GZZxdldxs4PZmuhbvpysa1F3VOwUuyuGHpoiH1m58XJ4Nil5xqmz6MTCCnrgkLEkMuSv2RLMQ1a9z9/GABiLKUfs32uLU8PEt0xBJWnEtSn1bcfJIwlb58SsdZ1mg=="
  )

  const urlencoded = new URLSearchParams()
  urlencoded.append("username", "API@pve")
  urlencoded.append("password", "ApiPass")

  const requestOptions = {
    method: "POST",
    headers: myHeaders,
    body: urlencoded,
    redirect: "follow",
  }

  console.log("API CALL OPTIONS: ", requestOptions)

  fetch("http://10.0.0.174:8006/api2/json/access/ticket", requestOptions)
    .then((response) => response.text())
    .then((result) => console.log("res: ", result))
    .catch((error) => console.log("error", error))
}
