<template>
  <MapTitle text="军工科研设备设施开放共享情况" />
  <div class="facility-title">
    <div class="title">历年军工科研设备设施开发共享情况</div>
  </div>

  <div id="industryDistribute" class="facility-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: {},
  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: "facility-echart-tooltip",
          formatter: (params) => {
            let html = `<span class="name">${params[0].axisValue}</span>`;
            html += `<div class="facility-echart-tooltip ">
                <span class="text text-color_1">${params[0].data.value}</span>台
               </div>`;

            return html;
          },
        },
        grid: {
          top: "8%",
          left: "3%",
          right: "10%",
          bottom: "3%",
          containLabel: true,
        },
        legend: {
          right: "2%",
        },
        yAxis: [
          {
            type: "category",
            data: xAxisValue,
          },
        ],
        xAxis: [
          {
            type: "value",
            max:1000,
            name: "数量(台)",
             axisLine: {
              show: true,
            },
          },
        ],
        series: [...seriesBarList],
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById("industryDistribute");
      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>
.facility-echart-tooltip {
  width: 80px;
}
.facility-echart-tooltip .name {
  padding-left: 10px;
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
  position: relative;
}
.facility-echart-tooltip .name::before {
  position: absolute;
  top: 5px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
  background: #4198ff;
}
.facility-echart-tooltip .text {
  padding-left: 20px;
  margin-bottom: 20px;
}
.facility-echart-tooltip .text:last-of-type {
  margin: 0;
}
.facility-echart-tooltip .text-color_1 {
  color: #4198ff;
}
</style>

<style lang="scss" scoped>
.facility-echarts-line {
  flex: 1;
  height: calc(100% - 60px);
  box-sizing: border-box;
}
.facility-title {
  padding-left: 10px;
  .title {
    color: #4198ff;
    margin: 10px 0;
    font-size: 16px;
  }
}
</style>
