<template>
  <MapTitle :text="echartsTitle" />
  <div class="describes">
    <div class="card-items">
      <div class="item" v-for="(item, index) in cardItems" :key="item.text">
        <span>{{ item.label }}</span>
        <div class="value-des">
          <span class="value-des-val">{{ item.value || "--" }}</span>
          <span v-if="index === 0" class="money"> 亿元</span>
          <span v-if="index === 1"> %</span>
        </div>
      </div>
    </div>

    <div class="text">{{ echartsDesText }}</div>
  </div>

  <div :id="echartsId" 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";

let MAP_ECHARTS = null;

const xAxisValue = ["2018年", "2019年", "2020年", "2021年"];

/**
 * 全省军民协同创新平台情况/国防科技工业产值规模分布 -- 折线图
 */
export default defineComponent({
  props: {
    echartsTitle: {
      type: String,
      required: true,
    },
    echartsDesText: {
      type: String,
      required: true,
    },
    cardItems: {
      type: Array,
      required: true,
      dafault: () => [],
    },
    echartsData: {
      type: Array,
      // required: true,
      dafault: () => [],
    },
    echartsAxisValue: {
      type: Array,
      // required: true,
      dafault: () => [],
    },
    echartsId: {
      type: String,
      required: true,
    },
  },
  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 seriesLine = this.seriesData.map((item) => {
        return {
          type: "line",
          itemStyle: {
            color: "#4198ff",
          },
          emphasis: {
            focus: "series",
          },
          label: {
            show: true,
            position: "top",
            color: "#19ecff",
          },
          areaStyle: {
            opacity: 0.8,
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: "#2b81cb",
              },
              {
                offset: 1,
                color: "#0e3c73",
              },
            ]),
          },

          data: item.value,
        };
      });

      return {
        tooltip: {
          trigger: "axis",
          className: "extend-tooltip",
          axisPointer: {
            type: "line",
            lineStyle: {
              type: "solid", //  打开就是实线类型
              color: "#4198ff",
            },
          },
          formatter:
            "<span class='title'>{b0}</span>: <br /> <span class='bar-text'>{c0}</span> 亿元",
        },
        legend: {},
        grid: {
          top: "5%",
          left: "0%",
          right: "5%",
          bottom: "9%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
          show: false,
        },
        xAxis: {
          type: "category",
          data: xAxisValue,
          //横坐标轴文字颜色
          axisLabel: {
            show: true,
            textStyle: {
              color: "#d4f7ff", //更改坐标轴文字颜色
            },
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#125ba3",
            },
          },
        },
        series: seriesLine,
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById(this.echartsId);
      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>
.extend-tooltip .title {
  color: #333;
  font-weight: bold;
}
.extend-tooltip .bar-text {
  padding-left: 10px;
  color: #4198ff;
}
</style>
<style lang="scss" scoped>
.incom-echarts-line {
  flex: 1;
  height: calc(100% - 190px);
  box-sizing: border-box;
}
.describes {
  margin: 0 10px;
  height: 150px;
  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: 10px 15px;
      margin-right: 15px;
      background: rgba(25, 125, 237, 0.15);
      border-radius: 5px;
      font-size: 14px;
      color: #d4f7ff;

      &:last-of-type {
        margin-right: 0;
      }
      .value-des {
        margin-top: 10px;
        font-size: 20px;
        color: #19ecff;
        .value-des-val {
          margin-right: 5px;
        }
        .money {
          font-size: 12px;
          color: #fff;
        }
        .value-des-unit {
          color: #4198ff;
        }
      }
    }
  }
  .text {
    color: #ffffff;
    margin-top: 10px;
    font-size: 14px;
  }
}
</style>
