<template>
  <div class="home-page">
    <div class="banner-section">
      <el-carousel height="280px" :interval="5000" arrow="hover" trigger="click">
        <el-carousel-item v-for="(banner, index) in banners" :key="index">
          <div class="banner-slide" :style="{ background: banner.bg_color || banner.bg }">
            <img v-if="banner.image" :src="getImageUrl(banner.image)" class="banner-bg-image" />
            <div class="banner-content">
              <h2>{{ banner.title }}</h2>
              <p>{{ banner.desc }}</p>
              <button class="banner-btn" @click="$router.push(banner.link)">{{ banner.button || banner.btn }}</button>
            </div>
            <div class="banner-icon" v-if="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.is_active ? 'active' : ''">
              {{ l.is_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-list">
        <div v-for="cat in categories" :key="cat.id" class="category-item" @click="goCategory(cat.id)">
          <div class="category-icon">
            <el-icon size="18"><Folder /></el-icon>
          </div>
          <div class="category-info">
            <h3>{{ cat.name }}</h3>
            <p>{{ cat.description || '精选好物' }}</p>
          </div>
          <el-icon class="category-arrow"><ArrowRight /></el-icon>
        </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, ArrowRight } from '@element-plus/icons-vue'
import { categoryApi, lotteryApi, articleApi, bannerApi } from '../../api'
import { getImageUrl } from '../../utils/image'

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

const defaultBanners = [
  { 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, banRes]: any[] = await Promise.all([
      categoryApi.list(),
      lotteryApi.list(),
      articleApi.list({ page: 1, page_size: 5 }),
      bannerApi.list(),
    ])
    categories.value = (catRes.data || []).slice(0, 6)
    lotteries.value = (lotRes.data || []).slice(0, 3)
    articles.value = (artRes.data || []).slice(0, 5)
    
    if (banRes.data && banRes.data.length > 0) {
      banners.value = banRes.data
    } else {
      banners.value = defaultBanners as any
    }
  } catch (e) {
    console.log('加载失败:', e)
    banners.value = defaultBanners as any
  }
})
</script>

<style scoped lang="scss">
.home-page {
  padding: 0;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
  box-sizing: border-box;
}

.banner-section {
  margin-bottom: 24px;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  position: relative;
}

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

.banner-bg-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 0;
}

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

.banner-content h2 {
  font-size: 32px;
  font-weight: 700;
  color: #fff;
  margin-bottom: 12px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.banner-content p {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.95);
  margin-bottom: 24px;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.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;
  width: 100%;
  box-sizing: border-box;
}

.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;
  width: 100%;
  box-sizing: border-box;
}

.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-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.category-item {
  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);
}

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

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

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

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

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

.category-arrow {
  color: rgba(255, 255, 255, 0.25);
  flex-shrink: 0;
}

.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) {
  position: relative !important;
}

:deep(.el-carousel__container) {
  position: relative !important;
  height: 280px;
}

:deep(.el-carousel__arrow) {
  top: 50% !important;
  transform: translateY(-50%) !important;
  background: rgba(0, 0, 0, 0.3) !important;
  color: #fff !important;
  border: none !important;
  width: 36px !important;
  height: 36px !important;
  font-size: 16px !important;
  margin-top: 0 !important;
}

:deep(.el-carousel__arrow:hover) {
  background: rgba(0, 0, 0, 0.5) !important;
}

:deep(.el-carousel__arrow--left) {
  left: 16px !important;
}

:deep(.el-carousel__arrow--right) {
  right: 16px !important;
}

:deep(.el-carousel__indicators) {
  position: absolute !important;
  bottom: 16px !important;
  left: 50% !important;
  transform: translateX(-50%) !important;
  z-index: 10 !important;
  margin: 0 !important;
  padding: 0 !important;
}

:deep(.el-carousel__indicator--horizontal .el-carousel__button) {
  background: rgba(255, 255, 255, 0.3);
  width: 24px;
  height: 4px;
  border-radius: 2px;
}

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

@media (max-width: 768px) {
  .home-page {
    padding: 0;
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .banner-section {
    margin-bottom: 16px;
    width: 100%;
    max-width: 100vw;
    position: relative;
  }

  .banner-slide {
    height: 200px;
    padding: 0 20px;
    border-radius: 0;
    width: 100%;
    max-width: 100vw;
    box-sizing: border-box;
  }

  .banner-content h2 {
    font-size: 20px;
    margin-bottom: 8px;
  }

  .banner-content p {
    font-size: 14px;
    margin-bottom: 16px;
  }

  .banner-btn {
    padding: 8px 20px;
    font-size: 13px;
  }

  .banner-icon {
    display: none;
  }

  .quick-actions {
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 20px;
    padding: 0 12px;
  }

  .action-item {
    flex: 1 1 45%;
    padding: 12px;
  }

  .action-icon {
    width: 36px;
    height: 36px;
  }

  .action-item span {
    font-size: 12px;
  }

  .section {
    margin-bottom: 20px;
    padding: 0 12px;
    width: 100% !important;
    max-width: 100vw !important;
    box-sizing: border-box !important;
    overflow-x: hidden !important;
  }

  .section-head {
    margin-bottom: 12px;
  }

  .section-head h2 {
    font-size: 16px;
  }

  .section-more {
    font-size: 12px;
  }

  .category-list {
    gap: 8px;
  }

  .category-item {
    padding: 12px;
    gap: 10px;
  }

  .category-icon {
    width: 36px;
    height: 36px;
  }

  .category-info h3 {
    font-size: 13px;
  }

  .category-info p {
    font-size: 11px;
  }

  .lottery-cards {
    gap: 8px;
  }

  .lottery-card {
    padding: 12px;
    gap: 10px;
  }

  .lottery-icon {
    width: 36px;
    height: 36px;
  }

  .lottery-info h3 {
    font-size: 13px;
  }

  .lottery-info p {
    font-size: 11px;
  }

  .lottery-status {
    font-size: 11px;
    padding: 2px 8px;
  }

  .lottery-btn {
    padding: 5px 12px;
    font-size: 11px;
  }

  .article-list {
    gap: 6px;
  }

  .article-item {
    padding: 10px;
    gap: 10px;
  }

  .article-icon {
    width: 28px;
    height: 28px;
  }

  .article-info h3 {
    font-size: 13px;
  }

  .article-info p {
    font-size: 11px;
  }

  .article-date {
    font-size: 11px;
  }

  :deep(.el-carousel) {
    position: relative !important;
  }

  :deep(.el-carousel__container) {
    height: 200px !important;
  }

  :deep(.el-carousel__arrow) {
    width: 28px !important;
    height: 28px !important;
    font-size: 14px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    margin-top: 0 !important;
  }

  :deep(.el-carousel__arrow--left) {
    left: 10px !important;
  }

  :deep(.el-carousel__arrow--right) {
    right: 10px !important;
  }

  :deep(.el-carousel__indicators) {
    bottom: 12px !important;
    position: absolute !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    z-index: 10 !important;
  }

  :deep(.el-carousel__indicator--horizontal .el-carousel__button) {
    width: 20px;
    height: 3px;
  }
}
</style>
