<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="css/plugins.dark.bundle.css" rel="stylesheet" type="text/css">
    <link href="css/style.dark.bundle.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
  <title>App</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="content">
    <div id="home" class="tab active">
      <h1>Welcome to Coldz Hub</h1>
      <p>Version 1.0</p>
      <button onclick="copyLink('server1')">Copy Discord Server Coldz hub Link</button>
    </div>

    <div id="coming-soon" class="tab">
      <h1>Credits</h1>
      <p>Created by Scarlet Script</p>
      <button onclick="copyLink('server1')">Copy Discord Server Coldz hub Link</button>
      <button onclick="copyLink('server2')">Copy Discord Server scarlet script Link</button>
    </div>

    <div id="signer" class="tab">
      <h1>signer</h1>
      <p>please wait when you sign</p>
  <div class="container">
    <form id="signForm" class="form">
      <div class="form-group">
        <label for="ipa">IPA File</label>
        <input type="file" id="ipa" accept=".ipa,application/octet-stream" required>
      </div>
      <div class="form-group">
        <label for="prov">mobileprovision Profile</label>
        <input type="file" id="prov" accept=".mobileprovision" required>
      </div>
      <div class="form-group">
        <label for="p12">P12 Certificate</label>
        <input type="file" id="p12" accept=".p12" required>
      </div>
      <div class="form-group">
        <label for="password">Password</label>
        <input type="text" id="password" required>
      </div>
      <button type="button" id="uploadButton">Upload and Sign</button>
    </form>

    <div id="progress-container">
      <div id="progress-bar"></div>
    </div>
   </div>
  </div>
  <!-- Bottom Navigation -->
  <nav>
    <a href="#" class="active" data-tab="home">
      <i class="fas fa-home"></i>
      Home
    </a>
    <a href="#" data-tab="coming-soon">
      <i class="fas fa-hourglass-half"></i>
      Credits
    </a>
    <a href="#" data-tab="signer">
      <i class="fas fa-file-signature"></i>
      Signer
    </a>

  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="js/plugins.bundle.js" type="text/javascript"></script>
  <script>
      document.addEventListener("DOMContentLoaded", function() {

  // Tab switching logic
  const links = document.querySelectorAll("nav a");
  const tabs = document.querySelectorAll(".tab");

  links.forEach(link => {
    link.addEventListener("click", (e) => {
      e.preventDefault();

      // Remove active class from all tabs and links
      tabs.forEach(tab => tab.classList.remove("active"));
      links.forEach(l => l.classList.remove("active"));

      // Add active class to the clicked link and its tab
      const target = link.getAttribute("data-tab");
      const targetTab = document.getElementById(target);

      if (targetTab) {
        targetTab.classList.add("active");
        link.classList.add("active");
      } else {
        console.error(`Tab with ID "${target}" not found.`);
      }
    });
  });

  // Copy Discord link to clipboard
  window.copyLink = function(serverCode) {
    let discordLink = '';
    if (serverCode === 'server1') {
      discordLink = 'https://discord.gg/um374RM5'; // First Discord Server
    } else if (serverCode === 'server2') {
      discordLink = 'https://discord.gg/pTmc8uEqJr'; // Second Discord Server
    }

    if (discordLink) {
      navigator.clipboard.writeText(discordLink).then(() => {
        alert("Discord link copied to clipboard!");
      }).catch(() => {
        alert("Failed to copy Discord link. Please try again.");
      });
    } else {
      alert("Invalid server code.");
    }
  };

  // Progress Handler
  function progressHandler(event) {
    const percent = event.loaded / event.total;
    const progressBar = document.getElementById("progress-bar");

    if (progressBar) {
      progressBar.style.opacity = 1;
      progressBar.style.width = percent * 100 + '%';
    }

    if (percent === 1) {
      const waitText = document.getElementById("waittext");
      if (waitText) {
        waitText.innerHTML = "<span class='fs-6 fw-semibold form-label mt-3'>Please wait while we download your IPA...</span>";
        waitText.className = "text-center fs-6 fw-semibold form-label mt-3";
      }
    }
  }

  // Store checkbox logic
  const storeCheckbox = document.getElementById("store");
  if (storeCheckbox) {
    storeCheckbox.addEventListener("click", function() {
      if (storeCheckbox.checked) {
        Swal.fire({
          text: "This will store your p12 and mobileprov on server, a password will still be required to sign.",
          title: "Are you sure?",
          icon: "info",
          buttonsStyling: false,
          confirmButtonText: "I'm sure!",
          showCancelButton: true,
          cancelButtonText: "Cancel",
          customClass: {
            confirmButton: "btn btn-danger",
            cancelButton: "btn btn-primary"
          },
        }).then(e => {
          if (e.isDismissed) {
            storeCheckbox.checked = false;
          }
        });
      }
    });
  }

  // Upload IPA
  const uploadButton = document.getElementById("uploadButton");
  if (uploadButton) {
    uploadButton.addEventListener("click", function() {
      (async () => {
        const formData = new FormData();
        const ipa = document.getElementById("ipa").files[0];
        const prov = document.getElementById("prov").files[0];
        const p12 = document.getElementById("p12").files[0];
        const password = document.getElementById("password").value;

        if (!ipa || !prov || !p12 || !password) {
          Swal.fire({
            text: "Please fill in all required fields.",
            icon: "error",
            confirmButtonText: "Ok, got it!",
            customClass: {
              confirmButton: "btn btn-primary",
            },
          });
          return;
        }

        formData.append("ipa", ipa);
        formData.append("mp", prov); // 'prov' corresponds to 'mp' in your API
        formData.append("p12", p12);
        formData.append("password", password);
        formData.append("expiryDays", "5"); // Fixed expiryDays value as in the screenshot

        const xhr = new XMLHttpRequest();
        xhr.open("POST", "https://install.applecertdev.xyz/sign", true);
        xhr.upload.addEventListener("progress", progressHandler, false);

        uploadButton.remove();
        xhr.onload = async function() {
          if (this.status === 200) {
            const data = JSON.parse(this.responseText);
            if (data.install_url) {
              Swal.fire({
                text: "IPA signed and ready to install!",
                icon: "success",
                showCancelButton: true,
                cancelButtonText: "Copy URL",
                customClass: {
                  confirmButton: "btn btn-primary",
                  cancelButton: "btn btn-danger"
                },
              }).then(function(e) {
                if (e.isConfirmed) {
                  window.location.href = data.install_url; // Direct install link
                } else if (e.isDismissed) {
                  navigator.clipboard.writeText(data.install_url); // Copy URL to clipboard
                }
              });
            } else {
              Swal.fire({
                text: data.message,
                icon: "error",
                confirmButtonText: "Ok, got it!",
                customClass: {
                  confirmButton: "btn btn-primary",
                },
              });
            }
          } else {
            Swal.fire({
              text: "An error occurred while uploading. Please try again.",
              icon: "error",
              confirmButtonText: "Ok, got it!",
              customClass: {
                confirmButton: "btn btn-primary",
              },
            });
          }
        };
        xhr.send(formData);
      })();
    });
  }
});
    </script>
</body>
</html>