<!-- 只展示修改的部分 -->
<template>
  <main class="loading-container" @click="showExploreButton && goToIndexPage()">
    <div class="car-loading" v-if="showProgressBar">
      <div class="car-container">
        <div class="car"></div>
      </div>

      <div class="progress-container">
        <img src="@/assets/loading/process.png" alt="进度条背景" class="progress-bg" />
        <div class="custom-progress-bar">
          <div class="progress-fill" :style="{ width: `${progress}%` }"></div>
        </div>
        <div class="progress-text">{{ Math.floor(progress) }}%</div>
      </div>
    </div>
    <!-- Logo加载页面 -->
    <div v-if="showLogo" class="logo-container">
      <div class="logo"></div>
      <div v-if="showExploreButton" class="explore-button">
        <span class="explore-line-left"></span>
        开始探索
        <span class="explore-line-right"></span>
      </div>
    </div>
  </main>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import wx from 'weixin-js-sdk'
import { useStore } from 'vuex'
const store = useStore()

const router = useRouter()
const progress = ref(0)
const showProgressBar = ref(true)
const showLogo = ref(false)
const showExploreButton = ref(false)
// 点击"开始探索"按钮，跳转到首页
const goToIndexPage = () => {
  store.dispatch('setIsFirstOpen', false)
  // 小程序
  wx.miniProgram.navigateTo({
    url: '../collection/index'
  })
  // wx.miniProgram.postMessage({
  //   data: {
  //     isFirstOpen: false
  //   }
  // })
  let baseUrl = window.location.href
  // const newUrl = baseUrl.split('?')[0] + '?isFrom=weixin&isFirstOpen=false'
  // window.location.href = newUrl
  // console.log(newUrl, 777)
}
onMounted(() => {
  // 3秒内完成进度
  const duration = 3000
  const interval = 30 // 每30ms更新一次进度
  const steps = duration / interval
  const increment = 100 / steps

  // 进度条更新
  const progressTimer = setInterval(() => {
    progress.value += increment

    if (progress.value >= 100) {
      clearInterval(progressTimer)
      progress.value = 100

      // 进度条加载完成后，隐藏进度条，显示logo
      setTimeout(() => {
        showProgressBar.value = false
        showLogo.value = true

        // logo加载2秒后，显示"开始探索"按钮
        setTimeout(() => {
          showExploreButton.value = true
        }, 2000)
      }, 500)
    }
  }, interval)
})
</script>

<style scoped lang="scss">
.loading-container {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: url('@/assets/loading/loading-bg.png') no-repeat center center;
  background-size: cover;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
}

.car-loading {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;

  .car-container {
    width: 100%;
    height: 100px;
    position: relative;
    display: flex;
    justify-content: center;
  }

  .car {
    width: 150px;
    height: 132px;
    background: url('@/assets/loading/carLoading.png') 0 0;
    position: relative;
    bottom: 0;
    background-size: 12500% 100%;
    /* 总宽度 */
    animation: car 3s steps(10) infinite;
    /* 使用steps(125)表示有125帧 */
  }

  @keyframes car {
    from {
      background-position: 0 0;
    }

    to {
      background-position: -12500% 0;
      /* 总宽度，与background-size一致 */
    }
  }

  .progress-container {
    width: 80%;
    max-width: 500px;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .progress-bg {
    width: 100%;
    height: auto;
  }

  .custom-progress-bar {
    position: absolute;
    top: 22%;
    left: 22%;
    transform: translateY(-50%);
    width: 78%;
    height: 16px;
    padding: 0 10px;
    box-sizing: border-box;
    background-color: transparent;
  }

  .progress-fill {
    height: 100%;
    border-radius: 8px;
    background: linear-gradient(to right,
        rgba(230, 162, 60, 0) 0%,
        rgba(230, 162, 60, 0) 20%,
        rgba(230, 162, 60, 0.5) 60%,
        rgba(230, 162, 60, 1) 100%);
    transition: width 0.3s ease;
  }

  .progress-text {
    margin-top: 10px;
    color: #fff;
    font-size: 18px;
    font-weight: bold;
  }
}

/* 新增的Logo页面样式 */
.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .logo {
    width: 300px;
    height: 300px;
    background: url('@/assets/loading/logo.png') 0 0;
    position: relative;
    bottom: 0;
    background-size: 2400% 100%;
    animation: sprite 3s steps(23) 1 forwards;
  }

  @keyframes sprite {
    from {
      background-position: 0 0;
    }

    to {
      background-position: -2300% 0;
    }
  }

  .explore-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #ECD6A4;
    /* 浅金色 */
    font-size: 16px;
    cursor: pointer;
    background-color: transparent;
    /* 透明背景 */
    margin-top: 100px;
    /* 与logo保持一定距离 */
    animation: buttonFadeIn 1s ease-in-out;
    transition: all 0.3s ease;
    position: relative;
    border: none;
    /* 移除边框 */
  }

  .explore-line-left {
    width: 125px;
    height: 1px;
    margin-right: 10px;
    background: linear-gradient(90deg, rgba(236, 214, 164, 0) 0%, #ECD6A4 100%);
    border-radius: 0px 0px 0px 0px;
    opacity: 0.5;
  }

  .explore-line-right {
    width: 125px;
    height: 1px;
    background: linear-gradient(90deg, #ECD6A4 0%, rgba(236, 214, 164, 0) 100%);
    border-radius: 0px 0px 0px 0px;
    opacity: 0.5;
    margin-left: 10px;
  }


  @keyframes buttonFadeIn {
    from {
      opacity: 0;
      transform: translateY(20px);
    }

    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
}
</style>
