<template>
  <div class="wrap">
    <!-- 页面顶部中间显示 公司/锅炉房/换热站 -->
    <div class="title">{{ headerText }}</div>

    <!-- Tabs -->
    <a-tabs v-model:activeKey="activeTab" centered @change="onTabChange">
      <a-tab-pane key="temp" tab="温度指标">
        <div ref="tempChartRef" class="chart-box"></div>
      </a-tab-pane>
      <a-tab-pane key="press" tab="压力指标">
        <div ref="pressChartRef" class="chart-box"></div>
      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<script lang="ts" setup>
import { ref, nextTick, defineProps, onUnmounted } from 'vue';
import * as echarts from 'echarts';
import { useMessage } from '/@/hooks/web/useMessage';
import { list } from '/@/views/heating/history/HeatanalysisHistory.api';

const props = defineProps({});
const { createMessage } = useMessage();

const activeTab = ref('temp'); // 默认进入温度指标 tab

// 日期：近7天（含今天）
const end = new Date();
end.setHours(23, 59, 59, 999);
const start = new Date(end);
start.setDate(end.getDate() - 6);
start.setHours(0, 0, 0, 0);

const pad = (n: number) => String(n).padStart(2, '0');
function formatDateTime(d: Date) {
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
}

const tempChartRef = ref<HTMLElement | null>(null);
const pressChartRef = ref<HTMLElement | null>(null);
let tempChart: echarts.ECharts | null = null;
let pressChart: echarts.ECharts | null = null;
let tempOption: any = null;
let pressOption: any = null;
const headerText = ref(''); // 页面顶部显示文本

function safeNum(v: any) {
  if (v === null || v === undefined || v === '') return null;
  const n = Number(v);
  return Number.isNaN(n) ? null : n;
}

function buildHeader(records: any[], record_: any) {
  if (records && records.length > 0) {
    const r = records[0];
    return `${r.view001Name || ''}/${r.view002Name || ''}/${r.view004Name || ''}`;
  }
  return `${record_.view001Name || ''}/${record_.view002Name || ''}/${record_.view004Name || ''}`.replace(/\/+$/, '');
}

function initChartsOptions(records: any[]) {
  records.sort((a: any, b: any) => new Date(a.datatime).getTime() - new Date(b.datatime).getTime());
  const xAxis = records.map((r: any) => r.datatime);

  const tempSeries = [
    { name: '一次供水温度', key: 'view005' },
    { name: '一次回水温度', key: 'view006' },
    { name: '二次供水温度', key: 'view009' },
    { name: '二次回水温度', key: 'view010' },
  ].map(s => ({
    name: s.name,
    type: 'line',
    showSymbol: false,
    connectNulls: false,
    data: records.map((r: any) => safeNum(r[s.key])),
  }));

  const pressSeries = [
    { name: '一次供水压力', key: 'view007' },
    { name: '一次回水压力', key: 'view008' },
    { name: '二次供水压力', key: 'view011' },
    { name: '二次回水压力', key: 'view012' },
  ].map(s => ({
    name: s.name,
    type: 'line',
    showSymbol: false,
    connectNulls: false,
    data: records.map((r: any) => safeNum(r[s.key])),
  }));

  const baseOption = {
    tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } },
    legend: { top: 36, left: 'center' },
    grid: { left: '5%', right: '5%', bottom: 70, top: 70, containLabel: true },
    xAxis: {
      type: 'category',
      data: xAxis,
      boundaryGap: false,
      axisLabel: {
        interval: 0,
        formatter: (val: string) => val.replace(' ', '\n'),
      },
    },
    yAxis: { type: 'value', scale: true },
  };

  tempOption = { ...baseOption, title: { text: '温度指标', left: 'center', top: 8 }, series: tempSeries };
  pressOption = { ...baseOption, title: { text: '压力指标', left: 'center', top: 8 }, series: pressSeries };
}

let resizeAdded = false;
function ensureResizeListener() {
  if (resizeAdded) return;
  const onResize = () => {
    tempChart?.resize();
    pressChart?.resize();
  };
  window.addEventListener('resize', onResize);
  onUnmounted(() => {
    window.removeEventListener('resize', onResize);
  });
  resizeAdded = true;
}

function init(record_: any) {
  const params = {
    pageNo: 1,
    pageSize: -1,
    regionType: '城区',
    view001: record_.view001,
    view002: record_.view002,
    view004: record_.view004 || -1,
    SDateStr: formatDateTime(start),
    EDateStr: formatDateTime(end),
  };

  list(params).then((res: any) => {
    const records = (res && res.records) ? res.records : [];
    headerText.value = buildHeader(records, record_);
    initChartsOptions(records);
    nextTick(() => {
      // 初始化温度图表（默认tab）
      if (tempChartRef.value) {
        tempChart = echarts.init(tempChartRef.value);
        tempChart.setOption(tempOption);
      }
      ensureResizeListener();
    });
  }).catch(err => {
    console.error(err);
    createMessage.error('获取数据失败');
  });
}

// tab切换时，第一次进来才 init
function onTabChange(key: string) {
  nextTick(() => {
    if (key === 'temp') {
      if (!tempChart && tempChartRef.value) {
        tempChart = echarts.init(tempChartRef.value);
        tempChart.setOption(tempOption);
      }
      tempChart?.resize();
    } else if (key === 'press') {
      if (!pressChart && pressChartRef.value) {
        pressChart = echarts.init(pressChartRef.value);
        pressChart.setOption(pressOption);
      }
      pressChart?.resize();
    }
  });
}

defineExpose({ init });

onUnmounted(() => {
  if (tempChart) { tempChart.dispose(); tempChart = null; }
  if (pressChart) { pressChart.dispose(); pressChart = null; }
});
</script>

<style lang="less" scoped>
.wrap {
  width: 80%;
  margin: 0 auto;
}
.title {
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  margin: 12px 0;
}
.chart-box {
  width: 100%;
  height: 60vh; /* 一个tab里可以让图高一点 */
}
</style>
