"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                                                               */
const Vue = require("vue");
const index = require("../../../../node_modules/@vueuse/core/dist/index.js");
const Trigger = require("./Trigger.vue2.js");
const Operator = require("./Operator.vue2.js");
const ValueInput = require("./ValueInput.vue2.js");
;/* empty css                    */
const _pluginVue_exportHelper = require("../../../../_virtual/_plugin-vue_export-helper.js");
const index$1 = require("../../../../node_modules/element-plus/es/components/switch/index.js");
const index$2 = require("../../../../node_modules/element-plus/es/components/form/index.js");
const index$3 = require("../../../../node_modules/element-plus/es/components/row/index.js");
const index$4 = require("../../../../node_modules/element-plus/es/components/col/index.js");
const index$5 = require("../../../../node_modules/element-plus/es/components/checkbox/index.js");
const index$6 = require("../../../../node_modules/element-plus/es/components/button/index.js");
const _hoisted_1 = { class: "filter-container" };
const _hoisted_2 = {
  key: 0,
  class: "logical-operator"
};
const _hoisted_3 = { class: "filter-option-content" };
const _hoisted_4 = {
  key: 0,
  class: "filter-item-rule"
};
const _hoisted_5 = { class: "filter-item-rule" };
const _sfc_main = {
  __name: "AdvancedFilter",
  props: {
    filterFields: Array,
    modelValue: Object
  },
  emits: [
    "update:modelValue",
    "addCondition",
    "delCondition",
    "delGroup"
  ],
  setup(__props, { emit: __emit }) {
    const $props = __props;
    const $emits = __emit;
    const filterRules = index.useVModel($props, "modelValue", $emits);
    const getFieldType = (fieldId) => {
      const field = $props.filterFields.find((e) => e.id == fieldId);
      return (field == null ? void 0 : field.type) || "input";
    };
    const getField = (fieldId) => {
      const field = $props.filterFields.find((e) => e.id == fieldId);
      return field || {};
    };
    const getOperatorOptions = (type) => {
      const baseOperators = [
        { value: "eq", label: "等于" },
        { value: "ne", label: "不等于" }
      ];
      const commonOperators = [
        { value: "in_any", label: "等于任意一个" },
        { value: "ni_any", label: "不等于任意一个" },
        { value: "contains", label: "包含" },
        { value: "not_contains", label: "不包含" }
      ];
      const nullOperators = [
        { value: "is_null", label: "为空" },
        { value: "is_not_null", label: "不为空" }
      ];
      const compareOperators = [
        { value: "gt", label: "大于" },
        { value: "gte", label: "大于等于" },
        { value: "lt", label: "小于" },
        { value: "lte", label: "小于等于" },
        { value: "range", label: "选择范围" }
      ];
      switch (type) {
        case "number":
          return [...baseOperators, ...compareOperators, ...nullOperators];
        case "date":
        case "datetime":
          return [...baseOperators, ...compareOperators, ...nullOperators];
        case "boolean":
          return [...baseOperators, ...nullOperators];
        case "select":
          return [...baseOperators, ...commonOperators, ...nullOperators];
        case "input":
        default:
          return [...baseOperators, ...commonOperators, ...nullOperators];
      }
    };
    const isOperatorValid = (operator, fieldType) => {
      const options = getOperatorOptions(fieldType);
      return options.some((opt) => opt.value == operator);
    };
    const getDefaultOperator = (fieldType) => {
      return "eq";
    };
    const handleFieldChange = (item) => {
      const fieldType = getFieldType(item.field);
      if (!isOperatorValid(item.operator, fieldType)) {
        item.operator = getDefaultOperator();
        item.value = null;
      } else {
        handleOperatorChange(item);
      }
    };
    const handleOperatorChange = (item) => {
      if (item.isField) return;
      getFieldType(item.field);
      const isMultiple = ["in_any", "ni_any", "contains", "not_contains"].includes(
        item.operator
      );
      const isRange = item.operator == "range";
      if (isRange) {
        if (item.value == null || typeof item.value !== "object") {
          item.value = { min: null, max: null };
        } else if (!("min" in item.value) || !("max" in item.value)) {
          item.value = { min: item.value.min || null, max: item.value.max || null };
        }
      } else if (isMultiple) {
        if (!Array.isArray(item.value) && item.value !== null) {
          item.value = [item.value];
        } else if (item.value == null) {
          item.value = [];
        }
      } else {
        if (Array.isArray(item.value) || typeof item.value == "object") {
          item.value = null;
        }
      }
    };
    const addRule = () => {
      filterRules.value.conditions.push({
        field: null,
        operator: "eq",
        value: null,
        showText: null,
        isField: false
      });
    };
    const handleDel = (index2) => {
      filterRules.value.conditions.splice(index2, 1);
      if (filterRules.value.conditions.length <= 0) {
        $emits("delGroup");
      }
      $emits("delCondition", index2);
    };
    const addGroup = () => {
      filterRules.value.groups.push({
        operator: "and",
        conditions: [
          {
            field: null,
            operator: "",
            value: null,
            showText: null,
            isField: false
          }
        ],
        groups: []
      });
    };
    const delGroup = (index2) => {
      filterRules.value.groups.splice(index2, 1);
    };
    return (_ctx, _cache) => {
      const _component_el_switch = index$1.ElSwitch;
      const _component_el_form_item = index$2.ElFormItem;
      const _component_el_col = index$4.ElCol;
      const _component_el_checkbox = index$5.ElCheckbox;
      const _component_el_button = index$6.ElButton;
      const _component_el_row = index$3.ElRow;
      const _component_el_form = index$2.ElForm;
      return Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
        Vue.unref(filterRules).conditions.length > 0 ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_2, [
          _cache[1] || (_cache[1] = Vue.createElementVNode("div", { class: "logical-operator__line" }, null, -1)),
          Vue.createVNode(_component_el_switch, {
            size: "default",
            modelValue: Vue.unref(filterRules).operator,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => Vue.unref(filterRules).operator = $event),
            "inline-prompt": "",
            style: { "--el-switch-on-color": "#409eff", "--el-switch-off-color": "#67c23a" },
            "active-value": "and",
            "inactive-value": "or",
            "active-text": "且",
            "inactive-text": "或"
          }, null, 8, ["modelValue"])
        ])) : Vue.createCommentVNode("", true),
        Vue.createElementVNode("div", _hoisted_3, [
          Vue.unref(filterRules).conditions.length > 0 ? (Vue.openBlock(), Vue.createBlock(_component_el_form, {
            key: 0,
            "label-width": 0,
            inline: true,
            model: Vue.unref(filterRules),
            size: "default"
          }, {
            default: Vue.withCtx(() => [
              (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(Vue.unref(filterRules).conditions, (item, index2) => {
                return Vue.openBlock(), Vue.createBlock(_component_el_row, {
                  key: `${item.field}-${index2}`,
                  gutter: 5,
                  class: "filter-item-rule"
                }, {
                  default: Vue.withCtx(() => [
                    Vue.createVNode(_component_el_col, {
                      xs: 24,
                      sm: 7
                    }, {
                      default: Vue.withCtx(() => [
                        Vue.createVNode(_component_el_form_item, {
                          prop: "conditions." + index2 + ".field",
                          style: { "width": "100%" },
                          size: "default"
                        }, {
                          default: Vue.withCtx(() => [
                            Vue.createVNode(Trigger.default, {
                              ref_for: true,
                              ref: "triggerRef",
                              options: $props.filterFields.filter((e) => e.value !== void 0),
                              "filter-rules": Vue.unref(filterRules),
                              modelValue: item.field,
                              "onUpdate:modelValue": [($event) => item.field = $event, ($event) => handleFieldChange(item)]
                            }, null, 8, ["options", "filter-rules", "modelValue", "onUpdate:modelValue"])
                          ]),
                          _: 2
                        }, 1032, ["prop"])
                      ]),
                      _: 2
                    }, 1024),
                    item.field ? (Vue.openBlock(), Vue.createBlock(_component_el_col, {
                      key: 0,
                      xs: 24,
                      sm: 5
                    }, {
                      default: Vue.withCtx(() => [
                        Vue.createVNode(_component_el_form_item, {
                          prop: "conditions." + index2 + ".operator",
                          style: { "width": "100%" },
                          size: "default"
                        }, {
                          default: Vue.withCtx(() => [
                            Vue.createVNode(Operator.default, {
                              ref_for: true,
                              ref: "operatorRef",
                              modelValue: item.operator,
                              "onUpdate:modelValue": [($event) => item.operator = $event, ($event) => handleOperatorChange(item)],
                              "field-type": getFieldType(item.field),
                              field: getField(item.field)
                            }, null, 8, ["modelValue", "onUpdate:modelValue", "field-type", "field"])
                          ]),
                          _: 2
                        }, 1032, ["prop"])
                      ]),
                      _: 2
                    }, 1024)) : Vue.createCommentVNode("", true),
                    item.field ? (Vue.openBlock(), Vue.createBlock(_component_el_col, {
                      key: 1,
                      xs: 24,
                      sm: 8
                    }, {
                      default: Vue.withCtx(() => [
                        Vue.createVNode(_component_el_form_item, {
                          prop: "conditions." + index2 + ".value",
                          style: { "width": "100%" }
                        }, {
                          default: Vue.withCtx(() => [
                            Vue.createVNode(ValueInput.default, {
                              modelValue: item.value,
                              "onUpdate:modelValue": ($event) => item.value = $event,
                              showText: item.showText,
                              "field-type": getFieldType(item.field),
                              operator: item.operator,
                              field: getField(item.field),
                              "all-fields": $props.filterFields,
                              "is-field": item.isField,
                              "onUpdate:isField": ($event) => item.isField = $event,
                              "onUpdate:showText": ($event) => item.showText = $event,
                              size: "default"
                            }, null, 8, ["modelValue", "onUpdate:modelValue", "showText", "field-type", "operator", "field", "all-fields", "is-field", "onUpdate:isField", "onUpdate:showText"])
                          ]),
                          _: 2
                        }, 1032, ["prop"])
                      ]),
                      _: 2
                    }, 1024)) : Vue.createCommentVNode("", true),
                    Vue.createVNode(_component_el_col, {
                      xs: 24,
                      sm: 4,
                      style: { "display": "flex", "align-items": "center", "justify-content": "space-between" }
                    }, {
                      default: Vue.withCtx(() => [
                        item.field ? (Vue.openBlock(), Vue.createBlock(_component_el_checkbox, {
                          key: 0,
                          modelValue: item.isField,
                          "onUpdate:modelValue": ($event) => item.isField = $event,
                          label: "变量",
                          size: "default",
                          style: { "margin-right": "10px" }
                        }, null, 8, ["modelValue", "onUpdate:modelValue"])) : Vue.createCommentVNode("", true),
                        Vue.createVNode(_component_el_button, {
                          plain: "",
                          circle: "",
                          type: "danger",
                          icon: "Delete",
                          size: "small",
                          onClick: ($event) => handleDel(index2)
                        }, null, 8, ["onClick"])
                      ]),
                      _: 2
                    }, 1024)
                  ]),
                  _: 2
                }, 1024);
              }), 128)),
              (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(Vue.unref(filterRules).groups, (item, index2) => {
                return Vue.openBlock(), Vue.createBlock(AdvancedFilter, {
                  key: index2,
                  onDelGroup: ($event) => delGroup(index2),
                  modelValue: Vue.unref(filterRules).groups[index2],
                  "onUpdate:modelValue": ($event) => Vue.unref(filterRules).groups[index2] = $event,
                  filterFields: $props.filterFields
                }, {
                  default: Vue.withCtx(() => [
                    Vue.createVNode(_component_el_button, {
                      onClick: ($event) => delGroup(index2),
                      icon: "CircleClose",
                      type: "danger",
                      link: "",
                      size: "default",
                      class: "filter-filter-item__delete"
                    }, {
                      default: Vue.withCtx(() => _cache[2] || (_cache[2] = [
                        Vue.createTextVNode(" 删除条件组 ")
                      ])),
                      _: 2
                    }, 1032, ["onClick"])
                  ]),
                  _: 2
                }, 1032, ["onDelGroup", "modelValue", "onUpdate:modelValue", "filterFields"]);
              }), 128)),
              Vue.unref(filterRules).groups.length == 0 && Vue.unref(filterRules).conditions.length == 0 ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_4)) : Vue.createCommentVNode("", true)
            ]),
            _: 1
          }, 8, ["model"])) : Vue.createCommentVNode("", true),
          Vue.createElementVNode("div", _hoisted_5, [
            Vue.createVNode(_component_el_button, {
              onClick: addRule,
              type: "primary",
              link: "",
              icon: "CirclePlus",
              size: "default",
              class: "filter-filter-item__add"
            }, {
              default: Vue.withCtx(() => _cache[3] || (_cache[3] = [
                Vue.createTextVNode(" 添加条件 ")
              ])),
              _: 1
            }),
            Vue.unref(filterRules).conditions.length ? (Vue.openBlock(), Vue.createBlock(_component_el_button, {
              key: 0,
              onClick: addGroup,
              type: "primary",
              link: "",
              icon: "CirclePlus",
              size: "default",
              class: "filter-filter-item__add"
            }, {
              default: Vue.withCtx(() => _cache[4] || (_cache[4] = [
                Vue.createTextVNode(" 添加条件组 ")
              ])),
              _: 1
            })) : Vue.createCommentVNode("", true),
            Vue.renderSlot(_ctx.$slots, "default", {}, void 0, true)
          ])
        ])
      ]);
    };
  }
};
const AdvancedFilter = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-fd919c69"]]);
exports.default = AdvancedFilter;
