<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
    <div>
      <div v-for="(item, index) in showList" :key="index" v-if="index < 4">
        <div :class="'tip' + (index + 1)">
          <div class="title">
            <el-row :span="24">
              <el-col :sm="15" style="color:red;font-size:20px;"
                >{{ item.eventTypeName }}
              </el-col>
              <el-col :sm="9" style="text-align:right;padding-right:10px;"
                >等级：
                <span
                  class="tip_level"
                  v-if="item.eventLevel == 1"
                  style="color:red"
                  >特别重大</span
                >
                <span
                  class="tip_level"
                  v-if="item.eventLevel == 2"
                  style="color:red"
                  >重大</span
                >
                <span class="tip_level" v-if="item.eventLevel == 3">一般</span>
              </el-col>
            </el-row>
          </div>
          <el-divider></el-divider>
          <div class="tip_warp">
            <el-row class="tip_content" type="flex" justify="space-around">
              <el-col :span="15">
                <el-row :span="24">
                  <el-col :span="24" style="color:red"
                    >报警地点：{{ item.occurrenceSite }}
                  </el-col>
                  <el-col :span="24"
                    >报警时间：{{ item.occurrenceTime }}</el-col
                  >
                  <el-col :title="item.type" :span="24"
                    >报警类型：{{ item.eventTypeName }}
                  </el-col>
                  <el-col
                    :title="item.resourceName + item.eventTypeName"
                    :span="24"
                  >
                    报警说明：{{ item.resourceName }}{{ item.eventTypeName }}
                  </el-col>
                </el-row>
              </el-col>
              <el-col class="tip_imgwarp" :span="8">
                <img v-if="item.path" :src="urlPath + item.path" alt="" />
                <img v-else src="./assets/images/videoImg.png" alt="" />
              </el-col>
            </el-row>
            <el-row
              class="btn_warp alarms_warp"
              type="flex"
              justify="space-around"
            >
              <el-col :span="3" :offset="1">
                <el-button
                  class="tip_btn"
                  type="primary"
                  size="medium"
                  round
                  @click="closeTip(item.id, 3)"
                  >已处理
                </el-button>
              </el-col>
              <el-col :span="4">
                <el-button
                  class="tip_btn"
                  type="info"
                  size="medium"
                  round
                  @click="closeTip(item.id, 2)"
                  >待处理
                </el-button>
              </el-col>
            </el-row>
          </div>
        </div>
      </div>
    </div>
    <dispose-info v-if="queryDetailVisible" ref="queryDetail"></dispose-info>
  </div>
</template>

<script>
import util from '@/libs/util.js'
export default {
  name: 'app',
  provide() {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      is3DShow: false,
      dialogVisible: false,
      isRouterAlive: true,
      queryDetailVisible: false,
      urlPath: window.CONFIG.urlPath,
      tipList: [],
      showList: [],
      src: window.CONFIG.bimPath
    }
  },
  watch: {},
  computed: {},
  mounted() {
    this.initWebSocket()
    this.initWebSocket2()
    this.getDateList()
    this.getDateList2()
    let typeList = JSON.parse(sessionStorage.getItem('typeList'))
    this.$store.dispatch('d2admin/typeList/set', typeList)
  },
  destroyed: function() {
    this.websocketclose()
  },
  methods: {
    reload() {
      this.isRouterAlive = false
      this.$nextTick(function() {
        this.isRouterAlive = true
      })
    },
    //初始化weosocket
    initWebSocket() {
      if (typeof WebSocket === 'undefined') {
        alert('您的浏览器不支持WebSocket')
        return false
      }
      // websocket地址？
      const wsuri = this.$http.adornUrl('').slice(5)
      console.log('wsuriLog:' + wsuri)
      let webpath = ''
      //wsuri.substring(0, wsuri.indexOf(':'))
      if (wsuri != null && wsuri.indexOf(':') <= -1) {
        webpath = 'ws://api.crsh.cn:8091/websocket/1001'
      } else {
        webpath =
          'ws:' +
          wsuri.substring(0, wsuri.indexOf(':')) +
          ':8091/websocket/1001'
      }

      console.log('webpathLog:' + webpath)
      this.websock = new WebSocket(webpath)
      this.websock.onopen = this.websocketonopen
      this.websock.onmessage = this.websocketonmessage
      this.websock.onerror = this.websocketonerror
      this.websock.onclose = this.websocketclose

      // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
      window.onbeforeunload = this.websocketclose
    },
    //连接成功
    websocketonopen() {
      console.log('WebSocket连接成功')
      // this.send()
    },
    //接收后端返回的数据
    websocketonmessage(e) {
      // 在这里使用后端返回的数据，对数据进行处理渲染
      // this.tipList = []
      this.showList = []
      if (
        sessionStorage.getItem('token') &&
        sessionStorage.getItem('token') !== undefined
      ) {
        //登录才能获取
        var alarmsInfo = JSON.parse(e.data)
        //console.log(alarmsInfo, '-------')
        if (alarmsInfo.status != 1) {
          for (var i in this.tipList) {
            if (alarmsInfo.id === this.tipList[i].id) {
              this.tipList.splice(i, 1)
              break
            }
          }
        } else {
          var flag = true
          for (var index in this.tipList) {
            if (alarmsInfo.id === this.tipList[index].id) {
              flag = false
              this.tipList.splice(index, 1)
              break
            }
          }
          this.tipList.push(alarmsInfo)
          if (flag) {
            var x = 0
            var resourceCode = this.$store.state.d2admin.bjCode.resourceCode
            console.log(this.$store.state.d2admin.bjCode.resourceCode)
            if (
              resourceCode &&
              resourceCode.split('+')[0] === alarmsInfo.resourceCode &&
              resourceCode.split('+')[2] === alarmsInfo.stationId
            ) {
              x =
                Number(resourceCode.substring(resourceCode.indexOf('+') + 1)) +
                1
              this.$store.dispatch(
                'd2admin/bjCode/set',
                alarmsInfo.resourceCode + '+' + x + '+' + alarmsInfo.stationId
              )
            } else {
              this.$store.dispatch(
                'd2admin/bjCode/set',
                alarmsInfo.resourceCode + '+0+' + alarmsInfo.stationId
              )
            }
          }

          this.tipList.sort(this.sortBy('occurrenceTime', false)) //按发生时间降序

          var alarmId = this.$store.state.d2admin.alramfj.alarmId
          var num = 0
          if (
            alarmId &&
            alarmId.substring(0, alarmId.indexOf('-')) === alarmsInfo.id
          ) {
            num = Number(alarmId.substring(alarmId.indexOf('-') + 1)) + 1
            this.$store.dispatch(
              'd2admin/alramfj/set',
              alarmsInfo.id + '-' + num
            )
          } else {
            this.$store.dispatch('d2admin/alramfj/set', alarmsInfo.id + '-0')
          }
          // console.log(this.$store.state.d2admin.alramfj.alarmId)
          // console.log(this.$store.state.d2admin.alramfj.alarmId.substring(0,this.$store.state.d2admin.alramfj.alarmId.indexOf('-')))
        }

        if (this.tipList.length < 4) {
          this.tipList.forEach((element) => {
            this.showList.push(element)
          })
        } else if (this.tipList.length >= 4) {
          for (let index = 0; index < 4; index++) {
            this.showList.push(this.tipList[index])
          }
        }
        if (this.showList) {
          this.showList.sort(this.sortBy('occurrenceTime', true))
        }
        this.$store.dispatch('d2admin/toast/set', this.tipList.length)
      }
    },
    //连接建立失败重连
    websocketonerror(e) {
      console.log(`连接失败的信息：`, e)
      this.initWebSocket() // 连接失败后尝试重新连接
    },
    //关闭连接
    websocketclose(e) {
      console.log('断开连接', e)
    },
    // ====================================
    //初始化weosocket2
    initWebSocket2() {
      if (typeof WebSocket === 'undefined') {
        alert('您的浏览器不支持WebSocket')
        return false
      }
      // websocket地址？
      const wsuri = this.$http.adornUrl('').slice(5)
      console.log('wsuriLog:' + wsuri)
      let webpath = ''
      //wsuri.substring(0, wsuri.indexOf(':'))
      if (wsuri != null && wsuri.indexOf(':') <= -1) {
        webpath = 'ws://api.crsh.cn:8091/websocket/1002'
      } else {
        webpath =
          'ws:' +
          wsuri.substring(0, wsuri.indexOf(':')) +
          ':8091/websocket/1002'
      }

      console.log('webpathLog:' + webpath)
      this.websock2 = new WebSocket(webpath)
      this.websock2.onopen = this.websocketonopen2
      this.websock2.onmessage = this.websocketonmessage2
      this.websock2.onerror = this.websocketonerror2
      this.websock2.onclose = this.websocketclose2

      // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
      window.onbeforeunload = this.websocketclose2
    },
    //连接成功
    websocketonopen2() {
      console.log('WebSocket连接成功2')
    },
    //接收后端返回的数据
    websocketonmessage2(e) {
      // 在这里使用后端返回的数据，对数据进行处理渲染
      if (
        sessionStorage.getItem('token') &&
        sessionStorage.getItem('token') != undefined
      ) {
        var paramsInfo = JSON.parse(e.data)
        // var paramsInfo = e.data
        console.log('paramsInfo:', e.data)
        sessionStorage.setItem('paramsList', JSON.stringify(paramsInfo))
        // 集合
        this.$store.dispatch('d2admin/typeList/set', paramsInfo)
      }
    },
    //连接建立失败重连
    websocketonerror2(e) {
      console.log(`连接失败的信息2：`, e)
      this.initWebSocket2() // 连接失败后尝试重新连接
    },
    //关闭连接
    websocketclose2(e) {
      console.log('断开连接2', e)
    },
    //登录后页面刷新的时候获取
    getDateList() {
      this.tipList = []
      this.showList = []
      if (
        sessionStorage.getItem('token') &&
        sessionStorage.getItem('token') !== undefined
      ) {
        this.$http({
          url: this.$http.adornUrlAlarm('/alarmsInfo/queryList'),
          method: 'post',
          params: this.$http.adornParams({
            status: 1
          })
        }).then((data) => {
          if (data && data.code === 0) {
            console.log('报警信息:', data.list)
            this.tipList = data.list

            if (this.tipList.length < 4) {
              this.tipList.forEach((element) => {
                this.showList.push(element)
              })
            } else if (this.tipList.length >= 4) {
              for (let index = 0; index < 4; index++) {
                this.showList.push(this.tipList[index])
              }
            }
            if (this.showList) {
              this.showList.sort(this.sortBy('occurrenceTime', true))
            }
            this.$store.dispatch('d2admin/toast/set', this.tipList.length)
          }
        })
      }
    },
    getDateList2() {
      if (
        sessionStorage.getItem('token') &&
        sessionStorage.getItem('token') !== undefined
      ) {
        this.$http({
          url: this.$http.adornUrl('/kanban/getListParam'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then((data) => {
          if (data && data.code === 0) {
            console.log('typeList:', data.bean)
            sessionStorage.setItem('paramsList', JSON.stringify(data.bean))
            this.$store.dispatch('d2admin/typeList/set', data.bean)
          }
        })
      }
    },
    /**数组根据数组对象中的某个属性值进行排序的方法
     * 使用例子：newArray.sort(sortBy('number',false)) //表示根据number属性降序排列;若第二个参数不传递，默认表示升序排序
     * @param attr 排序的属性 如number属性
     * @param rev true表示升序排列，false降序排序
     * */
    sortBy: function(attr, rev) {
      //第二个参数没有传递 默认升序排列
      if (rev == undefined) {
        rev = 1
      } else {
        rev = rev ? 1 : -1
      }

      return function(a, b) {
        a = a[attr]
        b = b[attr]
        if (a < b) {
          return rev * -1
        }
        if (a > b) {
          return rev * 1
        }
        return 0
      }
    },
    closeTip(id, status) {
      var ruleForm = {
        id: id,
        status: status,
        beSend: true
      }
      this.$http({
        url: this.$http.adornUrlAlarm(`/alarmsInfo/update`),
        method: 'post',
        data: ruleForm
      }).then((data) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500
          })
          this.$store.dispatch('d2admin/toast/set', this.tipList.length)
        } else {
          this.$message.error(data.msg)
        }
      })
    }
  }
}
</script>
<style lang="scss">
@import '~@/assets/style/public-class.scss';
.resource-container {
  .ol-overviewmap {
    left: 89%;
    button {
      display: none;
    }
  }
}

// -----------
.d2-layout-header-aside-group
  .d2-layout-header-aside-content
  .d2-theme-container
  .d2-theme-container-main
  .d2-theme-container-main-body
  .container-component
  .d2-container-full
  .d2-container-full__body {
  margin-bottom: 50px;
}

//-- 公共样式--//
//  page页背景色
.d2-container-full__body {
  background-color: #b9c9e3 !important;
}

// 弹出框
.el-dialog__title {
  line-height: 24px;
  font-size: 18px;
  color: #303133;
  padding-left: 10px;
}

.el-dialog__header {
  border-top: 3px solid #21acfc;
  padding: 14px 20px 10px !important;
  background-color: #edeef1;
  border-bottom: 1px solid #e9e9e9;
}

// 自定义===============
// 标题加粗
.title-bold {
  font-weight: bold;
  font-size: 17px;
}

// 标题左侧颜色
.title-left-color {
  border-left: 5px solid #21acfc;
  padding-left: 10px;
}

.treeCard .el-card__header {
  height: 42px;
  line-height: 42px;
  background: #e1edf4;
  padding: 0 0 0 10px !important;
}

.treeCard > div {
  padding: 0 !important;
}

.tableCard .el-card__header {
  height: 42px;
  line-height: 42px;
  background: #edeef1;
  padding: 0 0 0 10px !important;
}

.tableHeader .el-table__header-wrapper {
  height: 42px;
  line-height: 42px;
  background: #edeef1;
  padding: 0 0 0 0 !important;
  transform: translateY(0px);
}

.tableHeader .has-gutter {
  transform: translateY(-12px);
}

// .elTree{
// background:#f4f4f4!important;
// }

// 二级菜单
.monitor_menu {
  .menu_item {
    width: 120px;
    height: 46px;
    line-height: 46px;
    text-align: center;
    color: #ccc;
    box-shadow: 0px 0 2px #000 !important;
    background-image: linear-gradient(to top, #335274, #4d759a) !important;
    text-shadow: 0px 2px 1px rgba(0, 0, 0, 0.3) !important;
  }

  .is-active {
    color: #fff !important;
    background: linear-gradient(
      0deg,
      rgba(33, 172, 252, 1),
      rgba(48, 136, 193, 1)
    ) !important;
  }

  ul {
    a:nth-child(1) {
      li {
        border-radius: 9px 0 0 9px !important;
      }
    }

    a:last-child {
      li {
        border-radius: 0 9px 9px 0 !important;
      }
    }
  }
}

.el-table .cell {
  /*display: flex;*/
  /*justify-content: center;*/
}

//-- 公共样式--//
.my_checkbox {
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #32a5ea !important;
  }

  .el-checkbox__input.is-disabled + span.el-checkbox__label {
    color: #606266;
  }

  .el-checkbox__input.is-disabled .el-checkbox__inner {
    background-color: #fff;
  }

  .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner {
    background-color: #32a5ea;
    border-color: #32a5ea;
  }

  .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner::after {
    border-color: #fff;
  }
}

.my_radio {
  .el-radio__input.is-disabled + span.el-radio__label {
    color: #606266;
  }

  .el-radio__input.is-checked + .el-radio__label {
    color: #32a5ea !important;
  }

  .el-radio__input.is-checked .el-radio__inner {
    border-color: #32a5ea;
    background: #32a5ea;
  }

  .el-radio__input.is-disabled.is-checked .el-radio__inner::after {
    background-color: #f5f7fa;
  }

  .el-radio__input.is-disabled .el-radio__inner {
    background-color: #fff;
  }

  .el-radio__input.is-disabled.is-checked .el-radio__inner {
    border-color: #32a5ea;
    background: #32a5ea;
  }
}

.mycard {
  /* width: 80% !important; */
  margin: 0 auto !important;
}

.mywidth {
  width: 100% !important;
}

.box_main {
  margin-top: 10px;
  padding: 10px 20px !important;
  background-color: white;

  .el-card {
    border: 0;
    box-shadow: 0 0 !important;

    .el-table {
      border-left: 1px solid #ebeef5;
      border-right: 1px solid #ebeef5;
      border-top: 1px solid #ebeef5;
    }
  }

  .el-card__body {
    /*padding: 20px 10px 0 10px;*/
    /*padding-bottom: 0;*/
    padding: 0;
  }
}

//文本域样式
.el-textarea__inner {
  // font: 400 13.3333px Arial;
  font-weight: 400;
  font-family: Arial, Helvetica, sans-serif;
  font-size: inherit;
}

//按钮样式
.el-button--text {
  font-size: 14px !important;
  font-family: Arial, Helvetica, sans-serif !important;
}

//el-step步骤样式
.is-vertical {
  flex-basis: auto !important;
}

.box_footer {
  background-color: white;
}

.el-icon-back2 {
  background: url(assets/images/icon/back.png) center no-repeat;
  background-size: cover;
}

.el-icon-back2:before {
  content: '替';
  font-size: 10px;
  visibility: hidden;
}

.el_container-style {
  padding: 0px;
  height: 77.6vh;
}

.el_header-style {
  height: 42px !important;
  line-height: 42px;
  border: 1px solid rgba(195, 195, 195, 1);
  background: #f4f4f4;
}

.el-table td div {
  text-overflow: ellipsis !important;
  overflow: hidden !important;
  white-space: nowrap !important;
}

.el-table td,
.el-table th {
  padding: 0 !important;
  line-height: 49px;
  height: 49px;
}
</style>

<style lang="scss" scoped>
.tip1 {
  width: 400px;
  height: 220px;
  position: fixed;
  z-index: 100;
  bottom: 80px;
  right: 10px;
  border: 1px solid #ccc;
  background: white;

  .title {
    height: 40px;
    line-height: 40px;
    text-indent: 15px;
    // color: #fff;
    background: #f1f5f8;

    span {
      font-size: 17px;
      font-weight: 400;
    }
  }

  .tip_warp {
    height: 180px;
    background: #f1f5f8;

    .tip_content {
      padding: 10px;
      color: #000000;

      .tip_level {
        font-size: 17px;
        color: red;
      }
    }

    .tip_imgwarp {
      img {
        width: 130px;
        height: 85px;
      }
    }

    .btn_warp {
      padding-top: 10px;

      .tip_btn {
        width: 80px;
      }
    }
  }
}

.tip2 {
  width: 400px;
  height: 220px;
  position: fixed;
  z-index: 100;
  bottom: 80px;
  right: 420px;
  border: 1px solid #ccc;
  background: white;

  .title {
    height: 40px;
    line-height: 40px;
    text-indent: 15px;
    // color: #fff;
    background: #f1f5f8;

    span {
      font-size: 17px;
      font-weight: 400;
    }
  }

  .tip_warp {
    height: 180px;
    background: #f1f5f8;

    .tip_content {
      padding: 10px;
      color: #000000;

      .tip_level {
        font-size: 17px;
        color: red;
      }
    }

    .tip_imgwarp {
      img {
        width: 130px;
        height: 85px;
      }
    }

    .btn_warp {
      padding-top: 10px;

      .tip_btn {
        width: 80px;
      }
    }
  }
}

.tip3 {
  width: 400px;
  height: 220px;
  position: fixed;
  z-index: 100;
  bottom: 80px;
  right: 830px;
  border: 1px solid #ccc;
  background: white;

  .title {
    height: 40px;
    line-height: 40px;
    text-indent: 15px;
    // color: #fff;
    background: f1f5f8;

    span {
      font-size: 17px;
      font-weight: 400;
    }
  }

  .tip_warp {
    height: 180px;
    background: #f1f5f8;

    .tip_content {
      padding: 10px;
      color: #000000;

      .tip_level {
        font-size: 17px;
        color: red;
      }
    }

    .tip_imgwarp {
      img {
        width: 130px;
        height: 85px;
      }
    }

    .btn_warp {
      padding-top: 10px;

      .tip_btn {
        width: 80px;
      }
    }
  }
}

.tip4 {
  width: 400px;
  height: 220px;
  position: fixed;
  z-index: 100;
  bottom: 80px;
  right: 1240px;
  border: 1px solid #ccc;
  background: white;

  .title {
    height: 40px;
    line-height: 40px;
    text-indent: 15px;
    // color: #fff;
    background: f1f5f8;

    span {
      font-size: 17px;
      font-weight: 400;
    }
  }

  .tip_warp {
    height: 180px;
    background: #f1f5f8;

    .tip_content {
      padding: 10px;
      color: #000000;

      .tip_level {
        font-size: 17px;
        color: red;
      }
    }

    .tip_imgwarp {
      img {
        width: 130px;
        height: 85px;
      }
    }

    .btn_warp {
      padding-top: 10px;

      .tip_btn {
        width: 80px;
      }
    }
  }
}

.el-divider--horizontal {
  margin: 1px 0;
  padding: 1px 1px;
  background: 0 0;
  border-top: 1px dashed;
}

// ie 弹出层闪烁
.el-dialog__wrapper {
  transition: none !important;

  &.dialog-fade-enter-active {
    transition: none !important;
    -ms-animation: none !important;
  }

  &.dialog-fade-leave-active {
    transition: none !important;
    -ms-animation: none !important;
  }
}
</style>
