<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../../stores/user'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 是否折叠侧边栏
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = computed(() => {
  const path = route.path
  if (path === '/admin') return 'dashboard'
  if (path.includes('/admin/user')) return 'user'
  if (path.includes('/admin/music')) return 'music'
  if (path.includes('/admin/playlist')) return 'playlist'
  if (path.includes('/admin/singer')) return 'singer'
  if (path.includes('/admin/banner')) return 'banner'
  if (path.includes('/admin/type')) return 'type'
  return 'dashboard'
})

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 退出后台
const exitAdmin = () => {
  router.push('/')
}

// 退出登录
const logout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logout()
    router.push('/')
    ElMessage.success('已退出登录')
  }).catch(() => {})
}
</script>

<template>
  <div class="admin-container" :class="{ 'is-collapse': isCollapse }">
    <!-- 侧边栏 -->
    <div class="admin-sidebar">
      <div class="sidebar-header">
        <div class="logo">
          <img src="https://via.placeholder.com/40" alt="Logo">
          <span v-show="!isCollapse">音乐后台管理</span>
        </div>
        <el-icon class="collapse-icon" @click="toggleSidebar">
          <fold v-if="isCollapse" />
          <expand v-else />
        </el-icon>
      </div>

      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        :collapse="isCollapse"
        :collapse-transition="false"
        router
      >
        <el-menu-item index="dashboard" @click="router.push('/admin')">
          <el-icon><data-analysis /></el-icon>
          <template #title>控制面板</template>
        </el-menu-item>

        <el-menu-item index="user" @click="router.push('/admin/user')">
          <el-icon><user /></el-icon>
          <template #title>用户管理</template>
        </el-menu-item>

        <el-menu-item index="music" @click="router.push('/admin/music')">
          <el-icon><headset /></el-icon>
          <template #title>音乐管理</template>
        </el-menu-item>

        <el-menu-item index="playlist" @click="router.push('/admin/playlist')">
          <el-icon><files /></el-icon>
          <template #title>歌单管理</template>
        </el-menu-item>

        <el-menu-item index="singer" @click="router.push('/admin/singer')">
          <el-icon><avatar /></el-icon>
          <template #title>歌手管理</template>
        </el-menu-item>

        <el-menu-item index="banner" @click="router.push('/admin/banner')">
          <el-icon><picture /></el-icon>
          <template #title>轮播图管理</template>
        </el-menu-item>

        <el-menu-item index="type" @click="router.push('/admin/type')">
          <el-icon><collection-tag /></el-icon>
          <template #title>类型管理</template>
        </el-menu-item>
      </el-menu>
    </div>

    <!-- 主内容区 -->
    <div class="admin-main">
      <!-- 顶部导航 -->
      <div class="admin-header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="activeMenu !== 'dashboard'">
              {{
                activeMenu === 'user' ? '用户管理' :
                activeMenu === 'music' ? '音乐管理' :
                activeMenu === 'playlist' ? '歌单管理' :
                activeMenu === 'singer' ? '歌手管理' :
                activeMenu === 'banner' ? '轮播图管理' :
                activeMenu === 'type' ? '类型管理' : ''
              }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <div class="header-right">
          <el-dropdown>
            <span class="el-dropdown-link">
              {{ userStore.user?.nickname || userStore.user?.username }}
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="exitAdmin">返回前台</el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>

      <!-- 内容区 -->
      <div class="admin-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<style scoped>
.admin-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.admin-sidebar {
  width: 220px;
  height: 100%;
  background-color: #304156;
  transition: width 0.3s;
  display: flex;
  flex-direction: column;
}

.admin-container.is-collapse .admin-sidebar {
  width: 64px;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  color: #fff;
  border-bottom: 1px solid #1f2d3d;
}

.logo {
  display: flex;
  align-items: center;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 10px;
}

.collapse-icon {
  cursor: pointer;
  font-size: 20px;
}

.sidebar-menu {
  flex: 1;
  border-right: none;
  background-color: #304156;
}

.sidebar-menu :deep(.el-menu-item) {
  color: #bfcbd9;
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  color: #409eff;
  background-color: #263445;
}

.sidebar-menu :deep(.el-menu-item:hover) {
  background-color: #263445;
}

.admin-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.admin-header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

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

.header-right {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
}

.admin-content {
  flex: 1;
  padding: 20px;
  background-color: #f0f2f5;
}
</style>
