<template>
  <div class="home-page">
    <div class="banner-section">
      <el-carousel height="280px" :interval="5000" arrow="hover" indicator-position="outside">
        <el-carousel-item v-for="(banner, index) in banners" :key="index">
          <div class="banner-slide" :style="{ background: banner.bg }">
            <div class="banner-content">
              <h2>{{ banner.title }}</h2>
              <p>{{ banner.desc }}</p>
              <button class="banner-btn" @click="$router.push(banner.link)">{{ banner.btn }}</button>
            </div>
            <div class="banner-icon">
              <el-icon :size="80"><component :is="banner.icon" /></el-icon>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="quick-actions">
      <div class="action-item" @click="$router.push('/products')">
        <div class="action-icon" style="background: rgba(78, 110, 242, 0.15); color: #4e6ef2;">
          <el-icon size="22"><Goods /></el-icon>
        </div>
        <span>商品</span>
      </div>
      <div class="action-item" @click="$router.push('/lotteries')">
        <div class="action-icon" style="background: rgba(245, 158, 11, 0.15); color: #f59e0b;">
          <el-icon size="22"><Trophy /></el-icon>
        </div>
        <span>抽奖</span>
      </div>
      <div class="action-item" @click="$router.push('/articles')">
        <div class="action-icon" style="background: rgba(16, 185, 129, 0.15); color: #10b981;">
          <el-icon size="22"><Document /></el-icon>
        </div>
        <span>资讯</span>
      </div>
      <div class="action-item" @click="$router.push('/cart')">
        <div class="action-icon" style="background: rgba(124, 92, 252, 0.15); color: #7c5cfc;">
          <el-icon size="22"><ShoppingCart /></el-icon>
        </div>
        <span>购物车</span>
      </div>
    </div>

    <div class="section" v-if="lotteries.length">
      <div class="section-head">
        <h2>热门活动</h2>
      </div>
      <div class="lottery-cards">
        <div v-for="l in lotteries" :key="l.id" class="lottery-card" @click="$router.push(`/lotteries/${l.id}`)">
          <div class="lottery-icon">
            <el-icon size="20"><Trophy /></el-icon>
          </div>
          <div class="lottery-info">
            <h3>{{ l.name }}</h3>
            <p>{{ l.description }}</p>
          </div>
          <div class="lottery-right">
            <span class="lottery-status" :class="l.status === 'active' ? 'active' : ''">
              {{ l.status === 'active' ? '进行中' : '已结束' }}
            </span>
            <button class="lottery-btn">参与</button>
          </div>
        </div>
      </div>
    </div>

    <div class="section" v-if="articles.length">
      <div class="section-head">
        <h2>最新资讯</h2>
        <span class="section-more" @click="$router.push('/articles')">查看全部 →</span>
      </div>
      <div class="article-list">
        <div v-for="a in articles" :key="a.id" class="article-item" @click="$router.push(`/articles/${a.id}`)">
          <div class="article-icon">
            <el-icon size="18"><Document /></el-icon>
          </div>
          <div class="article-info">
            <h3>{{ a.title }}</h3>
            <p>{{ a.summary || a.content?.slice(0, 60) }}...</p>
          </div>
          <span class="article-date">{{ formatDate(a.created_at) }}</span>
        </div>
      </div>
    </div>

    <div class="section" v-if="categories.length">
      <div class="section-head">
        <h2>热门分类</h2>
        <span class="section-more" @click="$router.push('/products')">查看全部 →</span>
      </div>
      <div class="category-scroll">
        <div v-for="cat in categories" :key="cat.id" class="category-card" @click="goCategory(cat.id)">
          <div class="category-icon">
            <el-icon size="24"><Folder /></el-icon>
          </div>
          <div class="category-name">{{ cat.name }}</div>
          <div class="category-count">{{ cat.product_count || 0 }} 件</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, shallowRef } from 'vue'
import { useRouter } from 'vue-router'
import { Goods, Trophy, Document, ShoppingCart, Folder, Present, Star, Timer } from '@element-plus/icons-vue'
import { categoryApi, lotteryApi, articleApi } from '../../api'

const router = useRouter()
const categories = ref<any[]>([])
const lotteries = ref<any[]>([])
const articles = ref<any[]>([])

const banners = [
  { title: '新品上市', desc: '精选优质商品，限时特惠', btn: '立即选购', link: '/products', icon: shallowRef(Present), bg: 'linear-gradient(135deg, #4e6ef2 0%, #7c5cfc 100%)' },
  { title: '幸运抽奖', desc: '参与抽奖赢取好礼', btn: '参与活动', link: '/lotteries', icon: shallowRef(Star), bg: 'linear-gradient(135deg, #f59e0b 0%, #f97316 100%)' },
  { title: '限时秒杀', desc: '每日精选，超值优惠', btn: '查看详情', link: '/products', icon: shallowRef(Timer), bg: 'linear-gradient(135deg, #10b981 0%, #059669 100%)' },
]

function goCategory(id: number) {
  router.push({ path: '/products', query: { category_id: String(id) } })
}

function formatDate(date: string) {
  if (!date) return '-'
  const d = new Date(date)
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

onMounted(async () => {
  try {
    const [catRes, lotRes, artRes]: any[] = await Promise.all([
      categoryApi.list(),
      lotteryApi.list(),
      articleApi.list({ page: 1, page_size: 5 }),
    ])
    categories.value = (catRes.data || []).slice(0, 6)
    lotteries.value = (lotRes.data || []).slice(0, 3)
    articles.value = (artRes.data || []).slice(0, 5)
  } catch (e) {
    console.log('加载失败:', e)
  }
})
</script>

<style scoped lang="scss">
.home-page {
  max-width: 1200px;
  margin: 0 auto;
}

.banner-section {
  margin-bottom: 24px;
}

.banner-slide {
  height: 280px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 60px;
  border-radius: 12px;
  position: relative;
  overflow: hidden;
}

.banner-content {
  z-index: 1;
}

.banner-content h2 {
  font-size: 32px;
  font-weight: 700;
  color: #fff;
  margin-bottom: 12px;
}

.banner-content p {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
  margin-bottom: 24px;
}

.banner-btn {
  padding: 12px 32px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  cursor: pointer;
  transition: all 0.2s;
  backdrop-filter: blur(8px);
}

.banner-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}

.banner-icon {
  color: rgba(255, 255, 255, 0.2);
}

.quick-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 28px;
}

.action-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 16px;
  background: #2d2d44;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.action-item:hover {
  background: #35355a;
  transform: translateY(-2px);
}

.action-icon {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-item span {
  font-size: 13px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.7);
}

.section {
  margin-bottom: 28px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.section-head h2 {
  font-size: 17px;
  font-weight: 600;
  color: #fff;
}

.section-more {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.35);
  cursor: pointer;
  transition: color 0.15s;
}

.section-more:hover {
  color: #4e6ef2;
}

.category-scroll {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.category-card {
  flex-shrink: 0;
  width: 140px;
  padding: 16px;
  background: #2d2d44;
  border-radius: 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.category-card:hover {
  background: #35355a;
  transform: translateY(-2px);
}

.category-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 10px;
  border-radius: 12px;
  background: rgba(78, 110, 242, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4e6ef2;
}

.category-name {
  font-size: 14px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.85);
  margin-bottom: 4px;
}

.category-count {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.35);
}

.lottery-cards {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.lottery-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
  background: #2d2d44;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.lottery-card:hover {
  background: #35355a;
}

.lottery-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #4e6ef2, #7c5cfc);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.lottery-info {
  flex: 1;
  min-width: 0;
}

.lottery-info h3 {
  font-size: 14px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 4px;
}

.lottery-info p {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.35);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lottery-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.lottery-status {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.4);
}

.lottery-status.active {
  background: rgba(16, 185, 129, 0.12);
  color: #10b981;
}

.lottery-btn {
  padding: 6px 16px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  background: #4e6ef2;
  border: none;
  cursor: pointer;
  transition: background 0.15s;
}

.lottery-btn:hover {
  background: #5a7af5;
}

.article-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.article-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: #2d2d44;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  border: 1px solid rgba(255, 255, 255, 0.04);
}

.article-item:hover {
  background: #35355a;
}

.article-icon {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: rgba(16, 185, 129, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #10b981;
  flex-shrink: 0;
}

.article-info {
  flex: 1;
  min-width: 0;
}

.article-info h3 {
  font-size: 14px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.article-info p {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.35);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.article-date {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.25);
  flex-shrink: 0;
}

:deep(.el-carousel__indicators--outside) {
  margin-top: 12px;
}

:deep(.el-carousel__indicator--horizontal .el-carousel__button) {
  background: rgba(255, 255, 255, 0.2);
}

:deep(.el-carousel__indicator--horizontal.is-active .el-carousel__button) {
  background: #4e6ef2;
}

@media (max-width: 768px) {
  .banner-slide {
    height: 200px;
    padding: 0 24px;
  }

  .banner-content h2 {
    font-size: 24px;
  }

  .banner-icon {
    display: none;
  }

  .quick-actions {
    flex-wrap: wrap;
  }

  .action-item {
    flex: 1 1 45%;
  }
}
</style>
