<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>{{ item.value || "--" }}</span>
          {{index === 0?"亿元":"%"}}
        </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";

// const seriesData = [
//   {
//     name: "产值规模",
//     type: "level_2",
//     echartType: "bar",
//     barValue: [587.5, 137.6, 15.3, 71.1, 120.8, 65, 72, 14, 8.2, 20.3, 1.6],
//     growthRate: [19, 12.8, -0.1, 31.7, 28.5, 33.3, 80, 17, 0.4, 14.5, 33],
//   },
// ];

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

/**
 * 全省军民协同创新平台情况/国防科技工业产值规模分布 -- 柱状图
 */
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() {
      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 seriesBarList = this.seriesData.map((item, index) => {
        return {
          name: item.name,
          type: "bar",
          itemStyle: {
            color: "#4198FF",
          },
          barWidth: 12,
          data: item.barValue.map((barItem, barIndex) => {
            return {
              value: barItem,
              label: "产值规模",
            };
          }),
        };
      });

      const seriesLineList = this.seriesData.map((item) => {
        return {
          name: "增长率",
          type: "line",
          itemStyle: {
            color: "#75ce95",
          },
          lineStyle: {
            type: "dashed",
          },
          yAxisIndex: 1,
          data: item.growthRate.map((value) => {
            return {
              value,
              label: "增长率",
            };
          }),
        };
      });

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

            html += `<div class="industry-tooltip-des ">
                <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="industry-tooltip-des">
                <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;
          },
        },
        grid: {
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        legend: {
          right: "2%",
        },
        yAxis: [
          {
            type: "value",
            name: "金额(亿元)",
          },
          {
            type: "value",
            name: "增长率%",
            min: -10,
            max: 100,
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: [...seriesBarList, ...seriesLineList],
      };
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById(this.echartsId);
      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>
.industry-tooltip-des:first-of-type {
  margin-bottom: 5px;
}
.industry-tooltip-des .name {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.industry-tooltip-des {
  width: 100px;
}

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

.industry-tooltip-des .text-color_1 {
  color: #4198ff;
}
.industry-tooltip-des .text-color_2 {
  color: #75ce95;
}
.industry-tooltip-des .color_1::before {
  background: #4198ff;
}
.industry-tooltip-des .color_2::before {
  background: #75ce95;
}
</style>

<style lang="scss" scoped>
.incom-echarts-line {
  flex: 1;
  height: calc(100% - 160px);
  box-sizing: border-box;
}
.describes {
  margin: 0 10px;
  height: 120px;
  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: #f8f9fb;
      border-radius: 5px;
      font-size: 14px;
      color: #333;

      &:last-of-type {
        margin-right: 0;
      }
      .value-des {
        margin-top: 10px;
        font-size: 12px;
        color: #999;
        span {
          font-size: 20px;
          margin-right: 5px;
          color: #4198ff;
        }
      }
    }
  }
  .text {
    color: #4198ff;
    margin-top: 10px;
    font-size: 14px;
  }
}
</style>
