<template>
  <MapTitle text="全省军民融合创新示范绩效评价情况" />
  <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 = "#23bcf9",
  level_2 = "#67bbf9",
  level_3 = "#0ecedd",
  level_4 = "#eca933",
  level_5 = "#4198ff",
}

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

export default defineComponent({
  components: {
    MapTitle,
  },
  setup() { },
  data() {
    return {
      mapEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      const format = ["2022Three"];
      if (format.indexOf(this.curDataType) != -1) {
        const data = Mock[this.curDataType];
        // xAxisValue
        function formatData(classVal) {
          const classData = data.filter(item => item.class == classVal);
          const vals = [];
          xAxisValue.map((name, index) => {
            const col = classData.find(item => item.name == name + '市');
            vals[index] = col.score || 0;
          })
          return vals
        }
        let classType = [{
          name: "服务强军兴军",
          type: "level_1",
          class: "1",
          value: formatData("1"),
        },
        {
          name: "军民协同创新",
          type: "level_3",
          class: "2",
          value: formatData("2"),
        },
        {
          name: "产业融合发展",
          type: "level_2",
          class: "3",
          value: formatData("3"),
        },
        {
          name: "资源开放共享",
          type: "level_4",
          class: "4",
          value: formatData("4"),
        },
        {
          name: "五大体系建设",
          type: "level_5",
          class: "5",
          value: formatData("5"),
        },
        {
          name: "加减分项",
          type: "level_6",
          class: "6",
          value: formatData("6"),
        }]
        return classType
      } else {
        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",

          // borderColor: "#ffffff",
          axisPointer: {
            type: "shadow",
            shadowStyle: {
              color: "#1881e120",
            },
          },
          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: {
          textStyle: {
            color: "#fff",
          },
        },
        grid: {
          left: "0%",
          right: "0%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
          min: 0,
          max: 100,
          axisLine: {
            lineStyle: {
              color: "#d4f7ff",
            },
          },
          splitLine: {
            lineStyle: {
              // 使用深浅的间隔色
              type: "dashed",
              color: ["#125ba3"],
            },
          },
        },
        xAxis: {
          type: "category",
          data: xAxisValue,
          //横坐标轴文字颜色
          axisLabel: {
            show: true,
            textStyle: {
              color: "#d4f7ff", //更改坐标轴文字颜色
            },
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#125ba3",
            },
          },
        },
        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: #23bcf9;
}

.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>
