<template>
  <div class="bg">
    <!-- 头部 -->
    <div class="header">检测试验监控管理平台</div>
    <div>
      <a-button class="fanHui" @click="back">返回</a-button>
    </div>


    <div class="cent">
      <div class="cenhead">
        <div class="title">{{deviceName}}</div>
      </div>
      <div class="echarts" style="height:90%;width:100%;" ref="zhexian"></div>
    </div>



  </div>
</template>

<script>
import { listGraph } from "@/api/api";
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
export default {
  data() {
    return {
      // websocket
      path: 'ws://localhost:8080/itdmServer/itdmWebsocket/itdmSecondServer',
      // path: 'ws://127.0.0.1:8080/itdmServer/itdmWebsocket/itdmSecondServer',
      socket: '',

      // 通用数据1（电压、进水口出水口温度、水压）
      tY1: [],
      // 通用数据2（三个区域温度、湿度、气压）
      tY2: [],
      // 温箱试验数据
      tableDataW: [],
      // 力学试验数据
      tableDataL: [],
      // 设备维护数据
      tableDataS: [],
      // 选择设备
      deviceOptions: [], // 所有设备
      allDevOptions: [], // 备用所有设备
      // 报警数据
      tableDataB: [],
      // 语音播报
      boBaoWZ: [],
      yuYin: [],
      // 停止播报按钮状态
      typeAll: false,
      // // 历史预警统计
      getYJ: [],
      // 单条语音播报id
      yuYing: {
        deleteId: null,
        alarmType: null,
      },
      // 折线图初始化
      query: {
        id: this.$route.query.id
      },
      deviceSelect: [this.$route.query.id], // 选择后传给后端的设备id
      echartsData: [],
      deviceName: this.$route.query.deviceName
    }
  },
  mounted () {
    this.getList()
    // 设备初始化
    this.init()
    // this.drawZhexian() // 绘制折线图
    // echarts自适应
    window.addEventListener("resize", () =>{
      this.zheChart.resize();
    });
  },
  methods:{
    // 初始化折线图
    getList() {
      console.log(this.query.id)
      listGraph(this.query).then(res => {
        console.log(res)
        this.echartsData = res.data.data
        this.$nextTick(() => {
          this.drawZhexian()
        })
      })
    },
    // 返回按钮
    back(){
      this.typeAll = false
      // this.$router.push('/visualization2')
      this.$router.go(-1);//返回上一层
    },
    // 设备状况按钮
    zhengchang(num){
      console.log(num)
      this.deviceOptions = []
      console.log(this.deviceOptions)
      var deOption = this.allDevOptions
      if( !num && num == undefined ){
        this.deviceOptions = this.allDevOptions
        return false
      }
      for (var i = 0; i < deOption.length; i++) {
        console.log(deOption[i].deviceStatus)
        if(deOption[i].deviceStatus == num){
          this.deviceOptions.push(deOption[i])
          console.log(this.deviceOptions)
        }
      }
    },
    // 语音播报
    voiceBroadcast(text){
      this.voiceContent = new SpeechSynthesisUtterance(text);
      this.voiceContent.volume = 100;
      this.voiceContent.rate = 1;
      this.voiceContent.pitch = 1.5;
      console.log(this.voiceContent);
      window.speechSynthesis.speak(this.voiceContent)
    },
    // 绘制折线图
    drawZhexian(){
      var chartDom = this.$refs.zhexian
      this.zheChart = echarts.init(chartDom);

      var xAxisData = []
      var seriesData = []
      var seriesDatas = []
      console.log(this.echartsData)
      xAxisData = this.echartsData[0].xAxis
      seriesData = this.echartsData[0].series[0].data
      seriesDatas = this.echartsData[1].series[0].data
      console.log(1234,xAxisData,seriesData,seriesDatas)
      var startValue1 = xAxisData.length -7
      var endtValue1 = xAxisData.length -1

      // var setSeries = seriesData ? seriesData.map((res, index) => {
      //   console.log(res)
      //   return {
      //     name: res.name + '数值',
      //     type: 'line',
      //     smooth: true,
      //     symbol: 'none',
      //     // sampling: 'lttb', // 最大数值在中间
      //     itemStyle: {
      //       // color: res.s == '0' ? '#5efcf4' : '#FFC58B'
      //       color: '#5efcf4'
      //       // color: res.s == '0' ? '#5efcf4' : '#ffaaaa'
      //       // color: res.status === '1' ? '#60fcf5' : 'red'
      //     },
      //     data: res.data
      //   }
      // }) : []

      var option;

      option = {
        // backgroundColor:'#05799c97',
        // color: ['#0C65F6', '#00D68A'],
        color: ['#16D6FF', '#00FED2'],
        tooltip: {
          confine: true,
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor:'#257AA7',
          // backgroundColor:'transparent',
          borderColor:'#fff',
          // extraCssText: "box-shadow: 2px 2px 4px 2px rgba(37,122,167, 0.3);",
          // color:'#EBC69A',
          axisPointer: {
                  lineStyle: {
                      color: {
                          type: 'linear',
                          x: 0,
                          y: 0,
                          x2: 0,
                          y2: 1,
                          colorStops: [{
                              offset: 0,
                              color: 'rgba(126,199,255,0)' // 0% 处的颜色
                          }, {
                              offset: 0.5,
                              color: 'rgba(126,199,255,1)' // 100% 处的颜色
                              // offset: 0.9,
                              // color: '#EBC69A' // 100% 处的颜色
                          }, {
                              offset: 1,
                              color: 'rgba(126,199,255,0)' // 100% 处的颜色
                          }],
                          global: false // 缺省为 false
                      }
                  },
              },
        },
        grid: {
          left: '2%',
          right: '4%',
          bottom: '5%',
          top: '30px',
          containLabel: true,
        },
        legend: {
          icon: 'rect',
          orient: 'horizontal',
          right: '4%',
          itemWidth: 12,
          itemHeight: 12,
          formatter: ['{a|{name}}'].join('\n'),
          textStyle: {
            fontSize: 12,
            color: '#BCFFFC',
            height: 8,
            rich: {
              a: {
                verticalAlign: 'bottom',
              },
            },
          },
          data: ['温度', '湿度'],
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#6A93B9',
            },
          },
          axisLabel: {
            // 横坐标全部显示interval:0,横坐标自动折叠则不需此语句
            // interval:0,
            fontSize: 12,
            // color: '#6A93B9',
            margin: 15, // x轴距离文字距离
            color: '#BCFFFC',
          },
          axisTick: {
            show: false,
          },
          data: xAxisData
        },
        dataZoom: [
          {
            type: 'inside',
            // show: true,
            // realtime: true,
            // start: 75,
            // end: 100
            startValue: startValue1,
            endValue: endtValue1,
          },
          {
            // type: 'inside',
            // realtime: true,
            // start: 0,
            // end: 100
            show: true,
              realtime: true,
              height: 15,//这里可以设置dataZoom的尺寸
              backgroundColor: 'rgba(12,44,72, 0.3)',
              textStyle: false,
              bottom: 6,
          }
        ],
        yAxis: {
          type: 'value',
          min: 0,
          minInterval: 1,
          nameTextStyle: {
            fontSize: 12,
            color: '#BAE7FF',
            align: 'center',
          },
          splitLine: {
            lineStyle: {
              // color: 'rgba(255, 255, 255, 0.15)',
              color: '#6A93B9',
              type: 'dashed', // dotted 虚线
            },
          },
          splitArea: { show: false },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            fontSize: 12,
            fontFamily: 'Bebas',
            color: '#BCFFFC',
          },
        },
        series: [
          {
            type: 'line',
            // showSymbol: false,
            // smooth: true, // 是否曲线
            name: '温度', // 图例对应类别
            // data: [1000, 4000, 5000, 6000, 3000, 8000, 7000], // 纵坐标数据
            data: seriesData, // 纵坐标数据
            areaStyle: { // 区域颜色
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgba(22, 214, 255, 0.6)',
              }, {
                offset: 1,
                color: 'rgba(22, 214, 255, 0.1)',
              }]),
            },
          }, {
            type: 'line',
            name: '湿度',
            // data: [5000, 6000, 7000, 2000, 5000, 7000, 9000],
            data: seriesDatas,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgba(36,229,195, 0.6)',
              }, {
                offset: 1,
                color: 'rgba(36,229,195, 0.1)',
              }]),
            }
          }
        ],
      };
      option && this.zheChart.setOption(option);
    },
    // websocket
    init: function () {
      if(typeof(WebSocket) === "undefined"){
          alert("您的浏览器不支持socket")
      }else{
        // 实例化socket
        this.socket = new WebSocket(this.path)
        // 监听socket连接
        this.socket.onopen = this.open
        // 监听socket错误信息
        this.socket.onerror = this.error
        // 监听socket消息
        this.socket.onmessage = this.getMessage
        // 监听销毁
        this.socket.onclose = this.close
      }
    },
    open: function () {
        console.log("socket连接成功")
        this.send()
    },
    error: function () {
        console.log("连接错误")
    },
    getMessage: function (msg) {
      var data = JSON.parse(msg.data)
      // console.log(data)

      this.echartsData = data.data.data
      console.log(this.echartsData)

      this.$nextTick(() => {
        this.drawZhexian()
      })
    },
    send: function () {
      console.log(this.deviceSelect)
      this.socket.send(JSON.stringify({deviceIds:this.deviceSelect,day:1}))
    },
    close: function () {
      console.log("socket已经关闭")
    }
  },
  destroyed () {
    console.log(1111)
    // 销毁监听
    // this.close()
    this.socket.close()
  },
}
</script>

<style lang="less" scoped>
/* flex布局 */
.u-flex,.u-flex-ac,.u-flex-jac,.u-flex-jab,.u-flex-jaa,.u-flex-cjac,.u-flex-cjab,.jc-end{ display: flex; }
.fd-column,.u-flex-cjac,.u-flex-cjab{ flex-direction: column; }
.jc-center,.u-flex-jac,.u-flex-cjac{ justify-content: center; }
.jc-sb,.u-flex-jab,.u-flex-cjab{ justify-content: space-between; }
.jc-sa,.u-flex-jaa{ justify-content: space-around; }
.jc-start{ justify-content: flex-start; }
.jc-end{ justify-content: flex-end; }
.ai-center,.u-flex-ac,.u-flex-jab,.u-flex-jac,.u-flex-jaa,.u-flex-cjac{ align-items: center; }
.ai-start{ align-items: flex-start; }
.ai-end{ align-items: flex-end; }
.ai-stretch{ align-items: stretch; }
.f1{ flex: 1; }
.f-auto{ flex: auto; }
.bg{
  width: 100%;
  height: 100%;
  background: url('../../../assets/images/big.jpg') no-repeat center center;
  overflow: hidden;
}
.header{
  width: 100%;
  height: 60px !important;
  background: url('../../../assets/images/titleBg22.png') no-repeat center center;
  background-size: cover 100%;
  line-height: 60px;
  text-align: center;
  font-size: 35px;
  letter-spacing: 5px;
  color: rgb(188, 255, 252);
  font-weight: bold;
}
.leHeader{
  padding-left: 10px;
  width: 100%;
  height: 22px;
  // background-color: aquamarine;
  color: #44bae0;
  font-size: 20px;
  letter-spacing: 1px;
  cursor: pointer;
  font-weight: 600;
}
.ceHeader{
  padding-left: 10px;
  width: 50%;
  height: 22px;
  // background-color: aquamarine;
  color: #44bae0;
  font-size: 20px;
  letter-spacing: 1px;
  cursor: pointer;
  font-weight: 600;
}
.ceHeader2{
  padding-right: 10px;
  width: 50%;
  height: 30px;
  // background-color: aquamarine;
  color: #44bae0;
  font-size: 20px;
  letter-spacing: 1px;
  cursor: pointer;
  font-weight: 600;
  position: absolute;
  right: 0;
  top: 3.8%;
}
.geXian{
  margin-top: 3px;
  background: url('../../../assets/images/up.png') no-repeat center center;
  background-size: auto 220%;
  // background-color: rgb(255, 204, 127);
  width: 100%;
  height: 3%;
}
.cent{
  height: 88%;
  width: 98%;
  // border: 3px solid #148cb1;
  // background-color: #1880a06a;
  border: 2px solid rgba(32, 122, 183, .5);
    // box-shadow: 0 0 2vw #237ad4 inset;
    background: linear-gradient(#148cb1, #148cb1) left top,
    linear-gradient(#148cb1, #148cb1) left top,
    linear-gradient(#148cb1, #148cb1) right top,
    linear-gradient(#148cb1, #148cb1) right top,
    linear-gradient(#148cb1, #148cb1) left bottom,
    linear-gradient(#148cb1, #148cb1) left bottom,
    linear-gradient(#148cb1, #148cb1) right bottom,
    linear-gradient(#148cb1, #148cb1) right bottom;
    background-repeat: no-repeat;
    background-size: 0.2vw 1vw, 1vw 0.2vw;
    overflow-y: auto;
    // 内阴影
    box-shadow: inset 0px 0px 26px 0px #19ECFF;
    // 外阴影
    // box-shadow: 0px 0px 26px 0px rgba(32, 123, 183, 0.726);
  // border: 3px solid #398ba4;
  margin: 1% 1% 1% 1%;
}
.cenhead{
  width: 50%;
  height: 5%;
  // color: #06C4C4;
  font-size: 20px;
  // font-weight: 600;
  // padding-left: 2%;
  padding-top: 1%;
  padding-bottom: 2%;
  .title{
    width: 100px;
    height: 36px;
    border-radius: 2px;
    // background-color: #16D6FF;
    background-image:linear-gradient(90deg, #00bde8, #05799c);
    color: #d8fff8;
    margin-left: 4%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.cen{
  margin: 0 10px 18px 10px;
  padding: 1.6% 0 0 0;
  width: 40%;
  height: 90%;
  // background-color: #168dc073 !important;
  background: url('../../../assets/images/bg.png') no-repeat center center;
  background-size: auto 111.3%;
  // background: url('../../../assets/images/2/center2.png') no-repeat center center;
  // background-size: contain;
  position: absolute;
  left: 29.4%;
  bottom: 0%;
    // // 滚动条
    // overflow-y: auto;
    // 设备布局
    .device-data{
      flex-wrap: wrap;
      // 每一个设备
      .item-container{
        width: calc(23.5% - 6px);
        height: 96px;
        margin-bottom: 8px;
        margin-left: 6px;
        margin-right: 6px;
        font-size: 15px;
        padding-top: 3px;
        // border: 3px solid #148cb1;
        // border-width: 140px 40px 90px 40px;
        background-image:linear-gradient(0deg, #05799c, #05799c38);
        box-shadow: 0 10px 10px 0 rgba(0,0,0,.1);
        // border-image: url('../../assets/images/bgs.png') 60 100 50 90 fill;
        border-image-width: 26px 40px 18px 32px;
        border-image-repeat: stretch;
        position: relative;
      }
    }
}
/* 设置滚动条的样式 */
::-webkit-scrollbar {
  width: 10px;
}
/* 滚动槽 */
::-webkit-scrollbar-track {
  -webkit-box-shadow: inset006pxrgba(0,0,0,0.3);
  border-radius: 8px;
}
/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background:rgba(30, 179, 206, 0.459);
  -webkit-box-shadow: inset006pxrgba(0,0,0,0.5);
}
// ::-webkit-scrollbar-thumb:window-inactive {
//   background:rgba(25,189,242, 0.9);
//   // background:rgba(37, 173, 214, 0.788);
// }
//每个设备字体
.font{
  color: rgba(243, 243, 243, 0.918);
  display: flex;
  justify-content: center;
}
// 预警字体
  ::v-deep.font2{
  color: rgba(243, 243, 243, 0.918);
  font-size: 16px;
}
// 正常异常按钮显示
.font-bu{
  color: rgba(243, 243, 243, 0.918);
  display: flex;
  justify-content: center;
  margin: 10px;
}
// 报警文字
.yc{
  color: brown;
}
.shuiyewei{
  height: 82%;
  width: 96%;
  // margin: 10px;
  // background-color: aquamarine;
}
.shuiyewei2{
  height: 80%;
  width: 96%;
  margin: 10px;
}
// 返回按钮
.fanHui{
  background: #116B8D;
  color: rgb(206, 255, 239);
  border-color: rgba(47, 117, 129, 0.74);
  position: absolute;
  top: 1%;
  right: 1.5%;
}
// 点击按钮后按钮的颜色
.el-button.is-plain:hover, .el-button.is-plain:focus{
  background: #116B8D;
  color: rgb(206, 255, 239);
  border-color: rgba(47, 117, 129, 0.74);
}
.pause{
  // margin: 0 0 0 3%;
  background: #116B8D;
  color: rgb(206, 255, 239);
  border-color: rgba(47, 117, 129, 0.74);
  position: absolute;
  right: 2%;
}
.boB{
  color: rgb(206, 255, 239);
}
.all{
  background: #116B8D;
  color: rgb(206, 255, 239);
  border-color: rgba(47, 117, 129, 0.74);
}
// 表格
  ::v-deep .el-table{
    background-color: #114a688f !important;
    color: #c6f7ff;
    font-size: 14px;
  }
  ::v-deep .el-table th{
    background-color: #0A5D89 !important;
    color: #c6f7ff;
    font-size: 15px;
    border-bottom: 0;
  }
  ::v-deep .el-table--medium th{
    padding: 6.2px 0 !important;
  }
  ::v-deep .el-table--medium td{
    padding: 6.2px 0 !important;
  }
  ::v-deep .el-table tr{
    background-color: #114a6817;
    padding: 5px 0 !important;
  }
  ::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td{
    background-color: #446185 !important;
  }
  ::v-deep .el-table td{
    border-bottom: 0px;
  }
  ::v-deep .el-table th.is-leaf{
    border-bottom: 0px;
  }
  ::v-deep .el-table::before{
    height: 0px !important;
  }
  ::-webkit-th{
    width: 16px;
  }
  // 表格鼠标悬停背景颜色
  // ::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td{
  //   background-color: #546b94 !important;
  // }
  // ::v-deep .el-table--enable-row-transition .el-table__body td{
  //   background-color: #103E5F !important;
  // }
  // 上面的方法或者下面的都可以
  ::v-deep .el-table__body tr:hover>td{
    background-color: #103E5F !important;
  }
  ::v-deep .el-table__body tr.current-row>td{
    background-color: #103E5F !important;
  }
  // 表格滚动条
  /* 修改表格的滚动条（滚动条的宽度）*/
    ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
        width: 10px; // 横向滚动条
        height: 10px; // 纵向滚动条 必写
    }
    /* 表格滚动条的滑块*/
    ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
        background-color: rgba(30, 179, 206, 0.459);
        border-radius: 3px; //滚动条圆弧
    }
    /* 表格的表头与内容不对齐 */
    ::v-deep .el-table th.gutter {
        display: table-cell !important;
    }
  // 按钮样式
  .el-button--success {
    // background-image:linear-gradient(140deg, #517973, #10e9b3);
    // box-shadow: 0 10px 10px 0 rgba(0,0,0,.1);
    background-color: #06C4C4;
    border-color:#5bb486bd;
  }
  .el-button--danger {
    // background-image:linear-gradient(120deg, #df5858, #ffaeae);
    // box-shadow: 0 10px 10px 0 rgba(0,0,0,.1);
    background-color: #f0a182;
    border-color: #e49f84;
  }
  .el-button--info {
    // background-image:linear-gradient(120deg, #707070, #d3d2d2);
    // box-shadow: 0 10px 10px 0 rgba(0,0,0,.1);
    background-color: #b4b4b4de;
    border-color: #8d8d8d;
  }
  .butDanger {
    background-color: #f08282;
    border-color: #e48484;
    color: #ffffff;
  }

  // 自定义表格
  .row-top{
    margin: 5px 5px 5px 5px;
  }
  .bt-top{
    border-top: 1px solid #1797AD;
  }
  .bt-h100{
    height: 100px;
  }
  .bt-h60{
    height: 60px;
  }
  .bt-h250{
    height: 222px;
  }

  .bg-purple {
   text-align: center;
    color: rgba(243, 243, 243, 0.918);
    font-size: 13px;
    // font-weight: 800;
    border-left: 1px solid #1797AD;
  }
  .bg-purple-light {
  text-align: center;
    color: rgba(243, 243, 243, 0.918);
    font-size: 13px;
    // font-weight: 800;

    border-left: 1px solid #1797AD;
    border-right: 1px solid #1797AD;
  }
  .grid-content {
    padding-top: 5px;
    padding-left: 5px;
    padding-right: 5px;
    border-bottom: 1px solid #1797AD;
    min-height: 30px;
  }
</style>