<template>
  <div :class="['sidebar-container', { 'is-collapse': !sidebar.opened }]">
    <div class="sidebar-header">
      <div class="logo-wrapper">
        <span class="logo-icon">📊</span>
        <span v-show="sidebar.opened" class="logo-text">数据管理平台</span>
      </div>
    </div>
    <el-scrollbar class="sidebar-scroll">
      <el-menu
        :default-active="activeMenu"
        :collapse="!sidebar.opened"
        :collapse-transition="false"
        mode="vertical"
        class="sidebar-menu"
        router
        unique-opened
      >
        <template v-for="item in menuItems" :key="item.path">
          <el-menu-item v-if="!item.children || item.children.length === 0" :index="item.path">
            <el-icon :size="14"><component :is="getIcon(item.meta?.icon)" /></el-icon>
            <span>{{ item.meta?.title }}</span>
          </el-menu-item>
          <el-sub-menu v-else :index="item.path">
            <template #title>
              <el-icon :size="14"><component :is="getIcon(item.meta?.icon)" /></el-icon>
              <span>{{ item.meta?.title }}</span>
            </template>
            <template v-for="child in item.children" :key="child.path">
              <el-menu-item v-if="!child.children || child.children.length === 0" :index="child.path">
                <el-icon :size="12"><component :is="getIcon(child.meta?.icon)" /></el-icon>
                <span>{{ child.meta?.title }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { Refresh, Monitor, PieChart, Document, List, Plus, Bell, Setting } from '@element-plus/icons-vue'

const route = useRoute()
const store = useStore()

const sidebar = computed(() => store.state.app.sidebar)
const activeMenu = computed(() => route.path)

const iconMap = {
  'dashboard': Monitor,
  'monitor': Monitor,
  'example': PieChart,
  'order': Document,
  'cost': PieChart,
  'energy': Monitor,
  'pie-chart': PieChart,
  'list': List,
  'plus': Plus,
  'document': Document,
  'output': PieChart,
  'stop': Bell,
  'system': Setting,
  'user': Refresh,
  'role': Refresh,
  'menu': List,
  'config': Setting,
  'database': Monitor
}

const getIcon = (iconName) => {
  return iconMap[iconName] || Monitor
}

const menuItems = [
  {
    path: '/index',
    meta: { title: '工作台', icon: 'dashboard' },
    children: []
  },
  {
    path: '/dashboard',
    meta: { title: '数据大屏', icon: 'monitor' },
    children: [
      { path: '/dashboard/demo', meta: { title: '示例大屏', icon: 'example' }, children: [] },
      { path: '/dashboard/order', meta: { title: '订单大屏', icon: 'order' }, children: [] },
      { path: '/dashboard/cost', meta: { title: '成本大屏', icon: 'cost' }, children: [] },
      { path: '/dashboard/energy', meta: { title: '能源大屏', icon: 'energy' }, children: [] },
      { path: '/dashboard/acid-rolling', meta: { title: '酸轧数据大屏', icon: 'example' }, children: [] }
    ]
  },
  {
    path: '/screen-manage',
    meta: { title: '大屏管理', icon: 'pie-chart' },
    children: [
      { path: '/screen-manage', meta: { title: '大屏列表', icon: 'list' }, children: [] },
      { path: '/screen-manage/create', meta: { title: '新建大屏', icon: 'plus' }, children: [] }
    ]
  },
  {
    path: '/reports',
    meta: { title: '报表管理', icon: 'document' },
    children: [
      { path: '/reports', meta: { title: '报表列表', icon: 'list' }, children: [] },
      { path: '/reports/acid-rolling', meta: { title: '酸轧产出报表', icon: 'output' }, children: [] },
      { path: '/reports/acid-stop', meta: { title: '酸轧停机报表', icon: 'stop' }, children: [] }
    ]
  },
  {
    path: '/system',
    meta: { title: '系统管理', icon: 'system' },
    children: [
      { path: '/system/menu', meta: { title: '菜单管理', icon: 'menu' }, children: [] },
      { path: '/system/config', meta: { title: '系统配置', icon: 'config' }, children: [] }
    ]
  },
  {
    path: '/data-source',
    meta: { title: '数据源配置', icon: 'database' },
    children: []
  }
]
</script>

<style lang="scss" scoped>
.sidebar-container {
  flex-shrink: 0;
  width: 200px;
  height: 100vh;
  background: #ffffff;
  border-right: 1px solid #e8e8e8;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.15);
  transition: width 0.28s ease;
  overflow: hidden;

  &.is-collapse {
    width: 54px;
  }
}

.sidebar-header {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #3A71A8, #324157);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);

  .logo-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;

    .logo-icon {
      font-size: 20px;
      color: #ffffff;
    }

    .logo-text {
      font-size: 16px;
      font-weight: bold;
      color: #ffffff;
      transition: opacity 0.2s ease;
    }
  }
}

.sidebar-scroll {
  height: calc(100% - 50px);
  overflow-y: auto;
  overflow-x: hidden;

  &::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }

  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
    transition: background 0.2s;

    &:hover {
      background: #a8a8a8;
    }
  }
}

.sidebar-menu {
  border-right: none;
  background: transparent;
  padding: 4px;

  :deep(.el-menu) {
    border-right: none;
    background: transparent;
  }

  :deep(.el-menu-item),
  :deep(.el-sub-menu__title) {
    height: 38px;
    line-height: 38px;
    color: #333333;
    margin: 2px 0;
    padding: 0 8px;
    border-radius: 0;
    position: relative;
    border-left: 3px solid transparent;
    transition: all 0.2s ease;

    .el-icon {
      color: #8c8c8c;
      margin-right: 8px;
      font-size: 14px;
    }

    &:hover {
      background: rgba(0, 0, 0, 0.06);
      color: #1a1a1a;

      .el-icon {
        color: #555555;
      }
    }

    &.is-active {
      border-left: 3px solid #409eff;
      background: rgba(64, 158, 255, 0.1);
      color: #1a1a1a;
      font-weight: 600;

      .el-icon {
        color: #409eff;
      }
    }
  }

  :deep(.el-sub-menu__title) {
    &::after {
      font-size: 12px;
      color: #8c8c8c;
    }
  }

  :deep(.el-sub-menu .el-menu-item) {
    padding-left: 18px !important;
    font-size: 13px;

    &.is-active {
      padding-left: 18px !important;
    }
  }

  :deep(.el-sub-menu .el-sub-menu .el-menu-item) {
    padding-left: 28px !important;
    font-size: 12px;
  }

  &.is-collapse {
    :deep(.el-menu-item),
    :deep(.el-sub-menu__title) {
      padding: 0;
      text-align: center;

      span {
        display: none;
      }

      .el-icon {
        margin-right: 0;
      }
    }
  }
}

:deep(.el-menu--popup) {
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

  .el-menu-item {
    padding: 0 20px;
    border-left: none;

    &:hover {
      background: rgba(64, 158, 255, 0.08);
    }

    &.is-active {
      border-left: none;
      background: rgba(64, 158, 255, 0.12);
    }
  }
}
</style>