"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
require("../../../../../node_modules/element-plus/es/index.js");
;/* empty css                                                             */
;/* empty css                                                                               */
;/* empty css                                                                */
;/* empty css                                                                     */
;/* empty css                                                                 */
;/* empty css                                                                        */
;/* empty css                                                                  */
;/* empty css                                                                     */
;/* empty css                                                                  */
;/* empty css                                                                             */
require("../../../../../node_modules/element-plus/theme-chalk/el-date-picker.css.js");
;/* empty css                                                                    */
;/* empty css                                                                          */
;/* empty css                                                                           */
;/* empty css                                                                */
require("../../../../../node_modules/element-plus/theme-chalk/el-tab-pane.css.js");
const Vue = require("vue");
const index$1 = require("../../../../../node_modules/@element-plus/icons-vue/dist/index.js");
const vxeTable = require("vxe-table");
const index$e = require("../forms/text/index.js");
const index$a = require("../forms/select/index.js");
const index$b = require("../forms/multipleSelection/index.js");
const index$d = require("../forms/date/index.js");
const index$c = require("../forms/number/index.js");
const common = require("../../utils/common.js");
const parseFilterConfig = require("../../utils/parseFilterConfig.js");
const getInstance = require("../../utils/getInstance.js");
;/* empty css                   */
const _pluginVue_exportHelper = require("../../../../../_virtual/_plugin-vue_export-helper.js");
const index = require("../../../../../node_modules/element-plus/es/components/icon/index.js");
const index$2 = require("../../../../../node_modules/element-plus/es/components/tabs/index.js");
const index$3 = require("../../../../../node_modules/element-plus/es/components/input/index.js");
const index$4 = require("../../../../../node_modules/element-plus/es/components/button/index.js");
const index$5 = require("../../../../../node_modules/element-plus/es/components/collapse-transition/index.js");
const index$6 = require("../../../../../node_modules/element-plus/es/components/form/index.js");
const index$7 = require("../../../../../node_modules/element-plus/es/components/checkbox/index.js");
const index$8 = require("../../../../../node_modules/element-plus/es/components/date-picker/index.js");
const index$9 = require("../../../../../node_modules/element-plus/es/components/input-number/index.js");
const _hoisted_1 = { class: "dynamic-mode-config" };
const _hoisted_2 = { class: "dynamic-row" };
const _hoisted_3 = {
  key: 0,
  class: "back-icon"
};
const _hoisted_4 = {
  key: 1,
  class: "template-name"
};
const _hoisted_5 = {
  key: 2,
  class: "tab-display-section"
};
const _hoisted_6 = { class: "search-display-section" };
const _hoisted_7 = {
  key: 0,
  class: "search-input-group"
};
const _hoisted_8 = { class: "expand-filter-section" };
const _hoisted_9 = { class: "filter-form-section" };
const _hoisted_10 = { class: "form-items-draggable" };
const _hoisted_11 = {
  key: 1,
  style: { "display": "flex", "align-items": "center", "gap": "8px", "width": "100%" }
};
const _sfc_main = {
  __name: "DynamicQuery",
  props: {
    localConfig: {
      type: Object,
      default: () => ({})
    },
    fieldsData: {
      type: Array,
      default: () => []
    },
    isSubTable: {
      type: Boolean,
      default: false
    },
    readOnly: {
      type: Boolean,
      default: false
    },
    hiddenFormData: {
      type: Object,
      default: {}
    },
    isCache: {
      type: Boolean,
      default: false
    }
  },
  emits: ["click", "search"],
  setup(__props, { emit: __emit }) {
    var _a;
    const props = __props;
    const emit = __emit;
    const queryConfig = Vue.computed(() => {
      var _a2;
      return ((_a2 = props.localConfig) == null ? void 0 : _a2.queryConfig) || {};
    });
    const dynamicConfig = Vue.computed(() => {
      var _a2;
      return ((_a2 = queryConfig.value) == null ? void 0 : _a2.dynamicConfig) || {};
    });
    const activeTab = Vue.ref("");
    const tabConfigs = Vue.computed(() => {
      var _a2, _b, _c, _d;
      activeTab.value = ((_b = (_a2 = dynamicConfig.value) == null ? void 0 : _a2.tabConfigs) == null ? void 0 : _b.length) > 0 ? (_c = dynamicConfig.value) == null ? void 0 : _c.tabActive : "";
      return ((_d = dynamicConfig.value) == null ? void 0 : _d.tabConfigs) || [];
    });
    const fieldsData = Vue.computed(() => {
      var _a2, _b;
      return [
        ...((_a2 = props.localConfig) == null ? void 0 : _a2.fieldsData) || [],
        ...((_b = props.localConfig) == null ? void 0 : _b.hiddenFields) || []
      ];
    });
    const rightButtons = Vue.computed(() => {
      var _a2;
      return (((_a2 = props.localConfig) == null ? void 0 : _a2.customBtns.right) || []).filter((item) => {
        if (!item.showCondition) return true;
        return parseFilterConfig.checkFilterMatch(
          item.showCondition,
          props.hiddenFormData,
          fieldsData.value
        );
      });
    });
    const getBtnIcon = (btnType) => {
      const iconMap = {
        refresh: index$1.Refresh,
        export: index$1.Download,
        personalize: index$1.Setting,
        add: index$1.Plus,
        delete: index$1.Delete,
        edit: index$1.Edit,
        search: index$1.Search
      };
      return iconMap[btnType] || null;
    };
    const getBtnType = (btn) => {
      if (btn.btnType === "custom") {
        return "primary";
      }
      return btn.btnStyle || "default";
    };
    const handleClick = (btn, event) => {
      emit("click", {
        btn,
        event,
        type: "custom"
      });
    };
    const getFieldConfig = (fieldId) => {
      return props.fieldsData.find((field) => field.id === fieldId) || {};
    };
    const filterFields = Vue.ref([]);
    filterFields.value = props.localConfig.queryConfig.filterFields.map(
      (filterField) => {
        const fieldConfig = getFieldConfig(filterField.fieldId);
        return {
          ...filterField,
          ...fieldConfig
        };
      }
    );
    const filterValues = Vue.ref({});
    const searchInputValue = Vue.ref("");
    const ctx = getInstance.getCurrentVueInstance();
    const route = (_a = ctx.$route) == null ? void 0 : _a.path;
    if (route && props.isCache) {
      const routeParams = JSON.parse(sessionStorage.getItem(route));
      const filters = JSON.parse((routeParams == null ? void 0 : routeParams.filters) || "{}");
      searchInputValue.value = (filters == null ? void 0 : filters.dynamicFilter) || "";
      filterValues.value = common.reverseHandleSearchData(filters, filterFields.value);
    }
    const showAdvancedFilter = Vue.ref(false);
    const toggleAdvancedFilter = () => {
      const willShow = !showAdvancedFilter.value;
      showAdvancedFilter.value = willShow;
    };
    const handleSearch = () => {
      const filters = common.handleSearchData(filterValues.value, filterFields.value);
      const filterData = {
        ...currentTab.value,
        dynamicFilter: searchInputValue.value,
        ...filters
      };
      console.log("🚀 ~ 开始执行查询", filterData);
      emit("search", {
        filters: JSON.stringify(filterData)
      });
    };
    const currentTab = Vue.ref({});
    Vue.watch(
      activeTab,
      (newVal, oldVal) => {
        console.log("🚀 ~ Tab切换:", tabConfigs.value, "->", newVal);
        const tab = tabConfigs.value.find((item) => item.id == newVal);
        if (tab) {
          currentTab.value[tab.fieldName] = tab.fixedValue;
          handleSearch();
        }
      },
      {
        immediate: true
      }
    );
    const onSearchInputKeyup = (event) => {
      if (event.key === "Enter") {
        console.log("🚀 ~ 搜索输入框回车");
        handleSearch();
      }
    };
    const debounce = (fn, delay = 500) => {
      let timer = null;
      return (...args) => {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
          fn(...args);
        }, delay);
      };
    };
    const debouncedSearch = debounce(handleSearch, 500);
    Vue.watch(
      filterValues,
      (newVal) => {
        console.log("🚀 ~ 筛选值变化:", newVal);
        debouncedSearch();
      },
      { deep: true }
    );
    const getFilterComponent = (fieldType) => {
      const componentMap = {
        text: index$e.Text,
        select: index$a.Select,
        date: index$d.Date,
        number: index$c.Number,
        multipleSelection: index$b.MultipleSelection,
        switch: index$a.Select
      };
      return componentMap[fieldType] || index$e.Text;
    };
    const getComponentConfig = (field) => {
      return { ...field };
    };
    const getPlaceholder = (field, type = "default") => {
      const placeholderMap = {
        text: "请输入",
        select: "请选择",
        date: type === "start" ? "开始日期" : type === "end" ? "结束日期" : "请选择日期",
        number: "请输入",
        number_min: "最小值",
        number_max: "最大值",
        radio: "请选择",
        checkbox: "请选择",
        switch: "请选择"
      };
      if (field.fieldType === "date") {
        return placeholderMap.date;
      }
      if (field.fieldType === "number") {
        return type === "min" ? placeholderMap.number_min : type === "max" ? placeholderMap.number_max : placeholderMap.number;
      }
      return `${placeholderMap[field.fieldType] || "请输入"}${field.label || ""}`;
    };
    const dynamicPlaceholder = Vue.computed(() => {
      const searchFields = dynamicConfig.value.searchFields || [];
      if (searchFields.length == 0) {
        return "请输入筛选条件";
      }
      const labelArr = searchFields.map((item) => item.label);
      return `搜索${labelArr.join("/")}`;
    });
    return (_ctx, _cache) => {
      const _component_el_icon = index.ElIcon;
      const _component_el_tab_pane = index$2.ElTabPane;
      const _component_el_tabs = index$2.ElTabs;
      const _component_el_button = index$4.ElButton;
      const _component_el_input = index$3.ElInput;
      const _component_el_checkbox_button = index$7.ElCheckboxButton;
      const _component_el_checkbox_group = index$7.ElCheckboxGroup;
      const _component_el_date_picker = index$8.ElDatePicker;
      const _component_el_input_number = index$9.ElInputNumber;
      const _component_el_form_item = index$6.ElFormItem;
      const _component_el_form = index$6.ElForm;
      const _component_el_collapse_transition = index$5.ElCollapseTransition;
      const _directive_fec_auth = Vue.resolveDirective("fec-auth");
      return Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
        Vue.createElementVNode("div", _hoisted_2, [
          __props.localConfig.showReturn ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_3, [
            Vue.createVNode(_component_el_icon, null, {
              default: Vue.withCtx(() => [
                Vue.createVNode(Vue.unref(index$1.ArrowLeftBold))
              ]),
              _: 1
            })
          ])) : Vue.createCommentVNode("", true),
          __props.localConfig.showTitle ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_4, [
            _cache[2] || (_cache[2] = Vue.createElementVNode("div", { class: "template-name-line" }, null, -1)),
            Vue.createTextVNode(" " + Vue.toDisplayString(__props.localConfig.templateName), 1)
          ])) : Vue.createCommentVNode("", true),
          queryConfig.value.enabled && tabConfigs.value.length > 0 ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_5, [
            Vue.createVNode(_component_el_tabs, {
              modelValue: activeTab.value,
              "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => activeTab.value = $event),
              class: "dynamic-tabs"
            }, {
              default: Vue.withCtx(() => [
                (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(tabConfigs.value, (tab) => {
                  return Vue.openBlock(), Vue.createBlock(_component_el_tab_pane, {
                    key: tab.id,
                    label: tab.label,
                    name: tab.id
                  }, null, 8, ["label", "name"]);
                }), 128))
              ]),
              _: 1
            }, 8, ["modelValue"])
          ])) : Vue.createCommentVNode("", true),
          Vue.createElementVNode("div", _hoisted_6, [
            queryConfig.value.enabled ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_7, [
              Vue.createVNode(_component_el_input, {
                modelValue: searchInputValue.value,
                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => searchInputValue.value = $event),
                placeholder: dynamicPlaceholder.value,
                title: dynamicPlaceholder.value,
                class: "search-input",
                clearable: "",
                size: "default",
                onKeyup: onSearchInputKeyup
              }, Vue.createSlots({
                suffix: Vue.withCtx(() => [
                  Vue.createVNode(_component_el_icon, {
                    class: "el-input__icon search",
                    onClick: handleSearch
                  }, {
                    default: Vue.withCtx(() => [
                      Vue.createVNode(Vue.unref(index$1.Search))
                    ]),
                    _: 1
                  })
                ]),
                _: 2
              }, [
                dynamicConfig.value.advancedSearch ? {
                  name: "append",
                  fn: Vue.withCtx(() => [
                    Vue.createVNode(_component_el_button, {
                      type: "primary",
                      onClick: toggleAdvancedFilter
                    }, {
                      default: Vue.withCtx(() => [
                        Vue.createTextVNode(Vue.toDisplayString(showAdvancedFilter.value ? "隐藏高级搜索" : "高级搜索"), 1)
                      ]),
                      _: 1
                    })
                  ]),
                  key: "0"
                } : void 0
              ]), 1032, ["modelValue", "placeholder", "title"])
            ])) : Vue.createCommentVNode("", true),
            !__props.readOnly && !__props.isSubTable && rightButtons.value.length > 0 ? (Vue.openBlock(), Vue.createBlock(Vue.unref(vxeTable.VxeToolbar), {
              key: 1,
              ref: "toolbarRef"
            }, {
              tools: Vue.withCtx(() => [
                !__props.isSubTable && rightButtons.value.length ? (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, { key: 0 }, Vue.renderList(rightButtons.value, (btn) => {
                  return Vue.withDirectives((Vue.openBlock(), Vue.createBlock(_component_el_button, {
                    key: btn.id,
                    type: getBtnType(btn),
                    icon: getBtnIcon(btn.btnType),
                    onClick: ($event) => handleClick(btn, $event)
                  }, {
                    default: Vue.withCtx(() => [
                      Vue.createTextVNode(Vue.toDisplayString(btn.label), 1)
                    ]),
                    _: 2
                  }, 1032, ["type", "icon", "onClick"])), [
                    [_directive_fec_auth, btn.permission]
                  ]);
                }), 128)) : Vue.createCommentVNode("", true)
              ]),
              _: 1
            }, 512)) : Vue.createCommentVNode("", true)
          ])
        ]),
        Vue.createVNode(_component_el_collapse_transition, null, {
          default: Vue.withCtx(() => [
            Vue.withDirectives(Vue.createElementVNode("div", _hoisted_8, [
              Vue.createElementVNode("div", _hoisted_9, [
                Vue.createVNode(_component_el_form, {
                  ref: "formRef",
                  model: filterValues.value,
                  class: "filter-form",
                  "label-width": "100px",
                  inline: "",
                  size: "default"
                }, {
                  default: Vue.withCtx(() => [
                    Vue.createElementVNode("div", _hoisted_10, [
                      (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(filterFields.value, (field) => {
                        return Vue.openBlock(), Vue.createElementBlock("div", {
                          key: field.id,
                          style: Vue.normalizeStyle(field.isExclusiveRow ? "width:100%" : "")
                        }, [
                          Vue.createVNode(_component_el_form_item, {
                            label: field.label
                          }, {
                            default: Vue.withCtx(() => [
                              field.isButtonStyle ? (Vue.openBlock(), Vue.createBlock(_component_el_checkbox_group, {
                                key: 0,
                                modelValue: filterValues.value[field.fieldId],
                                "onUpdate:modelValue": ($event) => filterValues.value[field.fieldId] = $event
                              }, {
                                default: Vue.withCtx(() => [
                                  (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(field.optionConfig.options, (option) => {
                                    return Vue.openBlock(), Vue.createBlock(_component_el_checkbox_button, {
                                      key: option.value,
                                      value: option.value
                                    }, {
                                      default: Vue.withCtx(() => [
                                        Vue.createTextVNode(Vue.toDisplayString(option.label), 1)
                                      ]),
                                      _: 2
                                    }, 1032, ["value"]);
                                  }), 128))
                                ]),
                                _: 2
                              }, 1032, ["modelValue", "onUpdate:modelValue"])) : (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 1 }, [
                                field.fieldType == "date" ? (Vue.openBlock(), Vue.createBlock(_component_el_date_picker, {
                                  key: 0,
                                  modelValue: filterValues.value[field.fieldId],
                                  "onUpdate:modelValue": ($event) => filterValues.value[field.fieldId] = $event,
                                  type: "daterange",
                                  "start-placeholder": getPlaceholder(field, "start"),
                                  "end-placeholder": getPlaceholder(field, "end"),
                                  format: "YYYY-MM-DD",
                                  "value-format": "YYYYMMDD",
                                  clearable: "",
                                  style: { "width": "100%" }
                                }, null, 8, ["modelValue", "onUpdate:modelValue", "start-placeholder", "end-placeholder"])) : field.fieldType == "number" ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_11, [
                                  Vue.createVNode(_component_el_input_number, {
                                    modelValue: filterValues.value[field.fieldId],
                                    "onUpdate:modelValue": ($event) => filterValues.value[field.fieldId] = $event,
                                    placeholder: getPlaceholder(field, "min"),
                                    controls: false,
                                    "controls-position": "right",
                                    style: { "flex": "1" },
                                    clearable: ""
                                  }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"]),
                                  _cache[3] || (_cache[3] = Vue.createElementVNode("span", null, "-", -1)),
                                  Vue.createVNode(_component_el_input_number, {
                                    modelValue: filterValues.value[`${field.fieldId}_max`],
                                    "onUpdate:modelValue": ($event) => filterValues.value[`${field.fieldId}_max`] = $event,
                                    placeholder: getPlaceholder(field, "max"),
                                    controls: false,
                                    "controls-position": "right",
                                    style: { "flex": "1" },
                                    clearable: ""
                                  }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"])
                                ])) : (Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(getFilterComponent(field.fieldType)), Vue.mergeProps({
                                  key: 2,
                                  modelValue: filterValues.value[field.fieldId],
                                  "onUpdate:modelValue": ($event) => filterValues.value[field.fieldId] = $event,
                                  config: { ...field, eventHandlers: {} },
                                  ref_for: true
                                }, getComponentConfig(field), {
                                  placeholder: getPlaceholder(field)
                                }), null, 16, ["modelValue", "onUpdate:modelValue", "config", "placeholder"]))
                              ], 64))
                            ]),
                            _: 2
                          }, 1032, ["label"])
                        ], 4);
                      }), 128))
                    ])
                  ]),
                  _: 1
                }, 8, ["model"])
              ])
            ], 512), [
              [
                Vue.vShow,
                showAdvancedFilter.value && dynamicConfig.value.advancedSearch && filterFields.value.length > 0
              ]
            ])
          ]),
          _: 1
        })
      ]);
    };
  }
};
const DynamicQuery = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-84665bac"]]);
exports.default = DynamicQuery;
