<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" @click="showCreateModal = true">
        <text class="nav-icon">+</text>
      </view>
    </view>

    <!-- 播放列表 -->
    <scroll-view scroll-y class="content" :scroll-top="scrollTop">
      <view v-if="playlists.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="playlist-grid">
        <view
          v-for="item in playlists"
          :key="item.id"
          class="playlist-card"
          @click="goToDetail(item.id)"
        >
          <view class="playlist-cover">
            <text class="cover-icon">🎵</text>
            <view class="count-badge">
              <text>{{ item._count?.items || 0 }}</text>
            </view>
          </view>
          <view class="playlist-info">
            <text class="playlist-title">{{ item.name }}</text>
            <text class="playlist-meta">{{ item.description || '暂无描述' }}</text>
          </view>
        </view>
      </view>

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

    <!-- 创建播放列表弹窗 -->
    <view v-if="showCreateModal" class="modal">
      <view class="modal-mask" @click="showCreateModal = false"></view>
      <view class="modal-content">
        <view class="modal-header">
          <text class="modal-title">新建播放列表</text>
        </view>
        <view class="modal-body">
          <input
            v-model="newPlaylistName"
            class="input"
            placeholder="请输入播放列表名称"
            maxlength="50"
          />
          <textarea
            v-model="newPlaylistDesc"
            class="textarea"
            placeholder="请输入描述（可选）"
            maxlength="200"
          />
        </view>
        <view class="modal-footer">
          <view class="modal-btn cancel" @click="showCreateModal = false">
            <text>取消</text>
          </view>
          <view class="modal-btn confirm" @click="createPlaylist" :disabled="!newPlaylistName.trim()">
            <text>创建</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

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

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

const playlists = ref<any[]>([]);
const loading = ref(false);
const showCreateModal = ref(false);
const newPlaylistName = ref('');
const newPlaylistDesc = ref('');

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

// 跳转到详情
function goToDetail(id: number) {
  uni.navigateTo({ url: `/pages/playlists/detail?id=${id}` });
}

// 获取播放列表
async function fetchPlaylists() {
  loading.value = true;
  try {
    const data = await get('/playlists');
    playlists.value = data || [];
  } catch (error) {
    console.error('获取播放列表失败:', error);
  } finally {
    loading.value = false;
  }
}

// 创建播放列表
async function createPlaylist() {
  if (!newPlaylistName.value.trim()) return;

  try {
    await post('/playlists', {
      name: newPlaylistName.value.trim(),
      description: newPlaylistDesc.value.trim(),
    });
    uni.showToast({ title: '创建成功', icon: 'success' });
    showCreateModal.value = false;
    newPlaylistName.value = '';
    newPlaylistDesc.value = '';
    await fetchPlaylists();
  } catch (error) {
    uni.showToast({ title: '创建失败', icon: 'none' });
  }
}

onMounted(() => {
  fetchPlaylists();
});
</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: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  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: 40rpx;
  color: #ffffff;
}

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

.content {
  padding: 24rpx;
  height: calc(100vh - 160rpx);
}

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

.playlist-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
}

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

.playlist-cover {
  position: relative;
  width: 100%;
  height: 240rpx;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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

.playlist-info {
  padding: 20rpx;
}

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

.playlist-meta {
  font-size: 22rpx;
  color: #9ca3af;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

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

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  width: 85%;
  max-width: 600rpx;
  background: #ffffff;
  border-radius: 24rpx;
  overflow: hidden;
}

.modal-header {
  padding: 32rpx;
  border-bottom: 1rpx solid #f3f4f6;
}

.modal-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1f2937;
}

.modal-body {
  padding: 32rpx;
}

.input {
  width: 100%;
  height: 80rpx;
  padding: 0 24rpx;
  border: 1rpx solid #e5e7eb;
  border-radius: 12rpx;
  font-size: 28rpx;
  margin-bottom: 24rpx;
}

.textarea {
  width: 100%;
  min-height: 160rpx;
  padding: 20rpx 24rpx;
  border: 1rpx solid #e5e7eb;
  border-radius: 12rpx;
  font-size: 28rpx;
}

.modal-footer {
  display: flex;
  border-top: 1rpx solid #f3f4f6;
}

.modal-btn {
  flex: 1;
  height: 96rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30rpx;
}

.modal-btn.cancel {
  color: #6b7280;
  border-right: 1rpx solid #f3f4f6;
}

.modal-btn.confirm {
  color: #4f46e5;
  font-weight: 600;
}

.modal-btn.confirm[disabled] {
  opacity: 0.5;
}
</style>
