<template>
  <div class="videos-wrap">
    <video-player
      class="video-player vjs-custom-skin"
      ref="videoPlayer"
      :options="playerOptions"
      @play="play"
      @stop="stop"
    ></video-player>
  </div>
</template>

<script>
export default {
  name: "videos",
  props: ["videoList", "index"],
  data() {
    return {
      addPlayFlag: false,    // 是否已经添加视频播放次数
      videoData: "",
      currentIndex: "",
      playerOptions: {
        playsinline: true,
        autoplay: false, // 如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: true, // 导致视频一结束就重新开始。
        preload: "auto",
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        language: "zh-CN", // 将语言设置为简体中文
        // aspectRatio: '16:9',
        sources: [
          {
            src: this.videoList.VideoUrl, // 路径
            type: "video/mp4" // 类型
          }
        ],
        // poster: "../../static/images/test.jpg", //你的封面地址
        width: document.documentElement.clientWidth,
        notSupportedMessage: "此视频暂无法播放，请稍后再试", // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: false,
        /* controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: false, // 是否显示剩余时间功能
          fullscreenToggle: true // 全屏按钮
        }, */
        playing: false // 播放状态
      }
    };
  },
  created() { },
  mounted() {
    if (this.videoData.VideoUrl) {
      this.playerOptions.sources.src = this.videoData.VideoUrl;
    }
    this.autoPlayAction();
  },
  methods: {
    // 点击播放或者暂停视频
    playOrStop() {
      if (this.playing) {
        this.$nextTick(_ => {
          this.$refs.videoPlayer.player.pause();
          this.playing = false;
        });
      } else {
        this.$nextTick(_ => {
          this.$refs.videoPlayer.player.play();
          this.playing = true;
        });
      }
    },
    // 自动播放第一个视频
    autoPlayAction() {
      if (this.index === 0) {
        this.playing = true;
        this.$nextTick(_=>{
          this.playerOptions.autoplay = true;
          this.$refs.videoPlayer.player.play();
        })
      }
    },

    play() {
      // this.$refs.videoPlayer.player.load();
      this.$refs.videoPlayer.player.play();
      this.playing = true;
      if (!this.addPlayFlag) {
        this.$emit('addVideoCount', { videoData: this.videoData })
        this.addPlayFlag = true
      }
    },

    stop() {
      this.$refs.videoPlayer.player.pause();
      this.playing = false;
    }
  },
  watch: {
    videoList: {
      handler(newValue, oldValue) {
        this.videoData = newValue;
        this.playerOptions.sources.src = newValue.VideoUrl;
      },
      immediate: true, // 立即执行
      deep: true // 开启深度监听
    },
    currentIndex: {
      handler(newValue, oldValue) {
        this.currentIndex = newValue;
      },
      immediate: true,
      deep: true
    }
  }
};
</script>

<style lang="less" scoped></style>
