<template>
  <MapTitle text="经济建设与国防密切相关的建设项目贯彻国防要求落实情况" />
  <el-row class="economic-info" justify="space-between">
    <el-col :span="11">
      <div class="card-item">
        <span class="card-text">{{ countryData.name || "--" }}</span>

        <div class="info-wrapper">
          <div class="info-details">
            <div
              class="info-item"
              v-for="item in countryData.info"
              :key="item.label"
            >
              <span class="title">{{ item.label || "--" }}</span>
              <div>
                <span>{{ item.value || "--" }}</span>
                {{ item.unit }}
              </div>
            </div>
          </div>
          <!-- 环形进度条 -->
          <div v-show="false" id="circularEchart" class="circular"></div>
        </div>
      </div>
    </el-col>

    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>

    <el-col class="increase-rate" :span="12">
      <div class="card-item">
        <span class="card-text">{{ provinceData.name || "--" }}</span>

        <div class="info-wrapper">
          <div class="info-details">
            <div
              class="info-item"
              v-for="item in provinceData.info"
              :key="item.label"
            >
              <span class="title">{{ item.label || "--" }}</span>
              <div>
                <span>{{ item.value || "--" }}</span>
                {{ item.unit }}
              </div>
            </div>
          </div>
          <!-- 环形进度条 -->
          <div id="cirRightEchart" class="circular"></div>
        </div>
      </div>
    </el-col>
  </el-row>

  <div class="bar-title">军民融合"十百千"项目投资金额及进度情况</div>
  <div id="economicConstruct" class="economic-construct"></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;
let CRI_RIGHT_ECHART = null;
const xAxisValue = [
  "杭州",
  "宁波",
  "温州",
  "湖州",
  "嘉兴",
  "绍兴",
  "金华",
  "衢州",
  "舟山",
  "台州",
  "丽水",
];

/**
 * 经济建设与国防密切相关的建设项目贯彻国防要求落实情况——柱状图
 */
export default defineComponent({
  components: {
    MapTitle,
  },
  setup() {},
  data() {
    return {
      mapEcharts: null,
      cirLeftCharts: null,
      cirRightEchart: null,
    };
  },
  mounted() {
    this.initEcharts();
    this.initCircularEcharts();
  },
  computed: {
    ...mapState({
      curDataType: (state) => state.curDataType,
    }),
    seriesData() {
      return Mock[this.curDataType].seriesData;
    },
    countryData() {
      return Mock[this.curDataType].countryData;
    },
    provinceData() {
      return Mock[this.curDataType].provinceData;
    },
    optionBar() {
      const seriesBar = {
        name: "投资金额",
        type: "bar",
        itemStyle: {
          color: "#4198ff",
        },
        barWidth: 20,
        data: this.seriesData.value.map((item) => {
          return {
            name: item.name,
            value: item,
            label: "投资金额",
          };
        }),
      };

      const seriesLine = {
        name: "项目进度",
        type: "line",
        itemStyle: {
          color: "#3aba67",
        },
        yAxisIndex: 1,
        data: this.seriesData.rate.map((item) => {
          return {
            value: item.value,
            label: "项目进度",
          };
        }),
      };

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

            html += `<div class="economic-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="economic-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: "20%",
          left: "5%",
          right: "5%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            name: "金额(亿元)",
          },
          {
            type: "value",
            name: "进度%",
            min: 0,
            // max: 100,
            splitLine: {
              show: false,
            },
          },
        ],
        xAxis: {
          type: "category",
          data: xAxisValue,
        },
        series: [seriesBar, seriesLine],
      };
    },
    ptionCirLeft() {
      const value = 1;
      return {
        backgroundColor: "#fff",
        title: [
          {
            text: `建设进度`,
            left: "45%",
            top: "30%",
            textAlign: "center",
            textStyle: {
              fontSize: "16",
              fontWeight: "400",
              color: "#333",
              textAlign: "center",
            },
            subtext: `${(value * 100).toFixed(0)}%`,
            subtextStyle: {
              fontSize: "22",
              color: "#3aba67",
              textAlign: "center",
            },
          },
        ],
        polar: {
          radius: ["100%", "80%"],
        },
        angleAxis: {
          max: 100,
          clockwise: false,
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          splitLine: {
            show: false,
          },
        },
        radiusAxis: {
          type: "category",
          show: true,
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        series: [
          {
            name: "",
            type: "bar",
            roundCap: true,
            z: 2,
            showBackground: true,
            backgroundStyle: {
              color: "#e3e3e3",
            },
            data: [value * 100],
            coordinateSystem: "polar",
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0.5, 1, [
                  {
                    offset: 0,
                    color: "#60f795",
                  },
                  {
                    offset: 0.7,
                    color: "#3aba67d6",
                  },
                  {
                    offset: 1,
                    color: "#3aba67",
                  },
                ]),
              },
            },
          },
        ],
      };
    },
    ptionCirRight() {
      const value = this.provinceData?.process || 0;
      return {
        backgroundColor: "#fff",
        title: [
          {
            text: `建设进度`,
            left: "45%",
            top: "30%",
            textAlign: "center",
            textStyle: {
              fontSize: "16",
              fontWeight: "400",
              color: "#333",
              textAlign: "center",
            },
            subtext: `${value * 100}%`,
            subtextStyle: {
              fontSize: "16",
              color: "#4198ff",
              textAlign: "center",
            },
          },
        ],
        polar: {
          radius: ["100%", "80%"],
        },
        angleAxis: {
          max: 100,
          clockwise: false,
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          splitLine: {
            show: false,
          },
        },
        radiusAxis: {
          type: "category",
          show: true,
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        series: [
          {
            name: "",
            type: "bar",
            roundCap: true,
            z: 2,
            showBackground: true,
            backgroundStyle: {
              color: "#e3e3e3",
            },
            data: [value * 100],
            coordinateSystem: "polar",
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0.5, 1, [
                  {
                    offset: 0,
                    color: "#7eb9ff",
                  },
                  {
                    offset: 0.7,
                    color: "#4198ffe8",
                  },
                  {
                    offset: 1,
                    color: "#4198ff",
                  },
                ]),
              },
            },
          },
        ],
      };
    },
  },
  watch: {
    seriesData() {
      MAP_ECHARTS && MAP_ECHARTS.setOption(this.optionBar, true);
    },
    provinceData() {
      CRI_RIGHT_ECHART && CRI_RIGHT_ECHART.setOption(this.ptionCirRight, true);
    },
  },
  methods: {
    initEcharts() {
      const dom = document.getElementById("economicConstruct");
      MAP_ECHARTS = echarts.init(dom);
      MAP_ECHARTS.setOption(this.optionBar);
      this.mapEcharts = MAP_ECHARTS;
    },
    initCircularEcharts() {
      const dom = document.getElementById("circularEchart");
      const domCirRightEchart = document.getElementById("cirRightEchart");

      const cirLeftCharts = echarts.init(dom);
      CRI_RIGHT_ECHART = echarts.init(domCirRightEchart);

      cirLeftCharts.setOption(this.ptionCirLeft);
      CRI_RIGHT_ECHART.setOption(this.ptionCirRight);
      this.cirLeftCharts = cirLeftCharts;
      this.cirRightEchart = CRI_RIGHT_ECHART;
    },
  },
  unmounted() {
    if (this.mapEcharts) {
      this.mapEcharts.dispose();
      this.mapEcharts = null;
    }
  },
});
</script>
<style>
.economic-echart {
  width: 110px;
}
.economic-echart .name {
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}
.economic-echart .title {
  padding-left: 10px;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  position: relative;
}
.economic-echart .title::before {
  position: absolute;
  top: 5px;
  left: 0;
  content: " ";
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 50%;
}
.economic-echart .text {
  padding-left: 20px;
  margin-bottom: 20px;
  font-size: 20px;
}
.economic-echart .text-color_1 {
  color: #4198ff;
}
.economic-echart .text-color_2 {
  color: #3aba67;
}
.economic-echart .color_1::before {
  background: #4198ff;
}
.economic-echart .color_2::before {
  background: #3aba67;
}
</style>

<style lang="scss" scoped>
.economic-info {
  margin-top: 15px;
  height: 150px;
  box-sizing: border-box;
  .card-text {
    color: #4198ff;
    margin: 8px 0;
  }
  .card-item {
    .info-wrapper {
      margin-top: 5px;
      display: flex;
      // align-items: center;
      justify-content: space-between;
      .info-details {
        .info-item {
          padding-left: 12px;
          &:first-of-type {
            margin-bottom: 8px;
          }
          .title {
            font-weight: bold;
            color: #333;
            font-size: 14px;
            position: relative;
            &::before {
              position: absolute;
              display: inline-block;
              content: " ";
              top: 5px;
              left: -12px;
              width: 8px;
              height: 8px;
              border-radius: 50%;
              background: #ff8553;
            }
          }
          div {
            padding-left: 10px;
            display: flex;
            align-items: center;
            font-size: 14px;
            color: #999;
            span {
              font-size: 20px;
              margin-right: 5px;
              color: #4198ff;
            }
          }
        }
      }

      .circular {
        margin-right: 10px;
        margin-left: 40px;
        width: 100px;
        height: 100px;
      }
    }
  }
  .el-divider {
    margin: 15% auto;
    height: 70%;
    background: #f3f3f3;
  }
}

.bar-title {
  color: #4198ff;
  margin-bottom: 10px;
}
.economic-construct {
  flex: 1;
  min-height: 150px;
  height: calc(100% - 200px);
  box-sizing: border-box;
  margin-top: 10px;
}
</style>
