<template>
  <transition appear name="custom-classes-transition" leave-active-class="animated fadeOut faster">
    <div class="open-video">
      <!-- <div class="vmask"></div> -->
      <video x5-playsinline="true" playsinline="true" webkit-playsinline="true" class="video" ref="openvideo$" preload autoplay :src="videourl"></video>
      <div v-show="videourl" @click.stop="countdown==0&&emit('close')" class="jump">{{countdown>0?countdown+$t('common.jumpTips'):$t('common.jump')}}</div>
      <img v-show="videourl && bofanging" @click.stop="bofang" class="bofang" :src="require('@/assets/images/icon/bofang.png')" alt="" />
    </div>
  </transition>
</template>
<script setup>
import { ref, watch, defineEmits, computed, onMounted, nextTick, defineProps } from "vue";
import * as apis from "@/apis/index.js";
import { useI18n, getLocale } from '@/i18n'
const { t } = useI18n({ useScope: 'global' })
const openvideo$ = ref(null);

const videourl = ref(null);
const countdown = ref(5);

const timer = ref(null);


const bofanging = ref(true);

const emit = defineEmits(["close"]);

const bofang = () => {
  openvideo$.value.play();
};

onMounted(() => {
  nextTick(async () => {
    let res = await apis.get_video();
    if (!res.data) {
      return emit("close");
    }
    videourl.value = res.data.videoUrl;
    openvideo$.value.addEventListener("ended", () => {
      emit("close");
    });

    openvideo$.value.addEventListener("playing", () => {
      if (bofanging.value) {
        bofanging.value = false;
      }
      if (countdown.value >0 && !timer.value) {
        timer.value = setInterval(() => {
          if (countdown.value ==0) {
            clearInterval(timer.value)
            timer.value=null
            return
          }
          countdown.value --
        }, 1000);
      }
    });

    openvideo$.value.addEventListener("pause", () => {
      if (!bofanging.value) {
        bofanging.value = true;
      }
    });
    
    document.addEventListener(
      "WeixinJSBridgeReady",
      () => {
        openvideo$.value.play();
      },
      false
    );
  });
});
</script>
<style lang="scss" scoped>
.open-video {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: table;
  table-layout: fixed;
  z-index: 1100000;
  background-color: rgba(0, 0, 0, 1);

  .bofang {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    left: 50%;
    width: 80px;
    height: 80px;
    z-index: 999;
  }
  .vmask {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    display: inline-block;
    z-index: 2;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(10px);
  }
  .video {
    width: 100%;
    height: 100%;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    position: relative;
    z-index: 9;
  }
  .jump {
    position: absolute;
    right: 15px;
    top: 20px;
    background: rgba(255, 255, 255, 0.5);;
    text-align: center;
    line-height: 28px;
    font-size: 16px;
    z-index: 999;
    color: #333333;
    border-radius: 1px;
    padding: 4px 12px;
  }
}
</style>
