var A = Object.defineProperty;
var E = (e, i, n) => i in e ? A(e, i, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[i] = n;
var P = (e, i, n) => (E(e, typeof i != "symbol" ? i + "" : i, n), n);
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 o;
  const n = [];
  e.source.columnType === "GROUPGRIDCOLUMN" && e.children.forEach((s) => {
    n.push(z(s, i));
  });
  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: !i.noSort && e.source.enableSort ? "custom" : !1,
    columnType: e.source.columnType,
    tree: e.id === "pfzbname",
    className: e.id === "pfzbname" ? "pfzbname" : ""
  };
  return n.length > 0 && Object.assign(t, { children: n }), t;
}
function _(e) {
  const i = e.model, n = [], t = i.columns.map((o) => z(o, e));
  return i.children.forEach((o) => {
    const s = t.find((r) => r.key === o.codeName);
    s && o.id !== "lb" && n.push(s);
  }), e.singleSelect || n.splice(0, 0, { type: "selection", width: 60 }), n;
}
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 n(r) {
    const a = i(r);
    e.singleSelect && e.onSelectionChange([a]), e.onRowClick(a);
  }
  function t(r) {
    e.onDbRowClick(i(r));
  }
  function o(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: n, onDbRowClick: t, onSelectionChange: o, onSortChange: s };
}
function J(e) {
  function i(o) {
    !o || o === e.curPage || (e.curPage = o, e.load());
  }
  function n(o) {
    !o || o === e.size || (e.size = o, e.curPage === 1 && e.load());
  }
  function t() {
    e.curPage = 1, e.load();
  }
  return { onPageChange: i, onPageSizeChange: n, 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"), n = b(() => (e.curPage - 1) * e.size + 1), t = b(() => e.curPage * e.size);
    return {
      ns: i,
      start: n,
      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(n) {
    return await Promise.allSettled(
      Object.values(this.fieldColumns).map(async (t) => {
        await t.loadCodeList();
      })
    ), this.rows = n.map((t) => {
      const o = new j(t, this);
      return this.gridStateNotify(o, F.LOAD), o;
    }), this.handleGroup(n), n;
  }
  /**
   * 处理分组数据
   * @param {ControlVO} items
   * @return {*}
   * @author: zhujiamin
   * @Date: 2023-10-09 14:03:17
   */
  handleGroup(n) {
    const t = /* @__PURE__ */ new Map();
    n.forEach((o) => {
      const s = o.lb;
      t.has(s) || t.set(s, []), t.has(s) && t.get(s).push(o);
    }), this.groups = [], t.forEach((o, s) => {
      this.groups.push({
        caption: `${s}`,
        key: s,
        children: o
      });
    });
  }
}
function X(e, i, n, t = {}) {
  return M(e, () => {
    const o = new Q(i, n, t);
    return U(e, "gridRowActiveMode", o.setGridRowActiveMode.bind(o)), o;
  });
}
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(), n = S("grid"), t = X(i, e.modelData, e.context, e.params), [o] = 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 && (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: n,
      columns: o,
      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 n = i.codeName;
      e[n] = (t) => {
        const {
          row: o,
          column: s
        } = t;
        if (o.isGroupData && s._index === 0)
          return c("div", {
            class: this.ns.b("sum"),
            attrs: {
              title: o.first.lb
            }
          }, [c("span", [o.first.lb])]);
        const r = this.c.rows.find((a) => a.data.srfkey === o.srfkey);
        if (r)
          return c(this.c.providers[n].component, {
            props: {
              controller: this.c.columns[n],
              row: r
            },
            key: o.srfkey + n
          });
      };
    }), 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
};
