<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
      >
        <el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path">
          <el-icon :size="14"><component :is="getIcon(item.meta?.icon)" /></el-icon>
          <span>{{ item.meta?.title }}</span>
        </el-menu-item>
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { Monitor, PieChart, Document, Bell } 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 = {
  'monitor': Monitor,
  'order': Document,
  'cost': PieChart,
  'energy': Monitor,
  'oee': PieChart,
  'output': PieChart,
  'stop': Bell
}

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

const menuItems = [
  { path: '/dashboard/order', meta: { title: '订单大屏', icon: 'order' } },
  { path: '/dashboard/cost', meta: { title: '成本大屏', icon: 'cost' } },
  { path: '/dashboard/energy', meta: { title: '能源大屏', icon: 'energy' } },
  { path: '/dashboard/oee', meta: { title: 'OEE综合大屏', icon: 'oee' } },
  { path: '/dashboard/output', meta: { title: '产出监控大屏', icon: 'output' } },
  { path: '/dashboard/stop-analysis', meta: { title: '停机分析大屏', icon: 'stop' } },
  { path: '/dashboard/acid-rolling', meta: { title: '酸轧数据大屏', icon: 'example' } },
  { path: '/warehouse-overview', meta: { title: '库区总览大屏', icon: 'example' } }
]
</script>

<style lang="scss" scoped>
.sidebar-container {
  flex-shrink: 0;
  width: 200px;
  height: 100vh;
  background: linear-gradient(180deg, #0a1428 0%, #0d1b34 100%);
  border-right: 1px solid rgba(0, 212, 255, 0.2);
  box-shadow: 2px 2px 20px rgba(0, 0, 0, 0.3);
  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(90deg, #00a8cc 0%, #00d4ff 50%, #00a8cc 100%);
  border-bottom: 1px solid rgba(0, 212, 255, 0.3);

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

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

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

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

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

  &::-webkit-scrollbar-track {
    background: rgba(0, 212, 255, 0.1);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 212, 255, 0.3);
    border-radius: 3px;
    transition: background 0.2s;

    &:hover {
      background: rgba(0, 212, 255, 0.5);
    }
  }
}

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

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

  :deep(.el-menu-item) {
    height: 44px;
    line-height: 44px;
    color: #ffffff;
    margin: 6px 4px;
    padding: 0 16px;
    border-radius: 8px;
    position: relative;
    border-left: 3px solid transparent;
    transition: all 0.25s ease;
    background: rgba(0, 212, 255, 0.08);
    font-size: 14px;

    .el-icon {
      color: #00d4ff;
      margin-right: 12px;
      font-size: 16px;
    }

    &:hover {
      background: rgba(0, 212, 255, 0.25);
      color: #ffffff;
      transform: translateX(4px);

      .el-icon {
        color: #00d4ff;
        text-shadow: 0 0 10px rgba(0, 212, 255, 0.6);
      }
    }

    &.is-active {
      border-left: 3px solid #00d4ff;
      background: rgba(0, 212, 255, 0.3);
      color: #ffffff;
      font-weight: 600;
      box-shadow: 0 0 15px rgba(0, 212, 255, 0.25);

      .el-icon {
        color: #00d4ff;
        text-shadow: 0 0 15px rgba(0, 212, 255, 0.9);
      }
    }
  }

  &.is-collapse {
    :deep(.el-menu-item) {
      padding: 0;
      text-align: center;
      border-left: none;

      span {
        display: none;
      }

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

      &.is-active {
        border-left: none;
        background: rgba(0, 212, 255, 0.2);
      }
    }
  }
}
</style>
