<template>
  <el-form ref="bmRef" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="模型名称" prop="modelName">
      <el-input v-model="form.modelName" placeholder="请输入模型名称" />
    </el-form-item>
<!--    <el-form-item label="业务类型" prop="modelType">-->
<!--      <el-input v-model="form.modelType" placeholder="请输入业务类型" />-->
<!--    </el-form-item>-->
    <el-form-item label="备注">
      <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :autosize="{maxRows:6,minRows:3}" maxlength="300" show-word-limit/>
    </el-form-item>
  </el-form>
</template>
<script setup lang="ts">
import { addBm, updateBm,getBm } from "@/api/hl/bm";
const {proxy} = getCurrentInstance()
const bmRef=ref(null)
const emit=defineEmits(["success","changeModelId"])
const data=reactive({
  form:{
    modelName:"",
    modelType:"",
    remark:""
  },
  rules:{
    modelName:[
      {required:true,message:"请输入模型名称",trigger:"blur"}
    ],
  }
})
const { form, rules} = toRefs(data)

const submitForm = () => {
  proxy.$refs["bmRef"].validate(valid => {
    if (valid) {
      handleSubmit()
    }
  });
}

async function handleSubmit(){
  let modelId=0
  if (form.value.modelId != null) {
    await updateBm(form.value);
  } else {
    const res =  await addBm(form.value)
    modelId = res.data.modelId
  }
  emit("nextStep",modelId)

}

const reset=()=>{
  form.value = {
    modelName: null,
    modelType: null,
    remark:""
  };
  proxy.resetForm("bmRef");
}

const handleUpdate=(row)=>{
  reset()
  if(row){
    getBm(row.modelId).then(res=>{
      form.value.modelName = res.data.modelName
      form.value.modelType = res.data.modelType
      form.value.remark = res.data.remark
      form.value.modelId = res.data.modelId
    })
  }
}

defineExpose({submitForm,handleUpdate})

</script>
<style scoped lang="scss">
</style>
