<template>
  <div ref="areaSeverity" id="severityEcharts" style="width: 100%;"></div>
</template>
<script>
import { dicData } from "@/assets/commjs/dictypeData";
import { getAreaDownStatisticsData } from "@/api/center/DropEvent.js";
import yan from "@/assets/commjs/color";
export default {
  props: {
    item: {
      type: Object,
      default: null,
    },
    item2: {
      type: Object,
      default: null,
    },
    height: {
      type: Number,
      default: null,
    },
    height2: {
      type: Number,
      default: null,
    },
  },
  watch: {
    height2(a, b) {
      if (a == 24) {
        this.$refs.areaSeverity.style.height =
          document.getElementById("conmentId").offsetHeight - 50 + "px";
      } else {
        return;
      }
    },
    height(a, b) {
      if (a == 6 || a == 8 || a == 9 || a == 16) {
        setTimeout(() => {
          let str = document.getElementById("areaseveritytu").style.height;
          let strsec = str.split("p");
          let height = parseInt(strsec) - 50 + "px";
          this.$refs.areaSeverity.style.height = height;
        }, 200);
      } else {
        return;
      }
    },
  },
  data() {
    return {
      classificationData: [],
      areaData: [],
      time: this.timefamt(),
      formData: {
        deptIndex: JSON.parse(window.sessionStorage.getItem("Info")).deptId,
        searchBeginTime: "",
        searchEndTime: "",
      },
    };
  },
  created() {
    this.getclassificationData();
    this.queryData();
  },
  mounted() {
    setTimeout(() => {
      let str = document.getElementById("areaseveritytu").style.height;
      let strsec = str.split("p");
      let height = parseInt(strsec) - 50 + "px";
      this.$refs.areaSeverity.style.height = height;
    }, 200);
  },
  methods: {
    //获取统计类型
    getclassificationData() {
      var code = "Statistical_Type";
      this.classificationData = dicData(code, ["Report_Type"]);
      this.formData.statisticalType = this.classificationData[0];
    },
    queryData() {
      this.formData.searchBeginTime = this.time[0];
      this.formData.searchEndTime = this.time[1];
      getAreaDownStatisticsData(this.formData).then((res) => {
        this.areaData = res.data.param;
        this.qualifiedechart();
      });
    },
    timefamt() {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      month = month > 9 ? month : "0" + month;
      day = day < 10 ? "0" + day : day;
      var arr = [];
      var time1 = year + "-" + month + "-01";
      arr.push(time1);
      var time2 = year + "-" + month + "-" + day;
      arr.push(time2);
      return arr;
    },
    qualifiedechart() {
      let echartsColor = JSON.parse(window.localStorage.echartsColor);
      let xdata = [];
      let xVal = [];
      this.areaData.forEach((item) => {
        xdata.push(item.name);
        xVal.push(item.count);
      });
      const echarts = require("echarts");
      var myChartes = echarts.init(document.getElementById("severityEcharts"));
      myChartes.clear();
      //let echartsColor = JSON.parse(window.localStorage.echartsColor);
      let option = {
        title: {
          text: "区域暂降次数",
          left: "center",
          textStyle: {
            fontWeight: "400",
            color: "#fff",
            fontSize: 16,
          },
        },
        legend: {
          data: ["暂降次数"],
          right: "10px",
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "axis",

          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,
        },
        dataZoom: yan.EchartObject.dataZoom,
        grid: yan.EchartObject.gridf,
        color: echartsColor.FigureColor,
        xAxis: {
          type: "category",
          name:yan.EchartObject.area,
          axisLine: {
              show: true,
              lineStyle: {
              color: "#fff",
            },
              symbol: ["none", "arrow"],
            },
          axisLabel: {
            textStyle: {
              fontFamily:yan.EchartObject.fontFamily,
              color: "#fff",
              fontSize:yan.EchartObject.xNamefontSize
            },
          },
          axisTick: {
            alignWithLabel: true,
          },
          data: xdata,
        },
        yAxis: {
          type: "value",
          name: yan.EchartObject.times,
          minInterval: 1,
          axisLabel: {
            textStyle: {
                fontFamily:yan.EchartObject.fontFamily,
                color: "#fff",
                fontSize:yan.EchartObject.xNamefontSize
                //fontSize:10
              },
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
                color: "#fff",
            },
          },

          splitLine: {
            lineStyle: {
              // 使用深浅的间隔色
              color: "#fff",
              type: "dashed",
              opacity: 0.5,
            },
          },
        },
        series: [
          {
            name: "暂降次数",
            data: xVal,
            barMaxWidth:  yan.EchartObject.barMaxWidth,
            type: "bar",
            itemStyle: {
              normal: {
                color: function (params) {
                  if (params.value == 3.141592) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: "#999999",
                        },
                      ],
                      false
                    );
                  } else {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: echartsColor.FigureColor[0], // 0% 处的颜色
                        },
                      ],
                      false
                    );
                  }
                },
              },
            },
          },
        ],
      };
      myChartes.setOption(option)
window.echartsArr.push(myChartes);
      setTimeout(() => {
        myChartes.resize();
      }, 100);
      this.loadingd = false;
      let _this = this;
      _this.$erd.listenTo(_this.$refs.areaSeverity, (element) => {
        _this.$nextTick(() => {
          myChartes.resize();
        });
      });
      window.onresize = function () {
        myChartes.resize();
      };
    },
  },
};
</script>
