<template>
  <MapTitle
    text="全省军民融合创新示范绩效评价情况"
    :detailsHandle="detailsHandle"
  />
  <div id="performanceBar" class="performance-bar"></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;
enum Bar_Enum {
  level_1 = "#4f5d81",
  level_2 = "#429a9d",
  level_3 = "#007fb2",
  level_4 = "#eca933",
  level_5 = "#4191ff",
}

const xAxisValue = [
  "杭州",
  "宁波",
  "温州",
  "湖州",
  "嘉兴",
  "绍兴",
  "金华",
  "衢州",
  "舟山",
  "台州",
  "丽水",
];

export default defineComponent({
  components: {
    MapTitle,
  },
  setup() {},
  data() {
    return {
      mapEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      return Mock[this.curDataType];
    },
    optionBar() {
      const seriesList = this.seriesData.map((item, index) => {
        return {
          name: item.name,
          type: "bar",
          stack: "total",
          label: {
            show: true,
            color: "#fff",
          },
          barWidth: 26,
          itemStyle: {
            color: Bar_Enum[item.type],
          },
          emphasis: {
            show: false,
          },

          data: item.value.map((valItem) => {
            return {
              value: valItem,
              class: `color_${index + 1}`,
            };
          }),
        };
      });

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

              html += `<div class="per-formance-echart">
                <span class="${data.class} title">${item.seriesName}</span>
                <span class="text">${data.value}</span>
            </div>`;
            });

            return html;
          },
        },
        legend: {},
        grid: {
          left: "0%",
          right: "0%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
          min: 0,
          max: 100,
        },
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: seriesList,
      };
    },
  },
  watch: {
    seriesData() {
      MAP_ECHARTS.showLoading();
      MAP_ECHARTS.setOption(this.optionBar, true);
      MAP_ECHARTS.hideLoading();
    },
  },
  methods: {
    detailsHandle() {
      console.log("---点击详情跳转");
    },
    initEcharts() {
      const dom = document.getElementById("performanceBar");
      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>
.per-formance-echart {
  width: 120px;
  display: flex;
  justify-content: space-between;
}

.per-formance-echart .name {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.per-formance-echart .title {
  padding-left: 10px;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  position: relative;
}
.per-formance-echart .title::before {
  position: absolute;
  top: 5px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
  background: red;
}
.per-formance-echart .row-item {
  padding: 0 15px 10px 15px;

  display: flex;
  align-items: center;
  font-size: 12px;
  color: #999;
  justify-content: space-between;
}
.per-formance-echart .row-item div {
  margin-right: 15px;
}
.per-formance-echart .row-item p {
  font-size: 12px;
  color: #999;
}
.per-formance-echart .row-item p span {
  font-size: 18px;
  margin-right: 5px;
}
.per-formance-echart .text {
  color: #4191ff;
}
.per-formance-echart .color_1::before {
  background: #4f2d11;
}
.per-formance-echart .color_2::before {
  background: #429a9d;
}
.per-formance-echart .color_3::before {
  background: #007fb2;
}
.per-formance-echart .color_4::before {
  background: #eca933;
}
.per-formance-echart .color_5::before {
  background: #4191ff;
}
</style>

<style lang="scss" scoped>
.performance-bar {
  flex: 1;
  height: 245px;
  box-sizing: border-box;
  margin-top: 10px;
}
</style>
