<template>
  <MapTitle :text="echartsTitle" />

  <div :id="echartsId" class="incom-echarts-line"></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";

let MAP_ECHARTS = null;

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

/**
 *
 */
export default defineComponent({
  props: {
    echartsTitle: {
      type: String,
      required: true,
    },
    echartsId: {
      type: String,
      required: true,
    },
  },
  components: {
    MapTitle,
  },
  setup() {},
  data() {
    return {
      mapEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  watch: {
    seriesData() {
      MAP_ECHARTS.showLoading();
      MAP_ECHARTS.setOption(this.optionBar, true);
      MAP_ECHARTS.hideLoading();
    },
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      return Mock[this.curDataType].seriesData;
    },
    optionBar() {
      const seriesBarList = this.seriesData.map((item, index) => {
        return {
          type: "bar",
          itemStyle: {
            color: "#4198FF",
          },
           label: {
              show: true,
              position: "right",
            },
          barWidth: 18,
          data: item.value.map((barItem, barIndex) => {
            return {
              value: barItem,
            };
          }),
        };
      });

      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          className: "incom-echart-tooltip",
          formatter: (params) => {
            let html = `<span class="name">${params[0].axisValue}</span>`;
            html += `<div class="incom-echart-tooltip ">
                <span class="text text-color_1">${params[0].data.value}</span>个
               </div>`;

            return html;
          },
        },
        grid: {
          left: "2%",
          right: "10%",
          bottom: "3%",
          containLabel: true,
        },
        legend: {
          right: "2%",
        },
        yAxis: {
          type: "category",
          data: xAxisValue,
        },
        xAxis: {
          type: "value",
        },
        series: [...seriesBarList],
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById(this.echartsId);
      MAP_ECHARTS = echarts.init(dom);
      MAP_ECHARTS.setOption(this.optionBar);
      this.mapEcharts = MAP_ECHARTS;
    },
  },
  unmounted() {
    if (!this.mapEcharts) {
      return;
    }
    this.mapEcharts.dispose();
    this.mapEcharts = null;
  },
});
</script>
<style>
.incom-echart-tooltip {
  width: 80px;
}
.incom-echart-tooltip .name {
    padding-left: 10px;
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
   position: relative;
}
.incom-echart-tooltip .name::before {
  position: absolute;
  top: 5px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
  background: #4198ff;

}
.incom-echart-tooltip .text {
  padding-left: 20px;
  margin-bottom: 20px;
}
.incom-echart-tooltip .text:last-of-type {
  margin: 0;
}
.incom-echart-tooltip .text-color_1 {
  color: #4198ff;
}


</style>

<style lang="scss" scoped>
.incom-echarts-line {
  flex: 1;
  height: calc(100% - 40px);
  box-sizing: border-box;
}
</style>
