<template>
    <div class="section-loading" v-if="progress != 100">
      <div class="section1-wwbox">
        <img src="/img/section1/section1-ww.webp" alt="" class="fadeIn">
        <div class="title2 fadeIn"></div>
        <div>
          <div class="icon">
            <img src="./img/webp/wsicon.webp" alt="">
          </div>
          <p id="progress-bar"><span>{{ progress }}</span>%</p>
        </div>
      </div>
    </div>
</template>
  
<script setup>
import { onMounted, ref, watchEffect } from "vue";
import { emitter } from "../emitter.js";
import imgList from '../utils/loadImg'
defineOptions({
  name: "img-loading",
});
onMounted(()=>{
  loadImages(imgList)
})
const props = defineProps({
  fullpage: Object,
});
const progress = ref(0);
const framePro = ref(0)
const imgPro = ref(0)
watchEffect(() => {
    if (progress.proxy.$refs.fullpage) {
      fullpage.value = instance.proxy.$refs.fullpage;
    }
  });
emitter.on("updatePress", (val) => {
    framePro.value = val * 0.7 
    progress.value = Math.round(framePro.value + imgPro.value)
});
// 总任务 
function loadImages(list){ 
    const pageSize = 1 
    let pageNum = 0 ,totalNum = list.length;
    return new Promise((reslove,reject)=>{ 
        function run(){ 
            Promise.all(genetateTasks(list,pageSize,pageNum)).then(()=>{ 
                pageNum++
                const hasLength = pageSize * pageNum 
                imgPro.value = Math.round((hasLength/(list.length + 1))*30)
                progress.value = Math.round(framePro.value + imgPro.value)
                if(totalNum > hasLength){ 
                    run() 
                }else { 
                    reslove(true) 
                } }) 
           } 
           run() 
 }) }
// 子任务 
function genetateTasks(list,pageSize,pageNum){
    const promiseArr = [] 
    const start = pageNum * pageSize 
    const end = (pageNum + 1) * pageSize - 1 
    // progress.value = 
    for(let i = start;i<end;i++){ 
        const p = new Promise((reslove,reject)=>{ 
            const img = new Image() 
            img.src = list[i] 
            img.onload = img.onerror = reslove 
         }) 
         promiseArr.push(p) 
    } 
    return promiseArr 
}
// emitter.on("closePreloadinng");
// emitter.emit("preBaseLoadingdone");
</script>
<style lang="scss" scoped>
@import "/src/assets/style/index.css";
.icon {
    width: 25px;
    height: 25px;
    img {
        width: 100%;
    }
}
.text{
    text-align: center;
}
.scroll-icon {
  opacity: 1;
  z-index: 10;
  position: absolute;
  bottom: 1em;
  left: 50%;
  margin-left: -0.6em;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 2em;
  width: 1.2em;
  font-size: 22px;
  color: rgba(255, 255, 255, 0.5);
  border-radius: 2em;
  border: solid 2px;
  transition: 0.4s;
  &::before {
    content: "";
    display: inline-block;
    width: 5px;
    height: 5px;
    background-color: #fff;
    border-radius: 50%;
    animation: scroll-icon 2.2s infinite cubic-bezier(0.75, 0.05, 0.36, 1);
  }
}

@keyframes scroll-icon {
  0% {
    transform: translateY(-12px);
    opacity: 0;
  }

  30%,
  70% {
    opacity: 1;
  }
  100% {
    transform: translateY(12px);
    opacity: 0;
  }
}
</style>
