<template>
  <div class="industrial">
    <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
            >{{item.unit }}
          </div>
        </div>
      </div>

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

    <div :id="echartsId" class="incom-echarts-line"></div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import MapTitle from "@/components/mapTitle/index.vue";
import * as echarts from "echarts";

const seriesData = [
  {
    name: "国家部委、军委机关批复立项项目",
    type: "level_2",
    echartType: "bar",
    color: "#4198ff",
    barValue: [87, 137, 15, 71, 120, 65, 72, 14, 80, 20, 80],
  },
  {
    name: "省直部门新认定的领跑技术(产品)",
    type: "level_2",
    echartType: "bar",
    color: "#fac858",
    barValue: [187, 37, 115, 91, 180, 65, 32, 80, 89, 50, 80],
  },
];

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();
  },
  computed: {
    optionBar() {
      const seriesBarList = seriesData.map((item, index) => {
        return {
          name: item.name,
          type: "bar",
          itemStyle: {
            color: item.color,
          },
          barWidth: 12,
          data: item.barValue.map((barItem, barIndex) => {
            return {
              value: barItem,
              label: item.name,
            };
          }),
        };
      });

      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: "数量(个)",
        },
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: [...seriesBarList],
      };
    },
  },
  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: 200px;
}

.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: #fac858;
}
.industry-tooltip-des .color_1::before {
  background: #4198ff;
}
.industry-tooltip-des .color_2::before {
  background: #fac858;
}
</style>

<style lang="scss" scoped>
.industrial {
  height: 100%;
  .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>
