// js/wishlist.js
(function () {
  "use strict";

  // Sort direkt submitten
  const sortSel = document.getElementById("sortby");
  if (sortSel && sortSel.form) {
    sortSel.addEventListener("change", function () {
      this.form.submit();
    });
  }

  // Reservation Modal
  const reservationModal = document.getElementById("reservationModal");
  if (reservationModal) {
    reservationModal.addEventListener("show.bs.modal", function (ev) {
      const btn = ev.relatedTarget;
      if (!btn) return;
      const wishid = btn.getAttribute("data-wishid");
      const reserved = btn.getAttribute("data-reserved");

      reservationModal.querySelector("#modal-wishid").value = wishid || "";
      reservationModal.querySelector("#modal-reservedstat").value =
        reserved || "";

      const submitBtn = reservationModal.querySelector("#reservation-submit");
      const titleEl = reservationModal.querySelector("#reservationModalLabel");
      const infoEl = reservationModal.querySelector("#ReservationInfoText");

      if (reserved === "1") {
        submitBtn.textContent = "Reservierung aufheben";
        titleEl.textContent = "Reservierung aufheben";
        if (infoEl) infoEl.style.display = "none";
      } else {
        submitBtn.textContent = "Reservieren";
        titleEl.textContent = "Wunsch reservieren";
        if (infoEl) infoEl.style.display = "";
      }
    });
  }

  // Delete Modal
  const deleteModal = document.getElementById("deleteModal");
  if (deleteModal) {
    deleteModal.addEventListener("show.bs.modal", function (ev) {
      const btn = ev.relatedTarget;
      if (!btn) return;
      const card = btn.closest(".card");
      const title = card
        ? card.querySelector(".card-title")?.textContent?.trim() || ""
        : "";
      const wishid = btn.getAttribute("data-wishid") || "";

      // robust: suche entweder #DelWhishID ODER #WhishID
      const idInput = deleteModal.querySelector(
        '#DelWhishID, #WhishID, input[name="WhishID"]'
      );
      const titleEl = deleteModal.querySelector(
        "#del-whish-title, #whish-title"
      );

      if (idInput) idInput.value = wishid;
      if (titleEl) titleEl.textContent = title || "Wunsch";
    });
  }

  // Push Prio Modal
  const prioModal = document.getElementById("pushprioModal");
  if (prioModal) {
    prioModal.addEventListener("show.bs.modal", function (ev) {
      const btn = ev.relatedTarget;
      if (!btn) return;
      const card = btn.closest(".card");
      const title = card
        ? card.querySelector(".card-title")?.textContent?.trim() || ""
        : "";
      const wishid = btn.getAttribute("data-wishid") || "";

      const idInput = prioModal.querySelector(
        '#PrioWhishID, #WhishID, input[name="WhishID"]'
      );
      const titleEl = prioModal.querySelector(
        "#prio-whish-title, #whish-title"
      );

      if (idInput) idInput.value = wishid;
      if (titleEl) titleEl.textContent = title || "Wunsch";
    });
  }

  // Add/Edit Item Modal
  const itemModal = document.getElementById("itemModal");
  if (itemModal) {
    itemModal.addEventListener("show.bs.modal", function (ev) {
      const btn = ev.relatedTarget;
      if (!btn) return;
      const mode = btn.getAttribute("data-mode") || "add";

      const titleEl = itemModal.querySelector("#itemModalTitle");
      const actionEl = itemModal.querySelector("#ItemAction");
      const submitEl = itemModal.querySelector("#ItemSubmitBtn");
      const removeWrap = itemModal.querySelector("#RemoveImageWrap");
      const removeChk = itemModal.querySelector("#RemoveImage");

      // Felder
      const fTitle = itemModal.querySelector("#ItemTitle");
      const fDesc = itemModal.querySelector("#ItemDescription");
      const fPrice = itemModal.querySelector("#ItemPrice");
      const fLink = itemModal.querySelector("#ItemLink");
      const fImg = itemModal.querySelector("#ItemImage");
      const fId = itemModal.querySelector("#WhishID");
      const fQty = itemModal.querySelector("#ItemQty"); // <-- NEU

      if (mode === "edit") {
        titleEl.textContent = "Wunsch bearbeiten";
        actionEl.value = "edit";
        submitEl.textContent = "Speichern";
        if (removeWrap) removeWrap.style.display = "";
        if (removeChk) removeChk.checked = false;

        const wishid = btn.getAttribute("data-wishid") || "-1";
        const dTitle = btn.getAttribute("data-title") || "";
        const dDesc = btn.getAttribute("data-description") || "";
        const dPrice = btn.getAttribute("data-price") || "";
        const dLink = btn.getAttribute("data-link") || "";
        const dQty = btn.getAttribute("data-qty") || "1"; // <-- NEU

        if (fId) fId.value = wishid;
        if (fTitle) fTitle.value = dTitle;
        if (fDesc) fDesc.value = dDesc;
        if (fPrice) fPrice.value = dPrice;
        if (fLink) fLink.value = dLink;
        if (fImg) fImg.value = "";
        if (fQty) fQty.value = dQty; // <-- NEU
      } else {
        titleEl.textContent = "Wunsch hinzufügen";
        actionEl.value = "add";
        submitEl.textContent = "Hinzufügen";
        if (fId) fId.value = "-1";
        if (fTitle) fTitle.value = "";
        if (fDesc) fDesc.value = "";
        if (fPrice) fPrice.value = "";
        if (fLink) fLink.value = "";
        if (fImg) fImg.value = "";
        if (fQty) fQty.value = "1"; // <-- NEU
        if (removeWrap) removeWrap.style.display = "none";
        if (removeChk) removeChk.checked = false;
      }
    });
  }
})();
