<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="报警设备" prop="alarmDeviceName">
              <el-input v-model="queryParams.alarmDeviceName" placeholder="请输入报警设备" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="报警类型" prop="alarmType">
              <el-select v-model="queryParams.alarmType" placeholder="请选择报警类型" clearable>
                <el-option v-for="item in alarmTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="报警等级" prop="alarmLevel">
              <el-input v-model="queryParams.alarmLevel" placeholder="请输入报警等级" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="报警设备类型" prop="deviceType">
              <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable>
                <el-option v-for="dict in device_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="处理状态" prop="unhandled">
              <el-select v-model="queryParams.unhandled" placeholder="请选择处理状态" clearable>
                <el-option label="未处理" value="1" />
                <el-option label="全部" value="" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">
          <right-toolbar v-model:show-search="showSearch" @query-table="getList" />
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="detailList">
        <el-table-column label="报警设备ID" prop="alarmDeviceId" min-width="130" />
        <el-table-column label="报警设备名称" prop="alarmDeviceName" min-width="150" />
        <el-table-column label="报警位置" prop="alarmPosition" min-width="220" show-overflow-tooltip />
        <el-table-column label="报警时间" prop="alarmTime" min-width="160" />
        <el-table-column label="报警等级" prop="alarmLevel" width="110" />
        <el-table-column label="报警类型" min-width="130">
          <template #default="scope">{{ formatAlarmType(scope.row.alarmType) }}</template>
        </el-table-column>
        <el-table-column label="报警明细" prop="alarmDetail" min-width="260" show-overflow-tooltip />
        <el-table-column label="处理状态" align="center" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.handleTime ? 'success' : 'danger'">{{ scope.row.handleTime ? '已处理' : '未处理' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理时间" prop="handleTime" min-width="160" />
        <el-table-column label="处理人" prop="handler" width="120" />
        <el-table-column label="处理信息" prop="handleInfo" min-width="220" show-overflow-tooltip />
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="AlarmHistory" lang="ts">
import { listAlarmDetail } from '@/api/ems/alarmRule';
import { AlarmDetailQuery, AlarmDetailVO } from '@/api/ems/alarmRule/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { device_type } = toRefs<any>(proxy?.useDict('device_type'));

const alarmTypeOptions = [
  { label: '阈值报警', value: '1' },
  { label: '多级阈值报警', value: '2' },
  { label: '持续时间报警', value: '3' },
  { label: '变化率报警', value: '4' },
  { label: '停机报警', value: '5' }
];
const alarmTypeMap = Object.fromEntries(alarmTypeOptions.map((item) => [item.value, item.label]));
const formatAlarmType = (value?: string | number) => alarmTypeMap[String(value || '')] || String(value || '');

const detailList = ref<AlarmDetailVO[]>([]);
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<AlarmDetailQuery>({
  pageNum: 1,
  pageSize: 10,
  alarmDeviceName: '',
  alarmType: '',
  alarmLevel: '',
  deviceType: undefined,
  unhandled: ''
});

const getList = async () => {
  loading.value = true;
  try {
    const res = await listAlarmDetail(queryParams.value);
    detailList.value = res.rows || [];
    total.value = res.total || 0;
  } finally {
    loading.value = false;
  }
};

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

onMounted(() => {
  getList();
});
</script>
