<template>
  <div class="video-wrapper" :class="className">
    <div>
      <div v-swiper:mySwiper="swiperOption" ref="mySwiper">
        <div class="swiper-wrapper">
          <div class="swiper-slide" v-for="(item, index) in list" :key="index">
            <div class="item-media">
              <img :src="item.poster" />
              <div
                class="video-play"
                @click.stop="open(item)"
                @mouseenter.stop="handleVideoMouseEnter"
                @mousemove.stop="handleVideoMouseEnter"
              ></div>
            </div>
            <div class="item-text">
              <div class="item-text-tit">{{ item.title }}</div>
              <div class="item-text-desc">{{ item.desc }}</div>
            </div>
          </div>
        </div>
        <div class="swiper-pagination swiper-pagination-bullets"></div>
      </div>
    </div>
    <video
      v-if="isShow === true"
      class="video"
      :src="playVideo.src"
      controls="controls"
      preload="auto"
      autoplay="autoplay"
      x5-playsinline=""
      playsinline
      webkit-playsinline="true"
      ref="video"
    ></video>
    <div class="overlay" v-if="isShow === true" @click="close"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      list: [
        {
          title: '玛丽伍德大学校长致词',
          desc: 'Address from the President of Marywood University',
          src: 'https://webapp-pub.ezijing.com/project/marywood-plus/video2.mp4',
          poster: 'https://webapp-pub.ezijing.com/project/marywood-plus/video_poster1.png'
        },
        {
          title: '玛丽伍德大学介绍',
          desc: 'The Introduction of Marywood University',
          src: 'https://webapp-pub.ezijing.com/project/marywood/marywood.mp4',
          poster: 'https://webapp-pub.ezijing.com/project/marywood-plus/video_poster.png'
        }
      ],
      // 当前播放的视频
      playVideo: {},
      showType: '',
      showIndex: 0,
      mouseCursor: 'pointer',
      swiperOption: {
        speed: 400,
        autoplay: false,
        delay: 3000,
        loop: false,
        height: 500,
        slidesPerView: 'auto',
        centeredSlides: true
      }
    }
  },
  computed: {
    isMobile() {
      return this.$store.state.isMobile
    },
    className() {
      return {
        'is-h5': this.isMobile
      }
    }
  },
  methods: {
    handleMouseEnter(e) {
      const target = this.$refs.target.getBoundingClientRect()
      if (e.clientX - target.left < (target.right - target.left) / 2) {
        if (this.showIndex > 0) {
          this.mouseCursor =
            'url(https://www.harvard.edu/wp-content/themes/core/assets/img/theme/featured-stories/arrow-left-dark.svg) 30 30, e-resize'
          this.showType = 'left'
        } else {
          this.mouseCursor = 'default'
          this.showType = ''
        }
      } else {
        if (this.showIndex < this.list.length - 1) {
          this.mouseCursor =
            'url(https://www.harvard.edu/wp-content/themes/core/assets/img/theme/featured-stories/arrow-right-dark.svg) 30 30, e-resize'
          this.showType = 'right'
        } else {
          this.mouseCursor = 'default'
          this.showType = ''
        }
      }
    },
    handleVideoMouseEnter() {},
    handleClick() {
      console.log(this.showIndex)
      if (this.showType === 'left') {
        this.handlePrev()
      } else if (this.showType === 'right') {
        this.handleNext()
      }
    },
    handlePrev() {
      // 末尾的视频放到前面
      // const deleteVideo = this.list.splice(this.list.length - 1, 1)
      // this.list.unshift(deleteVideo[0])
      this.showIndex--
      if (this.showIndex <= 0) {
        this.mouseCursor = 'default'
        this.showType = ''
        this.showIndex = 0
      }
      this.$refs.mySwiper.swiper.slideTo(this.showIndex)
    },
    handleNext() {
      // 第一个视频放到最后
      // const deleteVideo = this.list.splice(0, 1)
      // this.list = this.list.concat(deleteVideo)
      this.showIndex++
      if (this.showIndex >= this.list.length - 1) {
        this.mouseCursor = 'default'
        this.showType = ''
        this.showIndex = this.list.length - 1
      }
      this.$refs.mySwiper.swiper.slideTo(this.showIndex)
    },
    open(item) {
      this.playVideo = item
      this.isShow = true
      this.$nextTick(() => {
        this.$refs.video.play()
      })
      document.getElementsByTagName('body')[0].style.height = '100%'
      document.getElementsByTagName('body')[0].style.overflow = 'hidden'
    },
    close() {
      this.isShow = false
      document.getElementsByTagName('body')[0].style.height = ''
      document.getElementsByTagName('body')[0].style.overflow = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.is-h5 {
  .overlay {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 1999;
    background: rgba(0, 0, 0, 0.5);
  }
  .video-list {
    display: flex;
    padding: 0 0.3rem;
    overflow-x: auto;
  }
  .video-item {
    width: 3.25rem;
  }
  .video-item + .video-item {
    margin-left: 0.1rem;
  }
  .video-poster {
    position: relative;
    width: 3.25rem;
    height: 2.04rem;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    &::after {
      content: '';
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      background: rgba(0, 0, 0, 0.3);
      z-index: 1;
    }
  }
  .video-play {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0.39rem;
    height: 0.39rem;
    background: url(https://webapp-pub.ezijing.com/project/marywood-plus/icon_play.png) no-repeat center center;
    background-size: contain;
    transform: translate(-50%, -50%);
    cursor: pointer;
    z-index: 2;
  }
  .video-content {
    padding: 0.1rem 0;
  }
  .video-title {
    text-align: center;
    font-size: 0.12rem;
    color: #484848;
  }
  .video-desc {
    text-align: center;
    font-size: 0.12rem;
    color: #484848;
  }
  .video {
    width: 100%;
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10000;
  }
  .swiper-container {
    // height: 4.5rem;
    // .swiper-wrapper{
    // }
    .swiper-slide {
      width: 3.2rem;
      position: relative;
      padding: 0 0.1rem;
      .item-media {
        width: 3rem;
        position: relative;
        img {
          width: 100%;
          height: 3rem;
        }
        .video-play {
          position: absolute;
          top: 50%;
          left: 50%;
          width: 79px;
          height: 79px;
          background: url(https://webapp-pub.ezijing.com/project/marywood-plus/icon_play.png) no-repeat center center;
          background-size: contain;
          transform: translate(-50%, -50%);
          cursor: pointer;
          z-index: 2200;
        }
        &:after {
          content: '';
          display: block;
          width: 100%;
          height: 100%;
          position: absolute;
          top: 0;
          left: 0;
          background: rgba(0, 0, 0, 0.7);
        }
      }
      .item-text {
        flex: 1;
        color: #454545;
        padding: 0.1rem 0.2rem 0;
        .item-text-tit {
          font-size: 0.22rem;
          line-height: 0.5rem;
        }
        .item-text-desc {
          font-size: 0.16rem;
        }
      }
      &.swiper-slide-active .item-media:after {
        display: none;
      }
    }
  }
}
</style>
