<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";
enum Echart_color_Enum {
  level_1 = "#4198ff",
  level_2 = "#4f5d81",
}

const seriesData = [
  {
    name: "国防科工军品收入",
    type: "level_1",
    echartType: "bar",
    growthRate: [80, 85, 95, 65, 85, 70, 85],
    barValue: [1820, 1332, 2045, 1852],
  },
  {
    name: "民口单位军品收入",
    type: "level_2",
    echartType: "bar",
    barValue: [1232, 1264, 2661, 1616],
    growthRate: [50, 60, 70, 80, 80, 60, 85],
  },
];

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

export default defineComponent({
  components: {
    MapTitle,
  },
  setup() {},
  data() {
    return {
      mapEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  computed: {
    option() {
      const seriesBarList = 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 = 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: 1000,
            max: 5000,
          },
          {
            type: "value",
            name: "增长率%",
            min: 0,
            max: 100,
            splitLine: {
              show: false,
            },
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: [...seriesBarList, ...seriesLineList],
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById("incomEcharts");
      const mapEcharts = echarts.init(dom);
      mapEcharts.setOption(this.option);
      this.mapEcharts = mapEcharts;
    },
  },
  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>
