const token = document.cookie.split("=")[1];
function createPartner() {
  const form = document.getElementById("createNewsForm");

  const PartnerNameInput = form.querySelector("#partnerName");
  const pictureInput = form.querySelector("#picture");
  const partnerLink = form.querySelector("#partnerLink");

  const partnerNameValue = PartnerNameInput.value;
  const pictureValue = pictureInput.value;
  const partnerLinkValue = partnerLink.value;

  const formData = new FormData();
  if (pictureInput.files.length > 0) {
    formData.append("file", pictureInput.files[0]);
  }
  axios
    .post("https://api.sanabel-altatweer.com/upload", formData)
    .then(function (response) {
      const imageUrl = response.data.url;
      axios
        .post(
          "https://api.sanabel-altatweer.com/partner",
          {
            name: partnerNameValue,
            link: partnerLinkValue,
            image: imageUrl,
          },
          {
            headers: {
              Authorization: `Bearer ${token}`,
            },
          }
        )
        .then(function (response) {
          window.location.href = "/html/partners.html";
        })
        .catch(function (error) {
          console.log(error);
          Swal.fire({
            icon: "error",
            title: "حدث خطأ",
            text: "حدث خطأ ما, من فضلك حاول مرة اخرى",
            confirmButtonColor: "#db792e",
          });
        });
    })
    .catch(function (error) {
      console.log(error);
      Swal.fire({
        icon: "error",
        title: "حدث خطأ",
        text: "حدث خطأ ما, من فضلك حاول مرة اخرى",
        confirmButtonColor: "#db792e",
      });
    });
}
function getAllPartners() {
  axios
    .get("https://api.sanabel-altatweer.com/partner")
    .then(function (response) {
      const newsContainer = document.getElementById("NewsContainer");

      response.data.forEach((partner) => {
        const newsRow = document.createElement("div");
        newsRow.className = "news-row d-flex align-center";

        // Create the img element
        const img = document.createElement("img");
        img.setAttribute("decoding", "async");
        img.src = `${partner.image.replace("http:", "http://")}`; // Assuming image URL comes from the news object
        img.alt = "";

        // Create the info div
        const infoDiv = document.createElement("div");
        infoDiv.className = "info";

        const h3 = document.createElement("h3");
        h3.textContent = partner.name;

        const link = document.createElement("a");
        link.href = partner.link; // Assuming the news object has a 'link' field
        link.textContent = "الذهاب للموقع";
        link.style.textDecoration = "none";

        infoDiv.appendChild(h3);
        infoDiv.appendChild(link);

        // Create the buttons-group div
        const buttonsGroup = document.createElement("div");
        buttonsGroup.className = "buttons-group";

        // const editButton = document.createElement("button");
        // editButton.type = "button";
        // editButton.className = "btn btn-secondary";
        // editButton.textContent = "تعديل";

        const deleteButton = document.createElement("button");
        deleteButton.type = "button";
        deleteButton.className = "btn btn-danger";
        deleteButton.textContent = "حذف";
        deleteButton.onclick = function () {
          deletePartner(partner.id); // Assuming you have a deleteNews function
        };
        buttonsGroup.appendChild(deleteButton);

        // Append elements to newsRow
        newsRow.appendChild(img);
        newsRow.appendChild(infoDiv);
        newsRow.appendChild(buttonsGroup);

        // Append newsRow to newsContainer
        newsContainer.appendChild(newsRow);
      });
    })
    .catch(function (error) {
      console.log(error);
      Swal.fire({
        icon: "error",
        title: "حدث خطأ",
        text: "حدث خطأ ما, من فضلك حاول مرة اخرى",
        confirmButtonColor: "#db792e",
      });
    });
}
function deletePartner(id) {
  axios
    .delete(`https://api.sanabel-altatweer.com/partner/${id}`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    })
    .then(function (response) {
      Swal.fire({
        icon: "success",
        title: response.data.message,
        confirmButtonColor: "#db792e",
      }).then((result) => {
        if (result.isConfirmed) {
          location.reload();
        }
      });
    })
    .catch(function (error) {
      console.log(error);
      Swal.fire({
        icon: "error",
        title: "حدث خطأ",
        text: "حدث خطأ ما, من فضلك حاول مرة اخرى",
        confirmButtonColor: "#db792e",
      });
    });
}
