<template>
  <div class="ai-assistant-page">
    <!-- 悬浮球入口（可以通过props控制显示/隐藏） -->
    <div class="floating-button" @click="togglePanel" v-if="!isPanelVisible">
      <img src="@/assets/imgs/ai-icon.svg" alt="AI助手" />
    </div>

    <!-- AI助手面板 -->
    <transition name="slide">
      <div class="assistant-panel" v-if="isPanelVisible">
        <div class="panel-header">
          <h2>AI思政助手</h2>
          <div class="header-actions">
            <el-button size="small" @click="handleFileUpload">📎 上传文件</el-button>
            <el-button size="small" @click="showHistory">📜 历史记录</el-button>
            <el-button size="small" @click="togglePanel">✕</el-button>
          </div>
        </div>

        <div class="panel-tabs">
          <div 
            class="panel-tab" 
            v-for="tab in tabs" 
            :key="tab.key"
            :class="{ active: activeTab === tab.key }"
            @click="activeTab = tab.key"
          >
            {{ tab.label }}
          </div>
        </div>

        <div class="panel-content">
          <component :is="currentComponent" />
        </div>
      </div>
    </transition>

    <!-- 历史对话记录弹窗 -->
    <el-dialog v-model="historyVisible" title="历史对话记录" width="600px">
      <DialogHistory @load-conversation="loadConversation" />
    </el-dialog>

    <!-- 文件解读与记录弹窗 -->
    <el-dialog v-model="fileDialogVisible" title="文件解读与记录" width="800px">
      <FileInterpretation />
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { ElButton, ElDialog } from 'element-plus';
import ChatInterface from './components/ChatInterface.vue';
import KnowledgeBase from './components/KnowledgeBase.vue';
import DialogHistory from './components/DialogHistory.vue';
import FileInterpretation from './components/FileInterpretation.vue';

const isPanelVisible = ref(false);
const activeTab = ref('chat');
const historyVisible = ref(false);
const fileDialogVisible = ref(false);

const tabs = [
  { key: 'chat', label: '对话' },
  { key: 'knowledge', label: '知识库' }
];

const componentMap: Record<string, any> = {
  'chat': ChatInterface,
  'knowledge': KnowledgeBase
};

const currentComponent = computed(() => componentMap[activeTab.value]);

function togglePanel() {
  isPanelVisible.value = !isPanelVisible.value;
}

function showHistory() {
  historyVisible.value = true;
}

function handleFileUpload() {
  fileDialogVisible.value = true;
}

function loadConversation(conversation: any) {
  // TODO: 加载历史对话
  historyVisible.value = false;
}
</script>

<style lang="scss" scoped>
.ai-assistant-page {
  position: relative;
}

.floating-button {
  position: fixed;
  bottom: 40px;
  right: 40px;
  width: 64px;
  height: 64px;
  background: linear-gradient(135deg, #C62828, #E53935);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(198, 40, 40, 0.4);
  transition: all 0.3s;
  z-index: 1000;
  
  &:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(198, 40, 40, 0.5);
  }
  
  img {
    width: 36px;
    height: 36px;
  }
}

.assistant-panel {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 450px;
  height: 650px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  z-index: 1000;
  overflow: hidden;
}

.panel-header {
  padding: 20px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #C62828, #E53935);
  color: white;
  
  h2 {
    font-size: 18px;
    font-weight: 600;
  }
}

.header-actions {
  display: flex;
  gap: 8px;
}

.panel-tabs {
  display: flex;
  background: #f5f5f5;
  padding: 0 16px;
}

.panel-tab {
  padding: 12px 20px;
  cursor: pointer;
  font-size: 14px;
  color: #666;
  position: relative;
  transition: all 0.3s;
  
  &:hover {
    color: #C62828;
  }
  
  &.active {
    color: #C62828;
    font-weight: 600;
    background: white;
    border-radius: 8px 8px 0 0;
  }
}

.panel-content {
  flex: 1;
  overflow: hidden;
}

.slide-enter-active,
.slide-leave-active {
  transition: all 0.3s ease;
}

.slide-enter-from,
.slide-leave-to {
  transform: translateY(100%);
  opacity: 0;
}
</style>

