<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="t('EquipmentManagement.DvRepair.index')" type="index" width="100" />
       <!-- <el-table-column label="项目ID" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectId`" :rules="formRules.subjectId" class="mb-0px!">
            <el-input v-model="row.subjectId" placeholder="请输入项目ID" />
          </el-form-item>
        </template>
      </el-table-column> -->
      <el-table-column :label="t('EquipmentManagement.DvRepair.lineSubjectCode')" 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="t('EquipmentManagement.DvRepair.placeholderLineSubjectCode')"
              :disabled="props.lineMode !== 'edit'"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column :label="t('EquipmentManagement.DvRepair.lineSubjectName')" 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="t('EquipmentManagement.DvRepair.placeholderLineSubjectName')"
              :disabled="props.lineMode !== 'edit'"
            />
          </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-input v-model="row.subjectType" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column> -->
      <el-table-column :label="t('EquipmentManagement.DvRepair.lineSubjectContent')" 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="t('EquipmentManagement.DvRepair.placeholderLineSubjectContent')"
              :disabled="props.lineMode !== 'edit'"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.lineMode === 'repair' || props.lineMode === 'readonlyWithResult'"
        :label="t('EquipmentManagement.DvRepair.lineRemark')"
        min-width="180"
      >
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input
              v-model="row.remark"
              :placeholder="t('EquipmentManagement.DvRepair.placeholderLineRemark')"
              :disabled="props.lineMode !== 'repair'"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.lineMode === 'repair' || props.lineMode === 'readonlyWithResult'"
        :label="t('EquipmentManagement.DvRepair.lineResult')"
        min-width="160"
      >
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.result`" :rules="formRules.result" class="mb-0px!">
            <el-radio-group v-model="row.result" :disabled="props.lineMode !== 'repair'">
              <el-radio :value="1">{{ t('EquipmentManagement.DvRepair.lineResultOk') }}</el-radio>
              <el-radio :value="2">{{ t('EquipmentManagement.DvRepair.lineResultNg') }}</el-radio>
            </el-radio-group>
          </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}.malfunction`" :rules="formRules.malfunction" class="mb-0px!">
            <el-input v-model="row.malfunction" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="故障描述资源" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.malfunctionUrl`" :rules="formRules.malfunctionUrl" class="mb-0px!">
            <el-input v-model="row.malfunctionUrl" placeholder="" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="维修情况" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.repairDes`" :rules="formRules.repairDes" class="mb-0px!">
            <el-input v-model="row.repairDes" 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 { DvRepairApi } from '@/api/mes/dvrepair'

const props = defineProps<{
  repairId: undefined
  lineMode?: 'edit' | 'repair' | 'readonlyWithResult'
}>()
const { t } = useI18n()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  repairId: [{ required: true, message: t('EquipmentManagement.DvRepair.validatorLineRepairIdRequired'), trigger: 'blur' }],
  subjectId: [{ required: true, message: t('EquipmentManagement.DvRepair.validatorLineSubjectIdRequired'), trigger: 'blur' }],
  subjectCode: [{ required: true, message: t('EquipmentManagement.DvRepair.validatorLineSubjectCodeRequired'), trigger: 'blur' }],
  subjectContent: [],
  remark: [
    {
      validator: (_: any, value: any, callback: any) => {
        if (props.lineMode === 'repair' && !value) {
          callback(new Error(t('EquipmentManagement.DvRepair.validatorLineRemarkRequired')))
          return
        }
        callback()
      },
      trigger: 'blur',
    },
  ],
  result: [
    {
      validator: (_: any, value: any, callback: any) => {
        if (props.lineMode === 'repair' && (value === undefined || value === null || value === '')) {
          callback(new Error(t('EquipmentManagement.DvRepair.validatorLineResultRequired')))
          return
        }
        callback()
      },
      trigger: 'change',
    },
  ],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.repairId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await DvRepairApi.getDvRepairLineListByRepairId(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,
    repairResult: 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>
