<template>
  <div
    class="listeningPracticePlayer"
    :style="{
      width: width * scale + 'px',
      height: height * scale + 'px',
      transform: `scale(${1 / scale})`
    }"
  >
    <div v-if="loading" class="loading-overlay">
      <div class="loadingBox">
        <div class="loadingItem"></div>
        <div class="loadingItem"></div>
        <div class="loadingItem"></div>
        <div class="loadingItem"></div>
      </div>
    </div>
    <iframe class="musicIframe" frameborder="0" :src="url" @load="handleIframeLoad"></iframe>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from "vue"
import { getToken } from "@/libs/auth"
import { YJL_URL_API } from "@/config/index"
import queryParams from "@/queryParams"

const props = withDefaults(
  defineProps<{
    width: number
    height: number
    scale?: number
    code: string
  }>(),
  {
    scale: 1
  }
)
const url = computed(() => {
  if (queryParams.fromType === "CLASS") {
    return `${YJL_URL_API}/#/view-figner?Authorization=${getToken()}&code=${props.code}&type=listenMode&linkSource=class&isPreView=true`
  } else {
    return `${YJL_URL_API}/#/view-figner?Authorization=${getToken()}&code=${props.code}&platform=pc&type=listenMode&linkSource=class`
  }
})

// 先关闭这个功能
const loading = ref(true)
function handleIframeLoad() {
  loading.value = false
}
</script>

<style lang="scss" scoped>
.listeningPracticePlayer {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  user-select: none;
  line-height: 1;
  transform-origin: 0 0;
  .musicIframe {
    width: 100%;
    height: 100%;
  }
  .loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    z-index: 10;
    color: #fff;
    .loadingBox {
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      align-content: space-between;
      animation: rotate 1.5s linear infinite;
      .loadingItem {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        background: #20bdff;
        opacity: 0.5;
        &:nth-child(2) {
          opacity: 1;
        }
      }
    }
    @keyframes rotate {
      from {
        transform: rotate(0deg);
      }
      to {
        transform: rotate(360deg);
      }
    }
  }
}
</style>
