"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
;/* empty css                                               */
;/* empty css                                                */
;/* empty css                                               */
;/* empty css                                               */
;/* empty css                                                */
;/* empty css                                                 */
const Vue = require("vue");
const datasource = require("../../../utils/datasource.js");
const QuickFilter = require("../../feedback/quickFilter/QuickFilter.vue.js");
const index = require("../../../../node_modules/vant/es/search/index.js");
const _sfc_main = {
  __name: "Search",
  props: {
    placeholder: {
      type: String,
      default: ""
    },
    filterMode: {
      type: String,
      default: "input"
    },
    dataSources: {
      type: Array,
      default: () => []
    },
    filterConfig: {
      type: Array,
      default: () => []
    }
  },
  emits: ["filterChange"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const currentInstance = Vue.getCurrentInstance();
    const ctx = currentInstance.proxy;
    const searchData = Vue.ref("");
    function onClickFilter() {
    }
    function onClickButton() {
      onSearch(searchData.value);
    }
    function onSearch(val) {
    }
    function onQuickFilterChange(result) {
      console.log("筛选数据:", result);
      emit("filterChange", result);
    }
    const filterData = Vue.ref([]);
    const fieldDataSources = /* @__PURE__ */ new Set();
    const dictionaryKeys = /* @__PURE__ */ new Set();
    const dataSourceOptions = Vue.ref({});
    const dictionaryOptions = Vue.ref({});
    const loadFieldDataSources = async () => {
      var _a, _b;
      const optionsDatas = [];
      const fields = props.filterConfig;
      for (const component of fields) {
        const { optionSource, dataSourceValue, dictionaryValue } = component.optionConfig;
        if (optionSource === "dataSource" && dataSourceValue) {
          fieldDataSources.add(dataSourceValue);
        } else if (optionSource === "dictionary" && dictionaryValue) {
          dictionaryKeys.add(dictionaryValue);
        }
      }
      for (const dataSourceId of fieldDataSources) {
        if (dataSourceOptions.value[dataSourceId]) continue;
        try {
          const dsManager = datasource.createDataSource({
            http: ctx.$http,
            dataSources: props.dataSources,
            currentDataSourceId: dataSourceId
          });
          const data = await dsManager.fetch();
          let options = [];
          if (Array.isArray(data)) {
            options = data;
          } else if (typeof data === "object") {
            options = Array.isArray(data.data) ? data.data : ((_a = data.data) == null ? void 0 : _a.records) || ((_b = data.data) == null ? void 0 : _b.list) || [];
          }
          dataSourceOptions.value[dataSourceId] = options;
          dsManager.destroy();
        } catch (error) {
          console.error(`Failed to load data source ${dataSourceId}:`, error);
        }
      }
      if (dictionaryKeys.size > 0) {
        try {
          const keyName = Array.from(dictionaryKeys).join(",");
          const response = await ctx.$http.postForm(
            "/base-server/parm/sysParmDic/getDicListByKey",
            {
              keyName
            }
          );
          const dictionaryData = response;
          if (typeof dictionaryData === "object") {
            Object.keys(dictionaryData).forEach((dictKey) => {
              if (dictionaryKeys.has(dictKey)) {
                const dictItems = dictionaryData[dictKey];
                if (Array.isArray(dictItems)) {
                  dictionaryOptions.value[dictKey] = dictItems;
                }
              }
            });
          }
        } catch (error) {
          console.error("Failed to load dictionary data:", error);
        }
      }
      fields.forEach((component) => {
        if (!component.optionConfig) return;
        let options = [];
        const {
          optionSource,
          dataSourceValue,
          dictionaryValue,
          displayField,
          valueField,
          idField,
          pidField
        } = component.optionConfig;
        if (optionSource === "dataSource" && dataSourceOptions.value[dataSourceValue]) {
          const rawOptions = dataSourceOptions.value[dataSourceValue];
          const labelField = displayField || "label";
          const keyField = valueField || "value";
          options = rawOptions.map((item) => ({
            label: item[labelField],
            value: item[keyField],
            ...item
          }));
          optionsDatas.push({ ...component, options });
        } else if (optionSource === "dictionary" && dictionaryOptions.value[dictionaryValue]) {
          const rawOptions = dictionaryOptions.value[dictionaryValue];
          const labelField = "optName";
          const keyField = "optCode";
          options = rawOptions.map((item) => ({
            label: item[labelField],
            value: item[keyField]
          }));
          optionsDatas.push({ ...component, options });
        } else if (optionSource === "custom") {
          optionsDatas.push({
            ...component,
            options: component.optionConfig.options || []
          });
        }
      });
      filterData.value = [...optionsDatas];
    };
    Vue.watch(
      () => props.filterConfig.map((item) => ({
        label: item.label
      })),
      () => {
        loadFieldDataSources();
      },
      { deep: true, immediate: true }
    );
    return (_ctx, _cache) => {
      const _component_van_search = index.Search;
      return Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, null, [
        Vue.createVNode(_component_van_search, {
          modelValue: Vue.unref(searchData),
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => Vue.isRef(searchData) ? searchData.value = $event : null),
          shape: "round",
          placeholder: __props.placeholder,
          "show-action": __props.filterMode == "input",
          clearable: "",
          onClickInput: _cache[1] || (_cache[1] = Vue.withModifiers(($event) => __props.filterMode == "router" && onClickFilter, ["stop"]))
        }, {
          action: Vue.withCtx(() => [
            Vue.createElementVNode("div", { onClick: onClickButton }, "搜索")
          ]),
          _: 1
        }, 8, ["modelValue", "placeholder", "show-action"]),
        Vue.createVNode(QuickFilter.default, {
          "filter-data": Vue.unref(filterData),
          onChange: onQuickFilterChange
        }, null, 8, ["filter-data"])
      ], 64);
    };
  }
};
exports.default = _sfc_main;
