<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="故障编码" prop="failureCode">
        <el-input v-model="formData.failureCode" placeholder="请输入故障编码" />
      </el-form-item>
      <el-form-item label="故障名称" prop="failureName">
        <el-input v-model="formData.failureName" placeholder="请输入故障名称" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          class="!w-200px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PMS_MAIN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否停机" prop="ifStop">
        <el-radio-group v-model="formData.ifStop">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="故障时间" prop="failureTime">
        <el-date-picker
          v-model="formData.failureTime"
          type="date"
          value-format="x"
          placeholder="选择故障时间"
        />
      </el-form-item>
      <el-form-item label="故障影响" prop="failureInfluence">
        <el-input v-model="formData.failureInfluence" placeholder="请输入故障影响" />
      </el-form-item>
      <el-form-item label="故障系统" prop="failureSystem">
        <el-input v-model="formData.failureSystem" placeholder="请输入故障系统" />
      </el-form-item>
      <el-form-item label="故障描述" prop="description">
        <Editor v-model="formData.description" height="150px" />
      </el-form-item>
      <el-form-item label="图片" prop="pic">
        <el-input v-model="formData.pic" placeholder="请输入图片" />
      </el-form-item>
      <el-form-item label="解决办法" prop="solution">
        <el-input v-model="formData.solution" placeholder="请输入解决办法" />
      </el-form-item>
      <el-form-item label="维修开始时间" prop="maintainStartTime">
        <el-date-picker
          v-model="formData.maintainStartTime"
          type="date"
          value-format="x"
          placeholder="选择维修开始时间"
        />
      </el-form-item>
      <el-form-item label="维修结束时间" prop="maintainEndTime">
        <el-date-picker
          v-model="formData.maintainEndTime"
          type="date"
          value-format="x"
          placeholder="选择维修结束时间"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="formData.deviceName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="流程实例id" prop="processInstanceId">
        <el-input v-model="formData.processInstanceId" placeholder="请输入流程实例id" />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入部门id" />
      </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 { IotMaintainApi, IotMaintainVO } from '@/api/pms/maintain'
import {DICT_TYPE, getBoolDictOptions, getStrDictOptions} from "@/utils/dict";

/** 维修工单 表单 */
defineOptions({ name: 'IotMaintainForm' })

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,
  failureCode: undefined,
  failureName: undefined,
  deviceId: undefined,
  status: undefined,
  ifStop: undefined,
  failureTime: undefined,
  failureInfluence: undefined,
  failureSystem: undefined,
  description: undefined,
  pic: undefined,
  solution: undefined,
  maintainStartTime: undefined,
  maintainEndTime: undefined,
  remark: undefined,
  deviceName: undefined,
  processInstanceId: undefined,
  auditStatus: undefined,
  deptId: undefined,
})
const formRules = reactive({
  failureCode: [{ required: true, message: '故障编码不能为空', trigger: 'blur' }],
  failureName: [{ required: true, message: '故障名称不能为空', trigger: 'blur' }],
  deviceId: [{ required: true, message: '设备id不能为空', trigger: 'blur' }],
  status: [{ 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 IotMaintainApi.getIotMaintain(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 IotMaintainVO
    if (formType.value === 'create') {
      await IotMaintainApi.createIotMaintain(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotMaintainApi.updateIotMaintain(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    failureCode: undefined,
    failureName: undefined,
    deviceId: undefined,
    status: undefined,
    ifStop: undefined,
    failureTime: undefined,
    failureInfluence: undefined,
    failureSystem: undefined,
    description: undefined,
    pic: undefined,
    solution: undefined,
    maintainStartTime: undefined,
    maintainEndTime: undefined,
    remark: undefined,
    deviceName: undefined,
    processInstanceId: undefined,
    auditStatus: undefined,
    deptId: undefined,
  }
  formRef.value?.resetFields()
}
</script>
