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                                                            */
import { computed, ref, watch, createElementBlock, openBlock, Fragment, createVNode, createBlock, createCommentVNode, withCtx, unref, isRef, createElementVNode, renderList, nextTick } from "vue";
import { Plus as plus_default, Rank as rank_default, Minus as minus_default } from "../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import { VueDraggable as lo } from "../../../node_modules/vue-draggable-plus/dist/vue-draggable-plus.mjs";
import _sfc_main$1 from "./DataSourceSelect.vue.mjs";
import FieldMapping from "./FieldMapping.vue2.mjs";
import { getEditConfigDataFields, getEditConfigData } from "../packages/utils/common.mjs";
/* empty css                   */
import _export_sfc from "../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElFormItem } from "../../../node_modules/element-plus/es/components/form/index.mjs";
import { ElSelect, ElOption } from "../../../node_modules/element-plus/es/components/select/index.mjs";
import { ElIcon } from "../../../node_modules/element-plus/es/components/icon/index.mjs";
import { ElInput } from "../../../node_modules/element-plus/es/components/input/index.mjs";
const _hoisted_1 = { class: "custom-options" };
const _hoisted_2 = { class: "option-header" };
const _hoisted_3 = { class: "drag-handle" };
const _hoisted_4 = ["onClick"];
const _hoisted_5 = { class: "data-source-fields" };
const _hoisted_6 = { class: "field-item" };
const _hoisted_7 = { class: "field-item" };
const _hoisted_8 = {
  class: "field-item",
  style: { "flex-direction": "column", "align-items": "start" }
};
const _sfc_main = {
  __name: "OptionConfig",
  props: {
    modelValue: {
      type: Object,
      default: () => ({
        optionSource: "dictionary",
        options: [
          {
            id: 1769067474103,
            label: "选项1",
            value: "1"
          },
          {
            id: 1769067474572,
            label: "选项2",
            value: "2"
          },
          {
            id: 1769067475160,
            label: "选项3",
            value: "3"
          }
        ],
        dataSourceValue: "",
        displayField: "",
        valueField: "",
        dictionaryValue: "",
        fieldMapping: []
      })
    }
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    var _a, _b, _c, _d, _e, _f, _g;
    const props = __props;
    const emit = __emit;
    computed(() => {
      const displayFields = getEditConfigDataFields() || [];
      const editConfigData = getEditConfigData();
      const hiddenList = (editConfigData == null ? void 0 : editConfigData.hiddenFields) || [];
      return [...displayFields, ...hiddenList];
    });
    const optionSource = ref(((_a = props.modelValue) == null ? void 0 : _a.optionSource) || "custom");
    const options = ref(((_b = props.modelValue) == null ? void 0 : _b.options) || []);
    const dataSourceValue = ref(((_c = props.modelValue) == null ? void 0 : _c.dataSourceValue) || "");
    const displayField = ref(((_d = props.modelValue) == null ? void 0 : _d.displayField) || "");
    const valueField = ref(((_e = props.modelValue) == null ? void 0 : _e.valueField) || "");
    const dictionaryValue = ref(((_f = props.modelValue) == null ? void 0 : _f.dictionaryValue) || "");
    const fieldMapping = ref(((_g = props.modelValue) == null ? void 0 : _g.fieldMapping) || []);
    const isUpdatingFromProps = ref(false);
    watch(
      () => props.modelValue,
      (newVal) => {
        if (newVal && !isUpdatingFromProps.value) {
          optionSource.value = newVal.optionSource || "custom";
          options.value = newVal.options || [];
          dataSourceValue.value = newVal.dataSourceValue || "";
          displayField.value = newVal.displayField || "";
          valueField.value = newVal.valueField || "";
          dictionaryValue.value = newVal.dictionaryValue || "";
          fieldMapping.value = newVal.fieldMapping || [];
        }
      },
      { deep: true }
    );
    const updateParent = () => {
      isUpdatingFromProps.value = true;
      const data = {
        optionSource: optionSource.value,
        options: options.value,
        dataSourceValue: dataSourceValue.value,
        displayField: displayField.value,
        valueField: valueField.value,
        dictionaryValue: dictionaryValue.value,
        fieldMapping: fieldMapping.value
      };
      emit("update:modelValue", data);
      nextTick(() => {
        isUpdatingFromProps.value = false;
      });
    };
    watch(
      [
        optionSource,
        options,
        dataSourceValue,
        displayField,
        valueField,
        dictionaryValue,
        fieldMapping
      ],
      updateParent,
      { deep: true, immediate: true }
    );
    function handleAddOption() {
      options.value.push({
        id: Date.now(),
        label: "",
        value: ""
      });
    }
    function handleDeleteOption(index) {
      options.value.splice(index, 1);
    }
    return (_ctx, _cache) => {
      const _component_el_option = ElOption;
      const _component_el_select = ElSelect;
      const _component_el_form_item = ElFormItem;
      const _component_el_icon = ElIcon;
      const _component_el_input = ElInput;
      return openBlock(), createElementBlock(Fragment, null, [
        createVNode(_component_el_form_item, {
          label: "选项来源",
          style: { "margin-bottom": "12px" }
        }, {
          default: withCtx(() => [
            createVNode(_component_el_select, {
              modelValue: unref(optionSource),
              "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(optionSource) ? optionSource.value = $event : null),
              placeholder: "请选择",
              teleported: false
            }, {
              default: withCtx(() => [
                createVNode(_component_el_option, {
                  label: "自定义",
                  value: "custom"
                }),
                createVNode(_component_el_option, {
                  label: "数据源",
                  value: "dataSource"
                }),
                createVNode(_component_el_option, {
                  label: "字典项",
                  value: "dictionary"
                })
              ]),
              _: 1
            }, 8, ["modelValue"])
          ]),
          _: 1
        }),
        unref(optionSource) === "custom" ? (openBlock(), createBlock(_component_el_form_item, { key: 0 }, {
          default: withCtx(() => [
            createElementVNode("div", _hoisted_1, [
              createElementVNode("div", _hoisted_2, [
                _cache[7] || (_cache[7] = createElementVNode("span", null, null, -1)),
                _cache[8] || (_cache[8] = createElementVNode("span", { class: "header-label" }, "显示值", -1)),
                _cache[9] || (_cache[9] = createElementVNode("span", { class: "header-label" }, "真实值", -1)),
                createElementVNode("div", {
                  class: "icon-btn add-btn",
                  onClick: handleAddOption
                }, [
                  createVNode(_component_el_icon, null, {
                    default: withCtx(() => [
                      createVNode(unref(plus_default))
                    ]),
                    _: 1
                  })
                ])
              ]),
              createVNode(unref(lo), {
                modelValue: unref(options),
                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(options) ? options.value = $event : null),
                animation: 150,
                handle: ".drag-handle"
              }, {
                default: withCtx(() => [
                  (openBlock(true), createElementBlock(Fragment, null, renderList(unref(options), (option, index) => {
                    return openBlock(), createElementBlock("div", {
                      key: option.id,
                      class: "option-item"
                    }, [
                      createElementVNode("div", _hoisted_3, [
                        createVNode(_component_el_icon, null, {
                          default: withCtx(() => [
                            createVNode(unref(rank_default))
                          ]),
                          _: 1
                        })
                      ]),
                      createVNode(_component_el_input, {
                        modelValue: option.label,
                        "onUpdate:modelValue": ($event) => option.label = $event,
                        placeholder: "显示值"
                      }, null, 8, ["modelValue", "onUpdate:modelValue"]),
                      createVNode(_component_el_input, {
                        modelValue: option.value,
                        "onUpdate:modelValue": ($event) => option.value = $event,
                        placeholder: "真实值"
                      }, null, 8, ["modelValue", "onUpdate:modelValue"]),
                      createElementVNode("div", {
                        class: "icon-btn delete-btn",
                        onClick: ($event) => handleDeleteOption(index)
                      }, [
                        createVNode(_component_el_icon, null, {
                          default: withCtx(() => [
                            createVNode(unref(minus_default))
                          ]),
                          _: 1
                        })
                      ], 8, _hoisted_4)
                    ]);
                  }), 128))
                ]),
                _: 1
              }, 8, ["modelValue"])
            ])
          ]),
          _: 1
        })) : unref(optionSource) === "dataSource" ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [
          createVNode(_component_el_form_item, null, {
            default: withCtx(() => [
              createVNode(_sfc_main$1, {
                modelValue: unref(dataSourceValue),
                "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => isRef(dataSourceValue) ? dataSourceValue.value = $event : null)
              }, null, 8, ["modelValue"])
            ]),
            _: 1
          }),
          createElementVNode("div", _hoisted_5, [
            createElementVNode("div", _hoisted_6, [
              _cache[10] || (_cache[10] = createElementVNode("span", { class: "field-label" }, "显示字段", -1)),
              createVNode(_component_el_input, {
                modelValue: unref(displayField),
                "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => isRef(displayField) ? displayField.value = $event : null),
                placeholder: "如：optName"
              }, null, 8, ["modelValue"])
            ]),
            createElementVNode("div", _hoisted_7, [
              _cache[11] || (_cache[11] = createElementVNode("span", { class: "field-label" }, "值字段", -1)),
              createVNode(_component_el_input, {
                modelValue: unref(valueField),
                "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => isRef(valueField) ? valueField.value = $event : null),
                placeholder: "如：optCode"
              }, null, 8, ["modelValue"])
            ]),
            createElementVNode("div", _hoisted_8, [
              _cache[12] || (_cache[12] = createElementVNode("span", {
                class: "field-label",
                style: { "width": "auto" }
              }, "选中数据映射表单字段", -1)),
              createVNode(FieldMapping, {
                modelValue: unref(fieldMapping),
                "onUpdate:modelValue": _cache[5] || (_cache[5] = ($event) => isRef(fieldMapping) ? fieldMapping.value = $event : null)
              }, null, 8, ["modelValue"])
            ])
          ])
        ], 64)) : unref(optionSource) === "dictionary" ? (openBlock(), createBlock(_component_el_form_item, { key: 2 }, {
          default: withCtx(() => [
            createVNode(_component_el_input, {
              modelValue: unref(dictionaryValue),
              "onUpdate:modelValue": _cache[6] || (_cache[6] = ($event) => isRef(dictionaryValue) ? dictionaryValue.value = $event : null),
              placeholder: "keyName"
            }, null, 8, ["modelValue"])
          ]),
          _: 1
        })) : createCommentVNode("", true)
      ], 64);
    };
  }
};
const OptionConfig = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-fe7440f2"]]);
export {
  OptionConfig as default
};
