<template>
  <MapTitle text="国防科工军品收入及民口单位军品收入情况" />
  <div id="incomEcharts" class="incom-echarts"></div>
</template>
<script lang="ts">
/**
 * 绩效评价情况——柱状图
 */
import { defineComponent } from "vue";
import MapTitle from "@/components/mapTitle/index.vue";
import * as echarts from "echarts";
import Mock from "./mock";
import { mapState } from "vuex";

enum Echart_color_Enum {
  level_1 = "#fac858",
  level_2 = "#4198ff",
}
let MAP_ECHARTS = null;

const xAxisValue = ["2018", "2019", "2020", "2021"];

export default defineComponent({
  components: {
    MapTitle,
  },
  setup() {},
  data() {
    return {
      mapEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      return Mock[this.curDataType];
    },
    option() {
      const seriesBarList = this.seriesData.map((item, index) => {
        return {
          name: item.name,
          type: "bar",
          itemStyle: {
            color: Echart_color_Enum[item.type],
          },
          barWidth: 20,
          data: item.barValue.map((barItem, barIndex) => {
            return {
              value: barItem,
              class: `color_${index + 1}`,
              rate: item.growthRate[barIndex],
            };
          }),
        };
      });

      const seriesLineList = this.seriesData.map((item) => {
        return {
          type: "line",
          tooltip: {
            show: false,
          },
          itemStyle: {
            color: Echart_color_Enum[item.type],
          },
          emphasis: {
            focus: "series",
          },
          yAxisIndex: 1,
          data: item.growthRate,
        };
      });

      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          className: "tooltip-income",
          formatter: (params) => {
            let html = `<span class="year">${params[0].axisValue}年</span>`;
            params.forEach((item) => {
              const { data = {} } = item;

              html += `<div class="tooltip-income">
              <span class="title">${item.seriesName}</span>
              <div class="row-item">
               <div>金额  <p><span class="${data.class}">${data.value}</span>亿元</p></div>
               <div>增长率  <p><span class="${data.class}">${data.rate}%</span></p></div>
              </div>
            </div>`;
            });

            return html;
          },
        },
        legend: {
          right: "2%",
        },
        grid: {
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            name: "亿元",
            min: 0,
            max: 200,
          },
          {
            type: "value",
            name: "增长率%",
            min: 0,
            max: 100,
            splitLine: {
              show: false,
            },
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: [...seriesBarList, ...seriesLineList],
      };
    },
  },
  watch: {
    seriesData() {
      MAP_ECHARTS.showLoading();
      MAP_ECHARTS.setOption(this.option, true);
      MAP_ECHARTS.hideLoading();
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById("incomEcharts");
      MAP_ECHARTS = echarts.init(dom);
      MAP_ECHARTS.setOption(this.option);
      this.mapEcharts = MAP_ECHARTS;
    },
  },
  unmounted() {
    if (!this.mapEcharts) {
      return;
    }
    this.mapEcharts.dispose();
    this.mapEcharts = null;
  },
});
</script>
<style>
.tooltip-income .year {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.tooltip-income .title {
  font-weight: 600;
  font-size: 12px;
  color: #666;
}
.tooltip-income .row-item {
  padding: 0 15px 10px 15px;

  display: flex;
  align-items: center;
  font-size: 12px;
  color: #999;
  justify-content: space-between;
}
.tooltip-income .row-item div {
  margin-right: 15px;
}
.tooltip-income .row-item p {
  font-size: 12px;
  color: #999;
}
.tooltip-income .row-item p span {
  font-size: 18px;
  margin-right: 5px;
}
.tooltip-income .color_1 {
  color: #fac858;
}
.tooltip-income .color_2 {
  color: #4198ff;
}
</style>
<style lang="scss" scoped>
.incom-echarts {
  flex: 1;
  height: 260px;
  box-sizing: border-box;
  margin-top: 10px;
}
</style>
