var q = Object.defineProperty;
var V = (e, n, i) => n in e ? q(e, n, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[n] = i;
var k = (e, n, i) => (V(e, typeof n != "symbol" ? n + "" : n, i), i);
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, safeRedirect as ie, OpenAppViewCommand as oe } from "@ibiz-template/runtime";
import { calcResPath as re } from "@ibiz-template/service";
const se = 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"), i = y(() => (e.curPage - 1) * e.size + 1), t = y(() => e.curPage * e.size);
    return {
      ns: n,
      start: i,
      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 o, u;
  const i = [];
  e.source.columnType === "GROUPGRIDCOLUMN" && e.children.forEach((r) => {
    i.push(L(r, n));
  });
  const t = {
    title: e.title,
    width: e.source.widthUnit === "STAR" ? void 0 : e.width,
    minWidth: e.width,
    align: ((o = e.source.align) == null ? void 0 : o.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 i.length > 0 && Object.assign(t, { children: i }), t;
}
function ae(e) {
  const n = e.model, i = [], t = n.columns.map((o) => L(o, e));
  return n.children.forEach((o) => {
    const u = t.find((r) => r.key === o.codeName);
    u && i.push(u);
  }), e.singleSelect || i.splice(0, 0, { type: "selection", width: 60 }), i;
}
function le(e) {
  const n = b([]);
  return e.nerve.self.evt.on("created", () => {
    n.value = ae(e);
  }), [y(() => {
    var g;
    const t = [...n.value], o = 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 (!o && !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 ue(e) {
  const n = (r) => e.items.find((h) => h.srfkey === r.srfkey);
  function i(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 o(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: i, onDbRowClick: t, onSelectionChange: o, onSortChange: u };
}
function ce(e) {
  function n(o) {
    !o || o === e.curPage || (e.curPage = o, e.load());
  }
  function i(o) {
    !o || o === e.size || (e.size = o, e.curPage === 1 && e.load());
  }
  function t() {
    e.curPage = 1, e.load();
  }
  return { onPageChange: n, onPageSizeChange: i, onPageReset: t };
}
class de extends ee {
  constructor() {
    super(...arguments);
    /**
     * 是否隐藏精简列
     *
     * @type {boolean}
     * @memberof StreamlineGridController
     */
    k(this, "enableHiddenColumn", !1);
  }
  async onInit() {
    var t;
    await super.onInit();
    const i = ((t = this.controlParams) == null ? void 0 : t.DEFAULT_STREAM) === "true";
    this.enableHiddenColumn = i;
  }
  /**
   * 切换精简列显示隐藏
   *
   * @param {boolean} hiddenColumn
   * @memberof StreamlineGridController
   */
  ToggleStreamlineColumn() {
    this.enableHiddenColumn = !this.enableHiddenColumn;
  }
}
const he = 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(), i = O("grid"), t = X(n, () => {
      const a = new de(e.modelData, e.context, e.params);
      return J(
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        n,
        "gridRowActiveMode",
        a.setGridRowActiveMode.bind(a)
      ), a;
    }), o = t.model.source.getPSControlParam(), u = ((F = o == null ? void 0 : o.ctrlParams) == null ? void 0 : F.IS_NOT_FLEX) === "true", r = b([]);
    (x = o == null ? void 0 : o.ctrlParams) != null && x.STREAMLINE_LISTS && (r.value = JSON.parse(o.ctrlParams.STREAMLINE_LISTS)), console.log("精简列", (H = r.value) == null ? void 0 : H.toString());
    const h = b(), [s] = le(t), {
      onRowClick: l,
      onDbRowClick: f,
      onSelectionChange: g,
      onSortChange: m
    } = ue(t), {
      onPageChange: w,
      onPageReset: A,
      onPageSizeChange: R
    } = ce(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: i,
      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((i) => {
      if (i.source.columnType === "GROUPGRIDCOLUMN")
        return;
      const t = i.codeName;
      e[t] = ({
        row: o,
        index: u
      }) => {
        const r = this.c.rows[u - 1];
        if (o.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: o.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": (i) => i.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(se, {
      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 me extends ne {
  async execAction(n, i, t, o, u) {
    const r = {}, { frontProcessType: h } = n;
    if (h === "OPENHTMLPAGE") {
      const s = te.fill(n.htmlPageUrl, i, t == null ? void 0 : t[0]);
      ie(s, { type: "_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,
          i,
          t,
          o
        ), g = ibiz.resourcePathUtil.calcPaths(
          f,
          l.codeName
        ), m = re(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,
        i,
        t,
        o
      ), 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 ge {
  constructor() {
    k(this, "handler");
  }
  async getHandler() {
    return this.handler || (this.handler = new me()), this.handler;
  }
}
class fe {
  constructor() {
    k(this, "component", "StreamlineGrid");
  }
}
const Ae = {
  install(e) {
    e.component("StreamlineGrid", he), ibiz.register.control.register(
      "GRID_STREAMLINE",
      new fe()
    ), ibiz.register.uiAction.register(
      "DEUIACTION_SHOW_ALL_COLUMN",
      new ge()
    );
  }
};
export {
  Ae as default
};
