<template>
  <div id="hjldsa">
    <div
      id="semi"
      ref="fgkjh"
      :style="`height:100%;`"
    ></div>
  </div>
</template>
  
  <script>
import yan from "@/assets/commjs/color";
import { getVoltageToleranceCurveData } from "@/api/statistics";
import { dicData } from "@/assets/commjs/dictypeData";
export default {
  props: {
    deptIndex: {
      type: String,
      default: undefined,
    },
    item: {
      type: Array,
      default: undefined,
    },
    volateLevelData: {
      type: Array,
      default: undefined,
    },
    typeData: {
      type: Array,
      default: undefined,
    },
  },
  data() {
    return {
      isLoading1: false,
      height: null,
      zoom: undefined, //图表焦点校验
      //ITIC模拟数据
      datalist: [],
      pointI: [],
      pointIun: [],
      pointF: [],
      pointFun: [],
      white: "#ffffff",
      top: [
        [0.001, 200],
        [0.003, 140],
        [0.003, 120],
        [0.5, 120],
        [0.5, 110],
        [10, 110],
        [1000, 110],
      ],
      bot: [
        [0.02, 0],
        [0.02, 70],
        [0.5, 70],
        [0.5, 80],
        [10, 80],
        [10, 90],
        [1000, 90],
      ],
      part: [
        [0.05, 0],
        [0.05, 50],
        [0.2, 50],
        [0.2, 70],
        [0.5, 70],
        [0.5, 80],
        [10, 80],
        [1000, 80],
      ],
      classificationData: [],
      form: {
        deptIndex: "",
        monitorFlag: 2,
        powerFlag: 2,
        serverName: "event-boot",
        searchBeginTime: this.getCurrentMonthFirst(),
        searchEndTime: this.getNowFormatDate(),
        statisticalType: {},
        loadType: null,
        scale: null,
      },
      myChartes: "",
    };
  },
  created() {
    this.getclassificationData();
  },
  mounted() {
    this.form.deptIndex = JSON.parse(
      window.sessionStorage.getItem("Info")
    ).deptId;
    this.resizeEcharth();
    this.zoom = 1 / document.body.style.zoom;
    window.addEventListener("resize", () => {
      this.zoom = 1 / document.body.style.zoom;
      if (this.myChartes !== "") {
        this.myChartes.resize();
      }
    });
    this.$nextTick(() => {
      this.semif();
    });
  },
  methods: {
    //公共的方法
    gongfunction() {
      var standI = 0;
      var unstandI = 0;
      var standF = 0;
      var unstandF = 0;
      this.pointI = [];
      this.pointIun = [];
      this.pointF = [];
      this.pointFun = [];
      var total = 0;
      // console.log(this.datalist);
      total = this.datalist.length;
      if (total == 0) {
      } else {
        for (var i = 0; i < this.datalist.length; i++) {
          var point = [];
          var xx = this.datalist[i].persistTime;
          var yy = this.datalist[i].eventValue * 100;
          var time = this.datalist[i].time.replace("T", " ");
          var company = this.datalist[i].gdName;
          var substation = this.datalist[i].subName;
          var index = this.datalist[i].lineId;
          var eventId = this.datalist[i].eventId;

          point = [xx, yy, time, company, substation, index, eventId];

          if (xx <= 0.003) {
            var line = 0;
            line = 230 - 30000 * xx;
            if (yy > line) {
              unstandI++;
              this.pointIun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            } else {
              standI++;
              this.pointI.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            }
          } else if (xx <= 0.02) {
            if (yy > 120) {
              unstandI++;
              this.pointIun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            } else {
              standI++;
              this.pointI.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            }
          } else if (xx <= 0.5) {
            if (yy > 120 || yy < 70) {
              unstandI++;
              this.pointIun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            } else {
              standI++;
              this.pointI.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            }
          } else if (xx <= 10) {
            if (yy > 110 || yy < 80) {
              unstandI++;
              this.pointIun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            } else {
              standI++;
              this.pointI.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            }
          } else {
            if (yy > 110 || yy < 90) {
              unstandI++;
              this.pointIun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            } else {
              standI++;
              this.pointI.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            }
          }

          if (xx < 0.05) {
            standF++;
            this.pointF.push({
              value: point,
              itemStyle: { normal: { color: "green" } },
            });
          } else if (xx < 0.2) {
            if (yy > 50) {
              standF++;
              this.pointF.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            } else {
              unstandF++;
              this.pointFun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            }
          } else if (xx < 0.5) {
            if (yy > 70) {
              standF++;
              this.pointF.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            } else {
              unstandF++;
              this.pointFun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            }
          } else {
            if (yy > 80) {
              standF++;
              this.pointF.push({
                value: point,
                itemStyle: { normal: { color: "green" } },
              });
            } else {
              unstandF++;
              this.pointFun.push({
                value: point,
                itemStyle: { normal: { color: "red" } },
              });
            }
          }
        }
      }
    },
    //获取统计类型
    getclassificationData() {
      // debugger
      var code = "Statistical_Type";
      this.classificationData = dicData(code, ["Load_Type"]);
      this.form.statisticalType = this.classificationData[0];
      // debugger
    },
    //获取当月第一天日期
    getCurrentMonthFirst() {
      var date = new Date();
      var year = date.getFullYear();
      date.setDate(1);
      var month = parseInt(date.getMonth() + 1);
      var day = date.getDate();
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      return year + "-" + month + "-" + day;
    },
    //获取当天日期
    getNowFormatDate() {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      return year + "-" + month + "-" + day;
    },
    //SeMIf4c曲线图
    async semif() {
      this.isLoading1 = true;
      this.datalist = [];
      this.form.searchBeginTime = this.item[0];
      this.form.searchEndTime = this.item[1];
      this.form.deptIndex = this.deptIndex;
      this.form.loadType = this.typeData;
      this.form.scale = this.volateLevelData;
      const data = await getVoltageToleranceCurveData(this.form);
      data.data.voltageToleranceCurveDataList.forEach((item) => {
        // console.log(item);
        if (item.eventValue < 2 && item.eventValue > 0) {
          this.datalist.push(item);
        }
      });
      // this.datalist = data.data.voltageToleranceCurveDataList;
      this.gongfunction();
      let white = "#000";
      const echarts = require("echarts");
      let domID1 = document.getElementById("semi");
      var myChartes = echarts.init(domID1);
      this.myChartes = myChartes;
      setTimeout(() => {
        (domID1.style.width =
          document.getElementById("hjldsa").offsetWidth - 0 + "px"),
          (domID1.style.height =
            window.sessionStorage.getItem("appheight") - 330 + "px");
      }, 0);
      this.isLoading1 = false;
      myChartes.clear();
      let echartsColor = JSON.parse(window.localStorage.echartsColor);
      var option = {
        // backgroundColor: "#f9f9f9", //地图背景色深蓝
        title: {
          text: "F47曲线",
          x: "center",
          textStyle: {
            fontSize: yan.EchartObject.titleNamefontSize1,
            color: echartsColor.WordColor,
          },
        },
        grid: yan.EchartObject.grid12,

        tooltip: {
          trigger: "item",
          show: true,
          axisPointer: {
            type: "shadow",
            label: {
              color: "#fff",
              fontSize: 16,
            },
          },
          textStyle: {
            color: "#fff",
            fontStyle: "normal",
            opacity: 0.35,
            fontSize: 14,
          },
          backgroundColor: "rgba(0,0,0,0.35)",
          borderWidth: 0,
          formatter: function (a) {
            let relVal = "";
            relVal =
              "<font style='color:" +
              "'>供电公司:" +
              "&nbsp" +
              "&nbsp" +
              a.value[3] +
              "</font><br/>";
            relVal +=
              "<font style='color:" +
              "'>变电站:" +
              "&nbsp" +
              "&nbsp" +
              a.value[4] +
              "</font><br/>";
            relVal +=
              "<font style='color:" +
              "'>发生时刻:" +
              "&nbsp" +
              "&nbsp" +
              a.value[2] +
              "</font><br/>";
            relVal +=
              "<font style='color:" +
              "'>持续时间:" +
              "&nbsp" +
              "&nbsp" +
              a.value[0].toFixed(3) +
              "s</font><br/>";
            relVal +=
              "<font style='color:" +
              "'>特征幅值:" +
              "&nbsp" +
              "&nbsp" +
              a.value[1].toFixed(3) +
              "%</font>";
            return relVal;
          },
        },
        legend: {
          data: ["分割线", "可容忍事件", "不可容忍事件"],
          // selectedMode: false,
          top: "30px",
          left: "10px",
          textStyle: {
            color: echartsColor.WordColor,
          },
        },
        color: ["yellow", "green", "red"],
        xAxis: [
          {
            type: "log",
            min: "0.001",
            max: "1000",
            splitLine: {
              show: false,
              lineStyle: {
                color: echartsColor.thread,
              },
            },
            axisLabel: {
              textStyle: {
                fontFamily: yan.EchartObject.fontFamily,
                color: echartsColor.WordColor,
                fontSize: yan.EchartObject.xNamefontSize,
              },
            },
            name: "s",
          },
        ],
        yAxis: [
          {
            type: "value",
            splitNumber: 10,
            minInterval: 3,
            name: yan.EchartObject.nuit1,
            axisLine: {
              show: true,
              lineStyle: {
                color: echartsColor.thread,
              },
            },
            axisLabel: {
              color: echartsColor.WordColor,
              fontSize: yan.EchartObject.yNamefontSize,
            },
          },
        ],
        series: [
          {
            name: "分割线",
            type: "line",
            data: this.part,
            showSymbol: false,
            tooltips: {
              show: false,
            },
          },
          {
            name: "可容忍事件",
            type: "scatter",
            symbol: "circle",
            data: this.pointF,
          },
          {
            name: "不可容忍事件",
            type: "scatter",
            symbol: "circle",
            data: this.pointFun,
          },
        ],
      };
      let pointFData = this.pointF;
      let pointFunData = this.pointFun;
      this.$emit("transfer-data1", data, pointFData, pointFunData);
      setTimeout(function () {
        myChartes.resize();
      }, 0);
      myChartes.setOption(option)
window.echartsArr.push(myChartes);
      setTimeout(() => {
        myChartes.resize();
      }, 100);
      let _this = this;
      myChartes.off("click");
      myChartes.on("click", function (params) {
        _this.$emit("BoXi", params.data.value[6]);
      });
      _this.$erd.listenTo(_this.$refs.fgkjh, (element) => {
        _this.$nextTick(() => {
          domID1.style.width =
            document.getElementById("hjldsa").offsetWidth - 0 + "px";
          myChartes.resize();
        });
      });
    },
    resizeEcharth() {
      var elementResizeDetectorMaker = require("element-resize-detector");
      var erd = elementResizeDetectorMaker();
      erd.listenTo(document.getElementById("hjldsa"), (element) => {
        // let  domeId=document.getElementById("ceshi")
        let domeId2 = document.getElementById("semi");
        domeId2.style.width =
          document.getElementById("hjldsa").offsetWidth - 0 + "px";
        if (this.myChartes !== "") {
          this.myChartes.resize();
        }
      });
    },
  },
};
</script>