<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="项目编码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectCode`" :rules="formRules.subjectCode" class="mb-0px!">
            <el-input v-model="row.subjectCode" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectName`" :rules="formRules.subjectName" class="mb-0px!">
            <el-input v-model="row.subjectName" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="项目类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectType`" :rules="formRules.subjectType" class="mb-0px!">
            <el-select v-model="row.subjectType" placeholder="">
                <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="项目内容" min-width="400">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectContent`" :rules="formRules.subjectContent" class="mb-0px!">
            <el-input type="textarea" v-model="row.subjectContent" placeholder="请输入项目名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="标准" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectStandard`" :rules="formRules.subjectStandard" class="mb-0px!">
            <el-input v-model="row.subjectStandard" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加维保计划项目</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { DvCheckApi } from '@/api/mes/dvcheck'

const props = defineProps<{
  planId: undefined // 计划ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  planId: [{ required: true, message: '计划ID不能为空', trigger: 'blur' }],
  subjectId: [{ required: true, message: '设备ID不能为空', trigger: 'blur' }],
  subjectCode: [{ required: true, message: '项目编码不能为空', trigger: 'blur' }],
  subjectContent: [{ required: true, message: '项目内容不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.planId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await DvCheckApi.getDvCheckSubjectListByPlanId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    planId: undefined,
    subjectId: undefined,
    subjectCode: undefined,
    subjectName: undefined,
    subjectType: undefined,
    subjectContent: undefined,
    subjectStandard: undefined,
    remark: undefined
  }
  row.planId = props.planId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

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

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

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