<template>
  <div :class="['x-direction-section-camera', direction ? 'out' : 'in']">
    <div
      v-for="(item, x) in cameras"
      :key="x"
      :style="item.style"
      :class="['x-camera', { act: cameraSelected.includes(x) || camerasFocus.includes(item.id) }]"
    >
      <div class="video-view">
        <VideoPlayer
          @click="() => videoFocus(x)"
          v-bind="{
            frameW: 368,
            frameH: 234,
            vcode: item.cameraIndexCode,
            pileNumber: item.pileNumber
          }"
        />
      </div>
      <div class="footer">
        <div class="pile">{{ item.pileNumber }}</div>
        <!-- <div class="speed">平均车速：80km/h</div> -->
      </div>

      <div @click="openVideo(item)" class="full-screen"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getCurrentInstance } from 'vue'
import { useRoute } from 'vue-router'
import bus from '@/utils/eventBus'
import VideoPlayer from '@/views/components/VideoPlayer/index.vue'

// import { useCamera as useCameraHome } from '@/flash/Home'

import { getCameras, setCameras } from '@/flash/Home'

// store
const app = getCurrentInstance()
const route = useRoute()

// 事件
const emits = defineEmits(['camera-change', 'click-trigger'])

// 参数
const props = withDefaults(
  defineProps<{
    _traffic: any
    tunnel: any
    direction: number
  }>(),
  {}
)

const camerasFocus = ref<string[]>([])

// 选中摄像头计算
const cameraSelected = computed(() => {
  const block = route.name

  if (block === 'Home') {
    return getCameras(props.direction)
  }

  return camerasFocus.value
})

// 路面摄像头铺设
const cameras = computed(() => {
  if (!props._traffic) return []
  return props._traffic.cameraFormate(props.tunnel.cameras, props.direction).map((item) => {
    return {
      ...item,
      style: {
        left: item.x + 'px'
      }
    }
  })
})

// 摄像头手动切换
const goSetCameras = (xcamera) => {
  const block = route.name

  if (block === 'Home') {
    return setCameras(xcamera, props.direction)
  } else {
    emits('camera-change', xcamera)
  }
}

onMounted(() => {
  bus.on('focus-camera', (ids) => {
    camerasFocus.value = ids
  })
})
</script>

<style lang="scss" scoped>
$base-url: '@/assets/images/home/';

.x-direction-section-camera {
  position: relative;
  //   left: 0;
  width: 100%;
  height: 256px;
  margin: 27px 0 15px;
  // z-index: 2;

  .x-camera {
    width: 368px;
    height: 256px;
    cursor: pointer;
    position: absolute;
    transform: translateX(-50%);

    .video-view {
      position: relative;
      height: 234px;
      box-sizing: border-box;
      border: 1px solid #8eb8d1;
      border-bottom: none;
      border-radius: 6px 6px 0 0;
      overflow: hidden;
    }
    .footer {
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      // position: absolute;
      padding: 3px 8px;
      box-sizing: border-box;
      // left: 0;
      // bottom: 0;
      background: #35669c;
      font-size: 12px;
      font-weight: 600;
      line-height: 18px;
      color: rgba(240, 245, 255, 0.95);
      border-radius: 0 0 6px 6px;
    }
  }
}
</style>
