<template>
  <view class="page">
    <!-- 顶部导航 -->
    <view class="nav-bar">
      <view class="nav-btn" @click="goBack">
        <text class="nav-icon">←</text>
      </view>
      <text class="nav-title">专辑</text>
      <view class="nav-btn" />
    </view>

    <!-- 骨架屏加载状态 -->
    <view v-if="loading && albumList.length === 0" class="skeleton-grid">
      <view v-for="i in 6" :key="i" class="skeleton-card">
        <view class="skeleton-cover"></view>
        <view class="skeleton-info">
          <view class="skeleton-title"></view>
          <view class="skeleton-meta"></view>
        </view>
      </view>
    </view>

    <!-- 专辑列表 - 使用 v-show 避免 scroll-top 初始化的 DOM 未就绪问题 -->
    <scroll-view scroll-y class="album-list" @scrolltolower="loadMore" :style="{ display: (loading && albumList.length === 0) ? 'none' : 'block' }" :scroll-top="scrollTop">
      <view v-if="albumList.length === 0 && !loading" class="empty">
        <text class="empty-icon">📚</text>
        <text class="empty-text">暂无专辑</text>
        <text class="empty-hint">创建你的第一个专辑吧</text>
      </view>

      <view v-else class="album-grid">
        <view
          v-for="item in albumList"
          :key="item.id"
          class="album-card"
          @click="goToAlbumDetail(item.id)"
        >
          <view class="album-cover" :style="{ background: getCoverGradient(item) }">
            <text class="cover-letter">{{ getTitleLetter(item.name) }}</text>
            <text class="cover-title-small">{{ item.name }}</text>
            <view class="album-count">{{ item.subscriberCount || 0 }}人订阅</view>
          </view>
          <view class="album-info">
            <text class="album-title">{{ item.name }}</text>
            <text class="album-desc">{{ item.description || '暂无描述' }}</text>
          </view>
        </view>
      </view>

      <view v-if="loading" class="loading">
        <text>加载中...</text>
      </view>

      <view v-if="!hasMore && albumList.length > 0" class="no-more">
        <text>没有更多了</text>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { get } from '../../utils/request';
import { getBookGradient, getTitleLetter } from '../../composables/useCoverStyle';

// scroll-top 用于避免 scrollTop 错误
const scrollTop = ref(0);

interface Album {
  id: number;
  name: string;
  description?: string;
  coverUrl?: string;
  subscriberCount: number;
}

const albumList = ref<Album[]>([]);
const page = ref(1);
const pageSize = 20;
const total = ref(0);
const loading = ref(false);
const hasMore = ref(true);

// 获取封面渐变色
function getCoverGradient(item: Album): string {
  return getBookGradient(item.id);
}

// 返回
function goBack() {
  const pages = getCurrentPages();
  if (pages.length > 1) {
    uni.navigateBack();
  } else {
    uni.switchTab({ url: '/pages/index/index' });
  }
}

// 跳转专辑详情
function goToAlbumDetail(albumId: number) {
  uni.navigateTo({ url: `/pages/album/index?id=${albumId}` });
}

// 获取专辑列表（使用书籍 API）
async function fetchAlbumList(isLoadMore = false) {
  if (loading.value) return;
  if (!isLoadMore) {
    page.value = 1;
  }

  loading.value = true;
  try {
    // 调用专辑列表 API
    const result = await get<{ albums: any[] }>('/book-generator/albums');
    const books = result.albums || [];

    // 转换为 Album 格式
    albumList.value = books.map((book: any) => ({
      id: book.id,
      name: book.title,
      description: book.description,
      subscriberCount: book.completedChapters || 0,
    }));
    total.value = books.length;
    hasMore.value = false;
  } catch (error) {
    console.error('获取专辑列表失败:', error);
  } finally {
    loading.value = false;
  }
}

// 加载更多
function loadMore() {
  if (hasMore.value && !loading.value) {
    page.value++;
    fetchAlbumList(true);
  }
}

onMounted(async () => {
  await fetchAlbumList();
});
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: #f5f5f5;
}

.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 44rpx 32rpx 24rpx;
  background: #ffffff;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-btn {
  width: 64rpx;
  height: 64rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-icon {
  font-size: 36rpx;
  color: #333;
}

.nav-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.album-list {
  height: calc(100vh - 100rpx);
  padding: 24rpx;
}

/* 骨架屏 */
.skeleton-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  padding: 24rpx;
}

.skeleton-card {
  width: calc(50% - 10rpx);
  background: #ffffff;
  border-radius: 16rpx;
  overflow: hidden;
}

.skeleton-cover {
  width: 100%;
  height: 200rpx;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

.skeleton-info {
  padding: 20rpx;
}

.skeleton-title {
  height: 28rpx;
  width: 80%;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 8rpx;
  margin-bottom: 12rpx;
}

.skeleton-meta {
  height: 22rpx;
  width: 50%;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 8rpx;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* 专辑列表 */
.album-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
}

.album-card {
  width: calc(50% - 10rpx);
  background: #ffffff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
}

.album-cover {
  position: relative;
  width: 100%;
  height: 200rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cover-icon {
  font-size: 80rpx;
  opacity: 0.6;
}

/* 文字海报风格封面 */
.cover-letter {
  position: absolute;
  top: -16rpx;
  left: 12rpx;
  font-size: 130rpx;
  font-weight: 900;
  color: rgba(255, 255, 255, 0.12);
  line-height: 1;
  pointer-events: none;
}

.cover-title-small {
  position: absolute;
  bottom: 40rpx;
  left: 16rpx;
  right: 16rpx;
  font-size: 22rpx;
  color: rgba(255, 255, 255, 0.85);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.album-count {
  position: absolute;
  bottom: 12rpx;
  right: 12rpx;
  background: rgba(0, 0, 0, 0.6);
  color: #ffffff;
  font-size: 22rpx;
  padding: 4rpx 12rpx;
  border-radius: 8rpx;
}

.album-info {
  padding: 20rpx;
}

.album-title {
  font-size: 28rpx;
  font-weight: 500;
  color: #1f2937;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 8rpx;
}

.album-desc {
  font-size: 22rpx;
  color: #9ca3af;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 空状态 */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
}

.empty-icon {
  font-size: 120rpx;
  margin-bottom: 24rpx;
}

.empty-text {
  font-size: 32rpx;
  color: #6b7280;
  margin-bottom: 12rpx;
}

.empty-hint {
  font-size: 26rpx;
  color: #9ca3af;
}

/* 加载状态 */
.loading,
.no-more {
  text-align: center;
  padding: 32rpx;
}

.loading text,
.no-more text {
  font-size: 24rpx;
  color: #9ca3af;
}
</style>
