<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 = [
  "杭州",
  "宁波",
  "温州",
  "湖州",
  "嘉兴",
  "绍兴",
  "金华",
  "衢州",
  "舟山",
  "台州",
  "丽水",
];

/**
 * 全省军民协同创新平台情况/国防科技工业产值规模分布 -- 柱状图
 */
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 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",
            shadowStyle: {
              color: "#1881e120",
            },
          },
          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%",
          textStyle: {
            color: "#d4f7ff",
          },
        },
        yAxis: [
          {
            type: "value",
            name: "金额(亿元)",
            splitLine: {
              lineStyle: {
                // 使用深浅的间隔色
                type: "dashed",
                color: ["#125ba3"],
              },
            },
            axisLine: {
              lineStyle: {
                color: "#d4f7ff",
                // width: 1, //这里是为了突出显示加上的
              },
            },
          },
          {
            type: "value",
            name: "增长率%",
            min: 0,
            max: 100,
            splitLine: {
              lineStyle: {
                // 使用深浅的间隔色
                type: "dashed",
                color: ["#125ba3"],
              },
            },
            axisLine: {
              lineStyle: {
                color: "#d4f7ff",
                // width: 1, //这里是为了突出显示加上的
              },
            },
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisValue,
          //横坐标轴文字颜色
          axisLabel: {
            show: true,
            textStyle: {
              color: "#d4f7ff", //更改坐标轴文字颜色
            },
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#125ba3",
            },
          },
        },
        series: [...seriesBarList, ...seriesLineList],
      };
    },
  },
  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>
.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: 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>
