<template>
  <div>
    <video
      :id="'TCPlayerVideo'"
      class="TCPlayer-video-container"
      preload="auto"
      crossOrigin="anonymous"
      playsinline
      autoplay
      :loop="false"
      :muted="true"
      :style="{
        width: props.width + 'px',
        height: props.height + 'px',
      }"
    />
  </div>
</template>

<script setup lang="ts">
  import { onMounted, onUnmounted, ref, watch, nextTick } from 'vue';

  const props = defineProps(['serialNumberValue', 'width', 'height']);

  // 视频控件初始化
  let player: any = null;

  function handlerPlayVideo() {
    nextTick(() => {
      if (player) {
        player.src(props.serialNumberValue);
      } else {
        player = TCPlayer('TCPlayerVideo', {});
        player.src(props.serialNumberValue);
      }
    });
  }

  function closePlayerVideo() {
    if (player) {
      player.dispose();
      player = null;
    }
  }

  watch(
    () => props.serialNumberValue,
    () => {
      handlerPlayVideo();
    },
  );

  onMounted(() => {
    handlerPlayVideo();
  });

  onUnmounted(() => {
    closePlayerVideo();
  });

  defineExpose({
    handlerPlayVideo,
    closePlayerVideo,
  });
</script>

<style lang="scss" scoped>
  video {
    display: block;
    object-fit: contain;
  }

  ::v-deep .vjs-live-control .vjs-live-display {
    width: 100px !important;
  }
</style>
