<template>
  <div ref="sag" id="sagdistrib" style="width: 100%; height: 235px"></div>
</template>
<script>
import yan from "@/assets/commjs/color";
import { dicData } from "@/assets/commjs/dictypeData";
import { getReasonTypeTime } from "@/api/monitoringpointStatistics/echart";
export default {
  props: {
    item: {
      type: Object,
      default: null,
    },
    item2: {
      type: Object,
      default: null,
    },
  },
  watch: {
    height2(a, b) {
      if (a == 24) {
        this.$refs.sag.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('sagdistribution').style.height
        let strsec = str.split('p')
        let height = parseInt(strsec) - 50 +'px'
        this.$refs.sag.style.height =  height
        },200)
      } else {
        return;
      }
    },
  },
  data() {
    return {
      tableData: [],
      statisticalType: null,
      classificationData: [],
      time: this.timefamt(),
      datamock: [],
    };
  },
  created() {
    var data = window.sessionStorage.getItem("Info");
    data = eval("(" + data + ")");
    this.deptIndex = data.deptId;
    this.getIndexType();
    this.queryData();
  },
  mounted() {
    setTimeout(()=>{
    let str = document.getElementById('sagdistribution').style.height
    let strsec = str.split('p')
    let height = parseInt(strsec) - 50 +'px'
     this.$refs.sag.style.height =  height
    },200)
  },
  methods: {
    queryData() {
      var formData = {
        endTime: this.time[0],
        lineIndex: "e5807e73521aa58dec3cb1cf74805281",
        startTime: this.time[1],
        flag: 0,
      };
        getReasonTypeTime(formData).then((res) => {
        this.datamock = res.data;
        this.$nextTick(() => {
          this.sagdistrib();
        });
      });
    },

    getIndexType() {
      var code = "Indicator_Type";
      this.classificationData = dicData(code, [
        "PLPC",
        "DYPC",
        "SXDYBPHD",
        "CSSB",
        "FXDL",
        "JXBDY",
      ]);
      this.statisticalType = this.classificationData[0];
    },
    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 today = year + "-" + month + "-" + day;
      var today2 = year + "-01-01";
      arr.push(today);
      arr.push(today2);
      return arr;
    },
    sagdistrib() {
      var months = new Array();
      var handle = new Array();
      if (this.datamock != null) {
        for (var i = 0; i < this.datamock.length; i++) {
          if ((this.datamock[i].month == "01") | (i == 0)) {
            if (this.timeFlag == 0) {
              months[i] =
                this.datamock[i].month + "\n(" + this.datamock[i].year + ")";
            } else {
              months[i] =
                this.datamock[i].month +
                "-" +
                this.datamock[i].day +
                "\n(" +
                this.datamock[i].year +
                ")";
            }
          } else {
            if (this.timeFlag == 0) {
              months[i] = this.datamock[i].month;
            } else {
              months[i] = this.datamock[i].month + "-" + this.datamock[i].day;
            }
          }
          handle[i] = this.datamock[i].times;
        }
      }
      const echarts = require("echarts");
      var myChartes = echarts.init(document.getElementById("sagdistrib"));
      let echartsColor = JSON.parse(window.localStorage.echartsColor);
      myChartes.clear();
      var option = {
      //  backgroundColor: "#fff", //背景色,
        title: {
          text:
            this.intervald == 3
              ? "月份统计"
              : this.intervald == 2
              ? "季度统计"
              : this.intervald == 1
              ? "年份统计"
              : "",
          x: "center",
          textStyle: {
            fontSize: yan.EchartObject.titleNamefontSize1,
            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)",
          formatter: function (params) {
            var tips = "";
            tips += "<font>时间:" + params[0].name + "</font><br/>";
            tips += "<font>暂降次数:" + params[0].data + "</font>";
            return tips;
          },
        },

        dataZoom: yan.EchartObject.dataZoom,
        color: ["#FF8C00"],
        legend: {
          data: ["暂降次数"],
         right:'10px',
          textStyle: {
          //  fontSize: yan.EchartObject.titleNamefontSize1,
            color: '#fff',
          },
        },
        grid: yan.EchartObject.gridf,

        xAxis: [
          {
            type: "category",
            data: months,
            name: "时间",
            axisLine: {
              lineStyle: {
                color: '#fff',
              },
            },
            axisLabel: {
              textStyle: {
                fontFamily: yan.EchartObject.fontFamily,
                color: '#fff',
                fontSize: yan.EchartObject.xNamefontSize,
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "次",
            nameTextStyle: {
              color: '#fff',
            },
            minInterval: 1,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#fff',
              },
            },
            axisLabel: {
              color: '#fff',
              fontSize: yan.EchartObject.xNamefontSize,
            },
            splitLine: {

              lineStyle: {
                // 使用深浅的间隔色
                color: ['#fff'],
                type: "dashed",
                opacity: 0.5,
              },
            },
          },
        ],
        series: [
          {
            name: "暂降次数",
            type: "bar",
            barMaxWidth: 30,
            barMinHeight: 5,
            itemStyle: {
              normal: {
                color: function (params) {
                  // console.log(params,'1111111111111111111111');
                  if (params.data == 0) {
                    return "#ccc";
                  } else {
                    return "#FF8C00";
                  }
                },
                label: {
                  show: true,
                  textStyle: {
                    label: { show: true, position: "top" },
                    color: "#8B008B",
                  },
                },
              },
            },
            data: handle,
          },
        ],
      };
      myChartes.setOption(option)
window.echartsArr.push(myChartes);
      setTimeout(() => {
        myChartes.resize();
      }, 100);
      this.loadingd = false;
      let _this = this;
      _this.$erd.listenTo(_this.$refs.sag, (element) => {
        _this.$nextTick(() => {
          myChartes.resize();
        });
      });
      window.onresize = function () {
        myChartes.resize();
      };
    },
  },
};
</script>
