/* empty css                                               */
/* empty css                                                        */
/* empty css                                                */
/* empty css                                               */
/* empty css                                               */
/* empty css                                                  */
/* empty css                                                */
/* empty css                                                        */
/* empty css                                                  */
/* empty css                                                 */
import { ref, createBlock, createCommentVNode, openBlock, withCtx, createElementBlock, Fragment, renderList, unref, createElementVNode, normalizeClass, toDisplayString, createVNode, createTextVNode } from "vue";
/* empty css                  */
import _export_sfc from "../../../../../../_virtual/_plugin-vue_export-helper.mjs";
import { DropdownMenu } from "../../../../node_modules/vant/es/dropdown-menu/index.mjs";
import { DropdownItem } from "../../../../node_modules/vant/es/dropdown-item/index.mjs";
import { Button } from "../../../../node_modules/vant/es/button/index.mjs";
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 = ref({});
    const confirmedSelections = ref({});
    const itemRefs = 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 = 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 = Button;
      const _component_van_dropdown_item = DropdownItem;
      const _component_van_dropdown_menu = DropdownMenu;
      return __props.filterData.length > 0 ? (openBlock(), createBlock(_component_van_dropdown_menu, {
        key: 0,
        "auto-locate": "",
        class: "fec-quick-filter"
      }, {
        default: withCtx(() => [
          (openBlock(true), createElementBlock(Fragment, null, renderList(__props.filterData, (filter, index) => {
            return openBlock(), createBlock(_component_van_dropdown_item, {
              key: `${filter.label}-${index}`,
              ref_for: true,
              ref: (el) => unref(itemRefs)[filter.fieldName] = el,
              onOpen: ($event) => dropdownItemOpen(filter.fieldName)
            }, {
              title: withCtx(() => [
                createElementVNode("span", _hoisted_1, [
                  createTextVNode(toDisplayString(filter.dataType === "select" ? getSelectedLabel(filter) || filter.label : filter.label) + " ", 1),
                  filter.dataType === "multiple" && getConfirmedCount(filter.fieldName) > 0 ? (openBlock(), createElementBlock("span", _hoisted_2, toDisplayString(getConfirmedCount(filter.fieldName)), 1)) : createCommentVNode("", true)
                ])
              ]),
              default: withCtx(() => [
                createElementVNode("div", _hoisted_3, [
                  createElementVNode("div", _hoisted_4, [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(filter.options, (item) => {
                      return openBlock(), createElementBlock("div", {
                        key: item.id || item.value,
                        class: normalizeClass(["tag-item", { active: isOptionSelected(filter.fieldName, item.value) }]),
                        onClick: ($event) => toggleOption(filter.fieldName, item.value)
                      }, toDisplayString(item.label), 11, _hoisted_5);
                    }), 128))
                  ]),
                  createElementVNode("div", _hoisted_6, [
                    createVNode(_component_van_button, {
                      size: "small",
                      onClick: ($event) => resetFilter(filter.fieldName)
                    }, {
                      default: withCtx(() => _cache[0] || (_cache[0] = [
                        createTextVNode("重置")
                      ])),
                      _: 2
                    }, 1032, ["onClick"]),
                    createVNode(_component_van_button, {
                      type: "primary",
                      size: "small",
                      onClick: ($event) => confirmFilter(filter.fieldName)
                    }, {
                      default: withCtx(() => _cache[1] || (_cache[1] = [
                        createTextVNode("确定")
                      ])),
                      _: 2
                    }, 1032, ["onClick"])
                  ])
                ])
              ]),
              _: 2
            }, 1032, ["onOpen"]);
          }), 128)),
          __props.filterData.length > 0 ? (openBlock(), createBlock(_component_van_dropdown_item, {
            key: 0,
            ref_key: "allFilterRef",
            ref: allFilterRef,
            title: "全部筛选"
          }, {
            default: withCtx(() => [
              createElementVNode("div", _hoisted_7, [
                (openBlock(true), createElementBlock(Fragment, null, renderList(__props.filterData, (filter, index) => {
                  return openBlock(), createElementBlock("div", {
                    key: `all-${filter.label}-${index}`,
                    class: "filter-group"
                  }, [
                    createElementVNode("h3", _hoisted_8, toDisplayString(filter.label), 1),
                    createElementVNode("div", _hoisted_9, [
                      (openBlock(true), createElementBlock(Fragment, null, renderList(filter.options, (item, optIndex) => {
                        return openBlock(), createElementBlock("div", {
                          key: `${filter.label}-${item.value}-${optIndex}`,
                          class: normalizeClass(["tag-item", {
                            active: isOptionSelected(filter.fieldName, item.value)
                          }]),
                          onClick: ($event) => toggleOption(filter.fieldName, item.value)
                        }, toDisplayString(item.label), 11, _hoisted_10);
                      }), 128))
                    ])
                  ]);
                }), 128)),
                createElementVNode("div", _hoisted_11, [
                  createVNode(_component_van_button, {
                    size: "small",
                    onClick: resetAllFilters
                  }, {
                    default: withCtx(() => _cache[2] || (_cache[2] = [
                      createTextVNode("重置")
                    ])),
                    _: 1
                  }),
                  createVNode(_component_van_button, {
                    type: "primary",
                    size: "small",
                    onClick: confirmAllFilters
                  }, {
                    default: withCtx(() => _cache[3] || (_cache[3] = [
                      createTextVNode("确定")
                    ])),
                    _: 1
                  })
                ])
              ])
            ]),
            _: 1
          }, 512)) : createCommentVNode("", true)
        ]),
        _: 1
      })) : createCommentVNode("", true);
    };
  }
};
const QuickFilter = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-3e7ddf36"]]);
export {
  QuickFilter as default
};
