const paymentData = "upi://pay?pa=demo@upi&pn=Demo%20Store&am=199&cu=INR";

// Generate QR
new QRCode(document.getElementById("qrcode"), {
  text: paymentData,
  width: 200,
  height: 200,
});

const upiBox = document.querySelector(".upi-box");
const upiInput = document.querySelector(".upi-box textarea");
const paymentRadios = document.querySelectorAll('input[name="payment"]');
const payBtn = document.querySelector(".pay-btn");

let selectedMethod = "card";

// Payment toggle
paymentRadios.forEach((radio) => {
  radio.addEventListener("change", () => {
    const label = radio.nextElementSibling.innerText;

    selectedMethod = label;

    if (label.includes("UPI")) {
      upiBox.style.display = "block";
    } else {
      upiBox.style.display = "none";
      upiInput.value = "";
    }
  });
});

// Pay button validation
payBtn.addEventListener("click", () => {
  if (selectedMethod.includes("UPI")) {
    if (upiInput.value.trim() === "") {
      alert("Please enter your UPI ID");
      upiInput.focus();
      return;
    }

    if (!upiInput.value.includes("@")) {
      alert("Please enter a valid UPI ID");
      upiInput.focus();
      return;
    }
  }

  // Success animation
  payBtn.innerText = "Processing...";
  payBtn.disabled = true;

  setTimeout(() => {
    window.location.href = "../html/page6.html";
  }, 1200);
});

// Back button
const backBtn = document.querySelector(".back");

backBtn.addEventListener("click", () => {
  window.location.href = "../html/page4.html";
});
