<template>
  <component :is="currentComponent" v-if="currentComponent !== 'newIndex'" />
  <div class="dashboard-container" v-else>
    <div class="welcome-section">
      <div class="welcome-content">
        <div class="logo-animation">
          <div class="pulse-ring"></div>
          <div class="pulse-ring delay-1"></div>
          <div class="pulse-ring delay-2"></div>
          <div class="logo">
            <i class="fas fa-home"></i>
          </div>
        </div>
        <h1 class="welcome-title">欢迎使用系统</h1>
        <p class="welcome-subtitle">正在为您加载个性化工作台</p>
        <div class="loading-bar">
          <div class="progress"></div>
        </div>
        <div class="loading-text">加载中... <span class="percentage">0%</span></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { mapState } from 'pinia'
import useUserStore from '@/store/modules/user'
import { getRouteNameByPostType } from '@/utils/routeMap'
import { getCurrentUserHomePage } from '@/api/system/postHomePage'
import Index0 from './index0.vue'
import Index1 from './index1.vue'
import Index2 from './index2.vue'
import Index3 from './index3.vue'
import Index4 from './index4.vue'
import Index5 from './index5.vue'
import Index6 from './index6.vue'


export default {
  name: 'Index',
  components: {
    Index0,
    Index1,
    Index2,
    Index3,
    Index4,
    Index5,
    Index6
  },
  setup() {
    const homePageRouteName = ref('')

    // 根据路由名称确定要加载的组件
    const currentComponent = computed(() => {
      // 直接使用homePageRouteName值作为组件名称，提高代码简洁性
      // 由于组件已注册，Vue会自动解析组件名称
      return homePageRouteName.value || 'newIndex'
    })

    // 获取首页路由名称
    const fetchHomePageRouteName = async () => {
      try {
        // 调用后端接口获取首页配置
        const response = await getCurrentUserHomePage()
        if (response.code === 200 && response.data) {
          // 使用返回数据中的homePageType值
          const homePageType = response.data.homePageType
          homePageRouteName.value = await getRouteNameByPostType(homePageType)
        } else {
          // 接口调用成功但无数据时使用默认首页
          console.warn('未获取到首页配置数据，使用默认首页')
          homePageRouteName.value = 'newIndex'
        }
      } catch (error) {
        console.error('获取首页配置失败:', error)
        // 接口调用失败时使用默认首页
        homePageRouteName.value = 'newIndex'
      }
    }

    return {
      homePageRouteName,
      currentComponent,
      fetchHomePageRouteName
    }
  },
  computed: {
    ...mapState(useUserStore, ['postType'])
  },
  async mounted() {
    // 组件挂载时获取首页路由名称
    if (this.fetchHomePageRouteName) {
      this.fetchHomePageRouteName()
    }
    // 如果是默认首页，启动加载动画
    if (!this.homePageRouteName || this.homePageRouteName === 'newIndex') {
      this.animateProgress();
    }
  },
  watch: {
    // 监听postType变化
    postType: {
      handler: async function(newVal) {
        if (this.fetchHomePageRouteName) {
          this.fetchHomePageRouteName()
        }
      },
      immediate: true
    }
  },
  methods: {
    animateProgress() {
      this.$nextTick(() => {
        const progress = document.querySelector('.progress');
        const percentage = document.querySelector('.percentage');
        if (progress && percentage) {
          let width = 0;
          const interval = setInterval(() => {
            if (width >= 100) {
              clearInterval(interval);
            } else {
              width++;
              progress.style.width = width + '%';
              percentage.textContent = width + '%';
            }
          }, 50);
        }
      });
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
  min-height: calc(100vh - 84px);
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;
}

.welcome-section {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 50vh;
  position: relative;
}

.welcome-content {
  text-align: center;
  z-index: 2;
  background: rgba(255, 255, 255, 0.9);
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  animation: slideUp 1s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.logo-animation {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 30px;
}

.pulse-ring {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #409eff;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.pulse-ring.delay-1 {
  animation-delay: 0.5s;
}

.pulse-ring.delay-2 {
  animation-delay: 1s;
}

@keyframes pulse {
  0% {
    transform: scale(0.8);
    opacity: 1;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}

.logo {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 50px;
  color: #409eff;
  background: white;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
}

.welcome-title {
  font-size: 2.5rem;
  color: #303133;
  margin-bottom: 15px;
  font-weight: 600;
}

.welcome-subtitle {
  font-size: 1.2rem;
  color: #606266;
  margin-bottom: 30px;
}

.loading-bar {
  width: 300px;
  height: 10px;
  background: #ebeef5;
  border-radius: 5px;
  margin: 30px auto;
  overflow: hidden;
}

.progress {
  height: 100%;
  background: linear-gradient(90deg, #409eff, #64ffda);
  border-radius: 5px;
  width: 0%;
  transition: width 0.3s ease;
}

.loading-text {
  font-size: 1rem;
  color: #909399;
}

.features-preview {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 25px;
  margin-top: 50px;
  padding: 0 20px;
}

.feature-card {
  background: white;
  border-radius: 15px;
  padding: 30px 20px;
  text-align: center;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  animation: fadeIn 0.8s ease-out;
}

.feature-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.feature-card:nth-child(1) {
  animation-delay: 0.2s;
}

.feature-card:nth-child(2) {
  animation-delay: 0.4s;
}

.feature-card:nth-child(3) {
  animation-delay: 0.6s;
}

.feature-card:nth-child(4) {
  animation-delay: 0.8s;
}

.feature-icon {
  font-size: 2.5rem;
  margin-bottom: 20px;
  color: #409eff;
}

.feature-card h3 {
  font-size: 1.3rem;
  color: #303133;
  margin-bottom: 15px;
}

.feature-card p {
  font-size: 0.95rem;
  color: #909399;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dashboard-container {
    padding: 15px;
  }
  
  .welcome-content {
    padding: 30px 20px;
    width: 90%;
  }
  
  .welcome-title {
    font-size: 2rem;
  }
  
  .welcome-subtitle {
    font-size: 1rem;
  }
  
  .logo {
    width: 60px;
    height: 60px;
    font-size: 35px;
  }
  
  .loading-bar {
    width: 100%;
    max-width: 250px;
  }
  
  .features-preview {
    grid-template-columns: 1fr;
    gap: 20px;
  }
}
</style>