function requestWithdraw() {
  const amount = Number(document.getElementById("amount").value);
  const method = document.getElementById("method").value;

  const user = JSON.parse(localStorage.getItem("flowpesa_onboarding"));

  if (!user || user.account_status !== "active") {
    alert("Session expired.");
    return;
  }

  if (user.kyc_level < 2) {
    alert("Complete ID verification to withdraw funds.");
    return;
  }

  if (!amount || amount < 5000) {
    alert("Minimum withdrawal is UGX 5,000.");
    return;
  }

  if (!method) {
    alert("Select a withdrawal destination.");
    return;
  }

  // ---- MOCK BALANCE ----
  let walletBalance = 50000; // later from API

  if (amount > walletBalance) {
    alert("Insufficient balance.");
    return;
  }

  // ---- CREATE PENDING TRANSACTION ----
  const tx = {
    type: "withdraw",
    direction: "debit",
    amount,
    reference: "Withdraw to " + method.toUpperCase(),
    status: "pending",
    date: "Just now"
  };

  let txs = JSON.parse(localStorage.getItem("flowpesa_transactions")) || [];
  txs.unshift(tx);
  localStorage.setItem("flowpesa_transactions", JSON.stringify(txs));

  alert("Withdrawal request submitted. Processing...");

  window.location.href = "transactions.html";
}
