<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="保养任务ID" prop="taskId">
        <el-input v-model="formData.taskId" placeholder="请输入保养任务ID" />
      </el-form-item>
      <el-form-item label="任务编号" prop="taskNumber">
        <el-input v-model="formData.taskNumber" placeholder="请输入任务编号" />
      </el-form-item>
      <el-form-item label="模块" prop="module">
        <el-input v-model="formData.module" placeholder="请输入模块" />
      </el-form-item>
      <el-form-item label="项目编号" prop="itemNumber">
        <el-input v-model="formData.itemNumber" placeholder="请输入项目编号" />
      </el-form-item>
      <el-form-item label="项目名称" prop="itemName">
        <el-input v-model="formData.itemName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="项目内容" prop="itemContent">
        <Editor v-model="formData.itemContent" height="150px" />
      </el-form-item>
      <el-form-item label="参考附件" prop="referenceAttachmentUrls">
        <el-input v-model="formData.referenceAttachmentUrls" placeholder="请输入参考附件" />
      </el-form-item>
      <el-form-item label="标准值" prop="standardValue">
        <el-input v-model="formData.standardValue" placeholder="请输入标准值" />
      </el-form-item>
      <el-form-item label="结果值" prop="actualValue">
        <el-input v-model="formData.actualValue" placeholder="请输入结果值" />
      </el-form-item>
      <el-form-item label="是否异常" prop="isAbnormal">
        <el-radio-group v-model="formData.isAbnormal">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="异常是否处理" prop="abnormalHandled">
        <el-radio-group v-model="formData.abnormalHandled">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="处理措施" prop="handlingMeasures">
        <el-input v-model="formData.handlingMeasures" placeholder="请输入处理措施" />
      </el-form-item>
      <el-form-item label="项目说明" prop="itemDescription">
        <Editor v-model="formData.itemDescription" height="150px" />
      </el-form-item>
      <el-form-item label="保养图片" prop="maintenanceAttachmentUrls">
        <el-input v-model="formData.maintenanceAttachmentUrls" placeholder="请输入保养图片" />
      </el-form-item>
      <el-form-item label="保养备注" prop="maintenanceNotes">
        <el-input v-model="formData.maintenanceNotes" placeholder="请输入保养备注" />
      </el-form-item>
      <el-form-item label="验收结果" prop="inspectionResult">
        <el-input v-model="formData.inspectionResult" placeholder="请输入验收结果" />
      </el-form-item>
      <el-form-item label="验收说明" prop="inspectionNotes">
        <el-input v-model="formData.inspectionNotes" placeholder="请输入验收说明" />
      </el-form-item>
      <el-form-item label="验收图片" prop="inspectionAttachmentUrls">
        <el-input v-model="formData.inspectionAttachmentUrls" placeholder="请输入验收图片" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MaintenanceTaskItemsApi, MaintenanceTaskItems } from '@/api/device/maintenancetaskitems'

/** 设备保养任务项目 表单 */
defineOptions({ name: 'MaintenanceTaskItemsForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  taskId: undefined,
  taskNumber: undefined,
  module: undefined,
  itemNumber: undefined,
  itemName: undefined,
  itemContent: undefined,
  referenceAttachmentUrls: undefined,
  standardValue: undefined,
  actualValue: undefined,
  isAbnormal: undefined,
  abnormalHandled: undefined,
  handlingMeasures: undefined,
  itemDescription: undefined,
  maintenanceAttachmentUrls: undefined,
  maintenanceNotes: undefined,
  inspectionResult: undefined,
  inspectionNotes: undefined,
  inspectionAttachmentUrls: undefined,
})
const formRules = reactive({
  taskId: [{ required: true, message: '保养任务ID不能为空', trigger: 'blur' }],
  taskNumber: [{ required: true, message: '任务编号不能为空', trigger: 'blur' }],
  module: [{ required: true, message: '模块不能为空', trigger: 'blur' }],
  itemNumber: [{ required: true, message: '项目编号不能为空', trigger: 'blur' }],
  itemName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
  itemContent: [{ required: true, message: '项目内容不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MaintenanceTaskItemsApi.getMaintenanceTaskItems(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MaintenanceTaskItems
    if (formType.value === 'create') {
      await MaintenanceTaskItemsApi.createMaintenanceTaskItems(data)
      message.success(t('common.createSuccess'))
    } else {
      await MaintenanceTaskItemsApi.updateMaintenanceTaskItems(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    taskId: undefined,
    taskNumber: undefined,
    module: undefined,
    itemNumber: undefined,
    itemName: undefined,
    itemContent: undefined,
    referenceAttachmentUrls: undefined,
    standardValue: undefined,
    actualValue: undefined,
    isAbnormal: undefined,
    abnormalHandled: undefined,
    handlingMeasures: undefined,
    itemDescription: undefined,
    maintenanceAttachmentUrls: undefined,
    maintenanceNotes: undefined,
    inspectionResult: undefined,
    inspectionNotes: undefined,
    inspectionAttachmentUrls: undefined,
  }
  formRef.value?.resetFields()
}
</script>