<template>
  <MapTitle text="承担军品任务情况" />

  <el-row>
    <el-col :span="8">
      <div class="title2X">国防科技工业军品收入规模及增长率</div>
      <div id="countryEcharts" class="echart-item"></div>
    </el-col>

    <el-col class="city-echarts" :span="15" :offset="1">
      <div class="title2X">各市军品收入规模及增长率</div>
      <div id="cityEcharts" class="echart-item"></div>
    </el-col>
  </el-row>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import MapTitle from "@/components/mapTitle/index.vue";
import * as echarts from "echarts";
import { Aim } from "@element-plus/icons";
import Mock from "./mock";
import { mapState } from "vuex";

let MAP_ECHARTS = null;
let CITY_ECHARTS  = null
const xAxisCountry = ["2018", "2019", "2020", "2021", "2022"];

// 城市增长率
const xAxisCity = [
  "杭州",
  "宁波",
  "温州",
  "湖州",
  "嘉兴",
  "绍兴",
  "金华",
  "衢州",
  "舟山",
  "台州",
  "丽水",
];

/**
 * 承担军需品情况
 */
export default defineComponent({
  components: {
    MapTitle,
    Aim,
  },
  setup() {},
  data() {
    return {
      countryEcharts: null,
      cityEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  watch: {
    seriesData() {
     MAP_ECHARTS.showLoading();
      MAP_ECHARTS.setOption(this.optionCountry, true);
      MAP_ECHARTS.hideLoading();
    },
    cityData() {
    CITY_ECHARTS.showLoading();
      CITY_ECHARTS.setOption(this.optionCity,true);
      CITY_ECHARTS.hideLoading();
    },
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      return Mock[this.curDataType].seriesData;
    },
    cityData() {
      return Mock[this.curDataType].cityData;
    },
    optionCountry() {
      const seriesBarList = this.seriesData.map((item) => {
        // console.log("测试", item.value);
        return {
          name: "收入规模",
          type: "bar",
          itemStyle: {
            color: "#4198ff",
          },
          barWidth: 20,

          data: item.value.map((item) => {
            return {
              value: item,
              label: "收入规模",
            };
          }),
        };
      });

      const seriesLineList = this.seriesData.map((item) => {
        return {
          name: "增长率",

          type: "line",
          stack: "Total",
          itemStyle: {
            color: "#fac858",
          },
          yAxisIndex: 1,
          emphasis: {
            focus: "series",
          },

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

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

            html += `<div class="munition-echart ">
                <span class=" title color_1">${params[0].data.label}</span>
                <br />
                <span class="text text-color_1">${params[0].data.value}</span>亿元
               </div>
              <div class="munition-echart">
                <span 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: {},
        grid: {
          top: "10%",
          left: "0%",
          right: "5%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            name: "收入(亿元)",
            min: 0,
            max: 200,
          },
          {
            type: "value",
            name: "增长率%",
            min: 0,
            max: 100,
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisCountry,
        },
        series: [...seriesBarList, ...seriesLineList],
      };
    },

    optionCity() {
      const seriesBar = {
        type: "bar",
        name: "收入规模",

        itemStyle: {
          color: "#4198ff",
        },
        barWidth: 20,

        data: this.cityData.value.map((item) => {
          return {
            value: item,
            label: "收入规模",
          };
        }),
      };

      const seriesLine = {
        type: "line",
        stack: "Total",
        itemStyle: {
          color: "#fac858",
        },
        yAxisIndex: 1,
        emphasis: {
          focus: "series",
        },
        name: "增长率",

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

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

            html += `<div class="munition-echart ">
                <span class=" title color_1">${params[0].data.label}</span>
                <br />
                <span class="text text-color_1">${params[0].data.value}</span>亿元
               </div>
              <div class="munition-echart">
                <span 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: {},
        grid: {
          top: "10%",
          left: "5%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            name: "规模(亿元)",
            min: 0,
            max: 50,
          },
          {
            type: "value",
            name: "增长率(%)",
            min: -50,
            max: 170,
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisCity,
        },
        series: [seriesBar, seriesLine],
      };
    },
  },
  methods: {
    initEcharts() {
      this.initCountryEcharts();
      this.initCityEcharts();
    },
    initCountryEcharts() {
      const dom = document.getElementById("countryEcharts");
     MAP_ECHARTS = echarts.init(dom);
      MAP_ECHARTS.setOption(this.optionCountry);
      this.countryEcharts = MAP_ECHARTS;
    },
    initCityEcharts() {
      const dom = document.getElementById("cityEcharts");
      CITY_ECHARTS = echarts.init(dom);
      CITY_ECHARTS.setOption(this.optionCity);
      this.cityEcharts = CITY_ECHARTS;
    },
  },
  unmounted() {
    if (this.countryEcharts) {
      this.countryEcharts.dispose();
      this.countryEcharts = null;
    }

    if (this.cityEcharts) {
      this.cityEcharts.dispose();
      this.cityEcharts = null;
    }
  },
});
</script>

<style>
.munition-echart {
  width: 110px;
}
.munition-echart .name {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.munition-echart .title {
  padding-left: 10px;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  position: relative;
}
.munition-echart .title::before {
  position: absolute;
  top: 5px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
}
.munition-echart .text {
  padding-left: 20px;
  margin-bottom: 20px;
  font-size: 20px;
}
.munition-echart .text:last-of-type {
  margin: 0;
}

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


<style lang="scss" scoped>
.echart-item {
  flex: 1;
  height: 300px;
  box-sizing: border-box;
  margin-top: 10px;
}

.title2X {
  margin-top: 15px;
  font-size: 14px;
  color: #4198ff;
}
</style>
