var q = Object.defineProperty;
var V = (e, n, o) => n in e ? q(e, n, { enumerable: !0, configurable: !0, writable: !0, value: o }) : e[n] = o;
var k = (e, n, o) => (V(e, typeof n != "symbol" ? n + "" : n, o), o);
import { GridModel as W, AppEntityModel as B } from "@ibiz-template/model";
import { useNamespace as O, useControlController as X, usePropsWatch as J } from "@ibiz-template/vue-util";
import { defineComponent as U, computed as y, h as C, ref as b, getCurrentInstance as Z, watch as K, onUnmounted as Q } from "vue";
import { cloneDeep as Y } from "lodash-es";
import { GridController as ee } from "@ibiz-template/controller";
import { StringUtil as te } from "@ibiz-template/core";
import { FrontUIActionHandler as ne, OpenAppViewCommand as oe } from "@ibiz-template/runtime";
import { calcResPath as ie } from "@ibiz-template/service";
const re = U({
  name: "AppGridPagination",
  props: {
    total: {
      type: Number,
      required: !0
    },
    curPage: {
      type: Number,
      required: !0
    },
    size: {
      type: Number,
      required: !0
    }
  },
  setup(e) {
    const n = O("grid-page"), o = y(() => (e.curPage - 1) * e.size + 1), t = y(() => e.curPage * e.size);
    return {
      ns: n,
      start: o,
      end: t
    };
  },
  methods: {
    onPageChange(e) {
      this.$emit("change", e);
    },
    onPageSizeChange(e) {
      this.$emit("page-size-change", e);
    },
    pageReset() {
      this.$emit("page-reset");
    }
  },
  render() {
    return C("div", {
      class: this.ns.b()
    }, [C("i-page", {
      attrs: {
        transfer: !0,
        total: this.total,
        "show-sizer": !0,
        "show-elevator": !0,
        current: this.curPage,
        "page-size": this.size,
        "page-size-opts": [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
        "show-total": !0
      },
      on: {
        "on-change": this.onPageChange,
        "on-page-size-change": this.onPageSizeChange
      }
    }, [C("span", {
      class: this.ns.b("btn")
    }, [C("i-button", {
      attrs: {
        icon: "md-refresh",
        title: "刷新"
      },
      on: {
        click: this.pageReset
      }
    })]), C("span", ["显示 ", this.start, " - ", this.end, " 条，共 ", this.total, " 条数据"])])]);
  }
});
function L(e, n) {
  var i, u;
  const o = [];
  e.source.columnType === "GROUPGRIDCOLUMN" && e.children.forEach((r) => {
    o.push(L(r, n));
  });
  const t = {
    title: e.title,
    width: e.source.widthUnit === "STAR" ? void 0 : e.width,
    minWidth: e.width,
    align: ((i = e.source.align) == null ? void 0 : i.toLowerCase()) || "center",
    slot: e.codeName,
    key: e.codeName,
    fieldName: e.deFieldName,
    ellipsis: !0,
    tooltip: !1,
    // todo 表格提示用title
    resizable: !0,
    sortable: !n.noSort && e.source.enableSort ? "custom" : !1,
    columnType: e.source.columnType,
    userParams: e.source.userParams,
    className: (u = e.source.getCellPSSysCss()) == null ? void 0 : u.cssName
  };
  return o.length > 0 && Object.assign(t, { children: o }), t;
}
function se(e) {
  const n = e.model, o = [], t = n.columns.map((i) => L(i, e));
  return n.children.forEach((i) => {
    const u = t.find((r) => r.key === i.codeName);
    u && o.push(u);
  }), e.singleSelect || o.splice(0, 0, { type: "selection", width: 60 }), o;
}
function ae(e) {
  const n = b([]);
  return e.nerve.self.evt.on("created", () => {
    n.value = se(e);
  }), [y(() => {
    var g;
    const t = [...n.value], i = t.find((m) => !m.width), u = (g = e.model.source.getPSControlParam()) == null ? void 0 : g.ctrlParams, r = (u == null ? void 0 : u.IS_NOT_FLEX) === "true";
    if (!i && !r) {
      let m = t.length - 1;
      t[m].key === "rowEdit" && (m -= 1), t[m] = {
        ...t[m],
        width: void 0
      };
    }
    const h = t.find((m) => m.columnType === "UAGRIDCOLUMN");
    if (h) {
      const m = h.userParams;
      m && m.fixed === "true" && (h.fixed = "right");
    }
    const s = t.length, { frozenFirstColumn: l, frozenLastColumn: f } = e.model;
    return t.forEach((m, w) => {
      l && w < l ? m.fixed = "left" : f && w >= s - f && (m.fixed = "right");
    }), t;
  }), n];
}
function le(e) {
  const n = (r) => e.items.find((h) => h.srfkey === r.srfkey);
  function o(r) {
    const h = n(r);
    if (e.singleSelect) {
      const s = e.selectedData.find(
        (l) => l.srfkey === r.srfkey
      );
      e.onSelectionChange(s ? [] : [h]);
    }
    e.onRowClick(h);
  }
  function t(r) {
    e.onDbRowClick(n(r));
  }
  function i(r) {
    if (!e.singleSelect) {
      const h = [];
      r.forEach((s) => {
        const l = n(s);
        l && h.push(l);
      }), e.onSelectionChange(h);
    }
  }
  function u(r) {
    const { column: h, order: s } = r, l = s === "asc" || s === "desc" ? s : void 0;
    e.setSort(h.fieldName, l), e.load();
  }
  return { onRowClick: o, onDbRowClick: t, onSelectionChange: i, onSortChange: u };
}
function ue(e) {
  function n(i) {
    !i || i === e.curPage || (e.curPage = i, e.load());
  }
  function o(i) {
    !i || i === e.size || (e.size = i, e.curPage === 1 && e.load());
  }
  function t() {
    e.curPage = 1, e.load();
  }
  return { onPageChange: n, onPageSizeChange: o, onPageReset: t };
}
class ce extends ee {
  constructor() {
    super(...arguments);
    /**
     * 是否隐藏精简列
     *
     * @type {boolean}
     * @memberof StreamlineGridController
     */
    k(this, "enableHiddenColumn", !1);
  }
  async onInit() {
    var t;
    await super.onInit();
    const o = ((t = this.controlParams) == null ? void 0 : t.DEFAULT_STREAM) === "true";
    this.enableHiddenColumn = o;
  }
  /**
   * 切换精简列显示隐藏
   *
   * @param {boolean} hiddenColumn
   * @memberof StreamlineGridController
   */
  ToggleStreamlineColumn() {
    this.enableHiddenColumn = !this.enableHiddenColumn;
  }
}
const de = U({
  props: {
    modelData: {
      type: W,
      required: !0
    },
    context: {
      type: Object,
      required: !0
    },
    params: {
      type: Object,
      default: () => ({})
    },
    /**
     * 表格行数据默认激活模式
     * - 0 不激活
     * - 1 单击激活
     * - 2 双击激活(默认值)
     *
     * @type {(number | 0 | 1 | 2)}
     */
    gridRowActiveMode: {
      type: Number,
      default: 2
    },
    modal: {
      type: Object
    }
  },
  setup(e) {
    var F, x, H;
    const {
      proxy: n
    } = Z(), o = O("grid"), t = X(n, () => {
      const a = new ce(e.modelData, e.context, e.params);
      return J(
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        n,
        "gridRowActiveMode",
        a.setGridRowActiveMode.bind(a)
      ), a;
    }), i = t.model.source.getPSControlParam(), u = ((F = i == null ? void 0 : i.ctrlParams) == null ? void 0 : F.IS_NOT_FLEX) === "true", r = b([]);
    (x = i == null ? void 0 : i.ctrlParams) != null && x.STREAMLINE_LISTS && (r.value = JSON.parse(i.ctrlParams.STREAMLINE_LISTS)), console.log("精简列", (H = r.value) == null ? void 0 : H.toString());
    const h = b(), [s] = ae(t), {
      onRowClick: l,
      onDbRowClick: f,
      onSelectionChange: g,
      onSortChange: m
    } = le(t), {
      onPageChange: w,
      onPageReset: A,
      onPageSizeChange: R
    } = ue(t), v = y(() => {
      var c, d;
      const a = (d = (c = e.modelData) == null ? void 0 : c.source.getPSControlParam()) == null ? void 0 : d.ctrlParams;
      return (a == null ? void 0 : a.HIGHLIGHT) !== "false";
    });
    let P = null;
    const _ = (a, c) => {
      P != null && window.clearTimeout(P), P = window.setTimeout(() => {
        const d = n.$refs.grid;
        d && (t.selectedData.find((T) => T.srfkey === a.srfkey) && d.clearCurrentRow(), d.toggleSelect && d.toggleSelect(c)), l(a);
      }, 300);
    }, M = (a) => {
      P != null && window.clearTimeout(P);
      const c = n.$refs.grid;
      c && (t.selectedData.find((p) => p.srfkey === a.srfkey) || c.clearCurrentRow()), f(a);
    }, I = b(0), S = b(null), D = b(null);
    let N = null, E = 0;
    const z = () => {
      var a;
      if (S.value && ((a = e.modal) == null ? void 0 : a.mode) !== "EMBED")
        if (t.model.source.enablePagingBar && D.value) {
          const c = D.value.$el, d = c.offsetHeight + parseFloat(window.getComputedStyle(c).paddingTop);
          I.value = S.value.offsetHeight - d;
        } else
          I.value = S.value.offsetHeight;
      t.model.source.height > 0 && (I.value = t.model.source.height);
    };
    K(S, (a, c) => {
      a && a !== c && (z(), window.ResizeObserver && S.value && (N = new ResizeObserver((d) => {
        const p = d[0].contentRect.height;
        p !== E && (z(), E = p);
      }), N.observe(S.value)));
    }), Q(() => {
      N && N.disconnect();
    });
    const $ = y(() => {
      const a = t.items.map((c) => (c._checked = t.selectedData.some((d) => d.srfkey === c.srfkey), c));
      return a.length && a.unshift({
        hiddenRow: !0,
        _checked: t.items.every((c) => c._checked)
      }), a;
    }), G = (a) => {
      const c = [];
      return a.forEach((d) => {
        if (!r.value.includes(d.key) && !r.value.includes(d.fieldName)) {
          const p = Y(d);
          if (d.children) {
            const T = G(d.children);
            p.children = T;
          }
          c.push(p);
        }
      }), c;
    }, j = y(() => t.enableHiddenColumn ? G(s.value) : s.value);
    return {
      c: t,
      ns: o,
      columns: s,
      tableData: $,
      onDbRowClick: f,
      onUIRowClick: _,
      onUIDbRowClick: M,
      onSelectionChange: g,
      onSortChange: m,
      onPageChange: w,
      onPageSizeChange: R,
      onPageReset: A,
      tableHeight: I,
      gridRef: S,
      girdPaginationRef: D,
      highlight: v,
      isNotFlex: u,
      grid: h,
      streamlineLists: r,
      streamColumn: j
    };
  },
  render() {
    var n;
    if (!this.c.complete)
      return;
    const e = {};
    return this.c.model.columns.forEach((o) => {
      if (o.source.columnType === "GROUPGRIDCOLUMN")
        return;
      const t = o.codeName;
      e[t] = ({
        row: i,
        index: u
      }) => {
        const r = this.c.rows[u - 1];
        if (i.hiddenRow)
          return C("span", {
            style: "word-break: break-word;white-space: pre-wrap;"
          }, ["隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列隐藏行的列"]);
        if (r)
          return C(this.c.providers[t].component, {
            props: {
              controller: this.c.columns[t],
              row: r
            },
            key: i.srfkey + t
          });
      };
    }), C("control-layout", {
      attrs: {
        modelData: this.c.model
      }
    }, [C("div", {
      ref: "gridRef",
      class: [this.ns.b(), this.ns.is("show-header", !this.c.model.source.hideHeader), this.ns.is("enable-page", this.c.model.source.enablePagingBar)],
      style: this.isNotFlex ? (n = this.grid) == null ? void 0 : n.tableStyle : void 0
    }, [C("i-table", {
      ref: "grid",
      attrs: {
        height: this.tableHeight,
        "show-header": !this.c.model.source.hideHeader,
        "highlight-row": this.c.singleSelect && this.highlight,
        data: this.tableData,
        columns: this.streamColumn,
        "row-class-name": (o) => o.hiddenRow ? "hidden-row" : "",
        "span-method": this.c.spanMethod.bind(this.c)
      },
      class: this.ns.b("content"),
      on: {
        "on-row-click": this.onUIRowClick,
        "on-row-dblclick": this.onUIDbRowClick,
        "on-selection-change": this.onSelectionChange,
        "on-sort-change": this.onSortChange
      },
      scopedSlots: e
    }), this.c.model.source.enablePagingBar && C(re, {
      ref: "girdPaginationRef",
      attrs: {
        total: this.c.total,
        curPage: this.c.curPage,
        size: this.c.size
      },
      on: {
        change: this.onPageChange,
        "page-size-change": this.onPageSizeChange,
        "page-reset": this.onPageReset
      }
    })])]);
  }
});
class he extends ne {
  async execAction(n, o, t, i, u) {
    const r = {}, { frontProcessType: h } = n;
    if (h === "OPENHTMLPAGE") {
      const s = te.fill(n.htmlPageUrl, o, t == null ? void 0 : t[0]);
      window.open(s, "_blank");
    }
    if (h === "DATAIMP") {
      const s = n.getPSAppDataEntity();
      if (s) {
        s.isFill === !1 && await s.fill(!0);
        const l = new B(s);
        await l.init();
        const { resultContext: f } = await this.handleParams(
          n,
          o,
          t,
          i
        ), g = ibiz.resourcePathUtil.calcPaths(
          f,
          l.codeName
        ), m = ie(f, g);
        let w = `${m}/${l.deNamePlural}/importtemplate`, A = `${m}/${l.deNamePlural}/importdata2`;
        const R = n.getPSAppDEDataImport();
        R && (w += `?srfimporttag=${R.codeName}`, A += `?srfimporttag=${R.codeName}`);
        const v = ibiz.overlay.createModal(
          "DataImport",
          {
            templateUrl: w,
            importUrl: A,
            dismiss: (P) => {
              v.dismiss(P);
            }
          },
          {
            width: "auto",
            placement: "center"
          }
        );
        return v.present(), await v.onWillDismiss(), {
          refresh: !0
        };
      }
    }
    if (["TOP", "WIZARD"].includes(h)) {
      const s = n.getFrontPSAppView();
      if (!s)
        throw new Error("未配置打开视图");
      await s.fill(!0);
      const { resultContext: l, resultParams: f } = await this.handleParams(
        n,
        o,
        t,
        i
      ), g = await ibiz.commands.execute(
        oe.TAG,
        s,
        l,
        f,
        u
      );
      (g == null ? void 0 : g.ok) === !1 && (r.closeView = !1, r.refresh = !1, r.cancel = !0), g != null && g.ok && g.data && (r.data = g.data);
    } else {
      const s = u == null ? void 0 : u.neuron.call.getGridController(), l = u == null ? void 0 : u.neuron.call.getThis();
      if (s && l) {
        const f = s.call.getGridController();
        f && (f.ToggleStreamlineColumn(), n.codeName.toLowerCase() === "jjzs" ? (l.toolbarState.show_all.visible = !0, l.toolbarState.show_streamline.visible = !1) : (l.toolbarState.show_all.visible = !1, l.toolbarState.show_streamline.visible = !0));
      }
    }
    return r;
  }
}
class me {
  constructor() {
    k(this, "handler");
  }
  async getHandler() {
    return this.handler || (this.handler = new he()), this.handler;
  }
}
class ge {
  constructor() {
    k(this, "component", "StreamlineGrid");
  }
}
const ve = {
  install(e) {
    e.component("StreamlineGrid", de), ibiz.register.control.register(
      "GRID_STREAMLINE",
      new ge()
    ), ibiz.register.uiAction.register(
      "DEUIACTION_SHOW_ALL_COLUMN",
      new me()
    );
  }
};
export {
  ve as default
};
