<template>
  <div class="learning">
    <div class="learning-container">
      <!-- 侧边栏 - 课程大纲 -->
      <div class="sidebar">
        <div class="course-info">
          <h3>{{ course?.title }}</h3>
          <div class="progress-info">
            <n-progress
              type="line"
              :percentage="progress"
              :show-indicator="false"
            />
            <span class="progress-text">进度: {{ progress }}%</span>
          </div>
        </div>
        
        <div class="lessons-list">
          <div
            v-for="(lesson, index) in lessons"
            :key="lesson.id"
            class="lesson-item"
            :class="{ active: currentLessonId === lesson.id }"
            @click="selectLesson(lesson.id)"
          >
            <div class="lesson-number">{{ index + 1 }}</div>
            <div class="lesson-content">
              <h4>{{ lesson.title }}</h4>
              <span class="duration">{{ lesson.duration }}</span>
            </div>
            <div class="lesson-status">
              <n-icon v-if="lesson.isCompleted" color="#18a058">
                <CheckmarkCircleOutline />
              </n-icon>
              <n-icon v-else-if="currentLessonId === lesson.id" color="#2080f0">
                <PlayCircleOutline />
              </n-icon>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 主要内容区域 -->
      <div class="main-content">
        <div class="video-section">
          <!-- 视频播放器 -->
          <div class="video-player">
            <div class="video-placeholder">
              <n-icon size="64" color="#ccc">
                <PlayCircleOutline />
              </n-icon>
              <p>视频播放器</p>
              <p v-if="currentLesson">{{ currentLesson.title }}</p>
            </div>
          </div>
          
          <!-- 视频控制栏 -->
          <div class="video-controls">
            <n-space>
              <n-button @click="previousLesson" :disabled="!hasPrevious">
                <template #icon>
                  <n-icon><ChevronBackOutline /></n-icon>
                </template>
                上一节
              </n-button>
              
              <n-button type="primary" @click="togglePlay">
                <template #icon>
                  <n-icon>
                    <PlayCircleOutline v-if="!isPlaying" />
                    <PauseCircleOutline v-else />
                  </n-icon>
                </template>
                {{ isPlaying ? '暂停' : '播放' }}
              </n-button>
              
              <n-button @click="nextLesson" :disabled="!hasNext">
                下一节
                <template #icon>
                  <n-icon><ChevronForwardOutline /></n-icon>
                </template>
              </n-button>
              
              <n-button @click="markAsCompleted" v-if="currentLesson && !currentLesson.isCompleted">
                标记为已完成
              </n-button>
            </n-space>
          </div>
        </div>
        
        <!-- 课程内容 -->
        <div class="lesson-content-section">
          <n-tabs default-value="description" size="large">
            <n-tab-pane name="description" tab="课程内容">
              <div class="lesson-description">
                <h2>{{ currentLesson?.title }}</h2>
                <p>{{ currentLesson?.description }}</p>
                
                <div class="lesson-materials">
                  <h3>学习资料</h3>
                  <n-empty description="暂无学习资料" />
                </div>
              </div>
            </n-tab-pane>
            
            <n-tab-pane name="notes" tab="笔记">
              <div class="notes-section">
                <h3>我的笔记</h3>
                <n-input
                  v-model:value="noteContent"
                  type="textarea"
                  placeholder="在这里记录学习笔记..."
                  :rows="10"
                />
                <div class="notes-actions">
                  <n-button type="primary" @click="saveNote">
                    保存笔记
                  </n-button>
                </div>
              </div>
            </n-tab-pane>
            
            <n-tab-pane name="qa" tab="问答">
              <div class="qa-section">
                <h3>课程问答</h3>
                <n-empty description="暂无问答内容" />
              </div>
            </n-tab-pane>
          </n-tabs>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useMessage } from 'naive-ui'
import { useCourseStore } from '@/stores/course'
import {
  PlayCircleOutline,
  PauseCircleOutline,
  CheckmarkCircleOutline,
  ChevronBackOutline,
  ChevronForwardOutline
} from '@vicons/ionicons5'

const route = useRoute()
const message = useMessage()
const courseStore = useCourseStore()

const currentLessonId = ref<number | null>(null)
const isPlaying = ref(false)
const noteContent = ref('')
const progress = ref(0)

const courseId = computed(() => Number(route.params.id))
const course = computed(() => courseStore.currentCourse)
const lessons = computed(() => courseStore.lessons)

const currentLesson = computed(() => {
  return lessons.value.find(lesson => lesson.id === currentLessonId.value)
})

const currentLessonIndex = computed(() => {
  return lessons.value.findIndex(lesson => lesson.id === currentLessonId.value)
})

const hasPrevious = computed(() => currentLessonIndex.value > 0)
const hasNext = computed(() => currentLessonIndex.value < lessons.value.length - 1)

// 选择课程
const selectLesson = (lessonId: number) => {
  currentLessonId.value = lessonId
  isPlaying.value = false
}

// 上一节课
const previousLesson = () => {
  if (hasPrevious.value) {
    const prevIndex = currentLessonIndex.value - 1
    currentLessonId.value = lessons.value[prevIndex].id
  }
}

// 下一节课
const nextLesson = () => {
  if (hasNext.value) {
    const nextIndex = currentLessonIndex.value + 1
    currentLessonId.value = lessons.value[nextIndex].id
  }
}

// 切换播放状态
const togglePlay = () => {
  isPlaying.value = !isPlaying.value
}

// 标记为已完成
const markAsCompleted = () => {
  if (currentLesson.value) {
    currentLesson.value.isCompleted = true
    updateProgress()
    message.success('已标记为完成')
  }
}

// 更新进度
const updateProgress = () => {
  const completedCount = lessons.value.filter(lesson => lesson.isCompleted).length
  progress.value = Math.round((completedCount / lessons.value.length) * 100)
  
  if (course.value) {
    courseStore.updateProgress(course.value.id, progress.value)
  }
}

// 保存笔记
const saveNote = () => {
  // TODO: 实现保存笔记功能
  message.success('笔记已保存')
}

onMounted(async () => {
  await courseStore.fetchCourseById(courseId.value)
  await courseStore.fetchLessons(courseId.value)
  
  // 选择第一节课
  if (lessons.value.length > 0) {
    currentLessonId.value = lessons.value[0].id
  }
  
  updateProgress()
})
</script>

<style scoped>
.learning {
  height: 100vh;
  overflow: hidden;
}

.learning-container {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100vh;
}

.sidebar {
  background: white;
  border-right: 1px solid #e8e8e8;
  overflow-y: auto;
}

.course-info {
  padding: 20px;
  border-bottom: 1px solid #e8e8e8;
}

.course-info h3 {
  margin: 0 0 16px;
  color: #333;
  font-size: 16px;
}

.progress-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-text {
  font-size: 14px;
  color: #666;
}

.lessons-list {
  padding: 0;
}

.lesson-item {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  transition: background-color 0.2s;
}

.lesson-item:hover {
  background: #f8f9fa;
}

.lesson-item.active {
  background: #e6f7ff;
  border-right: 3px solid #2080f0;
}

.lesson-number {
  width: 24px;
  height: 24px;
  background: #f0f0f0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  color: #666;
  margin-right: 12px;
}

.lesson-content {
  flex: 1;
}

.lesson-content h4 {
  margin: 0 0 4px;
  font-size: 14px;
  color: #333;
}

.duration {
  font-size: 12px;
  color: #999;
}

.lesson-status {
  margin-left: 8px;
}

.main-content {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.video-section {
  background: #000;
  position: relative;
}

.video-player {
  height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1a1a1a;
}

.video-placeholder {
  text-align: center;
  color: white;
}

.video-placeholder p {
  margin: 8px 0;
}

.video-controls {
  background: rgba(0, 0, 0, 0.8);
  padding: 16px 20px;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

.lesson-content-section {
  flex: 1;
  background: white;
  padding: 24px;
  overflow-y: auto;
}

.lesson-description h2 {
  margin: 0 0 16px;
  color: #333;
}

.lesson-description p {
  color: #666;
  line-height: 1.6;
  margin-bottom: 24px;
}

.lesson-materials h3,
.notes-section h3,
.qa-section h3 {
  margin: 0 0 16px;
  color: #333;
}

.notes-actions {
  margin-top: 16px;
  text-align: right;
}

@media (max-width: 768px) {
  .learning-container {
    grid-template-columns: 1fr;
  }
  
  .sidebar {
    display: none;
  }
}
</style>
