var A = Object.defineProperty;
var E = (e, i, o) => i in e ? A(e, i, { enumerable: !0, configurable: !0, writable: !0, value: o }) : e[i] = o;
var P = (e, i, o) => (E(e, typeof i != "symbol" ? i + "" : i, o), o);
import { PluginStaticResource as O } from "@ibiz-template/runtime";
import { GridModel as I } from "@ibiz-template/model";
import { useNamespace as S, useControlController as M, usePropsWatch as U } from "@ibiz-template/vue-util";
import { ref as w, computed as b, defineComponent as G, h as c, getCurrentInstance as H, watch as T, onUnmounted as L } from "vue";
import { GridController as $, GridRowController as j, GridNotifyState as F } from "@ibiz-template/controller";
const q = new O(import.meta.url);
class B {
  constructor() {
    P(this, "component", "RfpgGridControl");
  }
}
function z(e, i) {
  var n;
  const o = [];
  e.source.columnType === "GROUPGRIDCOLUMN" && e.children.forEach((s) => {
    o.push(z(s, i));
  });
  const t = {
    title: e.title,
    width: e.source.widthUnit === "STAR" ? void 0 : e.width,
    minWidth: e.width,
    align: ((n = e.source.align) == null ? void 0 : n.toLowerCase()) || "center",
    slot: e.codeName,
    key: e.codeName,
    fieldName: e.deFieldName,
    ellipsis: !0,
    tooltip: !1,
    // todo 表格提示用title
    resizable: !0,
    sortable: !i.noSort && e.source.enableSort ? "custom" : !1,
    columnType: e.source.columnType,
    tree: e.id === "pfzbname",
    className: e.id === "pfzbname" ? "pfzbname" : ""
  };
  return o.length > 0 && Object.assign(t, { children: o }), t;
}
function _(e) {
  const i = e.model, o = [], t = i.columns.map((n) => z(n, e));
  return i.children.forEach((n) => {
    const s = t.find((r) => r.key === n.codeName);
    s && n.id !== "lb" && o.push(s);
  }), e.singleSelect || o.splice(0, 0, { type: "selection", width: 60 }), o;
}
function V(e) {
  const i = w([]);
  return e.nerve.self.evt.on("created", () => {
    i.value = _(e);
  }), [b(() => {
    const t = [...i.value];
    if (!t.find((r) => !r.width)) {
      let r = t.length - 1;
      t[r].key === "rowEdit" && (r -= 1), t[r] = {
        ...t[r],
        width: void 0
      };
    }
    const s = t.find((r) => r.columnType === "UAGRIDCOLUMN");
    return s && (s.fixed = "right"), t;
  }), i];
}
function W(e) {
  const i = (r) => e.items.find((a) => a.srfkey === r.srfkey);
  function o(r) {
    const a = i(r);
    e.singleSelect && e.onSelectionChange([a]), e.onRowClick(a);
  }
  function t(r) {
    e.onDbRowClick(i(r));
  }
  function n(r) {
    if (!e.singleSelect) {
      const a = [];
      r.forEach((d) => {
        const f = i(d);
        f && a.push(f);
      }), e.onSelectionChange(a);
    }
  }
  function s(r) {
    const { column: a, order: d } = r, f = d === "asc" || d === "desc" ? d : void 0;
    e.setSort(a.fieldName, f), e.load();
  }
  return { onRowClick: o, onDbRowClick: t, onSelectionChange: n, onSortChange: s };
}
function J(e) {
  function i(n) {
    !n || n === e.curPage || (e.curPage = n, e.load());
  }
  function o(n) {
    !n || n === e.size || (e.size = n, e.curPage === 1 && e.load());
  }
  function t() {
    e.curPage = 1, e.load();
  }
  return { onPageChange: i, onPageSizeChange: o, onPageReset: t };
}
const K = G({
  name: "AppGridPagination",
  props: {
    total: {
      type: Number,
      required: !0
    },
    curPage: {
      type: Number,
      required: !0
    },
    size: {
      type: Number,
      required: !0
    }
  },
  setup(e) {
    const i = S("grid-page"), o = b(() => (e.curPage - 1) * e.size + 1), t = b(() => e.curPage * e.size);
    return {
      ns: i,
      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, " 条数据"])])]);
  }
});
class Q extends $ {
  constructor() {
    super(...arguments);
    /**
     * 分组数据
     * @return {*}
     * @author: zhujiamin
     * @Date: 2023-10-09 14:00:38
     */
    P(this, "groups", []);
  }
  /**
   * 部件加载后处理
   *
   * @author lxm
   * @date 2022-08-19 14:08:50
   */
  async afterLoad(o) {
    return await Promise.allSettled(
      Object.values(this.fieldColumns).map(async (t) => {
        await t.loadCodeList();
      })
    ), this.rows = o.map((t) => {
      const n = new j(t, this);
      return this.gridStateNotify(n, F.LOAD), n;
    }), this.handleGroup(o), o;
  }
  /**
   * 处理分组数据
   * @param {ControlVO} items
   * @return {*}
   * @author: zhujiamin
   * @Date: 2023-10-09 14:03:17
   */
  handleGroup(o) {
    const t = /* @__PURE__ */ new Map();
    o.forEach((n) => {
      const s = n.lb;
      t.has(s) || t.set(s, []), t.has(s) && t.get(s).push(n);
    }), this.groups = [], t.forEach((n, s) => {
      this.groups.push({
        caption: `${s}`,
        key: s,
        children: n
      });
    });
  }
}
function X(e, i, o, t = {}) {
  return M(e, () => {
    const n = new Q(i, o, t);
    return U(e, "gridRowActiveMode", n.setGridRowActiveMode.bind(n)), n;
  });
}
const Y = G({
  props: {
    modelData: I,
    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) {
    const {
      proxy: i
    } = H(), o = S("grid"), t = X(i, e.modelData, e.context, e.params), [n] = V(t), {
      onRowClick: s,
      onDbRowClick: r,
      onSelectionChange: a,
      onSortChange: d
    } = W(t), {
      onPageChange: f,
      onPageReset: k,
      onPageSizeChange: D
    } = J(t), N = (u, l) => {
      const h = i.$refs.grid;
      h && l !== void 0 && (h.toggleSelect && h.toggleSelect(l), h.highlightCurrentRow && h.highlightCurrentRow(l)), s(u);
    }, p = w(0), g = w(null), R = w(null);
    let m = null, y = 0;
    const v = () => {
      var u;
      if (g.value && ((u = e.modal) == null ? void 0 : u.mode) !== "EMBED")
        if (t.model.source.enablePagingBar && R.value) {
          const l = R.value.$el, h = l.offsetHeight + parseFloat(window.getComputedStyle(l).paddingTop);
          p.value = g.value.offsetHeight - h;
        } else
          p.value = g.value.offsetHeight;
      t.model.source.height > 0 && (p.value = t.model.source.height);
    };
    T(g, (u, l) => {
      u && u !== l && (v(), window.ResizeObserver && g.value && (m = new ResizeObserver((h) => {
        const C = h[0].contentRect.height;
        C !== y && (v(), y = C);
      }), m.observe(g.value)));
    }), L(() => {
      m && m.disconnect();
    });
    const x = b(() => {
      const u = [];
      return t.groups.forEach((l) => {
        if (!l.children.length)
          return;
        const C = [...l.children].shift();
        u.push({
          srfkey: l.caption,
          isGroupData: !0,
          first: C,
          children: l.children,
          _showChildren: !0
        });
      }), u;
    });
    return {
      c: t,
      ns: o,
      columns: n,
      tableData: x,
      onDbRowClick: r,
      onUIRowClick: N,
      onSelectionChange: a,
      onSortChange: d,
      onPageChange: f,
      onPageSizeChange: D,
      onPageReset: k,
      tableHeight: p,
      gridRef: g,
      girdPaginationRef: R
    };
  },
  render() {
    if (!this.c.complete)
      return;
    const e = {};
    return this.c.model.columns.forEach((i) => {
      if (i.source.columnType === "GROUPGRIDCOLUMN")
        return;
      const o = i.codeName;
      e[o] = (t) => {
        const {
          row: n,
          column: s
        } = t;
        if (n.isGroupData && s._index === 0)
          return c("div", {
            class: this.ns.b("sum"),
            attrs: {
              title: n.first.lb
            }
          }, [c("span", [n.first.lb])]);
        const r = this.c.rows.find((a) => a.data.srfkey === n.srfkey);
        if (r)
          return c(this.c.providers[o].component, {
            props: {
              controller: this.c.columns[o],
              row: r
            },
            key: n.srfkey + o
          });
      };
    }), c("control-layout", {
      attrs: {
        modelData: this.c.model
      }
    }, [c("div", {
      ref: "gridRef",
      class: [this.ns.b(), this.ns.b("rfpg-grid"), this.ns.is("show-header", !this.c.model.source.hideHeader), this.ns.is("enable-page", this.c.model.source.enablePagingBar)]
    }, [this.c.groups.length > 0 && c("i-table", {
      ref: "grid",
      attrs: {
        height: this.tableHeight,
        "show-header": !this.c.model.source.hideHeader,
        "highlight-row": !0,
        data: this.tableData,
        columns: this.columns,
        "row-key": "srfkey"
      },
      class: this.ns.b("content"),
      on: {
        "on-row-click": this.onUIRowClick,
        "on-row-dblclick": this.onDbRowClick,
        "on-selection-change": this.onSelectionChange,
        "on-sort-change": this.onSortChange
      },
      scopedSlots: e
    }), this.c.model.source.enablePagingBar && c(K, {
      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
      }
    })])]);
  }
}), re = {
  mateUrl: import.meta.url,
  install(e) {
    q.loadStyle(["/style.css"]), e.component("RfpgGridControl", Y), ibiz.register.control.register(
      "GRID_RENDER_RfpgGird",
      new B()
    );
  }
};
export {
  re as default
};
