<template>
  <div class="container">
    <el-card class="main-card" :body-style="{ padding: 0 }">
      <div class="header">
        <div class="toggle-link" @click="sidebarVisible = !sidebarVisible">
          ≡ {{ sidebarVisible ? '收起配置' : '展开配置' }}
        </div>
        <h1 class="title">智能客服</h1>
      </div>

      <div class="content-wrapper">
        <div class="main-content">
          <aside v-if="sidebarVisible" class="sidebar" :class="{ disabled: showChat }">
            <div class="collapse-section">
              <div class="collapse-header" @click="basicExpanded = !basicExpanded">
                <span>基础设置</span>
                <el-icon class="arrow">
                  <ArrowUp v-if="basicExpanded" />
                  <ArrowDown v-else />
                </el-icon>
              </div>
              <div v-if="basicExpanded" class="collapse-content">
                <el-form label-position="top" size="default">
                  <el-form-item label="语言">
                    <el-select v-model="config.language" style="width: 100%">
                      <el-option label="中文" value="中文" />
                    </el-select>
                  </el-form-item>

                  <el-form-item label="TTS">
                    <el-select v-model="config.tts" style="width: 100%">
                      <el-option label="Minimax" value="Minimax" />
                    </el-select>
                  </el-form-item>

                  <el-form-item label="背景音">
                    <el-select v-model="config.bgSound" style="width: 100%">
                      <el-option label="客服中心" value="客服中心" />
                    </el-select>
                  </el-form-item>

                  <el-form-item label="断句模式">
                    <el-radio-group v-model="config.punctuation" style="width: 100%">
                      <el-radio-button value="semantic">语义断句</el-radio-button>
                      <el-radio-button value="normal">普通断句</el-radio-button>
                    </el-radio-group>
                  </el-form-item>

                  <el-form-item label="打断模式">
                    <el-radio-group v-model="config.interrupt" style="width: 100%">
                      <el-radio-button value="smart">智能打断</el-radio-button>
                      <el-radio-button value="manual">手动打断</el-radio-button>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </div>
            </div>

            <div class="collapse-section">
              <div class="collapse-header" @click="otherExpanded = !otherExpanded">
                <span>其他设置</span>
                <el-icon class="arrow">
                  <ArrowUp v-if="otherExpanded" />
                  <ArrowDown v-else />
                </el-icon>
              </div>
              <div v-if="otherExpanded" class="collapse-content">
                <el-form label-position="top" size="default">
                  <el-form-item label="打断时长">
                    <div class="slider-value">{{ config.interruptTime }}ms</div>
                    <el-slider
                      v-model="config.interruptTime"
                      :min="100"
                      :max="500"
                      :step="10"
                    />
                    <div class="slider-labels">
                      <span>更灵敏</span>
                      <span>更稳定</span>
                    </div>
                  </el-form-item>

                  <el-form-item label="VAD 时长">
                    <div class="slider-value">{{ config.vadTime }}ms</div>
                    <el-slider
                      v-model="config.vadTime"
                      :min="100"
                      :max="800"
                      :step="10"
                    />
                    <div class="slider-labels">
                      <span>更快</span>
                      <span>更慢</span>
                    </div>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </aside>

          <main class="right-panel" v-if="!showChat" @wheel="handleWheel">
            <div class="agents-container">
              <div class="agents-list">
                <div
                  v-for="agent in paginatedAgents"
                  :key="agent.id"
                  class="agent-card"
                  :class="{ selected: selectedAgent === agent.id }"
                  @click="selectedAgent = agent.id"
                >
                  <div class="avatar" :class="agent.gender"></div>
                  <div class="gender-icon" :class="agent.gender">{{ agent.gender === 'female' ? '♀' : '♂' }}</div>
                  <div class="agent-name">{{ agent.name }}</div>
                </div>
              </div>
              <div class="pagination-dots" v-if="totalPages > 1">
                <span
                  v-for="page in totalPages"
                  :key="page"
                  class="dot"
                  :class="{ active: currentPage === page - 1 }"
                  @click="currentPage = page - 1"
                ></span>
              </div>
            </div>
            <div class="agents-footer">
              <el-button type="primary" round size="large" class="start-btn" @click="showChat = true">
                开始对话
              </el-button>
            </div>
          </main>

          <main class="right-panel chat-panel" v-else>
            <div class="chat-content">
              <div class="message ai-message">
                <div class="message-text">
                  你好，我是你的腾讯云音视频 AI 助手！你可以问我：
                  <div class="quick-questions">
                    <div class="question-item">😴 我最近总是失眠，有什么办法可以解决吗？</div>
                    <div class="question-item">🛏️ 怎么改善晚上睡不着、早上睡不醒的问题？</div>
                    <div class="question-item">🚀 超过半小时才睡着是正常的吗？</div>
                  </div>
                </div>
              </div>

              <div class="message user-message">
                <div class="message-text">
                  你好，我是你的睡眠顾问，专门帮人解决睡眠问题的，方便聊几句吗？
                  <el-icon class="pause-icon"><VideoPlay /></el-icon>
                </div>
              </div>
            </div>

            <div class="chat-footer">
              <el-button class="keyboard-btn" @click="isTextInput = !isTextInput">
                <el-icon><ChatDotRound v-if="!isTextInput" /><Microphone v-else /></el-icon>
              </el-button>

              <el-input v-if="isTextInput" placeholder="输入消息..." class="text-input" />
              <div v-else class="voice-input-btn" @click="isMicMuted = !isMicMuted">
                <el-icon class="mic-icon" :class="{ muted: isMicMuted }">
                  <Microphone v-if="!isMicMuted" />
                  <Mute v-else />
                </el-icon>
                <div class="input-placeholder"></div>
              </div>

              <el-button class="hangup-btn" @click="showChat = false">
                <el-icon><PhoneFilled /></el-icon>
              </el-button>
            </div>
          </main>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ArrowUp, ArrowDown, Microphone, PhoneFilled, VideoPlay, ChatDotRound, Mute } from '@element-plus/icons-vue'

const showChat = ref(false)
const isTextInput = ref(false)
const isMicMuted = ref(false)
const sidebarVisible = ref(true)
const basicExpanded = ref(true)
const otherExpanded = ref(false)
const currentPage = ref(0)

const agents = ref([
  { id: 1, name: '客服 001 号', gender: 'female' },
  { id: 2, name: '客服 002 号', gender: 'male' },
  { id: 3, name: '客服 003 号', gender: 'female' },
  { id: 4, name: '客服 004 号', gender: 'male' },
  { id: 5, name: '客服 005 号', gender: 'female' },
  { id: 6, name: '客服 006 号', gender: 'male' },
  { id: 7, name: '客服 007 号', gender: 'female' },
  { id: 8, name: '客服 008 号', gender: 'male' },
  { id: 9, name: '客服 009 号', gender: 'female' },
  { id: 10, name: '客服 010 号', gender: 'male' },
  { id: 11, name: '客服 011 号', gender: 'female' },
  { id: 12, name: '客服 012 号', gender: 'male' },
  { id: 13, name: '客服 013 号', gender: 'female' },
  { id: 14, name: '客服 014 号', gender: 'male' },
  { id: 15, name: '客服 015 号', gender: 'female' },
  { id: 16, name: '客服 016 号', gender: 'male' },
  { id: 17, name: '客服 017 号', gender: 'female' },
  { id: 18, name: '客服 018 号', gender: 'male' },
  { id: 19, name: '客服 019 号', gender: 'female' },
  { id: 20, name: '客服 020 号', gender: 'male' }
])

const config = ref({
  language: '中文',
  tts: 'Minimax',
  bgSound: '客服中心',
  punctuation: 'semantic',
  interrupt: 'smart',
  interruptTime: 200,
  vadTime: 400
})

const selectedAgent = ref(1)

const pageSize = 6
const totalPages = computed(() => Math.ceil(agents.value.length / pageSize))
const paginatedAgents = computed(() => {
  const start = currentPage.value * pageSize
  return agents.value.slice(start, start + pageSize)
})

const handleWheel = (e) => {
  e.preventDefault()
  // 支持垂直滚动（上下）和水平滚动（左右）
  const delta = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY

  if (delta > 0 && currentPage.value < totalPages.value - 1) {
    currentPage.value++
  } else if (delta < 0 && currentPage.value > 0) {
    currentPage.value--
  }
}
</script>

<style scoped>
:global(html),
:global(body) {
  height: 100%;
  overflow: hidden;
  margin: 0;
}

:global(#app) {
  height: 100%;
  overflow: hidden;
}

.container {
  min-height: 100vh;
  height: 100vh;
  background: linear-gradient(to bottom, #bfdbfe, #dbeafe, #e0f2fe);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 16px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', sans-serif;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
}

.container::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -20%;
  width: 80%;
  height: 100%;
  background: linear-gradient(135deg, rgba(147, 197, 253, 0.3) 0%, rgba(191, 219, 254, 0.2) 100%);
  transform: rotate(-15deg);
  border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
  box-shadow: 0 8px 32px rgba(59, 130, 246, 0.15);
  z-index: -1;
}

.container::after {
  content: '';
  position: absolute;
  bottom: -30%;
  left: -10%;
  width: 60%;
  height: 80%;
  background: linear-gradient(225deg, rgba(191, 219, 254, 0.25) 0%, rgba(224, 242, 254, 0.15) 100%);
  transform: rotate(25deg);
  border-radius: 70% 30% 30% 70% / 70% 70% 30% 30%;
  box-shadow: 0 8px 32px rgba(96, 165, 250, 0.12);
  z-index: -1;
}

.main-card {
  width: 100%;
  max-width: 1200px;
  border-radius: 24px !important;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important;
  height: calc(100vh - 117px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
}

:deep(.main-card .el-card__body) {
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.header {
  padding: 16px 20px;
  text-align: center;
  border-bottom: 1px solid #e5e7eb;
  background: white;
  border-radius: 24px 24px 0 0;
  position: relative;
}

.toggle-link {
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
  color: #9ca3af;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
  transition: color 0.2s;
}

.toggle-link:hover {
  color: #6b7280;
}

.title {
  font-size: 20px;
  font-weight: 500;
  color: #1f2937;
  margin: 0;
}

.content-wrapper {
  position: relative;
  background: white;
  border-radius: 0 0 24px 24px;
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.toggle-btn {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 10;
}

.main-content {
  display: flex;
  gap: 8px;
  padding: 0 12px 0 12px;
  flex: 1;
  height: auto;
  min-height: 0;
  max-height: none;
  align-items: stretch;
  overflow: hidden;
}

.sidebar {
  width: 320px;
  flex-shrink: 0;
  padding: 20px;
  background: #ffffff;
  border-radius: 8px;
  height: 100%;
  overflow-y: auto;
  min-height: 0;
  max-height: 100%;
  box-sizing: border-box;
}

.sidebar.disabled :deep(.el-form) {
  opacity: 0.5;
  pointer-events: none;
}

.right-panel {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  padding: 0;
  background: #f9fafb;
  border-radius: 8px;
  height: 100%;
  overflow: hidden;
  min-width: 0;
  position: relative;
}

.time-info {
  align-self: flex-end;
  color: #9ca3af;
  margin-bottom: 40px;
  font-size: 13px;
}

.agents-container {
  flex: 0 0 auto;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 36px;
  padding-bottom: 0;
  overflow: visible;
  min-height: 0;
  position: relative;
}

.agents-container .pagination-dots {
  position: absolute;
  top: calc(36px + 220px + 16px + 220px + 24px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
}

.agents-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0;
  flex-shrink: 0;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: calc(36px + 220px + 16px + 220px + 24px + 40px);
}

.agents-list {
  display: grid;
  grid-template-columns: repeat(3, 220px);
  gap: 16px;
  width: 100%;
  max-width: 100%;
  padding: 0 12px;
  margin-top: 0;
  margin-bottom: 0;
  justify-content: center;
}

.agent-card {
  width: 220px;
  height: 220px;
  padding: 10px;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  text-align: center;
  cursor: pointer;
  position: relative;
  transition: all 0.3s ease;
  background: white;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.agent-card:hover {
  border-color: #3b82f6;
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(59, 130, 246, 0.15);
}

.agent-card.selected {
  border-color: #3b82f6;
  background: #eff6ff;
  box-shadow: 0 0 0 1px #3b82f6, 0 8px 20px rgba(59, 130, 246, 0.2);
}

.delete-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(239, 68, 68, 0.9);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 10;
}

.agent-card:hover .delete-btn {
  opacity: 1;
}

.delete-btn:hover {
  background: rgba(220, 38, 38, 1);
}

.agent-card:hover {
  border-color: #3b82f6;
  transform: translateY(-4px);
  box-shadow: 0 12px 32px rgba(59, 130, 246, 0.25);
}

.agent-card.selected {
  border-color: #3b82f6;
  background: #eff6ff;
  box-shadow: 0 0 0 1px #3b82f6, 0 8px 24px rgba(59, 130, 246, 0.2);
}

.add-card {
  border: 2px dashed #d1d5db;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120px;
}

.add-card:hover {
  border-color: #3b82f6;
  background: #f9fafb;
  transform: translateY(-4px);
}

.add-icon {
  font-size: 36px;
  color: #9ca3af;
  margin-bottom: 8px;
}

.add-card:hover .add-icon {
  color: #3b82f6;
}

.add-text {
  font-size: 14px;
  color: #6b7280;
  font-weight: 500;
}

.add-card:hover .add-text {
  color: #3b82f6;
}

.avatar {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  margin: 0 auto 6px;
}

.avatar.female {
  background: linear-gradient(135deg, #fce7f3, #fbcfe8);
}

.avatar.male {
  background: linear-gradient(135deg, #dbeafe, #bfdbfe);
}

.gender-icon {
  position: absolute;
  top: 42px;
  right: calc(50% - 26px);
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 9px;
}

.gender-icon.female {
  background: #ec4899;
}

.gender-icon.male {
  background: #3b82f6;
}

.agent-name {
  font-size: 12px;
  color: #1f2937;
  font-weight: 500;
}

.start-btn {
  font-size: 16px;
  padding: 12px 48px;
  align-self: center;
  margin: 0;
}

.pagination-dots {
  display: flex;
  gap: 8px;
  justify-content: center;
  align-items: center;
  padding: 0;
  flex-shrink: 0;
}

.pagination-dots .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d1d5db;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination-dots .dot:hover {
  background: #9ca3af;
}

.pagination-dots .dot.active {
  width: 24px;
  border-radius: 4px;
  background: #3b82f6;
}

.slider-value {
  font-size: 14px;
  color: #1f2937;
  margin-bottom: 8px;
  font-weight: 500;
}

.slider-labels {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #9ca3af;
  margin-top: 4px;
}

/* 自定义折叠组件样式 */
.collapse-section {
  margin-bottom: 0;
}

.collapse-header {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 16px 0;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  font-size: 15px;
  color: #1f2937;
}

.collapse-header:hover {
  color: #3b82f6;
}

.collapse-header .arrow {
  font-size: 14px;
  color: #6b7280;
  margin-left: 8px;
}

.collapse-content {
  padding-bottom: 16px;
}

/* Element Plus 样式覆盖 */
:deep(.el-collapse) {
  border: none;
}

:deep(.el-collapse-item) {
  margin-bottom: 0;
}

:deep(.el-collapse-item__header) {
  font-weight: 600;
  font-size: 15px;
  color: #1f2937;
  border: none;
  padding: 16px 0;
  height: auto;
  line-height: 1.5;
  background: transparent;
  justify-content: flex-start;
}

:deep(.el-collapse-item__arrow) {
  font-size: 14px;
  color: #6b7280;
  margin-left: 8px;
  margin-right: 0;
}

:deep(.el-collapse-item__arrow.is-active) {
  transform: rotate(180deg);
}

:deep(.el-collapse-item__wrap) {
  border: none;
  background: transparent;
}

:deep(.el-collapse-item__content) {
  padding: 0 0 16px 0;
  color: inherit;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  color: #9ca3af;
  font-size: 13px;
  margin-bottom: 8px;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-radio-group) {
  width: 100%;
  display: flex;
}

:deep(.el-radio-button) {
  flex: 1;
}

:deep(.el-radio-button__inner) {
  width: 100%;
  border-radius: 6px;
}

:deep(.el-slider__runway) {
  height: 4px;
  background-color: #e5e7eb;
}

:deep(.el-slider__bar) {
  background-color: #3b82f6;
}

:deep(.el-slider__button) {
  width: 16px;
  height: 16px;
  border: 2px solid #3b82f6;
}

:deep(.el-button--primary) {
  background-color: #3b82f6;
  border-color: #3b82f6;
}

:deep(.el-button--primary:hover) {
  background-color: #2563eb;
  border-color: #2563eb;
}

/* 对话界面样式 */
.chat-panel {
  display: flex;
  flex-direction: column;
  padding: 0;
  height: 100%;
}

.chat-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px 20px 20px 20px;
}

.message {
  margin-bottom: 16px;
  margin-left: 0;
}

.ai-message .message-text {
  background: white;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.user-message {
  display: flex;
  justify-content: flex-start;
}

.user-message .message-text {
  background: white;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  position: relative;
}

.quick-questions {
  margin-top: 12px;
}

.question-item {
  padding: 8px 0;
  font-size: 14px;
  color: #333;
}

.pause-icon {
  position: absolute;
  right: 12px;
  bottom: 12px;
  color: #3b82f6;
}

.chat-footer {
  padding: 20px 16px;
  background: white;
  border-top: 1px solid #e5e7eb;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
}

.keyboard-btn {
  width: 48px;
  height: 48px;
  border: 1px solid #e5e7eb;
  background: white;
  flex-shrink: 0;
  border-radius: 50%;
  padding: 0;
  min-width: 48px;
}

.voice-input-btn {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 48px;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 24px;
  padding: 0 20px;
}

.mic-icon {
  font-size: 24px;
  color: #6b7280;
  flex-shrink: 0;
}

.text-input {
  flex: 1;
}

.input-placeholder {
  flex: 1;
  height: 2px;
  border-bottom: 2px dotted #d1d5db;
}

.hangup-btn {
  width: 56px;
  height: 56px;
  background: #ef4444;
  border: none;
  color: white;
  flex-shrink: 0;
  border-radius: 50%;
  padding: 0;
  min-width: 56px;
}

.hangup-btn:hover {
  background: #dc2626;
}


</style>
