<script lang="ts" setup>
import { NCarousel, NImage } from 'naive-ui'
import { computed, onMounted, ref } from 'vue'
import beacon from '@/assets/imgs/user/event-card-bg-icon.webp'
import CompetitionLayout from '@/components/custom/user/CompetitionLayout.vue'
import { PublishStatus } from '@/enum/business'
import { useRouterPush } from '@/hooks/common/router'
import { fetchGetActivityList } from '@/service/api/competition'
import { useActivityInfoStore } from '@/store/modules/activityinfo'

const { routerPushByKey } = useRouterPush()
const activityInfoStore = useActivityInfoStore()

interface ActivityEvent extends Api.Competition.ActivityDetail {
  id: number
  title: string
  subTitle?: string
  icon: string
  status: string
}

const events = ref<ActivityEvent[]>([])
const loading = ref(false)

async function getEvents() {
  loading.value = true
  try {
    const { data, error } = await fetchGetActivityList()
    if (error) {
      window.$message?.error(error.message)
      return
    }

    const list = data?.data || []
    if (list && Array.isArray(list)) {
      events.value = list
        .filter((item: any) => item.PublishStatus === PublishStatus.Published || item.PublishStatus === PublishStatus.Processing)
        .map((item: any) => ({
          ...item,
          id: item.Id,
          title: item.Name,
          subTitle: item.Description,
          icon: 'activity',
          status: 'active',
        }))
    }
  }
  finally {
    loading.value = false
  }
}

// Group events into chunks of 4 for the carousel
const eventChunks = computed(() => {
  const result = []
  for (let i = 0; i < events.value.length; i += 4) {
    result.push(events.value.slice(i, i + 4))
  }
  return result
})

function handleEnter(item: ActivityEvent) {
  activityInfoStore.setActivityInfo(item)
  routerPushByKey('user_groups', { query: { activityId: item.Id } })
}

function handleBack() {
  // console.log('back')
}

function handleNext() {
  // 首页的下一步操作，可以根据需求实现
  routerPushByKey('user_groups')
}

const showContent = ref(false)
onMounted(() => {
  setTimeout(() => {
    showContent.value = true
    getEvents()
  }, 100)
})
</script>

<template>
  <CompetitionLayout
    :show-back="false" :show-next="false" title="赛事总览" action-position="top" back-btn-text="返回"
    btn-theme="light" :title-bg-type="2" @back="handleBack"
    @next="handleNext"
  >
    <!-- Cards Section -->
    <div class="cards-container">
      <div v-if="loading" class="h-full w-full flex-center">
        <div class="i-svg-spinners-90-ring-with-bg text-4xl text-primary" />
      </div>
      <NCarousel v-else-if="events.length > 0" show-arrow :show-dots="false" draggable dot-placement="bottom" class="group h-full">
        <template #arrow="{ prev, next }">
          <div class="custom-arrow left" @click="prev">
            <icon-ic-round-arrow-back class="text-4xl text-icon text-white" />
          </div>
          <div class="custom-arrow right" @click="next">
            <icon-ic-round-arrow-forward class="text-4xl text-icon text-white" />
          </div>
        </template>
        <div v-for="(chunk, chunkIndex) in eventChunks" :key="chunkIndex" class="cards-wrapper">
          <TransitionGroup name="list-anim">
            <div
              v-for="(item, index) in chunk" v-show="showContent" :key="item.id" class="event-card"
              :style="{ '--delay': `${index * 0.1}s` }" @click="handleEnter(item)"
            >
              <div class="card-top-bar" />
              <div class="card-body">
                <div class="icon-wrapper">
                  <NImage
                    :src="beacon"
                    preview-disabled
                    object-fit="contain"
                  />
                </div>
                <div class="text-content">
                  <h3 class="event-title">
                    {{ item.title }}
                  </h3>
                  <p v-if="item.subTitle" class="event-subtitle">
                    {{ item.subTitle }}
                  </p>
                </div>
              </div>
              <div class="tassel" />
            </div>
          </TransitionGroup>
        </div>
      </NCarousel>
      <div v-else class="h-full w-full flex-center text-gray-500">
        暂无赛事数据
      </div>
    </div>
  </CompetitionLayout>
</template>

<style lang="scss" scoped>
@use 'sass:color';

.cards-container {
  width: 100%;
  padding: 0 40px;
}

.cards-wrapper {
  display: flex;
  justify-content: center;
  gap: 40px;
  flex-wrap: wrap;
}

.event-card {
  width: 340px;
  height: 480px;
  background: url('@/assets/imgs/user/event-card-bg.webp') no-repeat center center;
  background-size: 100% 100%;
  border-radius: 8px 8px 40px 40px;
  position: relative;
  cursor: pointer;
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;

  .card-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 40px 20px;
    text-align: center;
  }

  .icon-wrapper {
    width: 170px;
    height: 161px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 30px 0;
  }

  .text-content {
    .event-title {
      font-size: 1.1rem;
      font-weight: bold;
      color: $text-color;
      margin-bottom: 8px;
      line-height: 1.4;
    }

    .event-subtitle {
      font-size: 1.1rem;
      color: color.adjust($text-color, $lightness: 15%);
    }
  }

  &:hover {
    transform: translateY(-10px);

    .icon-wrapper {
      transform: scale(1.1) rotate(2deg);
      transition: transform 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    }
  }
}

// Transitions
.fade-slide-down-enter-active,
.fade-slide-down-leave-active {
  transition: all 0.8s ease;
}

.fade-slide-down-enter-from,
.fade-slide-down-leave-to {
  opacity: 0;
  transform: translateY(-30px);
}

.list-anim-enter-active,
.list-anim-leave-active {
  transition: all 0.6s cubic-bezier(0.5, 0, 0.25, 1);
  transition-delay: var(--delay);
}

.list-anim-enter-from,
.list-anim-leave-to {
  opacity: 0;
  transform: translateY(50px);
}

// Responsive
@media (max-width: 1024px) {
  .cards-wrapper {
    gap: 20px;
  }

  .event-card {
    width: 220px;
    height: 320px;
  }
}

@media (max-width: 768px) {
  .cards-wrapper {
    flex-direction: column;
    align-items: center;
  }

  .event-card {
    width: 90%;
    height: 120px;
    flex-direction: row;
    border-radius: 8px;

    .card-top-bar {
      width: 16px;
      height: 110%;
      top: -5%;
      left: -8px;
    }

    .tassel {
      display: none;
    }

    .card-body {
      flex-direction: row;
      text-align: left;
      padding: 10px 20px;
    }

    .icon-wrapper {
      width: 60px;
      height: 60px;
      margin-bottom: 0;
      margin-right: 20px;

      .event-icon {
        font-size: 32px;
      }
    }
  }
}

.custom-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 48px;
  height: 48px;
  background-color: rgba($primary-color, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: all 0.3s ease;
  opacity: 0; // 默认隐藏

  &:hover {
    background-color: $primary-color;
    transform: translateY(-50%) scale(1.1);
  }

  &.left {
    left: 10px;
  }

  &.right {
    right: 10px;
  }
}

// 鼠标悬停在轮播图区域时显示箭头
:deep(.n-carousel:hover) .custom-arrow,
.group:hover .custom-arrow {
  opacity: 1;
}
</style>
