<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('MoldManagement.MoldRepairLine.index')" type="index" width="100" />
      <el-table-column :label="t('MoldManagement.MoldRepairLine.subjectCode')" min-width="180">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectCode`" :rules="formRules.subjectCode" class="mb-0px!">
            <el-input
              v-model="row.subjectCode"
              :placeholder="t('MoldManagement.MoldRepairLine.placeholderSubjectCode')"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column :label="t('MoldManagement.MoldRepairLine.subjectName')" min-width="180">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.subjectName`" :rules="formRules.subjectName" class="mb-0px!">
            <el-input
              v-model="row.subjectName"
              :placeholder="t('MoldManagement.MoldRepairLine.placeholderSubjectName')"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column :label="t('MoldManagement.MoldRepairLine.subjectContent')" min-width="320">
        <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('MoldManagement.MoldRepairLine.placeholderSubjectContent')"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.lineMode === 'repair' || props.lineMode === 'readonlyWithResult'"
        :label="t('MoldManagement.MoldRepairLine.remark')"
        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('MoldManagement.MoldRepairLine.placeholderRemark')"
              :disabled="props.lineMode !== 'repair'"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.lineMode === 'repair' || props.lineMode === 'readonlyWithResult'"
        :label="t('MoldManagement.MoldRepairLine.result')"
        min-width="170"
      >
        <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('MoldManagement.MoldRepairLine.resultOk') }}</el-radio>
              <el-radio :value="2">{{ t('MoldManagement.MoldRepairLine.resultNg') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ {{ t('MoldManagement.MoldRepairLine.addLine') }}</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { MoldRepairApi } from '@/api/mold/moldrepair'

const { t } = useI18n()

const props = defineProps<{
  repairId: undefined
  lineMode?: 'edit' | 'repair' | 'readonlyWithResult'
}>()
const formLoading = ref(false)
const formData = ref([])
const formRules = reactive({
  repairId: [{ required: true, message: t('MoldManagement.MoldRepairLine.validatorRepairIdRequired'), trigger: 'blur' }],
  subjectId: [{ required: true, message: t('MoldManagement.MoldRepairLine.validatorSubjectIdRequired'), trigger: 'blur' }],
  subjectCode: [{ required: true, message: t('MoldManagement.MoldRepairLine.validatorSubjectCodeRequired'), trigger: 'blur' }],
  subjectContent: [{ required: true, message: t('MoldManagement.MoldRepairLine.validatorSubjectContentRequired'), trigger: 'blur' }],
  remark: [
    {
      validator: (_: any, value: any, callback: any) => {
        if (props.lineMode === 'repair' && !value) {
          callback(new Error(t('MoldManagement.MoldRepairLine.validatorRemarkRequired')))
          return
        }
        callback()
      },
      trigger: 'blur',
    },
  ],
  result: [
    {
      validator: (_: any, value: any, callback: any) => {
        if (props.lineMode === 'repair' && (value === undefined || value === null || value === '')) {
          callback(new Error(t('MoldManagement.MoldRepairLine.validatorResultRequired')))
          return
        }
        callback()
      },
      trigger: 'change',
    },
  ],
})
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,
    result: 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>
