<script setup lang="ts">
import type {IAppBaseErrorInfo} from "@/model/table/IAppBaseErrorInfo.ts";
import {ref} from "vue";
import ErrorBaseEquipmentTypeFormatter from "@/plugin/formatter/ErrorBaseEquipmentTypeFormatter.ts";
import ErrorLevelFormatter from "@/plugin/formatter/ErrorLevelFormatter.ts";
import ErrorTypeFormatter from "@/plugin/formatter/ErrorTypeFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import BaseErrorApi from "@/api/baseError.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => { modelValue.value = false; };
const errorBaseEquipmentTypeFormatter = new ErrorBaseEquipmentTypeFormatter(); // 设备报警基础资料设备型号格式化
const errorLevelFormatter = new ErrorLevelFormatter(); // 设备报警基础资料报警级别格式化
const errorTypeFormatter = new ErrorTypeFormatter(); // 设备报警基础资料报警类型格式化
const formData = ref<IAppBaseErrorInfo>({});

// 添加
const add = () => {
  if (!formData.value.equipmentType) {
    MessageUtils.warningMessage('请选择设备类型');
    return;
  }
  if (!formData.value.errCode || formData.value.errCode.trim().length == 0) {
    MessageUtils.warningMessage('请填写报警编号');
    return;
  }
  if (!formData.value.errMsg || formData.value.errMsg.trim().length == 0) {
    MessageUtils.warningMessage('请填写报警信息');
    return;
  }
  if (!formData.value.errLevel) {
    MessageUtils.warningMessage('请选择报警级别');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加上述数据？','添加确认').then(()=>{
    const loadingInstance = MessageUtils.loading('添加中');
    BaseErrorApi.addBaseError(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("添加成功");
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.errMessageBox(response.msg);
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(error => {});
}

</script>

<template>
  <el-dialog v-model="modelValue" title="新增基础报警信息资料" @close="close" :close-on-click-modal="false">
    <el-scrollbar style="height:50vh;">
      <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right" style="width: calc(100% - 10px)">
        <el-form-item label="设备类型:" required>
          <el-select v-model="formData.equipmentType"  placeholder="请选择设备型号" clearable>
            <el-option v-for="item in errorBaseEquipmentTypeFormatter.equipmentType" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报警编号:" required>
          <el-input v-model="formData.errCode" placeholder="请填写报警编号"></el-input>
        </el-form-item>
        <el-form-item label="报警类型:" required>
          <el-select v-model="formData.errType"  placeholder="请选择报警类型" clearable>
            <el-option v-for="item in errorTypeFormatter.errorType" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报警级别:" required>
          <el-select v-model="formData.errLevel"  placeholder="请选择报警级别" clearable>
            <el-option v-for="item in errorLevelFormatter.errorLevel" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报警信息:" required>
          <el-input v-model="formData.errMsg" placeholder="请填写报警信息" type="textarea" :maxlength="100" :rows="5" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="建议:">
          <el-input v-model="formData.suggest" placeholder="请填写建议" type="textarea" :maxlength="100" :rows="5" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="formData.remark" placeholder="请填写备注"></el-input>
        </el-form-item>
      </el-form>
    </el-scrollbar>
    <template #footer>
      <el-button-group>
        <el-button type="primary" @click="add">确定添加</el-button>
        <el-button type="danger" @click="close">关闭窗口</el-button>
      </el-button-group>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>