<template>
  <div>
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col" v-for="item in panels" :key="item.key">
        <div class="card-panel" @click="changeChartData(item.key)">
          <div class="card-panel-icon-wrapper" :class="item.iconClass">
            <svg-icon :icon-class="item.icon" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">{{ item.title }}</div>
            <count-to :start-val="0" :end-val="item.value" :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row style="background:#fff; padding:16px 16px 0; margin-top: 32px;">
      <div ref="chart" style="width: 100%; height: 400px;"></div>
    </el-row>
  </div>
</template>

<script>
import CountTo from 'vue-count-to';
import * as echarts from 'echarts';
import * as HarmfulInfoReportApi from '@/api/server/harmfulinforeport';

export default {
  components: { CountTo },
  data() {
    return {
      panels: [
        { key: 'monthCount', title: '本月上报总数', icon: 'date', iconClass: 'icon-education', value: 0 },
        { key: 'quarterCount', title: '季度上报总数', icon: 'job', iconClass: 'icon-search', value: 0 },
        { key: 'yearCount', title: '年上报总数', icon: 'education', iconClass: 'icon-table', value: 0 },
        { key: 'totalCount', title: '上报总数', icon: 'edit', iconClass: 'icon-peoples', value: 0 },
      ],
      allChartData: {
        monthCountList: [],
        quarterCountList: [],
        yearCountList: [],
        totalCountList: [],
      },
      currentChartKey: 'monthCountList',
      myChart: null,
    };
  },
  mounted() {
    this.fetchData();
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
    if (this.myChart) {
      this.myChart.dispose();
    }
  },
  methods: {
    async fetchData() {
      try {
        const res = await HarmfulInfoReportApi.indexdata();
        const data = res.data || {};

        // 更新卡片数字
        this.panels.forEach(p => {
          p.value = data[p.key] || 0;
        });

        // 存储所有图表数据，后续可扩展
        this.allChartData.monthCountList = data.monthCountList || [];
        this.allChartData.quarterCountList = data.quarterCountList || [];
        this.allChartData.yearCountList = data.yearCountList || [];
        this.allChartData.totalCountList = data.totalCountList || [];

        // 默认绘制月度数据图表
        this.currentChartKey = 'monthCountList';
        this.drawChart();
      } catch (e) {
        console.error('加载数据失败:', e);
      }
    },

    initChart() {
      this.myChart = echarts.init(this.$refs.chart);
    },

    drawChart() {
      if (!this.myChart) return;

      const data = this.allChartData[this.currentChartKey] || [];
      const option = {
        title: {
          text: '本年度每月上报数',
          left: 'center',
        },
        tooltip: {
          trigger: 'axis',
        },
        xAxis: {
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月',
            '7月', '8月', '9月', '10月', '11月', '12月'],
        },
        yAxis: {
          type: 'value',
          name: '上报数',
        },
        series: [
          {
            name: '上报数',
            type: 'bar',
            data: data,
            itemStyle: {
              color: '#409EFF',
            },
            barMaxWidth: 40,
          },
        ],
      };
      this.myChart.setOption(option);
    },

    changeChartData(key) {
      // 根据点击的卡片 key 切换对应的图表数据
      this.currentChartKey = key + 'List';
      this.drawChart();
    },

    resizeChart() {
      if (this.myChart) {
        this.myChart.resize();
      }
    },
  },
};
</script>

<style scoped lang="scss">
.panel-group {
  margin: 0 -20px;
  .card-panel-col {
    padding: 0 20px;
    margin-bottom: 24px;
  }
}
.card-panel {
  border-radius: 8px;
  border: 1px solid #eee;
  padding: 16px 24px;
  background-color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  .card-panel-icon-wrapper {
    flex-shrink: 0;
    width: 54px;
    height: 54px;
    border-radius: 50%;
    background: #409EFF;
    margin-right: 16px;
    display: flex;
    justify-content: center;
    align-items: center;
    &.icon-education {
      background-color: #2d8cf0;
    }
    &.icon-search {
      background-color: #19be6b;
    }
    &.icon-table {
      background-color: #ff9900;
    }
    &.icon-peoples {
      background-color: #ff4444;
    }
    .card-panel-icon {
      width: 26px;
      height: 26px;
      fill: #fff;
    }
  }
  .card-panel-description {
    flex-grow: 1;
    .card-panel-text {
      color: #999;
      font-size: 14px;
      margin-bottom: 6px;
    }
    .card-panel-num {
      font-size: 28px;
      font-weight: 700;
      color: #333;
    }
  }
  &:hover {
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
  }
}
</style>
