<template>
  <div ref="areamonth" id="monthEcharts" style="width: 100%;"></div>
</template>
<script>
import { dicData } from "@/assets/commjs/dictypeData";
import { getAreaData } from "@/api/Tableabove";
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.areamonth.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("areaviewmonth").style.height;
          let strsec = str.split("p");
          let height = parseInt(strsec) - 50 + "px";
          this.$refs.areamonth.style.height = height;
        }, 200);
      } else {
        return;
      }
    },
  },
  data() {
    return {
      classificationData: [],
      monthlyStatistics: [],
      time: this.timefamt(),
      formData: {
        deptIndex: JSON.parse(window.sessionStorage.getItem("Info")).deptId,
        monitorFlag: 2,
        powerFlag: 2,
        serverName: "event-boot",
        searchBeginTime: "",
        searchEndTime: "",
        statisticalType: {},
        timeFlag: 1,
      },
    };
  },
  created() {
    this.getclassificationData();
    this.queryData();
  },
  mounted() {
    setTimeout(() => {
      let str = document.getElementById("areaviewmonth").style.height;
      let strsec = str.split("p");
      let height = parseInt(strsec) - 50 + "px";
      this.$refs.areamonth.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];
      getAreaData(this.formData).then((res) => {
        this.monthlyStatistics = res.data.monthlyStatistics.monthCalculation;
        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 echartsndArr = [];
      let echartsArr = [];
      let echartswArr = [];
      this.monthlyStatistics.forEach((item) => {
        echartsndArr.push(item.month);
        echartsArr.push(item.linked);
        echartswArr.push(item.notAssociated);
      });
      const echarts = require("echarts");
      var myChartes = echarts.init(document.getElementById("monthEcharts"));
      myChartes.clear();
      //let echartsColor = JSON.parse(window.localStorage.echartsColor);
      let option = {
        title: {
          text:"月份",
          left: "center",
          textStyle: {
            fontWeight: "400",
            color: "#fff",
            fontSize: 16,
          },
        },
        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,
          formatter: function (params) {
            var tips = "";
            tips += params[0].name + "<br/>";
            // for (var i = 0; i < params.length; i++) {
            //   if (params[i].value == 3.14159) {
            //     tips += params[i].seriesName + ":暂无数据<br/>";
            //   } else {
            //     // tips += params[i].name + "<br/>";
            //     tips += params[i].seriesName + ":" + params[i].value + "<br/>";
            //   }
            // }
            params.forEach((item, i) => {
              if (item.value == 3.14159) {
                tips += item.seriesName + ":暂无数据<br/>";
              } else {
                tips += item.seriesName + ":" + item.value + "<br/>";
              }
            });
            return tips;
          },
        },
        grid: yan.EchartObject.gridj,
        dataZoom: yan.EchartObject.dataZoom,
        legend: {   
          data: ["未关联暂降次数", "已关联处理事件"],
          itemWidth: 16,
          itemHeight: 16,
          right: 80,
          itemGap: 28,
          textStyle: {
            color: "#fff",
            rich: {
              a: {
                verticalAlign: "middle",
              },
            },

            padding: [2, 0, 0, 0], //[上、右、下、左]
          },
        },
        xAxis: [
          {
            name: yan.EchartObject.month,
            type: "category",
            data: echartsndArr,
            axisLine: {
              show: true,
              lineStyle: {
                color: "#fff",
              },
              symbol: ["none", "arrow"],
            },
            axisLabel: {
              textStyle: {
                fontFamily: yan.EchartObject.fontFamily,
                color: "#fff",
                fontSize: yan.EchartObject.xNamefontSize,
              },
            },
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        color: [echartsColor.FigureColor[0],echartsColor.FigureColor[5]],
        yAxis: [
          {
            type: "value",
            name: yan.EchartObject.nuit2,
            nameTextStyle: {
                color: "#fff",
            },
            minInterval: 1,
            axisLine: {
              show: true,
              lineStyle: {
                color: "#fff",
              },
            },
            axisLabel: {
              textStyle: {
                fontFamily:yan.EchartObject.fontFamily,
                color: "#fff",
                fontSize:yan.EchartObject.xNamefontSize
                //fontSize:10
              },
            },
            splitLine: {
              lineStyle: {
                // 使用深浅的间隔色
                color: "#fff",
                type: "dashed",
                opacity: 0.5,
              },
            },
          },
        ],
        series: [
          // {
          //   name: '未关联暂降次数',
          //   type: 'bar',
          //   data: list1,
          // },
          {
            name: "未关联暂降次数",
            type: "bar",
            stack: "总数",
            barMaxWidth:  yan.EchartObject.barMaxWidth,

            label: {
              show: true,
            },

            data: echartswArr,
            itemStyle: {
              normal: {
                label: {
                  // show: true, //数字开启显示
                  textStyle: {
                    //数值样式
                    color: "#fff",
                    fontSize: 14,
                    fontWeight: 600,
                  },
                },
                color: function (params) {
                  // console.log(params,'1111111111111111111111');
                  if (params.data == 3.14159) {
                    return echartsColor.FigureColor[9];
                  } else {
                    return echartsColor.FigureColor[0];
                  }
                },
              },
            },
          },
          {
            name: "已关联处理事件",
            type: "bar",
            stack: "总数",
            // itemStyle: {
            //   color: echartsColor.FigureColor,
            // },
            // barWidth: 30,
            barMaxWidth:  yan.EchartObject.barMaxWidth,
            label: {
              show: true,
            },
            data: echartsArr,
            itemStyle: {
              normal: {
                label: {
                  // show: true, //数字开启显示
                  textStyle: {
                    //数值样式
                    color: "#fff",
                    fontSize: 14,
                    fontWeight: 600,
                  },
                },
                color: function (params) {
                  // console.log(params,'1111111111111111111111');
                  if (params.data == 3.14159) {
                    return echartsColor.FigureColor[9];
                  } else {
                    return echartsColor.FigureColor[5];
                  }
                },
              },
            },
          },
          // {
          //   name: '已关联处理事件',
          //   type: 'bar',
          //   data: list2,
          // },
        ],
      };
      myChartes.setOption(option)
window.echartsArr.push(myChartes);
      setTimeout(() => {
        myChartes.resize();
      }, 100);
      this.loadingd = false;
      let _this = this;
      _this.$erd.listenTo(_this.$refs.areamonth, (element) => {
        _this.$nextTick(() => {
          myChartes.resize();
        });
      });
      window.onresize = function () {
        myChartes.resize();
      };
    },
  },
};
</script>
