import "../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                       */
/* empty css                                                                  */
/* empty css                                                           */
/* empty css                                                               */
/* empty css                                                            */
/* empty css                                                         */
/* empty css                                                            */
/* empty css                                                            */
/* empty css                                                            */
/* empty css                                                                  */
/* empty css                                                              */
import "../../../node_modules/element-plus/theme-chalk/el-dropdown-menu.css.mjs";
import "../../../node_modules/element-plus/theme-chalk/el-dropdown-item.css.mjs";
/* empty css                                                              */
import { computed, ref, createElementBlock, openBlock, normalizeClass, createBlock, unref, withCtx, createVNode, createTextVNode, createElementVNode, createCommentVNode, Fragment, renderList, isRef, toDisplayString } from "vue";
import { Plus as plus_default, Delete as delete_default } from "../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import { getEditConfigDataFields, getEditConfigData } from "../packages/utils/common.mjs";
/* empty css                     */
import _export_sfc from "../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElDropdown, ElDropdownMenu, ElDropdownItem } from "../../../node_modules/element-plus/es/components/dropdown/index.mjs";
import { ElButton } from "../../../node_modules/element-plus/es/components/button/index.mjs";
import { ElScrollbar } from "../../../node_modules/element-plus/es/components/scrollbar/index.mjs";
import { ElCheckbox } from "../../../node_modules/element-plus/es/components/checkbox/index.mjs";
import { ElSelect, ElOption, ElOptionGroup } from "../../../node_modules/element-plus/es/components/select/index.mjs";
import { ElInput } from "../../../node_modules/element-plus/es/components/input/index.mjs";
const _hoisted_1 = { class: "dropdown-header" };
const _hoisted_2 = { class: "dropdown-content" };
const _hoisted_3 = {
  key: 1,
  class: "mapping-table"
};
const _hoisted_4 = { class: "mapping-header" };
const _hoisted_5 = { class: "mapping-cell value-header" };
const _hoisted_6 = { class: "mapping-cell field-header" };
const _hoisted_7 = { class: "mapping-body" };
const _hoisted_8 = { class: "mapping-cell value-cell" };
const _hoisted_9 = { class: "mapping-cell field-cell" };
const _hoisted_10 = { class: "mapping-cell action-cell" };
const _hoisted_11 = { class: "mapping-footer" };
const _hoisted_12 = { class: "dropdown-header" };
const _hoisted_13 = { class: "dropdown-content" };
const _sfc_main = {
  __name: "FieldSetMapping",
  props: {
    modelValue: {
      type: Array,
      default: () => []
    },
    // 表单字段的描述文本
    fieldLabel: {
      type: String,
      default: "表单字段"
    },
    // 映射字段的描述文本
    valueLabel: {
      type: String,
      default: "映射字段"
    },
    // 映射字段支持的选择项列表
    valueOptions: {
      type: Array,
      default: () => []
    },
    // 映射字段是否为选择模式（true: 下拉选择，false: 手动输入）
    valueSelectMode: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: ""
    }
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const mappings = computed({
      get: () => props.modelValue,
      set: (val) => emit("update:modelValue", val)
    });
    const emit = __emit;
    const selectedFields = ref([]);
    const formFields = computed(() => {
      const fields = getEditConfigDataFields() || [];
      return fields.map((item) => ({
        id: item == null ? void 0 : item.id,
        fieldId: item == null ? void 0 : item.id,
        label: item == null ? void 0 : item.label,
        fieldName: item == null ? void 0 : item.fieldName
      }));
    });
    const hiddenFields = computed(() => {
      const editConfigData = getEditConfigData();
      const fields = (editConfigData == null ? void 0 : editConfigData.hiddenFields) || [];
      return fields.map((item) => ({
        id: item == null ? void 0 : item.id,
        fieldId: item == null ? void 0 : item.id,
        label: item == null ? void 0 : item.label,
        fieldName: item == null ? void 0 : item.fieldName
      }));
    });
    function handleDropdownVisible(visible) {
      if (visible) {
        selectedFields.value = [];
      }
    }
    const dropdownRef = ref();
    function confirmAddMappings() {
      var _a;
      if (selectedFields.value.length === 0) {
        return;
      }
      const allFields = [...formFields.value, ...hiddenFields.value];
      selectedFields.value.forEach((selectedFieldName) => {
        const newMapping = {
          id: Date.now() + Math.random(),
          field: "",
          // 默认值为空
          value: selectedFieldName
          // 自动赋值为勾选的字段
        };
        const matchedField = allFields.find(
          (field) => field.fieldName === selectedFieldName
        );
        if (matchedField) {
          newMapping.field = matchedField.fieldId;
        }
        mappings.value.push(newMapping);
      });
      selectedFields.value = [];
      (_a = dropdownRef.value) == null ? void 0 : _a.handleClose();
    }
    function removeMapping(index) {
      mappings.value.splice(index, 1);
    }
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_el_checkbox = ElCheckbox;
      const _component_el_dropdown_item = ElDropdownItem;
      const _component_el_scrollbar = ElScrollbar;
      const _component_el_dropdown_menu = ElDropdownMenu;
      const _component_el_dropdown = ElDropdown;
      const _component_el_option = ElOption;
      const _component_el_select = ElSelect;
      const _component_el_input = ElInput;
      const _component_el_option_group = ElOptionGroup;
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(["field-mapping-container", __props.size])
      }, [
        unref(mappings).length === 0 ? (openBlock(), createBlock(_component_el_dropdown, {
          key: 0,
          "max-height": 500,
          trigger: "click",
          placement: "bottom",
          "hide-on-click": false,
          onVisibleChange: handleDropdownVisible,
          ref_key: "dropdownRef",
          ref: dropdownRef
        }, {
          dropdown: withCtx(() => [
            createVNode(_component_el_dropdown_menu, { class: "field-mapping-dropdown" }, {
              default: withCtx(() => [
                createElementVNode("div", _hoisted_1, [
                  _cache[4] || (_cache[4] = createElementVNode("span", { class: "header-title" }, "选择字段", -1)),
                  createVNode(_component_el_button, {
                    type: "primary",
                    size: "small",
                    onClick: confirmAddMappings
                  }, {
                    default: withCtx(() => _cache[3] || (_cache[3] = [
                      createTextVNode("确定")
                    ])),
                    _: 1
                  })
                ]),
                createElementVNode("div", _hoisted_2, [
                  createVNode(_component_el_scrollbar, { "max-height": 300 }, {
                    default: withCtx(() => [
                      (openBlock(true), createElementBlock(Fragment, null, renderList(__props.valueOptions, (option) => {
                        return openBlock(), createBlock(_component_el_dropdown_item, {
                          key: option.fieldName
                        }, {
                          default: withCtx(() => [
                            createVNode(_component_el_checkbox, {
                              modelValue: unref(selectedFields),
                              "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(selectedFields) ? selectedFields.value = $event : null),
                              value: option.fieldName,
                              label: option.label
                            }, null, 8, ["modelValue", "value", "label"])
                          ]),
                          _: 2
                        }, 1024);
                      }), 128)),
                      __props.valueOptions.length === 0 ? (openBlock(), createBlock(_component_el_dropdown_item, {
                        key: 0,
                        disabled: ""
                      }, {
                        default: withCtx(() => _cache[5] || (_cache[5] = [
                          createElementVNode("span", { class: "empty-text" }, "暂无可用字段", -1)
                        ])),
                        _: 1
                      })) : createCommentVNode("", true)
                    ]),
                    _: 1
                  })
                ])
              ]),
              _: 1
            })
          ]),
          default: withCtx(() => [
            createVNode(_component_el_button, {
              type: "primary",
              size: "small",
              icon: unref(plus_default),
              link: ""
            }, {
              default: withCtx(() => _cache[2] || (_cache[2] = [
                createTextVNode(" 新增字段映射 ")
              ])),
              _: 1
            }, 8, ["icon"])
          ]),
          _: 1
        }, 512)) : (openBlock(), createElementBlock("div", _hoisted_3, [
          createElementVNode("div", _hoisted_4, [
            createElementVNode("div", _hoisted_5, toDisplayString(__props.valueLabel), 1),
            createElementVNode("div", _hoisted_6, toDisplayString(__props.fieldLabel), 1),
            _cache[6] || (_cache[6] = createElementVNode("div", {
              class: "mapping-cell action-header",
              style: { "width": "50px" }
            }, null, -1))
          ]),
          createElementVNode("div", _hoisted_7, [
            (openBlock(true), createElementBlock(Fragment, null, renderList(unref(mappings), (item, index) => {
              return openBlock(), createElementBlock("div", {
                key: item.id,
                class: "mapping-row"
              }, [
                createElementVNode("div", _hoisted_8, [
                  __props.valueSelectMode ? (openBlock(), createBlock(_component_el_select, {
                    key: 0,
                    modelValue: item.value,
                    "onUpdate:modelValue": ($event) => item.value = $event,
                    placeholder: `选择${__props.valueLabel}`,
                    size: "small",
                    style: { "width": "100%" },
                    filterable: ""
                  }, {
                    default: withCtx(() => [
                      (openBlock(true), createElementBlock(Fragment, null, renderList(__props.valueOptions, (option) => {
                        return openBlock(), createBlock(_component_el_option, {
                          key: option.fieldName,
                          label: option.label,
                          value: option.fieldName
                        }, null, 8, ["label", "value"]);
                      }), 128))
                    ]),
                    _: 2
                  }, 1032, ["modelValue", "onUpdate:modelValue", "placeholder"])) : (openBlock(), createBlock(_component_el_input, {
                    key: 1,
                    modelValue: item.value,
                    "onUpdate:modelValue": ($event) => item.value = $event,
                    placeholder: `输入${__props.valueLabel}`,
                    size: "small"
                  }, null, 8, ["modelValue", "onUpdate:modelValue", "placeholder"]))
                ]),
                createElementVNode("div", _hoisted_9, [
                  createVNode(_component_el_select, {
                    modelValue: item.field,
                    "onUpdate:modelValue": ($event) => item.field = $event,
                    placeholder: `选择${__props.fieldLabel}`,
                    size: "small",
                    style: { "width": "100%" },
                    filterable: ""
                  }, {
                    default: withCtx(() => [
                      unref(formFields).length > 0 ? (openBlock(), createBlock(_component_el_option_group, {
                        key: 0,
                        label: "表单字段"
                      }, {
                        default: withCtx(() => [
                          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(formFields), (field) => {
                            return openBlock(), createBlock(_component_el_option, {
                              key: field.fieldId,
                              label: field.label,
                              value: field.fieldId
                            }, null, 8, ["label", "value"]);
                          }), 128))
                        ]),
                        _: 1
                      })) : createCommentVNode("", true),
                      unref(hiddenFields).length > 0 ? (openBlock(), createBlock(_component_el_option_group, {
                        key: 1,
                        label: "隐藏域"
                      }, {
                        default: withCtx(() => [
                          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(hiddenFields), (field) => {
                            return openBlock(), createBlock(_component_el_option, {
                              key: field.id,
                              label: field.label,
                              value: field.id
                            }, null, 8, ["label", "value"]);
                          }), 128))
                        ]),
                        _: 1
                      })) : createCommentVNode("", true)
                    ]),
                    _: 2
                  }, 1032, ["modelValue", "onUpdate:modelValue", "placeholder"])
                ]),
                createElementVNode("div", _hoisted_10, [
                  createVNode(_component_el_button, {
                    type: "danger",
                    size: "small",
                    icon: unref(delete_default),
                    text: "",
                    onClick: ($event) => removeMapping(index)
                  }, null, 8, ["icon", "onClick"])
                ])
              ]);
            }), 128))
          ]),
          createElementVNode("div", _hoisted_11, [
            createVNode(_component_el_dropdown, {
              "max-height": 500,
              trigger: "click",
              placement: "bottom",
              "hide-on-click": false,
              onVisibleChange: handleDropdownVisible,
              ref_key: "dropdownRef",
              ref: dropdownRef
            }, {
              dropdown: withCtx(() => [
                createVNode(_component_el_dropdown_menu, { class: "field-mapping-dropdown" }, {
                  default: withCtx(() => [
                    createElementVNode("div", _hoisted_12, [
                      _cache[9] || (_cache[9] = createElementVNode("span", { class: "header-title" }, "选择字段", -1)),
                      createVNode(_component_el_button, {
                        type: "primary",
                        size: "small",
                        onClick: confirmAddMappings
                      }, {
                        default: withCtx(() => _cache[8] || (_cache[8] = [
                          createTextVNode("确定")
                        ])),
                        _: 1
                      })
                    ]),
                    createElementVNode("div", _hoisted_13, [
                      createVNode(_component_el_scrollbar, { "max-height": 300 }, {
                        default: withCtx(() => [
                          (openBlock(true), createElementBlock(Fragment, null, renderList(__props.valueOptions, (option) => {
                            return openBlock(), createBlock(_component_el_dropdown_item, {
                              key: option.fieldName
                            }, {
                              default: withCtx(() => [
                                createVNode(_component_el_checkbox, {
                                  modelValue: unref(selectedFields),
                                  "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(selectedFields) ? selectedFields.value = $event : null),
                                  value: option.fieldName,
                                  label: option.label
                                }, null, 8, ["modelValue", "value", "label"])
                              ]),
                              _: 2
                            }, 1024);
                          }), 128)),
                          __props.valueOptions.length === 0 ? (openBlock(), createBlock(_component_el_dropdown_item, {
                            key: 0,
                            disabled: ""
                          }, {
                            default: withCtx(() => _cache[10] || (_cache[10] = [
                              createElementVNode("span", { class: "empty-text" }, "暂无可用字段", -1)
                            ])),
                            _: 1
                          })) : createCommentVNode("", true)
                        ]),
                        _: 1
                      })
                    ])
                  ]),
                  _: 1
                })
              ]),
              default: withCtx(() => [
                createVNode(_component_el_button, {
                  type: "primary",
                  size: "small",
                  icon: unref(plus_default),
                  link: ""
                }, {
                  default: withCtx(() => _cache[7] || (_cache[7] = [
                    createTextVNode(" 新增字段映射 ")
                  ])),
                  _: 1
                }, 8, ["icon"])
              ]),
              _: 1
            }, 512)
          ])
        ]))
      ], 2);
    };
  }
};
const FieldSetMapping = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-15f7f3da"]]);
export {
  FieldSetMapping as default
};
