/**
 * Copies formatted system information text from the .info-container element to clipboard.
 * It extracts all <h3> headings and <td> values inside .info-content, formats them,
 * and shows a temporary success message upon successful copy.
 */
jQuery(document).ready(function ($) {
  function copySystemInfo() {
    const systemInfoContainer = document.querySelector(".info-container");
    if (!systemInfoContainer) return;

    const systemInfoElements = systemInfoContainer.querySelectorAll(
      ".info-content h3, .info-content td",
    );

    let systemInfoText = "";

    systemInfoElements.forEach((element) => {
      const tagName = element.tagName.toLowerCase();

      if (tagName === "h3") {
        systemInfoText += `\n${element.innerText.trim()}\n\n`;
      }

      if (tagName === "td") {
        const labelElement = element.previousElementSibling;
        if (labelElement) {
          systemInfoText += `${labelElement.innerText.trim()}: ${element.innerText.trim()}\n`;
        }
      }
    });

    systemInfoText = systemInfoText.trim();

    // copy (modern + fallback)
    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(systemInfoText).then(showSuccessMsg);
    } else {
      const textarea = document.createElement("textarea");
      textarea.value = systemInfoText;
      textarea.style.position = "fixed";
      textarea.style.opacity = "0";

      document.body.appendChild(textarea);
      textarea.select();
      document.execCommand("copy");
      document.body.removeChild(textarea);

      showSuccessMsg();
    }
  }

  function showSuccessMsg() {
    // purana message hatao
    $(".gshfjf-copy-msg").remove();

    const msgDiv = document.createElement("div");
    msgDiv.className = "gshfjf-copy-msg";
    msgDiv.innerText = "Copied successfully";

    // 🔹 BUTTON KE NICHE ADD KARO
    $("#copy-system-info-free").after(msgDiv);

    // auto remove
    setTimeout(() => {
      $(msgDiv).fadeOut(300, function () {
        $(this).remove();
      });
    }, 3000);
  }

  // button click
  $(document).on("click", "#copy-system-info-free", function () {
    copySystemInfo();
  });

  // 🔹 Ensure first section open on page load
  $("#info-container").show();

  function accordionToggle(button, container) {
    $(button).on("click", function () {
      if ($(container).is(":visible")) {
        // second click → close same section
        $(container).slideUp();
      } else {
        // open clicked, close others
        $(".info-content").slideUp();
        $(container).slideDown();
      }
    });
  }

  accordionToggle("#show-info-button", "#info-container");
  accordionToggle("#show-wordpress-info-button", "#wordpress-info-container");
  accordionToggle("#show-Drop-info-button", "#Drop-info-container");
  accordionToggle("#show-active-info-button", "#active-info-container");
  accordionToggle("#show-netplug-info-button", "#netplug-info-container");
  accordionToggle("#show-acplug-info-button", "#acplug-info-container");
  accordionToggle("#show-server-info-button", "#server-info-container");
  accordionToggle("#show-database-info-button", "#database-info-container");
  accordionToggle("#show-wrcons-info-button", "#wrcons-info-container");
  accordionToggle("#show-ftps-info-button", "#ftps-info-container");
});
/**
 * Adds event listener to the copy button to trigger error log copying
 * once the DOM content is fully loaded.
 */

document.addEventListener("DOMContentLoaded", function () {
  /**
   * Copies the content of the error log textarea to the clipboard
   * and shows a temporary "Copied" confirmation message.
   */

  function copyErrorLog() {
    // Select the textarea containing the error log
    var textarea = document.querySelector(".errorlog");

    // Select the message div (button na niche no div)
    var copyMessage = document.querySelector(".gshfjf-copy-msg");

    if (textarea && copyMessage) {
      textarea.select();

      try {
        // Copy text
        document.execCommand("copy");

        // Show message
        copyMessage.classList.remove("d-none");

        // Hide message after 3 seconds
        setTimeout(function () {
          copyMessage.classList.add("d-none");
        }, 3000);
      } catch (err) {
        console.error("Unable to copy error log:", err);
      }

      textarea.blur();
    }
  }

  var copyButton = document.querySelector(".copy");

  if (copyButton) {
    copyButton.addEventListener("click", function (event) {
      event.preventDefault();
      copyErrorLog();
    });
  }
});
