<template>
  <section class="glass-panel">
    <div class="panel-head">
      <div>
        <p class="panel-eyebrow">Balance Circuits</p>
        <h3>回路差异明细</h3>
      </div>
      <el-tag size="small" effect="plain">共 {{ circuits.length }} 条回路</el-tag>
    </div>

    <el-empty v-if="!circuits.length" description="当前没有可展示的回路结果" />

    <el-table v-else :data="circuits" stripe>
      <el-table-column prop="monitorName" label="回路节点" min-width="180" />
      <el-table-column prop="upstreamValue" label="上游值" min-width="120">
        <template #default="{ row }">{{ formatNumber(row.upstreamValue) }}</template>
      </el-table-column>
      <el-table-column prop="downstreamTotal" label="下游汇总" min-width="120">
        <template #default="{ row }">{{ formatNumber(row.downstreamTotal) }}</template>
      </el-table-column>
      <el-table-column prop="diffValue" label="差异值" min-width="120">
        <template #default="{ row }">{{ formatNumber(row.diffValue) }}</template>
      </el-table-column>
      <el-table-column prop="diffRate" label="差异率" min-width="120">
        <template #default="{ row }">{{ formatPercent(row.diffRate) }}</template>
      </el-table-column>
      <el-table-column label="状态" min-width="100">
        <template #default="{ row }">
          <el-tag :type="row.abnormal ? 'danger' : 'success'" size="small">
            {{ row.abnormal ? '超阈值' : '正常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link @click="$emit('locate', row)">工况复盘</el-button>
        </template>
      </el-table-column>
    </el-table>
  </section>
</template>

<script setup lang="ts">
import type { MeterBalanceCircuitVO } from '@/api/ems/types';

defineOptions({
  name: 'MeterBalanceTopology'
});

defineProps<{
  circuits: MeterBalanceCircuitVO[];
}>();

defineEmits<{
  (event: 'locate', row: MeterBalanceCircuitVO): void;
}>();

const formatNumber = (value: unknown) => {
  if (value === null || value === undefined || value === '') {
    return '--';
  }
  const numberValue = Number(value);
  return Number.isNaN(numberValue) ? '--' : numberValue.toFixed(2);
};

const formatPercent = (value: unknown) => {
  if (value === null || value === undefined || value === '') {
    return '--';
  }
  const numberValue = Number(value);
  return Number.isNaN(numberValue) ? '--' : `${(numberValue * 100).toFixed(2)}%`;
};
</script>

<style scoped>
.glass-panel {
  padding: 18px 20px;
  border-radius: 24px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 18px 48px rgba(15, 23, 42, 0.08);
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-eyebrow {
  margin: 0 0 6px;
  color: #2563eb;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.panel-head h3 {
  margin: 0;
}
</style>
