<template>
  <div class="h-full" style="position: relative">
    <div class="c-title flex justify-between">
      <span>作业计划</span>
      <el-button
        style="font-size: 16px; position: absolute; right: 65%; top: 5px"
        size="mini"
        v-if="currentMap != '衡阳市'"
        type="text"
        @click="back()"
        >返回</el-button
      >
      <div>
        <el-date-picker
          size="mini"
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyyMMdd"
          style="width: 240px"
          @change="init"
        >
        </el-date-picker>
        <el-select
          style="width: 90px; margin-left: 10px"
          size="mini"
          v-model="type"
          @change="typeChange"
        >
          <el-option label="作业" value="1"></el-option>
          <el-option label="风险" value="2"></el-option>
        </el-select>
      </div>
    </div>

    <div style="display: flex; justify-content: space-between; height: 100%">
      <div id="hy-map" style="width: 100%; height: 100%"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
const mapEnum = {
  衡阳市: require("./衡阳市.json"),
  常宁市: require("./常宁市.json"),
  衡东县: require("./衡东县.json"),
  衡南县: require("./衡南县.json"),
  衡山县: require("./衡山县.json"),
  衡阳县: require("./衡阳县.json"),
  耒阳市: require("./耒阳市.json"),
  南岳区: require("./南岳区.json"),
  祁东县: require("./祁东县.json"),
  石鼓区: require("./石鼓区.json"),
  雁峰区: require("./雁峰区.json"),
  蒸湘区: require("./蒸湘区.json"),
  珠晖区: require("./珠晖区.json"),
};
import { getWorkNumStatisticsHome } from "@/api/powerdistribution/home";
import moment from "moment";
export default {
  name: "map",
  data() {
    return {
      dateRange: [
        moment().endOf("day").format("YYYYMMDD"),
        moment().endOf("day").format("YYYYMMDD"),
      ],
      loading: true,
      currentMap: "衡阳市",
      dataMap: {},
      type: "1",
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    typeChange(e) {
      this.init();
    },
    async init() {
      await this.getData();
    },
    async getData() {
      const params = {
        startDate: this.dateRange[0],
        endDate: this.dateRange[1],
      };
      const data = await getWorkNumStatisticsHome(params);
      Object.keys(mapEnum).forEach((key) => {
        (data || []).map((ite) => {
          if (key.includes(ite.company)) {
            this.dataMap[key] = { ...ite };
          }
        });
      });
      this.currentMap = "衡阳市";
      this.initMap();
    },
    back() {
      this.currentMap = "衡阳市";
      this.initMap();
    },
    async initMap(json = require("./衡阳市.json")) {
      var data = [],
        geoCoordMap = {};
      json.features.forEach((v) => {
        geoCoordMap[v.properties.name] = v.properties.center;
        data.push({
          name: v.properties.name,
          value: this.dataMap[v.properties.name] || {},
        });
      });
      var convertData = function (data) {
        var res = [];
        for (var i = 0; i < data.length; i++) {
          var geoCoord = geoCoordMap[data[i].name];
          if (geoCoord) {
            res.push({
              name: data[i].name,
              value: geoCoord.concat(data[i].value),
            });
          }
        }
        return res;
      };
      const chartDom = document.querySelector("#hy-map");
      const myChart = echarts.init(chartDom);
      echarts.registerMap("HK", json);
      let option = {
        tooltip: {
          trigger: "item",
          alwaysShowContent: true,
          formatter: (params) => {
            let str = "";
            Object.keys(this.dataMap).forEach((key) => {
              if (this.type == "1") {
                str +=
                  key +
                  "-作业数" +
                  `<span class="orange">${
                    this.dataMap[key]?.planSum || 0
                  }</span> ` +
                  `取消数 <span class="orange">${
                    this.dataMap[key]?.cancelSum || 0
                  }</span> ` +
                  `执行率 <span class="orange">${
                    this.dataMap[key]?.executeRatio || "0%"
                  }</span> <br/>`;
              }
              if (this.type == "2") {
                str +=
                  key +
                  "-风险作业数" +
                  `<span class="orange">${
                    this.dataMap[key]?.riskSum || 0
                  }</span> ` +
                  `三级作业数 <span class="orange">${
                    this.dataMap[key]?.planSum3 || 0
                  }</span> ` +
                  `四级作业数 <span class="orange">${
                    this.dataMap[key]?.planSum4 || 0
                  }</span> ` +
                  `五级作业数 <span class="orange">${
                    this.dataMap[key]?.planSum5 || 0
                  }</span> <br/>`;
              }
            });
            // const v = Array.isArray(params.value)
            //   ? params.value[2]
            //   : params.value;
            if (this.type == "2") {
              return this.currentMap == "衡阳市"
                ? str
                : params.name +
                    "-风险作业数 " +
                    `<span class="orange">${
                      params.data.value?.riskSum || 0
                    }</span> ` +
                    `三级作业数 <span class="orange">${
                      params.data.value?.planSum3 || 0
                    }</span> ` +
                    `四级作业数 <span class="orange">${
                      params.data.value?.planSum4 || 0
                    }</span> ` +
                    `五级作业数 <span class="orange">${
                      params.data.value?.planSum5 || 0
                    }</span> <br/>`;
            }
            return this.currentMap == "衡阳市"
              ? str
              : params.name +
                  "-作业数 " +
                  `<span class="orange">${
                    params.data.value?.planSum || 0
                  }</span> ` +
                  `取消数 <span class="orange">${
                    params.data.value?.cancelSum || 0
                  }</span> ` +
                  `执行率 <span class="orange">${
                    params.data.value?.executeRatio || "0%"
                  }</span> <br/>`;
          },
        },
        // visualMap: {
        //   show: false,
        //   seriesIndex: [0],
        //   inRange: {
        //     color: ["#02367C", "#3a92fe"],
        //   },
        // },
        // visualMap: {
        //   show: true,
        //   min: 0,
        //   max: 20000,
        //   left: "10%",
        //   bottom: "10%",
        //   calculable: true,
        //   seriesIndex: [0],
        //   inRange: {
        //     color: ["#04387b", "#467bc0"], // 蓝绿
        //   },
        // },
        geo: [
          {
            show: true,
            map: "HK",
            zoom: 1.1,
            roam: false,
            itemStyle: {
              normal: {
                borderColor: "#48B6ED",
                borderWidth: 5,
              },
            },
          },
        ],
        series: [
          {
            type: "map",
            mapType: "HK", //自定义扩展图表类型
            zoom: 1.1,
            roam: false,
            label: {
              normal: {
                show: true,
                formatter: "{b}",
                // : {c}
                textStyle: {
                  color: "#fff",
                },
              },
              emphasis: {
                show: true,
                textStyle: {
                  color: "#fff",
                },
              },
            },
            itemStyle: {
              normal: {
                areaColor: "#02367C",
                borderColor: "#004096",
                borderWidth: 1,
              },
              emphasis: {
                areaColor: "#004096",
              },
            },
            select: {
              label: {
                // 选中区域的label(文字)样式
                color: "#fff",
              },
              itemStyle: {
                areaColor: "#02367C", // 选中的区域颜色
              },
            },
            data: data,
          },
          {
            name: "Top 5",
            type: "effectScatter",
            coordinateSystem: "geo",
            data: convertData(
              data.sort(function (a, b) {
                return b.value - a.value;
              })
              // .slice(0, 10)
            ),
            symbolSize: function (val) {
              return 6;
            },
            showEffectOn: "render",
            rippleEffect: {
              brushType: "stroke",
            },
            hoverAnimation: true,
            label: {
              show: false,
              normal: {
                formatter: "{b}",
                position: "left",
                show: false,
              },
            },
            itemStyle: {
              normal: {
                color: (params) => {
                  const [x, y, v] = params.data.value;
                  if (this.type == "1") {
                    return v?.planSum ? "#44936c" : "gray";
                  }
                  return v?.riskSum ? "#44936c" : "gray";
                }, //标志颜色,
                shadowBlur: 2,
              },
            },
            zlevel: 1,
          },
        ],
      };

      myChart.setOption(option);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
      myChart.on("click", (params) => {
        this.currentMap = params.name;
        this.initMap(mapEnum[params.name]);
      });
      myChart.dispatchAction({
        type: "showTip",
        seriesIndex: 0,
        dataIndex: 3,
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.c-title {
  border-left: 4px solid #00a4ff;
  padding-left: 10px;
  color: #00a4ff;
}
.orange {
  color: orange !important;
}
</style>
