import "../../../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                                */
/* empty css                                                                     */
import { inject, ref, computed, resolveComponent, createElementBlock, openBlock, Fragment, createCommentVNode, createVNode, createBlock, unref, createSlots, withCtx, renderList, createTextVNode, toDisplayString } from "vue";
import { SubTitle } from "../subTitle/index.mjs";
import { H2 } from "../h2/index.mjs";
import { parseRouteParams } from "../../../utils/parseRouteParams.mjs";
import { checkFilterMatch } from "../../../utils/parseFilterConfig.mjs";
import { ElButton } from "../../../../../../node_modules/element-plus/es/components/button/index.mjs";
const _hoisted_1 = {
  key: 0,
  style: { "height": "60px", "position": "relative" }
};
const _sfc_main = /* @__PURE__ */ Object.assign({
  inheritAttrs: false
}, {
  __name: "SubTable",
  props: {
    config: {
      type: Object,
      required: true
    },
    localConfig: {
      type: Object,
      required: true
    },
    formMode: {
      type: String,
      default: ""
    }
  },
  setup(__props, { expose: __expose }) {
    const props = __props;
    const getFormData = inject("getFormData");
    const formData = ref(getFormData());
    inject("rowHeight");
    inject("gridLayout");
    const setFormItemHeight = inject("setFormItemHeight");
    const handleEvent = inject("handleEvent");
    const heightMode = computed(() => {
      return props.config.subTableHeightType || "auto";
    });
    const btnRollbackEvent = computed(() => {
      const btnRollback = props.config.btnRollback || [];
      const data = {};
      for (const item of btnRollback) {
        const handleFunc = () => {
          handleEvent.value(
            formData.value,
            {},
            { eventFlow: item.callbackEventConfig }
          );
        };
        data[item.id] = handleFunc;
      }
      return data;
    });
    const subTableData = computed(() => {
      if (props.config.dataSourceFrom == "main") {
        return formData.value[props.config.fieldName];
      }
      return [];
    });
    const fieldList = computed(() => {
      return [
        ...props.localConfig.fieldsData.map((item) => item.component),
        ...props.localConfig.hiddenFields
      ];
    });
    const initHiddenData = computed(() => {
      if (props.config.dataSourceFrom == "self") {
        const params = parseRouteParams(
          props.config.subTableParams,
          formData.value,
          fieldList.value
        );
        return params;
      }
      return {};
    });
    const subTableOption = ref({});
    const subTableFieldsData = computed(() => {
      var _a, _b;
      return [
        ...((_a = subTableOption.value) == null ? void 0 : _a.fieldsData) || [],
        ...((_b = subTableOption.value) == null ? void 0 : _b.hiddenFields) || []
      ];
    });
    const leftButtons = computed(() => {
      var _a, _b;
      return (((_b = (_a = subTableOption.value) == null ? void 0 : _a.customBtns) == null ? void 0 : _b.left) || []).filter((item) => {
        if (!item.showCondition) return true;
        return checkFilterMatch(
          item.showCondition,
          initHiddenData.value,
          subTableFieldsData.value
        );
      });
    });
    const rightButtons = computed(() => {
      var _a, _b;
      return (((_b = (_a = subTableOption.value) == null ? void 0 : _a.customBtns) == null ? void 0 : _b.right) || []).filter((item) => {
        if (!item.showCondition) return true;
        return checkFilterMatch(
          item.showCondition,
          initHiddenData.value,
          subTableFieldsData.value
        );
      });
    });
    function subTableLoaded(subOption, tableHeight) {
      subTableOption.value = subOption;
      const foundItem = props.localConfig.fieldsData.find(
        (item) => item.id == props.config.id
      );
      const titleHeight = props.config.titleMode != "none" ? 60 : 0;
      if (foundItem) {
        foundItem.fixedH = true;
        foundItem.h = tableHeight + titleHeight;
        setFormItemHeight(props.config.id, true, foundItem.h);
      }
    }
    const tableRef = ref();
    const handleClick = (btn, event) => {
      var _a;
      (_a = tableRef.value) == null ? void 0 : _a.handleCustomBtnClick({
        btn,
        event,
        type: "custom"
      });
    };
    __expose({
      subTableRef: tableRef
    });
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_fec_table = resolveComponent("fec-table");
      return openBlock(), createElementBlock(Fragment, null, [
        __props.config.titleMode != "none" ? (openBlock(), createElementBlock("div", _hoisted_1, [
          __props.config.titleMode == "label" ? (openBlock(), createBlock(unref(H2), {
            key: 0,
            config: __props.config
          }, createSlots({
            default: withCtx(() => [
              __props.formMode != "query" ? (openBlock(true), createElementBlock(Fragment, { key: 0 }, renderList(unref(rightButtons), (btn) => {
                return openBlock(), createElementBlock(Fragment, null, [
                  btn.btnType == "custom" ? (openBlock(), createBlock(_component_el_button, {
                    key: btn.id,
                    link: "",
                    type: "primary",
                    onClick: ($event) => handleClick(btn, $event)
                  }, {
                    default: withCtx(() => [
                      createTextVNode(toDisplayString(btn.label), 1)
                    ]),
                    _: 2
                  }, 1032, ["onClick"])) : createCommentVNode("", true)
                ], 64);
              }), 256)) : createCommentVNode("", true)
            ]),
            _: 2
          }, [
            unref(leftButtons) ? {
              name: "left",
              fn: withCtx(() => [
                __props.formMode != "query" ? (openBlock(true), createElementBlock(Fragment, { key: 0 }, renderList(unref(leftButtons), (btn) => {
                  return openBlock(), createElementBlock(Fragment, null, [
                    btn.btnType == "custom" ? (openBlock(), createBlock(_component_el_button, {
                      key: btn.id,
                      link: "",
                      type: "primary",
                      onClick: ($event) => handleClick(btn, $event)
                    }, {
                      default: withCtx(() => [
                        createTextVNode(toDisplayString(btn.label), 1)
                      ]),
                      _: 2
                    }, 1032, ["onClick"])) : createCommentVNode("", true)
                  ], 64);
                }), 256)) : createCommentVNode("", true)
              ]),
              key: "0"
            } : void 0
          ]), 1032, ["config"])) : __props.config.titleMode == "subLabel" ? (openBlock(), createBlock(unref(SubTitle), {
            key: 1,
            config: __props.config
          }, null, 8, ["config"])) : createCommentVNode("", true)
        ])) : createCommentVNode("", true),
        createVNode(_component_fec_table, {
          ref_key: "tableRef",
          ref: tableRef,
          templateKey: __props.config.subTableKey,
          isSubTable: "",
          onHeightLoaded: subTableLoaded,
          mode: __props.config.subTableMode,
          titleMode: __props.config.titleMode,
          hasPagination: __props.config.isSubTablePagination,
          heightMode: unref(heightMode),
          height: __props.config.subTableHeight,
          maxHeight: __props.config.subTableMaxHeight,
          tableData: unref(subTableData),
          initHiddenData: unref(initHiddenData),
          canLoadBySearchData: __props.config.reloadOnParamsChange,
          readonly: __props.config.isSubTableReadOnly || __props.formMode == "query",
          btnRollbackEvent: unref(btnRollbackEvent),
          subTableConfig: __props.config
        }, null, 8, ["templateKey", "mode", "titleMode", "hasPagination", "heightMode", "height", "maxHeight", "tableData", "initHiddenData", "canLoadBySearchData", "readonly", "btnRollbackEvent", "subTableConfig"])
      ], 64);
    };
  }
});
export {
  _sfc_main as default
};
