<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    label-width="350px"
    v-loading="formLoading"
  >
     <el-form-item label="项目目的与意义" prop="purposeSignificance">
      <Editor v-model="formData.purposeSignificance" height="150px" />
    </el-form-item>
    <el-form-item label="项目所属领域国内外产业、技术的现状和发展趋势" prop="currentSituation">
      <Editor v-model="formData.currentSituation" height="150px" />
    </el-form-item>
    <el-form-item label="项目现有工作基础" prop="workFoundation">
      <Editor v-model="formData.workFoundation" height="150px" />
    </el-form-item>
    <el-form-item label="项目实施目标及考核指标-考核指标依据" prop="projectAccording">
      <Editor v-model="formData.projectAccording" height="150px" />
    </el-form-item>
    <el-form-item label="主要研究开发内容-主要研究内容" prop="researchContents">
      <Editor v-model="formData.researchContents" height="150px" />
    </el-form-item>
    <el-form-item label="主要研究开发内容-技术路线" prop="researchRoadmap">
      <Editor v-model="formData.researchRoadmap" height="150px" />
    </el-form-item>
    <el-form-item label="主要研究开发内容-创新性" prop="researchInnovation">
      <Editor v-model="formData.researchInnovation" height="150px" />
    </el-form-item>
    <el-form-item label="课题实施的风险分析" prop="riskAnalysis">
      <Editor v-model="formData.riskAnalysis" height="150px" />
    </el-form-item>
    <el-form-item label="预期主要成果形式、知识产权归属" prop="projectAchie">
      <Editor v-model="formData.projectAchie" height="150px" />
    </el-form-item>
  </el-form>
</template>
<script setup lang="ts">
import { MainApi } from '@/api/contract/contract'

const props = defineProps<{
  contractId: undefined // 任务书ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  contractId: [{ required: true, message: '任务书ID不能为空', trigger: 'blur' }],
  purposeSignificance: [{ required: true, message: '项目目的与意义不能为空', trigger: 'blur' }],
  currentSituation: [{ required: true, message: '项目所属领域国内外产业、技术的现状和发展趋势不能为空', trigger: 'blur' }],
  workFoundation: [{ required: true, message: '项目现有工作基础不能为空', trigger: 'blur' }],
  projectAccording: [{ required: true, message: '项目实施目标及考核指标-考核指标依据不能为空', trigger: 'blur' }],
  researchContents: [{ required: true, message: '主要研究开发内容-主要研究内容不能为空', trigger: 'blur' }],
  researchRoadmap: [{ required: true, message: '主要研究开发内容-技术路线不能为空', trigger: 'blur' }],
  researchInnovation: [{ required: true, message: '主要研究开发内容-创新性不能为空', trigger: 'blur' }],
  riskAnalysis: [{ required: true, message: '课题实施的风险分析不能为空', trigger: 'blur' }],
  projectAchie: [{ required: true, message: '预期主要成果形式、知识产权归属不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.contractId,
  async (val) => {
    // 1. 重置表单
    formData.value = {
      id: undefined,
      contractId: undefined,
      purposeSignificance: undefined,
      currentSituation: undefined,
      workFoundation: undefined,
      projectAccording: undefined,
      researchContents: undefined,
      researchRoadmap: undefined,
      researchInnovation: undefined,
      riskAnalysis: undefined,
      projectAchie: undefined,
    }
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      const data = await MainApi.getInfoByContractId(val)
      if (!data) {
        return
      }
      formData.value = data
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>