<template>
  <!-- 展开状态 -->
  <el-menu
    class="history-menu"
    :collapse="isCollapsed"
    :default-active="chatStore.conversationId || ''"
    @select="handleSelect"
  >
    <el-menu-item-group class="history-item-group" v-if="!isCollapsed">
      <template #title>
        <el-icon><Clock /></el-icon>
        <span class="history-title">历史会话</span>
      </template>
      <template v-if="currentModeHistory.length">
        <el-menu-item 
          v-for="chat in currentModeHistory" 
          :key="chat.conversationId"
          :index="chat.conversationId"
          class="history-item"
        >
          <div class="history-item-content">
            <span class="history-item-title">{{ formatChatTitle(chat) }}</span>
            <el-button
              class="delete-btn"
              :icon="Delete"
              link
              @click.stop="handleDelete(chat.conversationId)"
            />
          </div>
        </el-menu-item>
      </template>
      <el-menu-item v-else disabled class="history-empty">
        <span class="mode-name">暂无历史会话</span>
      </el-menu-item>
    </el-menu-item-group>

    <!-- 折叠状态 -->
    <el-sub-menu v-else index="history" class="history-collapsed" popper-class="history-popper">
      <template #title>
        <el-icon><Clock /></el-icon>
      </template>
      <template v-if="currentModeHistory.length">
        <el-menu-item 
          v-for="chat in currentModeHistory" 
          :key="chat.conversationId"
          :index="chat.conversationId"
          class="history-collapsed-item"
          :title="formatChatTitle(chat)"
        >
          <template #title>{{ formatChatTitle(chat) }}</template>
        </el-menu-item>
      </template>
      <el-menu-item v-else disabled class="history-collapsed-empty">
        <el-icon><InfoFilled /></el-icon>
        <template #title>暂无历史会话</template>
      </el-menu-item>
    </el-sub-menu>
  </el-menu>
</template>

<script setup>
import { computed } from 'vue'
import { useChatStore } from '@/store/chat'
import { useSettingsStore } from '@/store/settings'
import { Clock, Delete, InfoFilled } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

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

const isCollapsed = computed(() => settingsStore.sidebarCollapsed)

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 handleSelect = (conversationId) => {
  if (conversationId !== 'history') {
    chatStore.switchConversation(conversationId)
  }
}

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

<style lang="scss" scoped>
.history-menu {
  background: transparent;
  border: none;
  padding: 8px;
}

.history-item-group {
  :deep(.el-menu-item-group__title) {
    padding: 8px 12px !important;
    font-size: 14px;
    font-weight: bold;
    color: var(--el-text-color-regular);
    display: flex;
    align-items: center;
    line-height: 20px;
    
    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-menu-item-group__content) {
    padding-left: 12px;
  }
  .history-title {
    margin-left: 12px;
    font-weight: bold;
    color: var(--el-text-color-regular);
  }
}

.history-item {
  height: 44px !important;
  line-height: 44px !important;
  color: var(--el-text-color-regular);
  border-radius: 8px;
  margin-bottom: 4px;
  margin-left: 12px;
  padding: 0 12px !important;
  overflow: hidden;
  transition: all 0.3s ease;

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

  &.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);
  }
}

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

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

.delete-btn {
  position: absolute;
  right: -8px;
  opacity: 0;
  transform: translateX(10px);
  transition: all 0.3s ease;
  height: 24px;
  width: 24px;
  padding: 0;
  color: var(--el-text-color-regular);
  flex-shrink: 0;
  border-radius: 4px;
  
  &:hover {
    color: var(--el-color-danger);
  }
  
  :deep(.el-icon) {
    font-size: 14px;
  }
}

.history-item:hover .delete-btn {
  opacity: 1;
  transform: translateX(0);
  color: var(--el-color-regular);
  &:hover {
    color: var(--el-color-danger);
  }
}

.history-empty {
  opacity: 0.6;
  cursor: not-allowed;
  color: var(--el-text-color-regular);
  font-size: 14px;
  padding: 0 12px !important;
  margin-right: 12px;
  height: 44px !important;
  line-height: 44px !important;
}

// 折叠状态样式
.history-collapsed {
  :deep(.el-sub-menu__title) {
    height: 40px;
    line-height: 40px;
    padding: 0 12px !important;
    
    .el-icon {
      font-size: 16px;
    }
  }
}
.history-popper {
    border-radius: 8px;
}
.history-collapsed-item {
  height: 40px !important;
  line-height: 40px !important;
  padding: 0 12px !important;
  margin: 8px 12px;
  border-radius: 8px;
  color: var(--el-text-color-regular);
  transition: all 0.3s ease;

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

  &.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);
    }
  }
}

.history-collapsed-empty {
  @extend .history-collapsed-item;
  opacity: 0.6;
  cursor: not-allowed;
  color: var(--el-text-color-secondary);

  &:hover {
    background-color: transparent;
    color: var(--el-text-color-secondary);
  }
}

// 暗黑模式适配
:deep(html.dark) {
  .history-collapsed-item {
    &:hover {
      background-color: var(--el-fill-color-darker);
    }
    
    &.is-active {
      background-color: var(--el-color-primary-dark-2);
      color: var(--el-color-white);
    }
  }
}
</style> 