""" @author: yy @desc：部件新增 @date: 2025-07-25 01:17:31 """
<template>
  <MOSTY.Modal v-if="visible" v-model:visible="visible" v-model:loading="loading" title="部件新增" @ok="handleSave">
    <MainForm ref="formRef" :editId="editId" :type="type" />
  </MOSTY.Modal>
</template>
<script setup>
import { computed, ref, watch, nextTick } from "vue"
import * as MOSTY from "@/components/MyComponents/index"
import { ElMessage } from "element-plus"
import MainForm from "./mainForm.vue"

import { getApi2, postApi2 } from "@/api/tobAcco_api.js"

const emit = defineEmits(["update:visible", "success"])
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  // 主配置ID
  id: {
    type: String,
    default: ""
  },
  // 编辑时，请求详情的ID
  editId: {
    type: String,
    default: ""
  },
  type: {
    type: String,
    default: "add"
  }
})

const visible = computed({
  get() {
    return props.visible
  },
  set(val) {
    emit("update:visible", val)
  }
})

const loading = ref(false)
const formRef = ref(null)

const handleSave = async () => {
  try {
    loading.value = true
    console.log(formRef.value)
    await formRef.value?.validate()
    await postApi2({ ...formRef.value.getForm() }, "/mosty-wljc/zhjtxt/replace")
    ElMessage.success("保存成功")
    visible.value = false
    emit("success")
  } catch (error) {
    console.log("error", error)
  } finally {
    loading.value = false
  }
}
</script>
