<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item :label="t('info.deviceClass')" prop="deviceType" >
            <el-tree-select
              v-model="formData.deviceType"
              :data="productClassifyList"
              :props="defaultProps"
              check-strictly
              node-key="id"
              :placeholder="t('deviceForm.category')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item :label="t('faultForm.faultImpact')" prop="failureInfluence">
          <el-input v-model="formData.failureInfluence" :placeholder="t('faultForm.faultImpactHolder')" />
        </el-form-item>
          </el-col>
        <el-col :span="12">
        <el-form-item :label="t('faultForm.faultySystem')" prop="failureSystem">
          <el-input v-model="formData.failureSystem" :placeholder="t('faultForm.faultySystemHolder')" />
        </el-form-item>
          </el-col>
        <el-col :span="12">
          <el-form-item :label="t('iotMaintain.solution')" prop="solutions">
            <el-input v-model="formData.solutions" :placeholder="t('iotMaintain.soHolder')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item :label="t('info.description')" prop="description">
          <el-input v-model="formData.description" type="textarea" height="150px" />
        </el-form-item>
          </el-col>
        <el-col :span="24">
        <el-form-item :label="t('iotMaintain.remark')" prop="remark">
          <el-input v-model="formData.remark" type="textarea" :placeholder="t('iotMaintain.remarkHolder')" />
        </el-form-item>
          </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('iotMaintain.ok') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('iotMaintain.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { IotInformationDbApi, IotInformationDbVO } from '@/api/pms/information'
import {defaultProps, handleTree} from "@/utils/tree";
import * as ProductClassifyApi from "@/api/pms/productclassify";
import {DeviceAttrModelApi} from "@/api/pms/deviceattrmodel";

/** 故障知识库 表单 */
defineOptions({ name: 'IotInformationDbForm' })

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

const productClassifyList = ref<Tree[]>([]) // 树形结构
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deviceType: undefined,
  failureInfluence: undefined,
  failureSystem: undefined,
  description: undefined,
  solutions: undefined,
  remark: undefined,
  processInstanceId: undefined,
  auditStatus: undefined,
})
const formRules = reactive({
  deviceType: [{ required: true, message: t('info.classHolder1'), trigger: 'change' }],
  failureInfluence: [{ required: true, message: t('info.faultyImpact'), trigger: 'change' }],
  solutions: [{ required: true, message: t('info.solution'), trigger: 'change' }],
})
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 IotInformationDbApi.getIotInformationDb(id)
    } finally {
      formLoading.value = false
    }
  }
  productClassifyList.value = handleTree(
    await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  )
}
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 IotInformationDbVO
    if (formType.value === 'create') {
      await IotInformationDbApi.createIotInformationDb(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotInformationDbApi.updateIotInformationDb(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceType: undefined,
    failureInfluence: undefined,
    failureSystem: undefined,
    description: undefined,
    solutions: undefined,
    remark: undefined,
    processInstanceId: undefined,
    auditStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>
