import "../../../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                                */
/* empty css                                                                        */
/* empty css                                                                     */
import "../../../../../../node_modules/element-plus/theme-chalk/el-tooltip.css.mjs";
/* empty css                                                                   */
import { computed, ref, createBlock, createElementBlock, openBlock, unref, normalizeClass, createSlots, withCtx, createCommentVNode, resolveDynamicComponent, mergeProps, withModifiers, createTextVNode, toDisplayString, createElementVNode } from "vue";
import { debounce } from "../form/debounce.mjs";
import { SubTitle } from "../subTitle/index.mjs";
import { H2 } from "../h2/index.mjs";
import { SubForm } from "../subForm/index.mjs";
import { SubTable } from "../subTable/index.mjs";
import { ApprovalHistory } from "../../bus/approvalHistory/index.mjs";
import { Document } from "../../bus/document/index.mjs";
import { Contract } from "../../bus/contract/index.mjs";
import * as index$1 from "../index.mjs";
import * as index from "../../bus/index.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 { ElLink } from "../../../../../../node_modules/element-plus/es/components/link/index.mjs";
import { ElTooltip } from "../../../../../../node_modules/element-plus/es/components/tooltip/index.mjs";
const _hoisted_1 = {
  key: 0,
  class: "iconfont icon-iconZheng credit-icon"
};
const _hoisted_2 = {
  key: 1,
  class: "iconfont icon-iconJian regulation-icon"
};
const _hoisted_3 = { key: 3 };
const _sfc_main = /* @__PURE__ */ Object.assign({
  inheritAttrs: false
}, {
  __name: "FormItem",
  props: {
    config: {
      type: Object,
      required: true
    },
    localConfig: {
      type: Object,
      required: true
    },
    modelValue: {
      type: [String, Number, Boolean, Array, Object],
      default: ""
    },
    error: {
      type: String,
      default: ""
    },
    formRef: {
      type: Object,
      default: null
    },
    formMode: {
      type: String,
      default: ""
    }
  },
  emits: ["update:modelValue", "change", "labelLink"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    function handleChange(config, val) {
      emit("change", config, val);
    }
    const componentType = computed(() => {
      const key = props.config.fieldType.charAt(0).toUpperCase() + props.config.fieldType.slice(1);
      return { ...index$1, ...index }[key];
    });
    const componentProps = computed(() => {
      const fieldProps = { ...props.config };
      if (fieldProps.fieldType !== "subTitle" && fieldProps.fieldType !== "h2") {
        fieldProps.placeholder = fieldProps.placeholder || "";
        fieldProps.disabled = fieldProps.disabled || false;
        fieldProps.readonly = fieldProps.readOnly || false;
      }
      if (fieldProps.fieldType === "subTitle") {
        fieldProps.content = fieldProps.label;
      }
      if (fieldProps.fieldType === "h2") {
        fieldProps.text = fieldProps.label;
      }
      delete fieldProps.id;
      return fieldProps;
    });
    const debouncedValidate = debounce((fieldName) => {
      if (!props.formRef || !fieldName) return;
      props.formRef.validateField(fieldName, (errorMessage) => {
        if (!errorMessage) {
          console.log(`字段 ${fieldName} 校验通过，错误已清除`);
        }
      });
    }, 300);
    const innerComponentRef = ref(null);
    const compValue = computed({
      get: () => props.modelValue,
      set: (val) => {
        emit("update:modelValue", val);
        if (props.error && props.config.fieldName && props.formRef) {
          debouncedValidate(props.config.fieldName);
        }
      }
    });
    function handleLabelLink() {
      emit("labelLink", props.config);
    }
    const getInnerComponentRef = () => {
      return innerComponentRef.value;
    };
    __expose({
      config: props.config,
      getInnerComponentRef
    });
    return (_ctx, _cache) => {
      const _component_el_link = ElLink;
      const _component_el_tooltip = ElTooltip;
      const _component_el_form_item = ElFormItem;
      return __props.config.fieldType == "h2" ? (openBlock(), createBlock(unref(H2), {
        key: 0,
        config: __props.config
      }, null, 8, ["config"])) : __props.config.fieldType == "subTitle" ? (openBlock(), createBlock(unref(SubTitle), {
        key: 1,
        config: __props.config
      }, null, 8, ["config"])) : __props.config.fieldType == "subForm" ? (openBlock(), createBlock(unref(SubForm), {
        key: 2,
        ref_key: "innerComponentRef",
        ref: innerComponentRef,
        config: __props.config,
        localConfig: __props.localConfig,
        formMode: __props.formMode
      }, null, 8, ["config", "localConfig", "formMode"])) : __props.config.fieldType == "subTable" ? (openBlock(), createBlock(unref(SubTable), {
        key: 3,
        ref_key: "innerComponentRef",
        ref: innerComponentRef,
        config: __props.config,
        localConfig: __props.localConfig,
        formMode: __props.formMode
      }, null, 8, ["config", "localConfig", "formMode"])) : __props.config.fieldType == "approvalHistory" ? (openBlock(), createBlock(unref(ApprovalHistory), {
        key: 4,
        ref_key: "innerComponentRef",
        ref: innerComponentRef,
        config: __props.config,
        localConfig: __props.localConfig,
        formMode: __props.formMode
      }, null, 8, ["config", "localConfig", "formMode"])) : __props.config.fieldType == "document" ? (openBlock(), createBlock(unref(Document), {
        key: 5,
        ref_key: "innerComponentRef",
        ref: innerComponentRef,
        config: __props.config,
        localConfig: __props.localConfig,
        formMode: __props.formMode
      }, null, 8, ["config", "localConfig", "formMode"])) : __props.config.fieldType == "contract" ? (openBlock(), createBlock(unref(Contract), {
        key: 6,
        ref_key: "innerComponentRef",
        ref: innerComponentRef,
        config: __props.config,
        localConfig: __props.localConfig,
        formMode: __props.formMode
      }, null, 8, ["config", "localConfig", "formMode"])) : __props.config.fieldType == "blank" || __props.config.fieldType == "divider" ? (openBlock(), createElementBlock("div", {
        key: 7,
        class: normalizeClass(__props.config.fieldType)
      }, null, 2)) : (openBlock(), createBlock(_component_el_form_item, {
        key: 8,
        prop: __props.config.fieldName,
        required: __props.config.required,
        error: __props.error,
        "label-width": 160,
        id: `comp_${__props.config.id}`
      }, createSlots({
        default: withCtx(() => [
          __props.config.labelMode != "label" ? (openBlock(), createBlock(resolveDynamicComponent(componentType.value), mergeProps({
            key: 0,
            ref_key: "innerComponentRef",
            ref: innerComponentRef,
            modelValue: compValue.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => compValue.value = $event)
          }, componentProps.value, {
            config: __props.config,
            formMode: __props.formMode,
            onChange: _cache[1] || (_cache[1] = (value) => {
              handleChange(__props.config, value);
            })
          }), null, 16, ["modelValue", "config", "formMode"])) : createCommentVNode("", true)
        ]),
        _: 2
      }, [
        __props.config.labelMode != "input" ? {
          name: "label",
          fn: withCtx(() => {
            var _a;
            return [
              __props.config.credit ? (openBlock(), createElementBlock("i", _hoisted_1)) : createCommentVNode("", true),
              __props.config.regulation ? (openBlock(), createElementBlock("i", _hoisted_2)) : createCommentVNode("", true),
              __props.config.labelLink ? (openBlock(), createBlock(_component_el_link, {
                key: 2,
                type: "primary",
                style: { "pointer-events": "all" },
                onClick: withModifiers(handleLabelLink, ["prevent"])
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(__props.config.label), 1)
                ]),
                _: 1
              })) : (openBlock(), createElementBlock("span", _hoisted_3, toDisplayString(__props.config.label), 1)),
              ((_a = __props.config.tooltip) == null ? void 0 : _a.trim()) ? (openBlock(), createBlock(_component_el_tooltip, {
                key: 4,
                content: __props.config.tooltip,
                placement: "top"
              }, {
                default: withCtx(() => _cache[2] || (_cache[2] = [
                  createElementVNode("i", { class: "iconfont icon-tooltip info-icon" }, null, -1)
                ])),
                _: 1
              }, 8, ["content"])) : createCommentVNode("", true)
            ];
          }),
          key: "0"
        } : void 0
      ]), 1032, ["prop", "required", "error", "id"]));
    };
  }
});
const _FormItem = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-ebc3ff2b"]]);
export {
  _FormItem as default
};
