<template>

  <div class="track-views">
  
    <el-scrollbar>
  
      <div class="view-block">
  
        <div class="header">
          <div class="icon"></div>
          <div class="text">卡口抓拍图片</div>
        </div>
  
        <div class="content"></div>
  
        <div class="bar"></div>
  
      </div>
  
      <div class="view-block">
  
        <div class="header">
          <div class="icon"></div>
          <div class="text">追踪车辆最近监控</div>
        </div>
  
        <div class="content">
          <CameraPlayer
            v-if="followCameras[0]"
            v-bind="{
              frameW: 360,
              frameH: 230,
              vcode: followCameras[0].cameraIndexCode,
              pileNumber: followCameras[0].pileNumber
            }"
          />
        </div>
  
        <div class="bar">
          {{ followCameras[0]?.pileNumber }}
        </div>
  
      </div>
  
      <div class="view-block">
  
        <div class="header">
          <div class="icon"></div>
          <div class="text">追踪车辆最近监控</div>
        </div>
  
        <div class="content">
          <CameraPlayer
            v-if="followCameras[1]"
            v-bind="{
              frameW: 360,
              frameH: 230,
              vcode: followCameras[1].cameraIndexCode,
              pileNumber: followCameras[1].pileNumber
            }"
          />
        </div>
  
        <div class="bar">
          {{ followCameras[0]?.pileNumber }}
        </div>
  
      </div>
  
    </el-scrollbar>
  
  </div>
  
  </template>
  
  <script lang='ts' setup>
  
  import CameraPlayer from '@/views/components/VideoPlayer/index.vue'
  
  import { useTunnelStoreWithOut } from '@/store/modules/tunnel'
  import { carTracked } from '../../_state'
  
  
  const tunnelStore = useTunnelStoreWithOut()
  
  // 摄像机
  const cameras = computed(() => {
    // console.log(carTracked.value)
    const tunnel = [
      tunnelStore.getUpTunnel,
      tunnelStore.getDownTunnel
    ][carTracked.value.direct]
    return tunnel.cameras.filter(item => item.cameraLable === 1)
  })
  
  // 跟随摄像机
  const followCameras = computed(() => {
  
    if (carTracked.value && cameras.value.length) {
  
      const carMilestone = carTracked.value.milestone
      const arr = [...cameras.value]
  
      arr.sort((a, b) => a.equipmentStation - b.equipmentStation) // 正序
      const x = arr.findIndex(item => item.equipmentStation >= carMilestone)
  
  
      // 下行
      if (carTracked.value.direct) {
        
        if (x === 0) {
          return arr.slice(2).reserve()
        }
        if (x === arr.length - 1) {
          return arr.slice(-2).reserve()
        }
        return [arr[x + 1], arr[x]]
  
      }
  
      // 上行
      if (!carTracked.value.direct) {
  
        if (x === 0) {
          return arr.slice(2)
        }
        if (x === arr.length - 1) {
          return arr.slice(-2)
        }
        return [arr[x], arr[x + 1]]
  
      }
  
    } else {
  
      return []
  
    }
    
  })
  
  </script>
  
  <style lang='scss' scoped>
  
  $Common_Url: '@/assets/images/3d/';
  
  .track-views {
    position: absolute;
    right: 0;
    top: 0;
    z-index: 9;
  
    height: 100%;
    overflow: hidden;
  
    .view-block {
      margin-bottom: 20px;
      position: relative;
      .header {
        display: flex;
        align-items: center;
        .icon {
          width: 30px;
          height: 30px;
          background: url($Common_Url + 'title.png') no-repeat;
          background-size: cover;
        }
        .text {
          height: 30px;
          line-height: 30px;
          color: white;
          font-size: 14px;
          font-weight: bold;
          padding-left: 15px;
          border-top-left-radius: 30px;
          border-bottom-left-radius: 30px;
          background: linear-gradient(90deg, rgba(87, 115, 162, 0.81) 0%, rgba(87, 115, 162, 0) 99%);
        }
      }
      .content {
        width: 360px;
        height: 230px;
        margin-top: 8px;
        background: black;
      }
  
      .bar {
        width: 360px;
        height: 32px;
        background: rgba(53, 102, 156, 0.7);
        position: absolute;
        left: 0;
        bottom: 0;
        font-size: 12px;
        font-weight: bold;
        color: white;
        padding: 0 10px;
        line-height: 32px;
        box-sizing: border-box;
      }
    }
  
  }
  
  </style>