<template>
  <div class="title">主营业务收入分布</div>

  <div id="incomDistribution" class="incom-distribution"></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";

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

/**
 * 主营业务收入分布——柱状图
 */
export default defineComponent({
  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 {
          name: "主营业务收入",
          type: "bar",
          itemStyle: {
            color: "#4198ff",
          },
          barWidth: 20,

          data: item.value.map((valItem) => {
            return {
              value: valItem,
              label: "主营业务收入",
            };
          }),
        };
      });

      const seriesLineList = this.seriesData.map((item) => {
        return {
          name: "增长率",
          type: "line",
          itemStyle: {
            color: "#fac858",
          },
          yAxisIndex: 1,
          emphasis: {
            focus: "series",
          },

          data: item.rateData.map((val) => {
            return {
              value: val,
              label: "增长率",
            };
          }),
        };
      });

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

            html += `<div class="income-echart ">
                <span class="${params[0].class} title color_1">${params[0].data.label}</span>
                <br />
                <span class="text text-color_1">${params[0].data.value}</span>亿元
               </div>
              <div class="income-echart">
                <span class="${params[1].data.class} title  color_2">${params[1].data.label}</span>
                  <br />
                <span class="text text-color_2">${params[1].data.value}%</span>
            </div>`;

            return html;
          },
        },
        legend: {
          right: "2%",
        },
        grid: {
          left: "0%",
          right: "5%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            name: "金额(亿元)",
            min: -10,
            max: 1600,
          },
          {
            type: "value",
            name: "增长率%",
            min: -10,
            max: 100,
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: [...seriesBarList, ...seriesLineList],
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById("incomDistribution");
      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>
.income-echart {
  width: 110px;
}
.income-echart .name {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.income-echart .title {
  padding-left: 10px;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  position: relative;
}
.income-echart .title::before {
  position: absolute;
  top: 8px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
}
.income-echart .text {
  padding-left: 20px;
  margin-bottom: 20px;
  font-size: 20px;
}
.income-echart .text:last-of-type {
  margin: 0;
}

.income-echart .text-color_1 {
  color: #4198ff;
}
.income-echart .text-color_2 {
  color: #eca933;
}
.income-echart .color_1::before {
  background: #4198ff;
}
.income-echart .color_2::before {
  background: #eca933;
}
</style>

<style lang="scss" scoped>
.title {
  color: #4198ff;
  margin: 30px 0 10px 0;
  font-size: 14px;
}
.incom-distribution {
  flex: 1;
  min-height: 315px;
  height: calc(100% - 70px);
  box-sizing: border-box;
  margin-top: 10px;
}
</style>
