<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getCollectedSinger, toggleCollectSinger } from '../../api/singer'
import { processImageUrl } from '../../utils/imageUtils'

const router = useRouter()

// 收藏的歌手列表
const collectSingers = ref([])

// 加载状态
const loading = ref(false)

// 获取收藏的歌手列表
const fetchCollectSingers = async () => {
  loading.value = true
  try {
    const res = await getCollectedSinger()
    if (res.code === 200) {
      // 处理数据
      const singers = res.data.records || res.data.list || []

      // 处理歌手数据
      collectSingers.value = singers.map(item => ({
        id: item.id,
        name: item.name,
        avatar: processImageUrl(item.avatar),
        type: item.type || 0,
        musicCount: item.musicCount || 0,

        fansCount: item.fansCount || 0,
        collectTime: item.collectTime ? new Date(item.collectTime).toLocaleDateString() : '未知'
      }))
    } else {
      ElMessage.error(res.message || '获取收藏的歌手列表失败')
    }
  } catch (error) {
    console.error('获取收藏的歌手列表失败:', error)
    ElMessage.error('获取收藏的歌手列表失败')
  } finally {
    loading.value = false
  }
}

// 查看歌手详情
const viewSingerDetail = (id) => {
  router.push(`/singer/${id}`)
}

// 取消收藏歌手
const cancelCollect = (id, event) => {
  event.stopPropagation()

  ElMessageBox.confirm('确定要取消收藏该歌手吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await toggleCollectSinger(id)
      if (res.code === 200) {
        // 从列表中移除该歌手
        collectSingers.value = collectSingers.value.filter(item => item.id !== id)
        ElMessage.success('已取消收藏')
      } else {
        ElMessage.error(res.message || '取消收藏失败')
      }
    } catch (error) {
      console.error('取消收藏失败:', error)
      ElMessage.error('取消收藏失败')
    }
  }).catch(() => {})
}

// 格式化粉丝数
const formatFans = (count) => {
  if (count < 10000) {
    return count.toString()
  } else if (count < 100000000) {
    return (count / 10000).toFixed(1) + '万'
  } else {
    return (count / 100000000).toFixed(1) + '亿'
  }
}

onMounted(() => {
  fetchCollectSingers()
})
</script>

<template>
  <div class="collect-singer-container">
    <div class="collect-singer-header">
      <h2>我收藏的歌手</h2>
    </div>

    <!-- 歌手列表 -->
    <div class="singer-list" v-loading="loading">
      <div
        v-for="singer in collectSingers"
        :key="singer.id"
        class="singer-item"
        @click="viewSingerDetail(singer.id)"
      >
        <div class="singer-avatar">
          <img :src="singer.avatar" :alt="singer.name">
          <div class="singer-overlay">
            <el-button
              circle
              icon="Delete"
              type="danger"
              @click="(event) => cancelCollect(singer.id, event)"
            ></el-button>
          </div>
        </div>
        <div class="singer-info">
          <div class="singer-name">{{ singer.name }}</div>
          <div class="singer-stats">
            <span>{{ singer.musicCount }}首歌曲</span>

            <span>{{ formatFans(singer.fansCount) }}粉丝</span>
          </div>
          <div class="singer-collect-time">
            收藏于 {{ singer.collectTime }}
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <el-empty
      v-if="collectSingers.length === 0 && !loading"
      description="暂无收藏的歌手"
    ></el-empty>
  </div>
</template>

<style scoped>
.collect-singer-container {
  padding: 0;
}

.collect-singer-header {
  margin-bottom: 20px;
}

.collect-singer-header h2 {
  margin: 0;
  font-size: 1.5rem;
  color: #333;
}

.singer-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 15px;
}

.singer-item {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  cursor: pointer;
}

.singer-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px 0 rgba(0, 0, 0, 0.1);
}

.singer-avatar {
  position: relative;
  width: 100%;
  padding-top: 100%;
  overflow: hidden;
}

.singer-avatar img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.singer-item:hover .singer-avatar img {
  transform: scale(1.05);
}

.singer-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, rgba(0,0,0,0.1), rgba(0,0,0,0.7));
  opacity: 0;
  transition: opacity 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.singer-item:hover .singer-overlay {
  opacity: 1;
}

.singer-info {
  padding: 15px;
}

.singer-name {
  font-weight: bold;
  margin-bottom: 8px;
  text-align: center;
}

.singer-stats {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 0.8rem;
  color: #999;
  margin-bottom: 8px;
}

.singer-stats span {
  white-space: nowrap;
}

.singer-collect-time {
  text-align: center;
  font-size: 0.8rem;
  color: #999;
}
</style>
