<script setup lang="ts">
/**
 *
 * @param {string} target - 挂载的目标元素
 * @param {boolean} loading - 是否显示loading
 * @param {boolean} isLoadingBackground - 是否显示背景
 */
interface internalProps {
  target?: string
  loading: boolean
  isLoadingBackground?: boolean
}

const props = withDefaults(defineProps<internalProps>(), {
  target: 'body',
  isLoadingBackground: false
})
</script>
<template>
  <Teleport :to="props.target">
    <div
      v-if="props.loading"
      class="v-loading-mask"
      :style="{ background: props.isLoadingBackground ? 'none' : '#2B2F36' }"
    >
      <div class="v-loading-spinner">
        <div>
          <img class="circular" src="./images/icon_loading.png" alt="" />
          <p class="loading-text">Loading...</p>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<style scoped>
.v-loading-mask {
  position: absolute;
  z-index: 2000;
  margin: 0;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  opacity: 0.7;
}

.v-loading-spinner {
  top: 50%;
  margin-top: -24px;
  width: 100%;
  text-align: center;
  position: absolute;
}

.circular {
  display: inline;
  height: 48px;
  width: 48px;
  margin-bottom: 8px;
  animation: loading-rotate 2s linear infinite;
}

.loading-text {
  color: var(--color-neutral-3);
}

@keyframes loading-rotate {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
