<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="设备ID" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.machineryId`" :rules="formRules.machineryId" class="mb-0px!">
            <el-input v-model="row.machineryId" placeholder="请输入设备ID" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="设备编码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.machineryCode`" :rules="formRules.machineryCode" class="mb-0px!">
            <el-input v-model="row.machineryCode" placeholder="请输入设备编码" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.machineryName`" :rules="formRules.machineryName" class="mb-0px!">
            <el-input v-model="row.machineryName" placeholder="请输入设备名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="品牌" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.machineryBrand`" :rules="formRules.machineryBrand" class="mb-0px!">
            <el-input v-model="row.machineryBrand" placeholder="请输入品牌" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="规格型号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.machinerySpec`" :rules="formRules.machinerySpec" class="mb-0px!">
            <el-input v-model="row.machinerySpec" 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' }],
  machineryId: [{ required: true, message: '设备ID不能为空', trigger: 'blur' }],
  machineryCode: [{ required: true, message: '设备编码不能为空', trigger: 'blur' }],
  machineryName: [{ 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.getDvCheckDeviceListByPlanId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    planId: undefined,
    machineryId: undefined,
    machineryCode: undefined,
    machineryName: undefined,
    machineryBrand: undefined,
    machinerySpec: 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>