<template>
  <div class="video-container">
    <div class="header">
      <ElSelect @change="showVideo" v-model="cameraId">
        <ElOption v-for="item in urls"
          :key="item.id" :label="item.deviceName" :value="item.iotDeviceId">
        </ElOption>
      </ElSelect>
    </div>
    <Transition name="fade" mode="out-in">
      <video controls autoplay muted class="video-stream" v-bind="$attrs" ref="videoContainerRef" />
    </Transition>
  </div>
</template>

<script>
import { HttpLivePlayer, openLiveVideo } from "./videoStream.js"

import {
  getNearCameraNew,
} from "@screen/pages/Home/components/RoadAndEvents/utils/httpList.js";

export default {
  name: 'VideoControls',
  components: {
  },
  props: {
    // 桩号
    pileNum: {
      type: String,
      default: null
    },
    rangeIndex: {
      type: String,
      default: null
    },
    showHeader: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      active: "video",
      player: null,
      urls:[],
      cameraId: null
    }
  },
  async mounted() {
    this.playVideo();
    this.$once("hook:beforeDestroy", () => this.player?.destroy());
  },
  methods: {
    
    async playVideo() {
      this.player?.destroy();
      
      let {code,data} = await getNearCameraNew(this.pileNum).catch(() => ({}));
      if (
        code != 200 ||
        (Array.isArray(data) ? !data?.length : !Object.keys(data || {}).length)
      ) {
        Message.warning("未获取到附近的相机信息");
        return;
      }
      if(data[this.rangeIndex]){
        this.urls = data[this.rangeIndex]
        this.cameraId = data[this.rangeIndex][0]['iotDeviceId']
        this.showVideo();
      }
    },
    showVideo(){
      this.player = new HttpLivePlayer(this.$refs.videoContainerRef, { camId: this.cameraId});
    }
  }
}
</script>

<style lang='scss' scoped>
.video-container {
  position: relative;
  background-color: #000;
  height: 240px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;

  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.24s ease;
  }

  .fade-enter-from,
  .fade-leave-to {
    opacity: 0;
  }

  .header {
    width: 100%;
    z-index: 999;
    display: flex;
    justify-content: space-between;
    .radio {
      background: #265A70;
      border-radius: 41px 41px 41px 41px;
      overflow: hidden;
      opacity: 1;
      border: 1px solid #3DE8FF;
      font-size: 12px;
      // font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      color: #FFFFFF;
      line-height: 14px;
      height: fit-content;

      // -webkit-background-clip: text;
      // -webkit-text-fill-color: transparent;

      .active {
        background-color: rgba(61, 232, 255, 1);
      }

      span {
        background-color: rgba(38, 90, 112, 1);
        padding: 4px 9px;
        display: inline-block;
        cursor: pointer;

        &:hover {
          background-color: rgba(61, 232, 255, 1);
        }
      }
    }

    .btn {
      background: #265A70;
      border-radius: 6px 6px 6px 6px;
      opacity: 1;
      border: 1px solid #3DE8FF;
      font-size: 12px;
      // font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      color: #FFFFFF;
      line-height: 14px;
      padding: 3px 9px;
      cursor: pointer;
      // -webkit-background-clip: text;
      // -webkit-text-fill-color: transparent;
    }
  }


  .video-stream,
  img {
    height: 100%;
    max-height: 100%;
    max-width: 100%;
  }

  .video-stream {
    width: 100%;
  }
}
</style>
