<script setup lang="ts">

import type {IErrorBaseSearch} from "@/interface/page/error/IErrorBaseSearch.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 type {IAppBaseErrorInfo} from "@/model/table/IAppBaseErrorInfo.ts";
import BaseErrorApi from "@/api/baseError.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {PageDataResponse} from "@/interface/api/PageDataResponse.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import AddBaseError from "@/components/page/baseError/AddBaseError.vue";
import ImportBaseError from "@/components/page/baseError/ImportBaseError.vue";
import BaseErrorDetail from "@/components/page/baseError/BaseErrorDetail.vue";

const searchParams = ref<IErrorBaseSearch>({}); // 搜索信息
const errorBaseEquipmentTypeFormatter = new ErrorBaseEquipmentTypeFormatter(); // 设备报警基础资料设备型号格式化
const errorLevelFormatter = new ErrorLevelFormatter(); // 设备报警基础资料报警级别格式化
const errorTypeFormatter = new ErrorTypeFormatter(); // 设备报警基础资料报警类型格式化
const pageSize = ref<number>(100); // 分页单页数据
const pageIndex = ref<number>(1); // 分页页码
const totalPages = ref<number | undefined>(0); // 总数据数
const tableData = ref<IAppBaseErrorInfo[]>([]); // 表格数据
const showAdd = ref<boolean>(false); // 是否展示添加
const showImport = ref<boolean>(false); // 是否展示导入
const showDetail = ref<boolean>(false); // 是否展示详情
const detailData = ref<IAppBaseErrorInfo>({}); // 详情数据

// 查询
const query = () => {
  const loadingInstance = MessageUtils.loading("正在查询");
  BaseErrorApi.queryBaseError(searchParams.value, pageIndex.value, pageSize.value).then(res => {
    const stringify = JSON.stringify(res.data);
    const response = JSON.parse(stringify) as AppServeDataResponse<PageDataResponse<IAppBaseErrorInfo>>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      tableData.value = response.data.data ?? [];
      totalPages.value = response.data.totalCount;
      MessageUtils.successMessage("查询成功");
      return;
    }
    MessageUtils.errMessageBox(response.msg, '错误')
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  });
};
// 重置查询参数
const resetInput = () => {
  searchParams.value = {};
};
// 添加
const add = () => {
  showAdd.value = true;
};
// 批量导入
const addImport = () => {
  showImport.value = true;
};
// 分页
const paginationChange = () => {
  query();
};
// 编辑
const handleEdit = (index: number, row: any) => {
  detailData.value = {
    id: row.id,
    equipmentType: row.equipmentType,
    errCode: row.errCode,
    errLevel: row.errLevel,
    errType: row.errType,
    errMsg: row.errMsg,
    suggest: row.suggest,
    remark: row.remark,
  };
  showDetail.value = true;
};
</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>设备报警基础资料</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="searchParams" label-position="top" label-width="150px" inline>
          <el-form-item label="设备型号：" style="width: 250px;">
            <el-select v-model="searchParams.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="报警编号：" style="width: 250px;">
            <el-input v-model="searchParams.code"  placeholder="请输入报警编号" clearable></el-input>
          </el-form-item>
          <el-form-item label="报警类型：" style="width: 250px;">
            <el-select v-model="searchParams.errorType"  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="报警级别：" style="width: 250px;">
            <el-select v-model="searchParams.errorLevel"  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="报警信息：" style="width: 250px;">
            <el-input v-model="searchParams.message" placeholder="支持模糊查询" clearable></el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row style="width: 100%">
        <el-button-group style="width: 100%;margin-left: 10px; margin-bottom: 10px">
          <el-button type="primary" @click="query">查询/刷新</el-button>
          <el-button type="warning" @click="resetInput">重置查询参数</el-button>
          <el-button type="success" @click="add">添加资料</el-button>
          <el-button type="info" @click="addImport">批量导入</el-button>
        </el-button-group>
      </el-row>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="tableData" stripe border style="width: 100%" max-height="calc(100vh - 320px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip fixed="left"/>
        <el-table-column prop="id" label="ID号" width="150" align="center" show-overflow-tooltip />
        <el-table-column prop="equipmentType" label="设备类型" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="errorBaseEquipmentTypeFormatter.errorBaseEquipmentTypeFormatter(scope.row.equipmentType).type">
              {{errorBaseEquipmentTypeFormatter.errorBaseEquipmentTypeFormatter(scope.row.equipmentType).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="errCode" label="报警代码" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="errLevel" label="报警级别" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="errorLevelFormatter.errorLevelTagStyleFormatter(scope.row.errLevel).type" effect="dark">
              {{errorLevelFormatter.errorLevelTagStyleFormatter(scope.row.errLevel).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="errType" label="报警类型" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="errorTypeFormatter.errorTypeTagStyleFormatter(scope.row.errType).type">
              {{errorTypeFormatter.errorTypeTagStyleFormatter(scope.row.errType).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="errMsg" label="报警信息" width="150" align="center" show-overflow-tooltip/>
        <el-table-column prop="suggest" label="建议" width="180" align="center" show-overflow-tooltip/>
        <el-table-column prop="remark" label="备注" min-width="150" align="left" show-overflow-tooltip/>
        <el-table-column label="操作" width="80" align="center" fixed="right">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="margin: 10px"
                     background
                     v-model:current-page="pageIndex"
                     v-model:page-size="pageSize"
                     :page-sizes="[10, 100, 200, 300]"
                     size="small"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="totalPages"
                     @size-change="paginationChange"
                     @current-change="paginationChange"
      />
    </el-row>
  </div>
<!--  添加信息框-->
  <AddBaseError v-model="showAdd" @reLoadingTableData="query"/>
<!--  批量导入基础报警信息-->
  <ImportBaseError v-model="showImport" @reLoadingTableData="query"/>
<!--  基础资料详情-->
  <BaseErrorDetail v-model="showDetail" @reLoadingTableData="query" :form-data="detailData"/>
</template>

<style scoped>

</style>