<template>
  <MapTitle text="地方为国防和军队建设培养输送人才情况" />
  <div class="describes">
    <div class="card-items">
      <div class="item">
        <span>入伍总人数</span>
        <div class="value-des">
          <span>1470</span>人
        </div>
      </div>

      <div class="item">
        <span>入伍总比列</span>
        <div class="value-des">
          <span>0.5%</span>
        </div>
      </div>
    </div>

    <div class="text">各市输送人才情况</div>
  </div>

  <div id="locPerson" class="incom-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";

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

/**
 * 地方为国防和军队建设培养输送人才情况
 */
export default defineComponent({
  props: {},
  components: {
    MapTitle,
  },
  setup() {},
  data() {
    return {
      mapEcharts: null,
    };
  },
  mounted() {
    this.initEcharts();
  },
  watch: {
    seriesData() {
      this.mapEcharts.showLoading();
      this.mapEcharts.setOption(this.optionBar);
      this.mapEcharts.hideLoading();
    },
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      return Mock[this.curDataType].seriesData;
    },
    optionBar() {
      const seriesBar = this.seriesData.map((item) => {
        return {
          name: "入伍人数",

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

          data: item.value.map((item) => {
            return {
              value: item,
              label: "入伍人数",
            };
          }),
        };
      });

      const seriesLine = this.seriesData.map((item) => {
        return {
          name: "入伍比率",
          type: "line",
          stack: "Total",
          itemStyle: {
            color: "#429a9d",
          },
          yAxisIndex: 1,
          emphasis: {
            focus: "series",
          },

          data: item.rate.map((item) => {
            return {
              value: item,
              label: "入伍比率",
            };
          }),
        };
      });

      // const seriesLine = {};

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

            html += `<div class="person-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="person-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: "12%",
          left: "0%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            name: "入伍人数",
            min: 0,
            max: 700,
          },
          {
            type: "value",
            name: "入伍率(%)",
            min: 0,
            max: 1,
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisCity,
        },
        series: [...seriesBar, ...seriesLine],
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById("locPerson");
      const mapEcharts = echarts.init(dom);
      mapEcharts.setOption(this.optionBar);
      this.mapEcharts = mapEcharts;
    },
  },
  unmounted() {
    if (!this.mapEcharts) {
      return;
    }
    this.mapEcharts.dispose();
    this.mapEcharts = null;
  },
});
</script>
<style>
.person-echart {
  width: 110px;
}
.person-echart .name {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.person-echart .title {
  padding-left: 10px;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  position: relative;
}
.person-echart .title::before {
  position: absolute;
  top: 5px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
}
.person-echart .text {
  padding-left: 20px;
  margin-bottom: 20px;
  font-size: 20px;
}
.person-echart .text:last-of-type {
  margin: 0;
}

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

<style lang="scss" scoped>
.incom-echarts-line {
  flex: 1;
  height: 250px;
  box-sizing: border-box;
}
.describes {
  margin: 0 10px 5px 0;
  box-sizing: border-box;
  .card-items {
    margin: 15px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    .item {
      flex: 1;
      padding: 8px 15px;
      margin-right: 15px;
      background: #f8f9fb;
      border-radius: 5px;
      font-size: 14px;
      color: #333;
      display: flex;
      align-items: center;
      justify-content: space-between;
      &:last-of-type {
        margin-right: 0;
      }
      .value-des {
        font-size: 12px;
        color: #999;
        display: flex;
        align-items: center;
        span {
          font-size: 20px;
          margin-right: 5px;
          color: #4198ff;
        }
      }
    }
  }
  .text {
    color: #4198ff;
    margin-top: 10px;
    font-size: 14px;
  }
}
</style>
