<template>
  <div>
    <div style="height:30px;height:20px;position:absolute;right:10px">
      <el-tag  style="background:#CC0000;width:30px;height:15px;border:1px solid #CC0000;float:left;margin-top:2px"></el-tag>
        <span style="color:#CC0000;font-weight:400;;float:left">&nbsp&nbsp完整性<60% &nbsp&nbsp</span>
      <el-tag size="mini" style="background:#FFCC33;width:30px;height:15px;border:1px solid #FFCC33;float:left;margin-top:2px"></el-tag>
        <span style="color:#FFCC33;font-weight:400;float:left" >&nbsp&nbsp60%≤完整性<90% &nbsp&nbsp</span>
      <el-tag style="background:#339966;width:30px;height:15px;border:1px solid #339966;float:left;margin-top:2px"></el-tag>
        <span style="color:#339966;font-weight:400;float:left" >&nbsp&nbsp完整性≥90%</span>
   </div>
  <div ref="datazhi" id="dataintegrity" style="width: 100%"></div>
</div>
</template>
<script>
import yan from "@/assets/commjs/color";
import { dicData } from "@/assets/commjs/dictypeData";
import api from "@/api/integraliy/integraliy";
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.datazhi.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('datantegritydiagram').style.height
        let strsec = str.split('p')
        let height = parseInt(strsec) - 50 +'px'
        this.$refs.datazhi.style.height =  height
        },200)
      } else {
        return;
      }
    },
  },
  data() {
    return {
      tableData: [],
      statisticalType: null,
      classificationData: [],
      time: this.timefamt(),
      formData1:{
        statisticalType:null,
        scale:null,
        manufacturer:null,
        loadType:null,
        monitorFlag: 2,
        powerFlag: 2,
        deptIndex:JSON.parse(window.sessionStorage.getItem('Info')).deptId    
      },
      voltageleveloption:[],
      terminaloption:[],
      interfereoption:[],
      classificationData:[],
      xdata: [],
      ydata: [],
    };
  },
  created() {
    this.getVoltage();
    this.getManufacturer();
    this.getclassificationData();
    this.getLoadType();
    this.queryData();
  },
  mounted() {
    setTimeout(()=>{
    let str = document.getElementById('datantegritydiagram').style.height
    let strsec = str.split('p')
    let height = parseInt(strsec) - 50 +'px'
     this.$refs.datazhi.style.height =  height
    },200)
  },
  methods: {
    //字典获取数据电压等级
    getVoltage() {
      var code = "Dev_Voltage_Stand";
      this.voltageleveloption = dicData(code, []);
      this.formData1.scale = this.voltageleveloption;
    },
    //字典获取数据终端厂家
    getManufacturer() {
      var code = "Dev_Manufacturers";
      this.terminaloption = dicData(code, []);
      this.formData1.manufacturer = this.terminaloption;
    },
    //字典获取数据干扰源类型
    getLoadType() {
      var code = "Interference_Source";
      this.interfereoption = dicData(code, []);
      this.formData1.loadType = this.interfereoption;
    },
    //获取统计类型
    getclassificationData() {
      var code = "Statistical_Type";
      this.classificationData = dicData(code, ["Report_Type"]);
      this.formData1.statisticalType = this.classificationData[0];
    },

    queryData() {
      this.formData1.searchBeginTime=this.time[0]
      this.formData1.searchEndTime=this.time[1]
      api.IntegrityIcon(this.formData1).then((res) => {
        if(res.code =='A0000'){
          let arr =[]
          let arry =[]
          //let arry =[]
          res.data.forEach(item=>{
              arr.push(item.type)
            if(item.single !==null){
              if(item.single==0 ||item.single==3.14159){
                item.single=1
              }
              arry.push(item.single)
            }
            if(item.ratio !==null){
              this.ydata1.push(item.ratio)
            }
            this.xdata = arr
            this.ydata= arry
          
          })
        }
          
        this.datategrity();
      });
    },
    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;
    },
    datategrity() {
      const echarts = require("echarts");
      var myChartes = echarts.init(document.getElementById("dataintegrity"));
      var option = {
        title: {
          text: this.title,
          left: "center",

          //subtext: '单位(%)'
        },
 
        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 += params[0].name+'<br/>';
            for (var i = 0; i < params.length; i++) {
              if (params[i].value ==1 ) {
                tips +=  "数据完整性:暂无数据<br/>";
              } else {
                tips +=  "数据完整性:"+params[i].value  + "<br/>";
              }
            }
            return tips;
          },
        },
        grid: yan.EchartObject.gridf,
        xAxis: [
          {
            name:yan.EchartObject.area,
            type: "category",
            data: this.xdata,
            splitLine: {
              show: false,
              lineStyle: {
              color: "#fff",
            },
            },
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
            textStyle: {
              fontFamily:yan.EchartObject.fontFamily,
              color: "#fff",
              fontSize:yan.EchartObject.xNamefontSize
              //fontSize:10
            },
          },
            axisLine: {
              show: true,
              lineStyle: {
              color: "#fff",
            },
              symbol: ["none", "arrow"],
            },
          },
        ],
        
        yAxis: [
          {
            name: yan.EchartObject.nuit1,
            type: "value",
            min: 0,
            max: 100,
            splitLine: {
              show: false,
              lineStyle: {
              color: "#ccc",
            },
            },
            axisLine: {
              show: true,
              lineStyle: {
              color: "#fff",
            },
              symbol: ["none", "arrow"],
            },
            axisLabel: {
              textStyle: {
                fontFamily:yan.EchartObject.fontFamily,
                color: "#fff",
                fontSize:yan.EchartObject.xNamefontSize
                //fontSize:10
              },
            },
            splitArea: {
              show: false,
            },
          },
        ],
        dataZoom: yan.EchartObject.dataZoom,
        series: [
          {
            name: this.timeValue,
            barMaxWidth:  yan.EchartObject.barMaxWidth,
            type: "bar",
            label: {
              normal: {
                show: false,
                position: "top",
              },
            },
            itemStyle: {
              normal: {
                // 随机显示
                //color:function(d){return "#"+Math.floor(Math.random()*(256*256*256-1)).toString(16);}

                // 定制显示（按顺序）
                color: function (params) {
                  if (params.value >= 90) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.Integrity1,
                        },
                      ],
                      false
                    );
                  } else if (params.value >= 60 && params.value <= 90) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.Integrity2,
                        },
                      ],
                      false
                    );
                  } else if (params.value <= 60 && params.value > 3.14159 &&  params.value !== 1)  {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: yan.ColorObject.Integrity3,
                        },
                      ],
                      false
                    );
                  } else if (params.value = 1) {
                    return new echarts.graphic.LinearGradient(
                      0,
                      1,
                      0,
                      0,
                      [
                        {
                          offset: 1,
                          color: "#ccc",
                        },
                      ],
                      false
                    );
                  }
                },
              },
            },
            markLine: {
              silent: false,
              symbol: "circle",
              data: [
                {
                  name: "",
                  yAxis: 100,
                  lineStyle: {
                    color: yan.ColorObject.Integrity1,
                  },
                  label: {
                   // position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color: yan.ColorObject.Integrity1,
                    },
                  },
                },
                {
                  name: "",
                  yAxis: 90,
                  lineStyle: {
                    color: yan.ColorObject.Integrity2,
                  },
                  label: {
                    //position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color: yan.ColorObject.Integrity2,
                    },
                  },
                },
                {
                  name: "",
                  yAxis: 60,
                  lineStyle: {
                    color: yan.ColorObject.Integrity3,
                  },
                  label: {
                    //position: "middle",
                    formatter: "{b}",
                    textStyle: {
                      color: yan.ColorObject.Integrity3,
                    },
                  },
                },
              ],
            },
            data: this.ydata,
          },
       
        ],
      };
      myChartes.setOption(option)
window.echartsArr.push(myChartes);
      setTimeout(() => {
        //由于网格布局拖拽放大缩小图表不能自适应，这里设置一个定时器使得echart加载为一个异步过程
        this.$nextTick(() => {
          myChartes.resize();
        });
      }, 0);
      let _this = this;
      _this.$erd.listenTo(_this.$refs.datazhi, (element) => {
        _this.$nextTick(() => {
          myChartes.resize();
        });
      });
      window.onresize = function () {
        myChartes.resize();
      };
    },
  },
};
</script>
