<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="300">
        <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>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加维修项目</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { MoldRepairApi } from '@/api/mold/moldrepair'

const props = defineProps<{
  repairId: undefined
}>()
const formLoading = ref(false)
const formData = ref([])
const formRules = reactive({
  repairId: [{ 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()

watch(
  () => props.repairId,
  async (val) => {
    formData.value = []
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      formData.value = await MoldRepairApi.getMoldRepairLineListByRepairId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

const handleAdd = () => {
  const row = {
    id: undefined,
    repairId: undefined,
    subjectId: undefined,
    subjectCode: undefined,
    subjectName: undefined,
    subjectType: undefined,
    subjectContent: undefined,
    subjectStandard: undefined,
    malfunction: undefined,
    malfunctionUrl: undefined,
    repairDes: undefined,
    remark: undefined,
  }
  row.repairId = props.repairId
  formData.value.push(row)
}

const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

const validate = () => {
  return formRef.value.validate()
}

const getData = () => {
  return formData.value
}

const setData = (rows: any[]) => {
  formData.value = Array.isArray(rows) ? rows : []
}

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

