/**
 * 实盘下单监控:开仓按钮灰显 + 旁注(强制清仓/冷静期/日冻结等).
 */
(function (global) {
  function apply(data) {
    const d = data || {};
    const btn =
      document.getElementById("om-submit-btn") ||
      document.querySelector("#add-order-form button.om-submit");
    const noteEl = document.getElementById("om-open-block-note");
    if (!btn && !noteEl) return;

    const canTrade = d.can_trade !== false;
    let note = (d.open_block_note || "").trim();
    const fc = d.force_close || {};
    const rs = d.risk_status || {};
    if (!note && fc.enabled && fc.executing) {
      const grace = fc.grace_minutes != null ? fc.grace_minutes : 5;
      note =
        "强制清仓窗口内(北京时间 " +
        (fc.hour_label || "--:--") +
        " 起 " +
        grace +
        " 分钟),暂不可开仓";
    }
    if (!note && rs.can_trade === false && rs.reason) {
      note = String(rs.reason);
    }
    if (!note && !canTrade) {
      note = "当前不可开仓";
    }

    if (btn) {
      btn.disabled = !canTrade;
      btn.classList.toggle("is-blocked", !canTrade);
      btn.setAttribute("aria-disabled", canTrade ? "false" : "true");
      if (!canTrade) {
        btn.title = note || "当前不可开仓";
      } else {
        btn.removeAttribute("title");
      }
    }
    if (noteEl) {
      if (!canTrade && note) {
        noteEl.hidden = false;
        noteEl.textContent = note;
      } else {
        noteEl.hidden = true;
        noteEl.textContent = "";
      }
    }
  }

  global.OpenSubmitGate = { apply: apply };
})(window);
