<template>
  <view class="page">
    <!-- 搜索栏 -->
    <view class="search-bar" @click="goToSearch">
      <text class="search-icon">🔍</text>
      <text class="search-placeholder">搜索专辑...</text>
    </view>

    <!-- 骨架屏加载状态 -->
    <scroll-view scroll-y class="album-list" @scrolltolower="loadMore" v-if="loading && albumList.length === 0">
      <view 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>
    </scroll-view>

    <!-- 专辑列表 -->
    <scroll-view scroll-y class="album-list" @scrolltolower="loadMore" v-else>
      <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)"
        >
          <view class="album-cover" :style="{ background: getCoverGradient(item) }">
            <text class="cover-icon">🎵</text>
            <view class="album-count">{{ item.audioCount || 0 }}个音频</view>
          </view>
          <view class="album-info">
            <text class="album-title">{{ item.name }}</text>
            <text class="album-desc">{{ item.description || '暂无描述' }}</text>
            <text class="album-meta">{{ item.subscriberCount || 0 }}人订阅</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 class="bottom-bar">
      <button class="action-btn" @click="goToAlbums">
        <text class="btn-icon">📚</text>
        <text class="btn-text">管理专辑</text>
      </button>
      <button class="generate-btn" @click="handleGenerate">
        <text class="btn-icon">+</text>
        <text class="btn-text">新建生成</text>
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import { get } from '../../utils/request';

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

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

// 封面颜色映射
const coverColors = [
  ['#4f46e5', '#818cf8'],
  ['#ef4444', '#f87171'],
  ['#f97316', '#fb923c'],
  ['#22c55e', '#4ade80'],
  ['#3b82f6', '#60a5fa'],
  ['#a855f7', '#c084fc'],
  ['#ec4899', '#f472b6'],
  ['#14b8a6', '#2dd4bf'],
];

// 获取封面渐变色
function getCoverGradient(item: Album): string {
  const colors = coverColors[item.id % coverColors.length] || coverColors[0];
  return `linear-gradient(135deg, ${colors[0]} 0%, ${colors[1]} 100%)`;
}

// 获取专辑列表
async function fetchAlbumList(isLoadMore = false) {
  if (loading.value) return;
  if (!isLoadMore) {
    page.value = 1;
  }

  loading.value = true;
  try {
    const result = await get<{ list: Album[]; total: number; totalPages: number }>('/albums', {
      page: page.value,
      pageSize,
    });

    if (isLoadMore) {
      albumList.value.push(...result.list);
    } else {
      albumList.value = result.list;
    }
    total.value = result.total;
    hasMore.value = page.value < result.totalPages;
  } catch (error) {
    console.error('获取专辑列表失败:', error);
  } finally {
    loading.value = false;
  }
}

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

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

// 跳转专辑管理页
function goToAlbums() {
  uni.navigateTo({ url: '/pages/albums/index' });
}

// 跳转搜索页
function goToSearch() {
  uni.navigateTo({ url: '/pages/search/index' });
}

// 生成新音频（跳转到生成页面）
function handleGenerate() {
  uni.switchTab({ url: '/pages/create/index' });
}

// 初始化
onMounted(async () => {
  await fetchAlbumList();
});

// 页面显示时刷新
onShow(() => {
  fetchAlbumList();
});
</script>

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

.search-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  padding-top: env(safe-area-inset-top);
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  display: flex;
  align-items: center;
  padding-left: 24rpx;
  padding-right: 24rpx;
  z-index: 100;
}

.search-icon {
  font-size: 28rpx;
  margin-right: 12rpx;
}

.search-placeholder {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.8);
}

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

.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;
}

.album-grid {
  display: flex;
  flex-wrap: wrap;
  padding: 24rpx;
  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;
}

.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: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 8rpx;
}

.album-meta {
  font-size: 22rpx;
  color: #9ca3af;
}

.loading,
.no-more {
  text-align: center;
  padding: 32rpx;
}

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

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

.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; }
}

/* 底部操作栏 */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  gap: 20rpx;
  padding: 20rpx 32rpx;
  padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  background: #ffffff;
  box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.04);
}

.action-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 88rpx;
  background: #f5f5f5;
  border-radius: 44rpx;
  border: none;
}

.action-btn::after {
  border: none;
}

.generate-btn {
  flex: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 88rpx;
  background: linear-gradient(135deg, #4f46e5 0%, #818cf8 100%);
  border-radius: 44rpx;
  border: none;
}

.generate-btn::after {
  border: none;
}

.btn-icon {
  font-size: 32rpx;
  color: #ffffff;
  margin-right: 8rpx;
}

.btn-text {
  font-size: 28rpx;
  font-weight: 600;
  color: #ffffff;
}

.action-btn .btn-text {
  color: #333;
}
</style>
