/**
 * 手机端:交易记录 / 复盘记录紧凑列表(币种 · 方向 · 盈亏),点击展开详情.
 */
(function (global) {
  "use strict";

  var resizeTimer = null;

  function refreshTradeRecords() {
    var UI = global.InstanceUI;
    if (!UI) return;
    var card = document.querySelector(".records-card");
    if (!card) return;
    var tableWrap = card.querySelector(".table-wrap");
    var table = tableWrap && tableWrap.querySelector("table");
    if (!table) return;

    var listEl = card.querySelector(".mobile-record-list");
    var mobile = UI.isMobileCompactRecords();

    if (!mobile) {
      if (listEl) listEl.remove();
      return;
    }

    if (!listEl) {
      listEl = document.createElement("div");
      listEl.className = "mobile-record-list";
      tableWrap.parentNode.insertBefore(listEl, tableWrap);
    }

    var rows = table.querySelectorAll('tr[id^="trade-row-"]');
    listEl.innerHTML = rows.length
      ? Array.prototype.map
          .call(rows, function (tr) {
            return UI.renderMobileTradeRow(tr);
          })
          .join("")
      : '<div class="journal-empty-msg">暂无交易记录</div>';

    listEl.querySelectorAll(".mobile-record-row").forEach(function (btn) {
      btn.addEventListener("click", function () {
        var rowId = btn.getAttribute("data-row-id");
        var tr = rowId && document.getElementById(rowId);
        if (tr) UI.openTradeRecordDetailModal(tr);
      });
    });
  }

  function onResize() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function () {
      refreshTradeRecords();
      if (typeof global.loadJournals === "function" && document.getElementById("journal-list")) {
        global.loadJournals();
      }
    }, 180);
  }

  function init() {
    refreshTradeRecords();
    global.addEventListener("resize", onResize);
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
  } else {
    init();
  }

  global.InstanceRecordsMobile = {
    refresh: refreshTradeRecords,
  };
})(typeof window !== "undefined" ? window : globalThis);
