<template>
  <transition name="fade">
    <div v-if="playerStore.showPlaylist" class="drawer-backdrop" @click.self="playerStore.togglePlaylist()">
      <transition name="slide-right" appear>
        <div v-if="playerStore.showPlaylist" class="drawer-panel" @click.stop>
          <div class="drawer-header">
            <div class="header-left">
              <Icon icon="lucide:list-music" class="header-icon" />
              <span class="header-title">播放列表</span>
              <span class="header-count">({{ playerStore.playlist.length }})</span>
            </div>
            <div class="header-actions">
              <button class="icon-btn small" @click="playerStore.clearPlaylist()" title="清空列表">
                <Icon icon="lucide:trash-2" />
              </button>
              <button class="icon-btn small" @click="playerStore.togglePlaylist()" title="关闭">
                <Icon icon="lucide:x" />
              </button>
            </div>
          </div>

          <div class="drawer-body">
            <div v-if="playerStore.playlist.length === 0" class="empty-state">
              <Icon icon="lucide:music-4" class="empty-icon" />
              <span class="empty-text">暂无歌曲</span>
              <span class="empty-hint">搜索并添加歌曲吧</span>
            </div>

            <div v-else class="song-list">
              <div
                v-for="(song, index) in playerStore.playlist"
                :key="song.id || song.name + song.artist + index"
                class="song-row"
                :class="{ current: index === playerStore.currentIndex }"
                @dblclick="playSongAt(index)"
              >
                <div class="song-index">
                  <Icon v-if="index === playerStore.currentIndex && playerStore.isPlaying" icon="lucide:volume-2" class="playing-icon" />
                  <span v-else>{{ index + 1 }}</span>
                </div>
                <div class="song-info">
                  <div class="song-name">{{ song.name }}</div>
                  <div class="song-artist">{{ song.artist || '未知歌手' }}</div>
                </div>
                <div class="song-actions">
                  <button class="icon-btn tiny" @click.stop="playSongAt(index)" title="播放">
                    <Icon icon="lucide:play" />
                  </button>
                  <button class="icon-btn tiny" @click.stop="playerStore.removeFromPlaylist(index)" title="从列表移除">
                    <Icon icon="lucide:x" />
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { Icon } from '@iconify/vue';
import { usePlayerStore } from '../../stores/player';

const playerStore = usePlayerStore();

const playSongAt = async (index: number) => {
  const song = playerStore.playlist[index];
  if (song) {
    await playerStore.playFromList(song, playerStore.playlist);
  }
};
</script>

<style scoped>
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 2000;
  display: flex;
  justify-content: flex-end;
}

.drawer-panel {
  width: 420px;
  max-width: 100vw;
  height: 100vh;
  background-color: var(--color-bg-secondary);
  border-left: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.2);
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-icon {
  color: var(--color-accent);
  font-size: 20px;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-primary);
}

.header-count {
  font-size: 13px;
  color: var(--color-text-muted);
}

.header-actions {
  display: flex;
  gap: 4px;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 12px;
  color: var(--color-text-muted);
  padding: 40px 20px;
}

.empty-icon {
  font-size: 60px;
  opacity: 0.3;
}

.empty-text {
  font-size: 15px;
  color: var(--color-text-secondary);
}

.empty-hint {
  font-size: 12px;
  opacity: 0.7;
}

/* Song list */
.song-list {
  display: flex;
  flex-direction: column;
}

.song-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  cursor: pointer;
  transition: background 0.15s;
  border-bottom: 1px solid transparent;
}

.song-row:hover {
  background: var(--color-bg-tertiary);
}

.song-row.current {
  background: var(--color-accent-soft);
}

.song-row.current .song-name {
  color: var(--color-accent);
}

.song-index {
  width: 28px;
  text-align: center;
  color: var(--color-text-muted);
  font-size: 12px;
  flex-shrink: 0;
}

.playing-icon {
  color: var(--color-accent);
  font-size: 14px;
}

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

.song-name {
  font-size: 13px;
  color: var(--color-text-primary);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.song-artist {
  font-size: 11px;
  color: var(--color-text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.song-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.song-row:hover .song-actions {
  opacity: 1;
}

/* Icon btn (local compact variant) */
.icon-btn {
  background: transparent;
  border: none;
  color: var(--color-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  transition: all 0.15s;
  flex-shrink: 0;
}

.icon-btn:hover {
  color: var(--color-text-primary);
  background: var(--color-bg-tertiary);
}

.icon-btn.small { width: 30px; height: 30px; }
.icon-btn.tiny { width: 26px; height: 26px; padding: 0; color: var(--color-text-muted); font-size: 13px; }

/* Scrollbar */
.drawer-body::-webkit-scrollbar {
  width: 6px;
}

.drawer-body::-webkit-scrollbar-track {
  background: transparent;
}

.drawer-body::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}

/* Transitions */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.slide-right-enter-active,
.slide-right-leave-active {
  transition: transform 0.25s ease;
}

.slide-right-enter-from,
.slide-right-leave-to {
  transform: translateX(100%);
}

@media (max-width: 600px) {
  .drawer-panel {
    width: 100vw;
  }
}
</style>
