<template>
  <div class="mode-selector-container">
    <div 
      class="mode-selector-backdrop" 
      :class="{ visible: !settingsStore.sidebarCollapsed }"
      @click="settingsStore.toggleSidebar"
      v-if="isMobile"
    ></div>
    <div class="mode-selector" :class="{ 'collapsed': settingsStore.sidebarCollapsed }">
      <div class="mode-header">
        <!-- Expanded Logo -->
        <div class="mode-header-title" v-if="!settingsStore.sidebarCollapsed">
          <img src="/lgogo.svg"  alt="Logo"  class="mode-header-icon" />
        </div>
        <!-- Collapsed Logo -->
        <div class="mode-header-collapsed-logo" v-if="settingsStore.sidebarCollapsed">
             <img src="/logo.svg" alt="Logo" class="mode-header-icon collapsed" />
        </div>
      </div>
      <el-menu
        :default-active="chatStore.currentMode"
        class="mode-menu"
        :collapse="settingsStore.sidebarCollapsed"
        @select="selectMode"
      >
        <el-menu-item class="mode-item new-chat-item" @click="handleNewChat">
          <el-icon><Plus /></el-icon>
          <span class="mode-name">新建会话</span>
        </el-menu-item>

        <el-menu-item v-for="mode in chatModes" :key="mode.id" :index="mode.id" class="mode-item">
          <el-icon><component :is="mode.icon" /></el-icon>
          <span class="mode-name">{{ mode.name }}</span>
        </el-menu-item>

       
      </el-menu>
      <ChatHistoryList />
      <!-- Settings Button Popover -->
      <el-popover
        placement="top-end"
        trigger="click"
        popper-class="settings-popover-popper"
      >
        <template #reference>
          <div class="sidebar-footer">
            <div class="settings-button" title="设置">
              <el-icon><Setting /></el-icon>
              <span v-if="!settingsStore.sidebarCollapsed" class="button-text">设置</span>
            </div>
          </div>
        </template>
        <!-- Popover Content -->
        <SettingsPanel />
      </el-popover>

    </div>
  </div>
</template>

<script setup>
import { useChatStore } from '@/store/chat'
import { useSettingsStore } from '@/store/settings'
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { ChatDotRound, ChatLineRound, ChatRound, Service, Setting, Clock, Delete, Plus } from '@element-plus/icons-vue'
import { ElMenu, ElMenuItem, ElIcon, ElPopover, ElMessageBox } from 'element-plus'
import SettingsPanel from '@/components/settings/SettingsPanel.vue'
import ChatHistoryList from '@/views/chat/components/ChatHistoryList.vue'

const chatStore = useChatStore()
const settingsStore = useSettingsStore()

const chatModes = computed(() => {
  return Object.keys(chatStore.chatModes).map(key => ({
    id: key,
    name: chatStore.chatModes[key].name,
    icon: key === 'general' ? ChatRound : 
          key === 'assistant' ? Service :
          key === 'chat' ? ChatDotRound : ChatLineRound
  }))
})

// 获取当前模式下的历史会话
const currentModeHistory = computed(() => {
  return chatStore.conversations.filter(chat => chat.mode === chatStore.currentMode)
})

const formatChatTitle = (chat) => {
  if (chat.messages && chat.messages.length > 0) {
    const firstMessage = chat.messages[0].question
    return firstMessage.length > 20 ? firstMessage.substring(0, 20) + '...' : firstMessage
  }
  return `会话 ${chat.conversationId.substring(0, 8)}`
}

const handleDelete = async (conversationId) => {
  try {
    await ElMessageBox.confirm(
      '确定要删除这个会话吗？删除后无法恢复。',
      '删除确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        confirmButtonClass: 'el-button--danger'
      }
    )
    chatStore.deleteConversation(conversationId)
  } catch {
    // 用户取消删除
  }
}

const selectMode = (id) => {
  // 检查是否是会话ID
  const isConversationId = chatStore.conversations.some(chat => chat.conversationId === id)
  if (isConversationId) {
    chatStore.switchConversation(id)
  } else {
    chatStore.setCurrentMode(id)
  }
}

const handleNewChat = () => {
  chatStore.startNewChat()
}

const isMobile = ref(false)

const checkMobile = () => {
  isMobile.value = window.innerWidth <= 768
}

onMounted(() => {
  checkMobile()
  window.addEventListener('resize', checkMobile)
})

onUnmounted(() => {
  window.removeEventListener('resize', checkMobile)
})
</script>

<style lang="scss" scoped>
.mode-selector-container {
  height: 100vh;
  position: relative;
}

.mode-selector {
  width: 240px;
  height: 100%;
  background-color: var(--el-color-primary-light-7);
  border-right: 1px solid var(--el-border-color-light);
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;

  body:not(.no-animations) & {
    transition: width 0.3s ease-in-out;
  }
  
  &.collapsed {
    width: 64px;
  }
}

.mode-header {
  height: 60px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  
  &-title {
    display: flex;
    align-items: center;
    
    .mode-header-icon {
      height: 32px;
      will-change: filter;
      
      body:not(.no-animations) & {
        transition: filter 300ms;
      }
      
      &:hover {
        filter: drop-shadow(0 0 0.75em rgba(var(--el-color-primary-rgb), 0.6)); 
      }
    }
  }
}

.mode-menu {
  background-color: transparent;
  border: none;
  padding: 8px;
  overflow: visible;
}

.new-chat-item {
  margin-bottom: 12px !important;
  border: 1px solid var(--el-border-color-lighter);
  background-color: var(--el-fill-color-light) !important;

  &:hover {
    box-shadow: var(--el-box-shadow-light);
    color: var(--el-color-primary);
  }
}

:deep(.mode-item) {
  height: 44px;
  line-height: 44px;
  color: var(--el-text-color-regular);
  border-radius: 8px;
  margin-bottom: 4px;
  padding: 0 12px !important;
  overflow: hidden;

  .el-icon {
    margin: 0;
    font-size: 18px;
    width: 18px;
  }
  
  &.is-active {
    background-image: linear-gradient(135deg, var(--el-color-primary) 0%, var(--el-color-primary-light-3) 100%);
    color: var(--el-color-white);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);

    .el-icon {
      color: var(--el-color-white);
    }
  }
  
  &:not(.is-active):hover {
    background-color: var(--el-fill-color-light);
    color: var(--el-color-primary);
  }
  
  .mode-name {
    margin-left: 12px;
    font-size: 14px;
    white-space: nowrap;
    opacity: 1;
    transform: translateX(0);
    
    body:not(.no-animations) & {
       transition: opacity 0.3s ease-in-out;
    }
  }
}

.mode-selector.collapsed {
  .mode-header {
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60px;
    
    .mode-header-collapsed-logo {
      display: flex;
      align-items: center;
      height: 100%;
    }

    .mode-header-icon.collapsed {
      height: 32px;
      filter: drop-shadow(0 0 0.75em rgba(var(--el-color-primary-rgb), 0.6));
      vertical-align: middle;
    }
  }
  
  .mode-menu {
    padding: 8px 12px;
  }

  :deep(.el-menu-item) {
    width: 40px;
    height: 40px;
    padding: 0 !important;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 4px auto;

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

    .mode-name {
      display: none;
    }
  }

  .settings-button {
    width: 40px;
    height: 40px;
    .mode-name,
    .button-text {
      opacity: 0;
    }
  }
}

.sidebar-footer {
  height: 48px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: auto;
  flex-shrink: 0;
}

.settings-button {
  display: flex;
  align-items: center;
  height: 40px;
  width: 100%;
  padding: 0 12px;
  color: var(--el-text-color-regular);
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out;

  .el-icon {
    margin: 0;
    font-size: 18px;
    width: 18px;
  }

  &:hover {
    background-color: var(--el-fill-color-light);
    color: var(--el-color-primary);
  }

  .button-text {
    margin-left: 12px;
    font-size: 14px;
    white-space: nowrap;
    opacity: 1;
    transition: opacity 0.3s ease-in-out;
  }
}

/* 移动端样式 */
@media screen and (max-width: 768px) {
  .mode-selector-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--el-overlay-color-lighter);
    z-index: 9;
    display: none;
    
    &.visible {
      display: block;
    }
  }
  
  .mode-selector {
    position: fixed;
    left: 0;
    top: 0;
    
    &.collapsed {
      transform: translateX(-100%);
    }
  }
}

.mode-selector.collapsed {
  .mode-header {
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60px;
    
    .mode-header-collapsed-logo {
      display: flex;
      align-items: center;
      height: 100%;
    }

    .mode-header-icon.collapsed {
        height: 32px;
        filter: drop-shadow(0 0 0.75em rgba(var(--el-color-primary-rgb), 0.6));
        vertical-align: middle;
    }
  }
  
  .mode-menu {
    padding: 8px 12px;
  }

  :deep(.el-menu-item) {
    width: 40px;
    height: 40px;
    padding: 0 !important;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 4px auto;

    .el-icon {
      margin: 0;
    }

    .mode-name {
      display: none;
    }
  }

  .settings-button {
    width: 40px;
    height: 40px;
    .mode-name,
    .button-text {
      opacity: 0;
    }
  }
}

.history-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  position: relative;
  min-width: 0;
}

.history-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 24px;
  min-width: 0;
}

.delete-btn {
  position: absolute;
  right: -8px;
  opacity: 0;
  transform: translateX(10px);
  transition: all 0.3s ease;
  height: 24px;
  width: 24px;
  padding: 0;
  color: #f56c6c;
  flex-shrink: 0;
  
  &:hover {
    color: #f56c6c;
    background-color: rgba(245, 108, 108, 0.1);
  }
  
  :deep(.el-icon) {
    font-size: 14px;
  }
}

.history-item {
  &:hover .delete-btn {
    opacity: 1;
    transform: translateX(0);
  }
}

.history-empty {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>

<style lang="scss">
.settings-popover-popper {
  min-width: 350px !important;
  padding: 0 !important;
  border: none !important;
  box-shadow: var(--el-box-shadow-light);
  border-radius: 8px;
}
</style>

<style lang="scss">
/* Dark mode override */
html.dark .mode-selector {
    background-color: var(--el-color-primary-dark-2);
    border-right-color: var(--el-border-color-dark);
  
  /* Dark mode hover */
  .mode-menu :deep(.el-menu-item):not(.is-active):hover {
      background-color: var(--el-fill-color);
      color: var(--el-color-primary);
  }
}
</style> 