"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
;/* empty css                                               */
;/* empty css                                                        */
;/* empty css                                                */
;/* empty css                                               */
;/* empty css                                               */
;/* empty css                                                  */
;/* empty css                                                */
;/* empty css                                                        */
;/* empty css                                                  */
;/* empty css                                                 */
const Vue = require("vue");
;/* empty css                  */
const _pluginVue_exportHelper = require("../../../../../../_virtual/_plugin-vue_export-helper.js");
const index = require("../../../../node_modules/vant/es/dropdown-menu/index.js");
const index$1 = require("../../../../node_modules/vant/es/dropdown-item/index.js");
const index$2 = require("../../../../node_modules/vant/es/button/index.js");
const _hoisted_1 = { class: "dropdown-title" };
const _hoisted_2 = {
  key: 0,
  class: "title-badge"
};
const _hoisted_3 = { class: "filter-container" };
const _hoisted_4 = { class: "tag-list" };
const _hoisted_5 = ["onClick"];
const _hoisted_6 = { class: "filter-footer" };
const _hoisted_7 = { class: "filter-container" };
const _hoisted_8 = { class: "group-title" };
const _hoisted_9 = { class: "tag-list" };
const _hoisted_10 = ["onClick"];
const _hoisted_11 = { class: "filter-footer" };
const _sfc_main = {
  __name: "QuickFilter",
  props: {
    filterData: {
      type: Array,
      default: () => []
    }
  },
  emits: ["change"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const activeSelections = Vue.ref({});
    const confirmedSelections = Vue.ref({});
    const itemRefs = Vue.ref({});
    const isOptionSelected = (fieldName, value) => {
      var _a;
      return ((_a = activeSelections.value[fieldName]) == null ? void 0 : _a.includes(value)) || false;
    };
    const getConfirmedCount = (fieldName) => {
      var _a;
      return ((_a = confirmedSelections.value[fieldName]) == null ? void 0 : _a.length) || 0;
    };
    const toggleOption = (fieldName, value) => {
      if (!activeSelections.value[fieldName]) {
        activeSelections.value[fieldName] = [];
      }
      const filter = props.filterData.find((f) => f.fieldName === fieldName);
      const isMultiple = (filter == null ? void 0 : filter.dataType) === "multiple";
      if (isMultiple) {
        const idx = activeSelections.value[fieldName].indexOf(value);
        if (idx > -1) {
          activeSelections.value[fieldName].splice(idx, 1);
        } else {
          activeSelections.value[fieldName].push(value);
        }
      } else {
        activeSelections.value[fieldName] = [value];
      }
    };
    const getSelectedLabel = (filter) => {
      var _a;
      const selected = (_a = confirmedSelections.value[filter.fieldName]) == null ? void 0 : _a[0];
      if (selected) {
        const option = filter.options.find((o) => o.value === selected);
        return (option == null ? void 0 : option.label) || "";
      }
      return "";
    };
    const resetFilter = (fieldName) => {
      activeSelections.value[fieldName] = [];
    };
    const confirmFilter = (fieldName) => {
      var _a;
      confirmedSelections.value[fieldName] = [
        ...activeSelections.value[fieldName] || []
      ];
      (_a = itemRefs.value[fieldName]) == null ? void 0 : _a.toggle();
      emitChange();
    };
    const allFilterRef = Vue.ref(null);
    const resetAllFilters = () => {
      props.filterData.forEach((filter) => {
        activeSelections.value[filter.fieldName] = [];
      });
    };
    const confirmAllFilters = () => {
      var _a;
      props.filterData.forEach((filter) => {
        confirmedSelections.value[filter.fieldName] = [
          ...activeSelections.value[filter.fieldName] || []
        ];
      });
      (_a = allFilterRef.value) == null ? void 0 : _a.toggle();
      emitChange();
    };
    const dropdownItemOpen = (fieldName) => {
      activeSelections.value[fieldName] = [
        ...confirmedSelections.value[fieldName] || []
      ];
    };
    const emitChange = () => {
      const result = [];
      props.filterData.forEach((filter) => {
        const values = confirmedSelections.value[filter.fieldName] || [];
        if (values.length > 0) {
          result.push({
            fieldName: filter.fieldName,
            label: filter.label,
            dataType: filter.dataType,
            values
          });
        }
      });
      emit("change", result);
    };
    return (_ctx, _cache) => {
      const _component_van_button = index$2.Button;
      const _component_van_dropdown_item = index$1.DropdownItem;
      const _component_van_dropdown_menu = index.DropdownMenu;
      return __props.filterData.length > 0 ? (Vue.openBlock(), Vue.createBlock(_component_van_dropdown_menu, {
        key: 0,
        "auto-locate": "",
        class: "fec-quick-filter"
      }, {
        default: Vue.withCtx(() => [
          (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(__props.filterData, (filter, index2) => {
            return Vue.openBlock(), Vue.createBlock(_component_van_dropdown_item, {
              key: `${filter.label}-${index2}`,
              ref_for: true,
              ref: (el) => Vue.unref(itemRefs)[filter.fieldName] = el,
              onOpen: ($event) => dropdownItemOpen(filter.fieldName)
            }, {
              title: Vue.withCtx(() => [
                Vue.createElementVNode("span", _hoisted_1, [
                  Vue.createTextVNode(Vue.toDisplayString(filter.dataType === "select" ? getSelectedLabel(filter) || filter.label : filter.label) + " ", 1),
                  filter.dataType === "multiple" && getConfirmedCount(filter.fieldName) > 0 ? (Vue.openBlock(), Vue.createElementBlock("span", _hoisted_2, Vue.toDisplayString(getConfirmedCount(filter.fieldName)), 1)) : Vue.createCommentVNode("", true)
                ])
              ]),
              default: Vue.withCtx(() => [
                Vue.createElementVNode("div", _hoisted_3, [
                  Vue.createElementVNode("div", _hoisted_4, [
                    (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(filter.options, (item) => {
                      return Vue.openBlock(), Vue.createElementBlock("div", {
                        key: item.id || item.value,
                        class: Vue.normalizeClass(["tag-item", { active: isOptionSelected(filter.fieldName, item.value) }]),
                        onClick: ($event) => toggleOption(filter.fieldName, item.value)
                      }, Vue.toDisplayString(item.label), 11, _hoisted_5);
                    }), 128))
                  ]),
                  Vue.createElementVNode("div", _hoisted_6, [
                    Vue.createVNode(_component_van_button, {
                      size: "small",
                      onClick: ($event) => resetFilter(filter.fieldName)
                    }, {
                      default: Vue.withCtx(() => _cache[0] || (_cache[0] = [
                        Vue.createTextVNode("重置")
                      ])),
                      _: 2
                    }, 1032, ["onClick"]),
                    Vue.createVNode(_component_van_button, {
                      type: "primary",
                      size: "small",
                      onClick: ($event) => confirmFilter(filter.fieldName)
                    }, {
                      default: Vue.withCtx(() => _cache[1] || (_cache[1] = [
                        Vue.createTextVNode("确定")
                      ])),
                      _: 2
                    }, 1032, ["onClick"])
                  ])
                ])
              ]),
              _: 2
            }, 1032, ["onOpen"]);
          }), 128)),
          __props.filterData.length > 0 ? (Vue.openBlock(), Vue.createBlock(_component_van_dropdown_item, {
            key: 0,
            ref_key: "allFilterRef",
            ref: allFilterRef,
            title: "全部筛选"
          }, {
            default: Vue.withCtx(() => [
              Vue.createElementVNode("div", _hoisted_7, [
                (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(__props.filterData, (filter, index2) => {
                  return Vue.openBlock(), Vue.createElementBlock("div", {
                    key: `all-${filter.label}-${index2}`,
                    class: "filter-group"
                  }, [
                    Vue.createElementVNode("h3", _hoisted_8, Vue.toDisplayString(filter.label), 1),
                    Vue.createElementVNode("div", _hoisted_9, [
                      (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(filter.options, (item, optIndex) => {
                        return Vue.openBlock(), Vue.createElementBlock("div", {
                          key: `${filter.label}-${item.value}-${optIndex}`,
                          class: Vue.normalizeClass(["tag-item", {
                            active: isOptionSelected(filter.fieldName, item.value)
                          }]),
                          onClick: ($event) => toggleOption(filter.fieldName, item.value)
                        }, Vue.toDisplayString(item.label), 11, _hoisted_10);
                      }), 128))
                    ])
                  ]);
                }), 128)),
                Vue.createElementVNode("div", _hoisted_11, [
                  Vue.createVNode(_component_van_button, {
                    size: "small",
                    onClick: resetAllFilters
                  }, {
                    default: Vue.withCtx(() => _cache[2] || (_cache[2] = [
                      Vue.createTextVNode("重置")
                    ])),
                    _: 1
                  }),
                  Vue.createVNode(_component_van_button, {
                    type: "primary",
                    size: "small",
                    onClick: confirmAllFilters
                  }, {
                    default: Vue.withCtx(() => _cache[3] || (_cache[3] = [
                      Vue.createTextVNode("确定")
                    ])),
                    _: 1
                  })
                ])
              ])
            ]),
            _: 1
          }, 512)) : Vue.createCommentVNode("", true)
        ]),
        _: 1
      })) : Vue.createCommentVNode("", true);
    };
  }
};
const QuickFilter = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-3e7ddf36"]]);
exports.default = QuickFilter;
