(function () {
  "use strict";

  const root = document.getElementById("options-settings-root");
  if (!root) return;

  async function apiJson(url, opts) {
    const r = await fetch(url, Object.assign({ credentials: "same-origin" }, opts || {}));
    return r.json();
  }

  function setMsg(id, text, isErr) {
    const el = document.getElementById(id);
    if (!el) return;
    el.textContent = text || "";
    el.classList.toggle("opt-error", !!isErr);
  }

  const swapBtn = document.getElementById("opt-set-swap-btn");
  if (swapBtn) {
    swapBtn.addEventListener("click", async function () {
      const amount = parseFloat(document.getElementById("opt-set-swap-amount").value);
      if (!amount || amount <= 0) {
        setMsg("opt-set-swap-msg", "请输入有效数量", true);
        return;
      }
      const d = await apiJson("/api/options/spot/swap", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          direction: document.getElementById("opt-set-swap-dir").value,
          amount: amount,
        }),
      });
      setMsg("opt-set-swap-msg", d.ok ? "兑换单已提交" : (d.msg || "失败"), !d.ok);
    });
  }

  const intBtn = document.getElementById("opt-set-int-btn");
  if (intBtn) {
    intBtn.addEventListener("click", async function () {
      const amount = parseFloat(document.getElementById("opt-set-int-amount").value);
      if (!amount || amount <= 0) {
        setMsg("opt-set-int-msg", "请输入有效数量", true);
        return;
      }
      const d = await apiJson("/api/options/transfer", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          ccy: document.getElementById("opt-set-int-ccy").value,
          from: document.getElementById("opt-set-int-from").value,
          to: document.getElementById("opt-set-int-to").value,
          amount: amount,
        }),
      });
      setMsg("opt-set-int-msg", d.ok ? "划转成功" : (d.msg || "失败"), !d.ok);
    });
  }

  const crossBtn = document.getElementById("opt-set-cross-btn");
  if (crossBtn) {
    crossBtn.addEventListener("click", async function () {
      const amount = parseFloat(document.getElementById("opt-set-cross-amount").value);
      if (!amount || amount <= 0) {
        setMsg("opt-set-cross-msg", "请输入有效数量", true);
        return;
      }
      const d = await apiJson("/api/options/cross-transfer", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          ccy: document.getElementById("opt-set-cross-ccy").value,
          amount: amount,
          from_account: document.getElementById("opt-set-cross-from").value,
          to_account: document.getElementById("opt-set-cross-to").value,
          direction: document.getElementById("opt-set-cross-dir").value,
        }),
      });
      setMsg("opt-set-cross-msg", d.ok ? "划转成功" : (d.msg || "失败"), !d.ok);
      if (d.ok && typeof refreshAccountSnapshot === "function") refreshAccountSnapshot();
    });
  }
})();
