<!-- 小程序视频监控 -->
<template>
  <div class="miniProgramVideo">
    <p class="null-text" v-if="nullType">{{nullType}}</p>
    <div class="video-max-box" v-if="buttonType&&!fullVideoType">
      <H5PlayerVideo class="video-box" v-for="(item,index) in videoList" :key="index"
                     :videoProps="item" :style="'width:'+item.width+'px;height:'+item.height+'px;'"></H5PlayerVideo>
    </div>
    <fullH5PlayerVideo v-if="fullVideoType" :fullVideoProps="fullVideoProps"></fullH5PlayerVideo>
  </div>
</template>
<script>
  import H5PlayerVideo from '@/components/H5PlayerVideo.vue';
  import fullH5PlayerVideo from '@/components/fullH5PlayerVideo.vue';
  import { iotCameraFindByCondition,iotCameraGetPlaybackURLs } from "@/api/index";
  export default {
    name: 'index',
    components: {
      H5PlayerVideo,
      fullH5PlayerVideo
    },
    data () {
      return {
        buttonType: false,
        width: null,
        height: null,
        videoList: [],
        nullType:false,
        //报警视频数据
        videoProps:null,
        //全屏视频参数
        fullVideoProps:{},
        fullVideoType:false,
      }
    },
    created () {
      const ratio = 0.5625;
      const winWidth = window.innerWidth;
      let width = parseInt(winWidth - 20);
      let height = parseInt(this.accMul(width, ratio));
      this.$set(this, 'width', width);
      this.$set(this, 'height', height)
    },
    mounted () {
      this.getUrl();
    },
    methods: {
      //全屏开启-关闭轮播
      stopTime(cameraIndexCode){
        this.$set(this,'fullVideoProps',{cameraIndexCode:cameraIndexCode});
        this.$set(this,'fullVideoType',true);
      },
      //全屏关闭-开启轮播
      outFullScreen(){
        this.$set(this,'fullVideoType',false);
        this.$set(this,'fullVideoProps',{});
      },
      getUrl() {
        let text = decodeURIComponent(window.location.href);
        if(text.indexOf('touken') != -1){
          this.subVideo(text);
        }else{
          this.$set(this,'nullType','参数异常,请联系管理员');
        }
      },
      subVideo(text){
        let self = this;
        if(text.indexOf('touken') == -1){
          this.$set(this,'nullType','touken参数异常,请联系管理员');
          return
        }
        if(text.indexOf('source') == -1){
          this.$set(this,'nullType','source参数异常,请联系管理员');
          return
        }
        if(text.indexOf('type') == -1){
          this.$set(this,'nullType','type参数异常,请联系管理员');
          return
        }
        let urlList = text.split("?")[1].split("&");
        let urlData = {};
        urlList.forEach((item) => {
          urlData[item.split("=")[0]] = item.split("=")[1];
        });
        localStorage.setItem('touken',urlData.touken)
        // type 1.楼栋 2.楼层 3.楼道 4.实验室 5.楼道+实验室
        let obj = {
          page:'1',
          pageSize:'20',
          protocol:window.location.href.indexOf('https') !== -1?'wss':'ws',
          streamType:1,
        };
        if(urlData.type == 1){
          obj.buildId = urlData.buildId;
        }else  if(urlData.type == 2){
          obj.floorId = urlData.floorId;
        }else  if(urlData.type == 3){
          obj.passageway = urlData.floorId;
        }else  if(urlData.type == 4){
          obj.subIds = [urlData.subId];
        }else if(urlData.type == 5){
          obj.passageway = urlData.floorId;
          obj.subIds = [urlData.subId];
        }
        if(urlData.source == '2'){
          obj.source = 2;
        }else if (urlData.source == '5'){
          obj.source = 5;
        }
        if(urlData.type){
          iotCameraFindByCondition(obj).then(response => {
            if (!response.data.records[0]){
              this.$set(this,'nullType','该实验室未配置摄像头');
            }else{
              let list = [];
              for(let i=0;i<response.data.records.length;i++){
                list.push(
                  {
                    width: this.width, //(宽度:非必传-默认600)
                    height: this.height, //(高度:非必传-默认338)
                    url: response.data.records[i].streamUrl,
                    cameraIndexCode: response.data.records[i].deviceNo,
                  }
                )
              }
              this.$set(this,'videoList',list)
              this.$nextTick(()=>{
                setTimeout(function(){
                  self.$set(self, 'buttonType', true);
                },1000);
              })
            }
          });
        }else{
          this.$set(this,'nullType','参数异常,请联系管理员');
        }
      },
      //乘法
      accMul(arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try {
          m += s1.split(".")[1].length
        } catch (e) {
        }
        try {
          m += s2.split(".")[1].length
        } catch (e) {
        }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
      },
    },
  }
</script>
<style scoped lang="scss">
  .miniProgramVideo{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .null-text{
      text-align: center;
      font-size:16px;
      line-height:80px;
    }
    .video-max-box {
      padding-top:10px;
      overflow-y: scroll;
      overflow-x: hidden;
    }

    .video-box {
      margin: 0 auto 10px;
    }
  }
</style>
