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                                                               */
/* empty css                                                                  */
import { getCurrentInstance, useModel, ref, watch, computed, createBlock, openBlock, withCtx, createVNode, createElementVNode, createElementBlock, Fragment, renderList, createTextVNode, unref } from "vue";
import { Plus as plus_default } from "../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import { getEditConfigData, getEditConfigDataFields, setEditConfigData } from "../utils/common.mjs";
import ValueSelector from "../../components/ValueSelector.vue.mjs";
import api from "../../api/index.mjs";
/* empty css            */
import _export_sfc from "../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElMessage } from "../../../../node_modules/element-plus/es/components/message/index.mjs";
import { ElDialog } from "../../../../node_modules/element-plus/es/components/dialog/index.mjs";
import { ElForm, 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 { ElButton } from "../../../../node_modules/element-plus/es/components/button/index.mjs";
const _hoisted_1 = { class: "config-list" };
const _hoisted_2 = { class: "config-row" };
const _hoisted_3 = { class: "config-list" };
const _hoisted_4 = { class: "config-row" };
const _hoisted_5 = { class: "config-list" };
const _hoisted_6 = { class: "config-row" };
const _hoisted_7 = {
  class: "config-row",
  style: { "margin-top": "10px" }
};
const _hoisted_8 = { class: "dialog-footer" };
const _sfc_main = {
  __name: "index",
  props: {
    "modelValue": { required: true },
    "modelModifiers": {}
  },
  emits: ["update:modelValue"],
  setup(__props, { expose: __expose }) {
    var _a;
    const currentInstance = getCurrentInstance();
    const ctx = currentInstance.proxy;
    const dialogVisible = useModel(__props, "modelValue");
    const prodConfig = ref(((_a = getEditConfigData()) == null ? void 0 : _a.prodConfig) || {});
    watch(
      () => dialogVisible.value,
      (newVisible) => {
        var _a2;
        if (newVisible) {
          const savedConfig = ((_a2 = getEditConfigData()) == null ? void 0 : _a2.prodConfig) || {};
          if (savedConfig.optionConfigs) {
            optionConfigs.value = savedConfig.optionConfigs.map((item) => ({
              ...item,
              id: generateId()
            }));
          }
          if (savedConfig.defaultValueConfigs) {
            defaultValueConfigs.value = savedConfig.defaultValueConfigs.map(
              (item) => ({
                ...item,
                id: generateId()
              })
            );
          }
          if (savedConfig.validationConfigs) {
            validationConfigs.value = savedConfig.validationConfigs.map((item) => ({
              ...item,
              id: generateId()
            }));
          }
          fetchProductConfig();
        }
      }
    );
    const fieldsDataAll = computed(() => {
      var _a2;
      return [
        ...getEditConfigDataFields(),
        ...((_a2 = getEditConfigData()) == null ? void 0 : _a2.hiddenFields) || []
      ];
    });
    const parsedConfig = ref([]);
    const optionConfigs = ref([]);
    const defaultValueConfigs = ref([]);
    const validationConfigs = ref([]);
    function generateId() {
      return Date.now() + Math.random().toString(36).substr(2, 9);
    }
    function addOptionConfig() {
      optionConfigs.value.push({
        id: generateId(),
        pageField: "",
        productConfigField: ""
      });
    }
    function removeOptionConfig(index) {
      optionConfigs.value.splice(index, 1);
    }
    function addDefaultValueConfig() {
      defaultValueConfigs.value.push({
        id: generateId(),
        pageField: "",
        productConfigField: ""
      });
    }
    function removeDefaultValueConfig(index) {
      defaultValueConfigs.value.splice(index, 1);
    }
    function addValidationConfig() {
      validationConfigs.value.push({
        id: generateId(),
        pageField: "",
        format: "default",
        minProductField: "",
        maxProductField: ""
      });
    }
    function removeValidationConfig(index) {
      validationConfigs.value.splice(index, 1);
    }
    const handleClose = (done) => {
      handleCancel();
      done();
    };
    const handleCancel = () => {
      optionConfigs.value = [
        {
          id: generateId(),
          pageField: "",
          productConfigField: ""
        }
      ];
      defaultValueConfigs.value = [
        {
          id: generateId(),
          pageField: "",
          productConfigField: ""
        }
      ];
      validationConfigs.value = [
        {
          id: generateId(),
          pageField: "",
          minProductField: "",
          maxProductField: ""
        }
      ];
      parsedConfig.value = [];
      dialogVisible.value = false;
    };
    const handleConfirm = () => {
      const editConfig = getEditConfigData();
      console.log("🚀 ~ handleConfirm ~ finalConfig.value:", finalConfig.value);
      const updatedConfig = {
        ...editConfig,
        prodConfig: finalConfig.value
      };
      setEditConfigData(updatedConfig);
      handleCancel();
    };
    const fetchProductConfig = async () => {
      try {
        const response = await api.getProductConfig(ctx.$http);
        parsedConfig.value = response.map((item) => ({
          fieldName: item.fieldName || "",
          fieldDesc: item.label || item.fieldName || "",
          // 保留原始数据中的其他字段
          ...item
        }));
        if (parsedConfig.value.length === 0) {
          ElMessage.warning("未获取到产品配置数据");
        }
      } catch (error) {
        ElMessage.error("获取产品配置失败");
        console.error("获取产品配置失败:", error);
        parsedConfig.value = [];
      }
    };
    const finalConfig = computed(() => {
      const result = {
        prodId: prodConfig.value.prodId,
        optionConfigs: optionConfigs.value,
        defaultValueConfigs: defaultValueConfigs.value,
        validationConfigs: validationConfigs.value
      };
      return result;
    });
    __expose({});
    return (_ctx, _cache) => {
      const _component_el_form_item = ElFormItem;
      const _component_el_option = ElOption;
      const _component_el_select = ElSelect;
      const _component_el_button = ElButton;
      const _component_el_form = ElForm;
      const _component_el_dialog = ElDialog;
      return openBlock(), createBlock(_component_el_dialog, {
        modelValue: dialogVisible.value,
        "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => dialogVisible.value = $event),
        title: "产品配置",
        width: "600px",
        "close-on-click-modal": false,
        "before-close": handleClose,
        draggable: "",
        overflow: "",
        "append-to-body": "",
        class: "fec-dev-designer-dialog"
      }, {
        footer: withCtx(() => [
          createElementVNode("span", _hoisted_8, [
            createVNode(_component_el_button, { onClick: handleCancel }, {
              default: withCtx(() => _cache[12] || (_cache[12] = [
                createTextVNode("取消")
              ])),
              _: 1
            }),
            createVNode(_component_el_button, {
              type: "primary",
              onClick: handleConfirm
            }, {
              default: withCtx(() => _cache[13] || (_cache[13] = [
                createTextVNode(" 确定 ")
              ])),
              _: 1
            })
          ])
        ]),
        default: withCtx(() => [
          createVNode(_component_el_form, {
            "label-position": "top",
            "label-width": "auto",
            model: prodConfig.value
          }, {
            default: withCtx(() => [
              createVNode(_component_el_form_item, null, {
                label: withCtx(() => _cache[2] || (_cache[2] = [
                  createElementVNode("span", { class: "prod-config-label" }, "产品ID字段", -1)
                ])),
                default: withCtx(() => [
                  createVNode(ValueSelector, {
                    modelValue: prodConfig.value.prodId,
                    "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => prodConfig.value.prodId = $event),
                    readOnly: "",
                    size: "default"
                  }, null, 8, ["modelValue"])
                ]),
                _: 1
              }),
              createVNode(_component_el_form_item, null, {
                label: withCtx(() => _cache[3] || (_cache[3] = [
                  createElementVNode("span", { class: "prod-config-label" }, "可选项配置", -1)
                ])),
                default: withCtx(() => [
                  createElementVNode("div", _hoisted_1, [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(optionConfigs.value, (item, index) => {
                      return openBlock(), createElementBlock("div", {
                        key: item.id,
                        class: "config-item"
                      }, [
                        createElementVNode("div", _hoisted_2, [
                          createVNode(_component_el_select, {
                            modelValue: item.pageField,
                            "onUpdate:modelValue": ($event) => item.pageField = $event,
                            placeholder: "选择页面字段",
                            filterable: "",
                            clearable: "",
                            style: { "width": "200px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(fieldsDataAll.value, (field) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: field.id,
                                  label: field.label,
                                  value: field.id
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_select, {
                            modelValue: item.productConfigField,
                            "onUpdate:modelValue": ($event) => item.productConfigField = $event,
                            placeholder: "选择产品配置项",
                            filterable: "",
                            clearable: "",
                            style: { "width": "250px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(parsedConfig.value, (config) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: config.fieldName,
                                  label: config.fieldDesc,
                                  value: config.fieldName
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_button, {
                            type: "danger",
                            size: "small",
                            link: "",
                            onClick: ($event) => removeOptionConfig(index)
                          }, {
                            default: withCtx(() => _cache[4] || (_cache[4] = [
                              createTextVNode(" 删除 ")
                            ])),
                            _: 2
                          }, 1032, ["onClick"])
                        ])
                      ]);
                    }), 128)),
                    createVNode(_component_el_button, {
                      type: "primary",
                      size: "small",
                      icon: unref(plus_default),
                      link: "",
                      onClick: addOptionConfig
                    }, {
                      default: withCtx(() => _cache[5] || (_cache[5] = [
                        createTextVNode(" 新增 ")
                      ])),
                      _: 1
                    }, 8, ["icon"])
                  ])
                ]),
                _: 1
              }),
              createVNode(_component_el_form_item, null, {
                label: withCtx(() => _cache[6] || (_cache[6] = [
                  createElementVNode("span", { class: "prod-config-label" }, "默认值配置", -1)
                ])),
                default: withCtx(() => [
                  createElementVNode("div", _hoisted_3, [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(defaultValueConfigs.value, (item, index) => {
                      return openBlock(), createElementBlock("div", {
                        key: item.id,
                        class: "config-item"
                      }, [
                        createElementVNode("div", _hoisted_4, [
                          createVNode(_component_el_select, {
                            modelValue: item.pageField,
                            "onUpdate:modelValue": ($event) => item.pageField = $event,
                            placeholder: "选择页面字段",
                            filterable: "",
                            clearable: "",
                            style: { "width": "200px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(fieldsDataAll.value, (field) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: field.id,
                                  label: field.label,
                                  value: field.id
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_select, {
                            modelValue: item.productConfigField,
                            "onUpdate:modelValue": ($event) => item.productConfigField = $event,
                            placeholder: "选择产品配置项",
                            filterable: "",
                            clearable: "",
                            style: { "width": "250px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(parsedConfig.value, (config) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: config.fieldName,
                                  label: config.fieldDesc,
                                  value: config.fieldName
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_button, {
                            type: "danger",
                            size: "small",
                            link: "",
                            onClick: ($event) => removeDefaultValueConfig(index)
                          }, {
                            default: withCtx(() => _cache[7] || (_cache[7] = [
                              createTextVNode(" 删除 ")
                            ])),
                            _: 2
                          }, 1032, ["onClick"])
                        ])
                      ]);
                    }), 128)),
                    createVNode(_component_el_button, {
                      type: "primary",
                      size: "small",
                      icon: unref(plus_default),
                      link: "",
                      onClick: addDefaultValueConfig
                    }, {
                      default: withCtx(() => _cache[8] || (_cache[8] = [
                        createTextVNode(" 新增 ")
                      ])),
                      _: 1
                    }, 8, ["icon"])
                  ])
                ]),
                _: 1
              }),
              createVNode(_component_el_form_item, null, {
                label: withCtx(() => _cache[9] || (_cache[9] = [
                  createElementVNode("span", { class: "prod-config-label" }, "校验配置", -1)
                ])),
                default: withCtx(() => [
                  createElementVNode("div", _hoisted_5, [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(validationConfigs.value, (item, index) => {
                      return openBlock(), createElementBlock("div", {
                        key: item.id,
                        class: "config-item"
                      }, [
                        createElementVNode("div", _hoisted_6, [
                          createVNode(_component_el_select, {
                            modelValue: item.pageField,
                            "onUpdate:modelValue": ($event) => item.pageField = $event,
                            placeholder: "选择页面字段",
                            filterable: "",
                            clearable: "",
                            style: { "width": "235px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(fieldsDataAll.value, (field) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: field.id,
                                  label: field.label,
                                  value: field.id
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_select, {
                            modelValue: item.format,
                            "onUpdate:modelValue": ($event) => item.format = $event,
                            placeholder: "格式",
                            clearable: "",
                            style: { "width": "235px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              createVNode(_component_el_option, {
                                label: "默认格式",
                                value: "default"
                              }),
                              createVNode(_component_el_option, {
                                label: "金额",
                                value: "amount"
                              }),
                              createVNode(_component_el_option, {
                                label: "百分比",
                                value: "percent"
                              })
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_button, {
                            type: "danger",
                            size: "small",
                            link: "",
                            onClick: ($event) => removeValidationConfig(index)
                          }, {
                            default: withCtx(() => _cache[10] || (_cache[10] = [
                              createTextVNode(" 删除 ")
                            ])),
                            _: 2
                          }, 1032, ["onClick"])
                        ]),
                        createElementVNode("div", _hoisted_7, [
                          createVNode(_component_el_select, {
                            modelValue: item.minProductField,
                            "onUpdate:modelValue": ($event) => item.minProductField = $event,
                            placeholder: "最小值配置项",
                            filterable: "",
                            clearable: "",
                            style: { "width": "235px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(parsedConfig.value, (config) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: config.fieldName,
                                  label: config.fieldDesc,
                                  value: config.fieldName
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"]),
                          createVNode(_component_el_select, {
                            modelValue: item.maxProductField,
                            "onUpdate:modelValue": ($event) => item.maxProductField = $event,
                            placeholder: "最大值配置项",
                            filterable: "",
                            clearable: "",
                            style: { "width": "235px", "margin-right": "10px" }
                          }, {
                            default: withCtx(() => [
                              (openBlock(true), createElementBlock(Fragment, null, renderList(parsedConfig.value, (config) => {
                                return openBlock(), createBlock(_component_el_option, {
                                  key: config.fieldName,
                                  label: config.fieldDesc,
                                  value: config.fieldName
                                }, null, 8, ["label", "value"]);
                              }), 128))
                            ]),
                            _: 2
                          }, 1032, ["modelValue", "onUpdate:modelValue"])
                        ])
                      ]);
                    }), 128)),
                    createVNode(_component_el_button, {
                      type: "primary",
                      size: "small",
                      icon: unref(plus_default),
                      link: "",
                      onClick: addValidationConfig
                    }, {
                      default: withCtx(() => _cache[11] || (_cache[11] = [
                        createTextVNode(" 新增 ")
                      ])),
                      _: 1
                    }, 8, ["icon"])
                  ])
                ]),
                _: 1
              })
            ]),
            _: 1
          }, 8, ["model"])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
};
const ProdDialog = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-cc93f7f2"]]);
export {
  ProdDialog as default
};
