<template>
  <div class="page-main">
    <div class="page-top row-12">
      <div class="col-4">开始时间：<t-date-picker v-model="startDate" type="dateTime" style="width:200px" placeholder="请选择开始时间"></t-date-picker>
      </div>
      <div class="col-4">结束时间：<t-date-picker v-model="endDate" type="dateTime" style="width:200px" placeholder="请选择结束时间"></t-date-picker>
      </div>
      <div class="col-4 button-box" align="end">
        <t-button color="primary" @click="getData">查询</t-button>
        <t-button @click="initData">重置</t-button>
      </div>
    </div>
    <div class="page-content">
      <div class="distinguish">
        <b>识别图片</b>
        <div v-if="distinguishUrl !== ''">
          <img :src="distinguishUrl" class="distinguish-img">
        </div>
        <div v-else>
          <t-empty size="lg"></t-empty>
        </div>
      </div>
      <div class="distinguish">
        <b>识别视频</b>
        <div v-if="distinguishUrl !== ''">
          <div class="distinguish-video">
            <video-player ref="videoPlayer" :playsinline="true" :options="playerOptions" class="video-player vjs-custom-skin"></video-player>
          </div>
        </div>
        <div v-else>
          <t-empty size="lg"></t-empty>
        </div>
      </div>
    </div>
    <div class="page-bottom">
      <div>
        <b>识别结果</b>
        <div class="bottom-result">
          <div class="page-top row-12">
            <div class="result-txt col-6">设备位置：<span style="color:rgba(0, 0, 0, 0.65)">{{ devicePosition }}</span></div>
            <div class="result-txt col-6">抓拍时间：<span style="color:rgba(0, 0, 0, 0.65)">{{ captureTIme }}</span></div>
          </div>
          <div class="result-txt" style="margin-top:24px">识别结果：<span style="color:rgba(0, 0, 0, 0.65)">{{ defineResult }}</span></div>
        </div>
      </div>
      <div class="img-viewer">
        <t-carousel dots="none" :speed="10000000000" arrow="always" style="width: 100%">
          <t-carousel-item v-for="(item1,index1) in dataImgList" :key="index1" style="width: 100%;padding-left: 10%">
            <div v-for="(item, index) in item1" :key="index" :src="item.idenPictureUrl" class="image-carousel">
              <img :src="'http://10.1.194.18:9000/ai-image/'+item.idenPictureUrl" class="img-viewer-size" @click="getParticularsData(item.aiIdenLogId,index)">
              <span :class="{'fileName' : index === fileNameIndex}" style="width: 132px;margin-right: 14px;text-align:center">{{ item.fileName }}</span>
            </div>
          </t-carousel-item>
        </t-carousel>
      </div>
    </div>
  </div>
</template>
<script>
import sysapi from '@/api/videoSurveillance'
export default {
  data() {
    return {
      startDate: '',
      endDate: '',
      videoId: '', // 视频ID
      captureTIme: '',
      defineResult: '',
      devicePosition: '',
      distinguishUrl: '',
      dataImg: [],
      dataImgList: [],
      fileNameIndex: 0,
      playerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: true, // 如果true,浏览器准备好时开始回放。
        muted: true, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '13:10', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        // fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        // 是否流体自适应容器宽高
        fluid: true,
        // // 设置视频播放器的显示宽度（以像素为单位）
        // width: '100px',
        // // 设置视频播放器的显示高度（以像素为单位）
        // height: '300px',
        sources: [{
          type: 'application/x-mpegURL', // 这里的种类支持很多种：基本视频格式、直播、流媒体等，具体可以参看git网址项目
          src: '' // url地址
        }],

        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          // durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true, // 全屏按钮,
          pictureInPictureToggle: false
        }
      }
    }
  },
  mounted() {
    this.videoId = this.$route.query.videoId
    this.initData()
  },
  methods: {
    // 获取识别记录查询数据
    initData() {
      var date = new Date()
      this.endDate = this.formatDateTime(date)
      date.setDate(date.getDate() - 30)
      this.startDate = this.formatDateTime(date)
      this.getData()
    },
    getData() {
      if (this.startDate === ''|| this.endDate === '') {
        this.$Message.danger('请选择时间段')
      }
      var flag = this.startreend(this.startDate, this.endDate)
      if (flag) {
        var data = { params: { beginDay: this.startDate, endDay: this.endDate, id: this.videoId, pageNumber: 1, pageSize: 10000 }}
        sysapi.getDistinguishData(data).then(res => {
          console.log(res.data)
          this.dataImg = res.data.data
          if (res.data.data.length > 0) {
            this.getParticularsData(res.data.data[0].aiIdenLogId, 0)
            this.dataImgList = this.arrayGroup(res.data.data, 10)
          } else {
            this.devicePosition = ''
            this.defineResult = ''
            this.captureTIme = ''
            this.distinguishUrl = ''
            this.playerOptions.sources[0].src = ''
            this.$Message.warning('所选时间段没有识别记录')
          }
        })
      } else {
        this.$Message.danger('开始时间不能在结束时间的后面')
      }
    },
    arrayGroup(array, subGroupLength) {
      let index = 0
      let newArray = []
      while(index < array.length) {
        newArray.push(array.slice(index, index += subGroupLength))
      }
      return newArray
    },
    // 字符串更改为时间类型
    stringtodate (date) {
      date = date.substring(0, 19)
      date = date.replace(/-/g, '/')
      var timestamp = new Date(date).getTime()
      return timestamp
    },
    // 验证开始时间·和结束时间
    startreend (startdate, enddate) {
      startdate = this.stringtodate(startdate)
      enddate = this.stringtodate(enddate)
      if (startdate > enddate) {
        return false
      } else {
        return true
      }
    },
    getParticularsData(aiIdenLogId, index) {
      this.fileNameIndex = index
      sysapi.queryOneAiIdenLog(aiIdenLogId).then(res => {
        console.log(res.data)
        this.devicePosition = res.data.data.alarmInfo.monitorSceneName
        this.defineResult = res.data.data.alarmInfo.alarmMemo
        this.captureTIme = res.data.data.pictureInfo.fileDateTimeStr
        this.distinguishUrl = res.data.data.pictureInfo.fileUrl
        this.playerOptions.sources[0].src = res.data.data.videoInfo
      })
    },
    // 重置数据
    resetData() {
      this.startDate = ''
      this.endDate = ''
    },
    formatDateTime (date) {
      let y = date.getFullYear()
      let m = date.getMonth() + 1
      m = m < 10 ? ('0' + m) : m
      let d = date.getDate()
      d = d < 10 ? ('0' + d) : d
      let h = date.getHours()
      h = h < 10 ? ('0' + h) : h
      let minute = date.getMinutes()
      minute = minute < 10 ? ('0' + minute) : minute
      let second = date.getSeconds()
      second = second < 10 ? ('0' + second) : second
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
    }
  }
}
</script>
<style lang="scss" scoped>
.page-main{
    padding: 24px;
    .page-top{
        display: flex;
        .col-4{
            padding: 0;
        }
        .col-6{
            padding: 0;
        }
        .button-box{
        display: flex;
        justify-content: flex-end;
        & button{
            margin-left: 8px;
        }
    }
    }
    .page-content{
        margin-left: -24px;
        padding: 24px 0;
        display: flex;
        .distinguish{
            margin-left: 24px;
            display: flex;
            flex-direction: column;
            .distinguish-img{
                border-radius: 4px;
                margin-top: 10px;
                height: 380px;
                width: 100%;;
            }
            .distinguish-video{
                border-radius: 4px;
                margin-top: 10px;
                height: 380px;
                width: 500px;;
            }
        }
    }
    .page-bottom{
        .bottom-result{
            margin-top: 12px;
            padding: 24px;
            border: 1px solid rgba(233, 233, 233, 1);
            .result-txt{
                color: rgba(0, 0, 0, 0.45)
            }
        }
        .img-viewer{
            margin-top: 24px;
            display: flex;
            .image-carousel{
              display: flex;
              width: 9%;
              height: 80%;
              flex-direction: column;
              .img-viewer-size{
                cursor:pointer;
                width: 100%;
                height: 100%;
                margin-right: 14px;
                padding-left: 2%;
              }
            }
        }
    }
    .carousel-control-prev-icon{
      color: #009bf3;
    }
}
.fileName{
  color: #009bf3;
}
</style>
