<template>
  <div class="page">
    <section class="page-hero alarm-hero">
      <div>
        <h1>报警管理</h1>
        <p>聚焦待处理、处理中和高等级报警，支持快速闭环处理。</p>
      </div>
      <div class="alarm-stats">
        <span>当前 {{ alarms.length }}</span>
        <span>高危 {{ highRiskCount }}</span>
      </div>
    </section>
    <div class="toolbar">
      <div class="filter-group">
        <el-select v-model="status" placeholder="报警状态" clearable style="width: 180px" @change="handleFilterChange">
          <el-option label="待处理" value="OPEN" />
          <el-option label="处理中" value="PROCESSING" />
          <el-option label="已恢复" value="RESOLVED" />
          <el-option label="已忽略" value="IGNORED" />
        </el-select>
        <el-tree-select
          v-model="selectedRegionId"
          :data="regions"
          :props="{ label: 'name', children: 'children' }"
          node-key="id"
          check-strictly
          clearable
          placeholder="按区域筛选"
          style="width: 240px"
          @change="handleFilterChange"
        />
      </div>
    </div>
    <el-card class="modern-card" shadow="never">
      <el-table :data="alarms" v-loading="loading" class="modern-table">
        <el-table-column prop="title" label="报警标题" />
        <el-table-column prop="lamp.name" label="设备" />
        <el-table-column prop="lamp.deviceType" label="设备类型" width="120" />
        <el-table-column prop="region.name" label="区域" />
        <el-table-column label="等级" width="120">
          <template #default="{ row }">
            <el-tag :type="levelTag(row.level)" effect="dark">{{ alarmLevelText(row.level) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态"  width="120">
          <template #default="{ row }">
            <el-tag :type="row.status === 'RESOLVED' ? 'success' : row.status === 'PROCESSING' ? 'warning' : 'danger'">
              {{ alarmStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发生时间" width="180">
          <template #default="{ row }">{{ formatTime(row.occurredAt) }}</template>
        </el-table-column>
        <!-- <el-table-column label="操作" width="160">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="handleAlarm(row)">处理</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <el-pagination
        class="pagination"
        layout="total, sizes, prev, pager, next"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus';
import { computed, onMounted, ref } from 'vue';
import { alarmApi, regionApi } from '../../api/modules';
import { alarmLevelText, alarmStatusText } from '../../utils/labels';

const alarms = ref<any[]>([]);
const regions = ref<any[]>([]);
const status = ref('');
const selectedRegionId = ref('');
const loading = ref(false);
const page = ref(1);
const pageSize = ref(20);
const total = ref(0);
const highRiskCount = computed(() => alarms.value.filter((alarm) => ['HIGH', 'CRITICAL'].includes(alarm.level)).length);

async function load() {
  loading.value = true;
  try {
    const result = await alarmApi.list({
      status: status.value,
      regionId: selectedRegionId.value,
      page: page.value,
      pageSize: pageSize.value,
    });
    alarms.value = result.items ?? [];
    total.value = result.total ?? 0;
  } finally {
    loading.value = false;
  }
}

function handleFilterChange() {
  page.value = 1;
  void load();
}

function handlePageChange(value: number) {
  page.value = value;
  void load();
}

function handleSizeChange(value: number) {
  pageSize.value = value;
  page.value = 1;
  void load();
}

async function handleAlarm(row: any) {
  const { value } = await ElMessageBox.prompt('请输入处理备注', '处理报警', {
    confirmButtonText: '标记已恢复',
    cancelButtonText: '取消',
  });
  await alarmApi.handle(row.id, { status: 'RESOLVED', action: 'RESOLVE', remark: value });
  await load();
}

function levelTag(level: string) {
  return level === 'CRITICAL' || level === 'HIGH' ? 'danger' : level === 'MEDIUM' ? 'warning' : 'info';
}

function formatTime(value: string) {
  return value ? new Date(value).toLocaleString() : '-';
}

onMounted(async () => {
  regions.value = (await regionApi.tree()) as any[];
  await load();
});
</script>

<style scoped>
.alarm-hero {
  background:
    radial-gradient(circle at 90% 20%, rgba(255, 91, 76, 0.3), transparent 30%),
    radial-gradient(circle at 18% 80%, rgba(35, 145, 255, 0.18), transparent 34%),
    linear-gradient(180deg, rgba(8, 47, 98, 0.92), rgba(3, 24, 55, 0.9));
}

.alarm-stats {
  display: flex;
  gap: 10px;
}

.alarm-stats span {
  padding: 10px 14px;
  border: 1px solid rgba(255, 105, 77, 0.38);
  border-radius: 6px;
  color: #ffd9d2;
  background: rgba(93, 23, 27, 0.48);
  box-shadow: inset 0 0 16px rgba(255, 92, 76, 0.14);
  font-weight: 600;
}

.filter-group {
  display: flex;
  gap: 10px;
}

.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
