<template>
  <div class="dustMonitor">
    <customHeader>色谱仪报表分析</customHeader>
    <div class="content-container">
      <div class="file-list">
        <ul>
          <li v-for="item in selectList" :key="item.fileId" :class="{ selected: item.fileId === selectedFileId }" @click="handleFileClick(item)">
            {{ item.fileName }}
          </li>
        </ul>
      </div>
      <div class="table-container">
        <a-table :columns="columns" :data-source="tableData" size="small" :scroll="{ y: 300 }" class="tableW"> </a-table>
        <div id="lineChart" class="line-chart"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { columns } from './bundleSpy-table.data';
import { getbundleSpyInfoList, getAllFileList } from './bundleSpy-table.api';
import customHeader from '/@/components/vent/customHeader.vue';
import * as echarts from 'echarts';

let selectList = ref<any[]>([]);

let formSearch = reactive({
  pageNum: 1,
  pageSize: 1000,
  fileId: '',
  fileName: '',
});
let tableData = ref<any[]>([]);
let selectedFileId = ref<string | null>(null);
//获取色谱仪报表
async function getTableList(params: any) {
  let res = await getbundleSpyInfoList({ type: 'bundleSpy', ...params });
  const content = res.content;
  let contentArr = JSON.parse(content);
  tableData.value = contentArr;
  console.log(contentArr, 'contentArr');
  updateChart(contentArr);
}

//折线图
function updateChart(data: any) {
  const chartDom = document.getElementById('lineChart');
  const myChart = echarts.init(chartDom);
  const categories = data.map((item: any) => item.jcdd);
  const c2h2AveValues = data.map((item: any) => parseFloat(item.c2h2_ave));
  const c2h4AveValues = data.map((item: any) => parseFloat(item.c2h4_ave));
  const ch4AveValues = data.map((item: any) => parseFloat(item.ch4_ave));
  const co2AveValues = data.map((item: any) => parseFloat(item.co2_ave));
  const coAveValues = data.map((item: any) => parseFloat(item.co_ave));
  const o2AveValues = data.map((item: any) => parseFloat(item.o2_ave));
  const n2AveValues = data.map((item: any) => parseFloat(item.n2_ave));
  const c2h6AveValues = data.map((item: any) => parseFloat(item.c2h6_ave));

  const option = {
    title: {
      text: '色谱仪报表分析',
      textStyle: {
        color: '#ffffff', // 设置标题颜色
      },
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(28, 72, 105, 0.5)', // 设置 tooltip 背景为透明
      textStyle: {
        color: '#ffffff', // 设置 tooltip 字体颜色为白色
      },
      axisPointer: {
        label: {
          show: true,
          backgroundColor: '#071c44',
        },
      },
    },
    legend: {
      top: 10,
      textStyle: {
        color: '#ffffffff',
      },
    },
    xAxis: {
      type: 'category',
      data: categories,
      splitLine: { show: true, lineStyle: { color: 'rgba(21,80,126,.5)' } },
      axisLabel: {
        interval: 0, // 显示所有标签
        color: '#ffffff',
        formatter: function (value: string) {
          return value.length > 15 ? value.slice(0, 15) + '...' : value; // 截断长标签
        },
      },
    },
    yAxis: [
      {
        type: 'value',
        name: 'O₂/N₂',
        max: 100,
        splitLine: { show: true, lineStyle: { color: 'rgba(21,80,126,.5)' } },
        axisLabel: {
          color: '#ffffff', // 设置 y 轴字体颜色
        },
      },
      {
        type: 'value',
        name: '其他气体',
        splitLine: { show: true, lineStyle: { color: 'rgba(21,80,126,.5)' } },
        axisLabel: {
          color: '#ffffff', // 设置 y 轴字体颜色
        },
      },
    ],
    series: [
      {
        name: 'C₂H₂平均值',
        data: c2h2AveValues,
        type: 'line',
        yAxisIndex: 1,
      },
      {
        name: 'C₂H₄平均值',
        data: c2h4AveValues,
        type: 'line',
        yAxisIndex: 1,
      },
      {
        name: 'CH₄平均值',
        data: ch4AveValues,
        yAxisIndex: 1,
        type: 'line',
      },
      {
        name: 'CO₂平均值',
        data: co2AveValues,
        yAxisIndex: 1,
        type: 'line',
      },
      {
        name: 'CO平均值',
        data: coAveValues,
        yAxisIndex: 1,
        type: 'line',
      },
      {
        name: 'O₂平均值',
        data: o2AveValues,
        yAxisIndex: 0,
        type: 'line',
      },
      {
        name: 'N₂平均值',
        data: n2AveValues,
        yAxisIndex: 0,
        type: 'line',
      },
      {
        name: 'C2H6平均值',
        data: c2h6AveValues,
        yAxisIndex: 1,
        type: 'line',
      },
    ],
  };
  myChart.setOption(option);
}
//获取所有文件列表
async function getAllFile() {
  let res = await getAllFileList({ type: 'bundleSpy' });
  selectList.value = res.records.map((item: any) => ({
    fileId: item.fileId,
    fileName: item.fileName,
  }));
  if (selectList.value.length > 0) {
    formSearch.fileId = selectList.value[0].fileId;
    getSearch();
  }
}
// 处理文件点击事件
function handleFileClick(item: any) {
  formSearch.fileId = item.fileId;
  formSearch.fileName = item.fileName;
  selectedFileId.value = item.fileId;
  getSearch();
}
//查询
function getSearch() {
  const selectedFile = selectList.value.find((item) => item.fileId === formSearch.fileId);
  const params = {
    fileId: formSearch.fileId,
    fileName: selectedFile ? selectedFile.fileName : '',
  };
  getTableList(params);
}
onMounted(() => {
  getTableList({ type: 'bundleSpy' });
  getAllFile().then(() => {
    if (selectList.value.length > 0) {
      formSearch.fileId = selectList.value[0].fileId;
      selectedFileId.value = selectList.value[0].fileId;
      getSearch();
    }
  });
});
</script>

<style lang="less" scoped>
@import '/@/design/theme.less';

.content-container {
  display: flex;
  width: 100%;
  height: 100%;
}

.file-list {
  width: 20%;
  padding: 10px;
  margin-right: 10px;
  margin-bottom: 50px;
  border: 1px solid #99e8ff66;
  background: #27546e1a;
  box-shadow: 0px 0px 20px 7px rgba(145, 233, 254, 0.7) inset;
  -moz-box-shadow: 0px 0px 20px 7px rgba(145, 233, 254, 0.7) inset;
  -webkit-box-shadow: 0px 0px 50px 1px rgb(149 235 255 / 5%) inset;
}

.file-list ul {
  list-style: none;
  padding: 0;
}

.file-list li {
  color: #fff;
  padding: 5px;
  cursor: pointer;
}

.file-list li:hover,
.file-list li.selected {
  background: #1c4869;
}

.table-container {
  margin-top: 10px;
  width: 80%;
  box-sizing: border-box;
}

.dustMonitor {
  width: 100%;
  height: 100%;
  padding: 10px 10px 15px 10px;
  box-sizing: border-box;
  position: relative;
}

:deep(.zxm-table-thead > tr > th:last-child) {
  border-right: 1px solid #91e9fe !important;
}

:deep(.zxm-picker-input > input) {
  color: #fff;
}

:deep(.zxm-select:not(.zxm-select-customize-input) .zxm-select-selector) {
  border: 1px solid var(--vent-form-item-border) !important;
  background-color: #ffffff00 !important;
}

:deep(.zxm-select-selection-item) {
  color: #fff !important;
}
.line-chart {
  width: 100%;
  height: 400px;
  margin-top: 50px;
}
</style>
