<template>
  <div class="prepare-navigation">
    <!-- 课程包概览 -->
    <div class="nav-section">
      <div
        class="nav-title"
        :class="{ active: selectedSection === 'overview' }"
        @click="handleSelectSection('overview')"
      >
        <ClipboardList :size="18" :stroke-width="2" class="title-icon" />
        课程包概览
      </div>
      <div v-if="selectedSection === 'overview'" class="nav-items">
        <div
          class="nav-item"
          :class="{ active: selectedItem === 'basic' }"
          @click.stop="handleSelectItem('basic')"
        >
          <span class="item-dot"></span>
          基本信息
        </div>
        <div
          class="nav-item"
          :class="{ active: selectedItem === 'intro' }"
          @click.stop="handleSelectItem('intro')"
        >
          <span class="item-dot"></span>
          课程介绍
        </div>
        <div
          class="nav-item"
          :class="{ active: selectedItem === 'schedule' }"
          @click.stop="handleSelectItem('schedule')"
        >
          <span class="item-dot"></span>
          排课计划参考
        </div>
        <div
          class="nav-item"
          :class="{ active: selectedItem === 'environment' }"
          @click.stop="handleSelectItem('environment')"
        >
          <span class="item-dot"></span>
          环创建设
        </div>
      </div>
    </div>

    <!-- 包含课程 -->
    <div class="nav-section">
      <div class="nav-title">
        <BookOpen :size="18" :stroke-width="2" class="title-icon" />
        包含课程 ({{ lessons.length }})
      </div>

      <div class="nav-items">
        <!-- 导入课 -->
        <div
          v-if="introductionLesson"
          class="nav-lesson"
          :class="{ active: selectedLessonId === introductionLesson.id }"
          @click="handleSelectLesson(introductionLesson)"
        >
          <div class="lesson-header">
            <BookOpen :size="16" :stroke-width="2" class="lesson-icon" />
            <span class="lesson-name">导入课</span>
            <span class="lesson-time">{{ introductionLesson.duration }}分钟</span>
          </div>
          <div v-if="selectedLessonId === introductionLesson.id" class="lesson-items">
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'resources' }"
              @click.stop="handleSelectItem('resources')"
            >
              <FolderOutlined /> 核心资源
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'objectives' }"
              @click.stop="handleSelectItem('objectives')"
            >
              <AimOutlined /> 教学目标
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'preparation' }"
              @click.stop="handleSelectItem('preparation')"
            >
              <ToolOutlined /> 教学准备
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'steps' }"
              @click.stop="handleSelectItem('steps')"
            >
              <OrderedListOutlined /> 教学过程 ({{ introductionLesson.steps?.length || 0 }})
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'reflection' }"
              @click.stop="handleSelectItem('reflection')"
            >
              <FileTextOutlined /> 教学反思
            </div>
          </div>
        </div>

        <!-- 集体课 -->
        <div
          v-if="collectiveLesson"
          class="nav-lesson"
          :class="{ active: selectedLessonId === collectiveLesson.id }"
          @click="handleSelectLesson(collectiveLesson)"
        >
          <div class="lesson-header">
            <Users :size="16" :stroke-width="2" class="lesson-icon" />
            <span class="lesson-name">集体课</span>
            <span class="lesson-time">{{ collectiveLesson.duration }}分钟</span>
          </div>
          <div v-if="selectedLessonId === collectiveLesson.id" class="lesson-items">
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'resources' }"
              @click.stop="handleSelectItem('resources')"
            >
              <FolderOutlined /> 核心资源
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'objectives' }"
              @click.stop="handleSelectItem('objectives')"
            >
              <AimOutlined /> 教学目标
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'preparation' }"
              @click.stop="handleSelectItem('preparation')"
            >
              <ToolOutlined /> 教学准备
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'steps' }"
              @click.stop="handleSelectItem('steps')"
            >
              <OrderedListOutlined /> 教学过程 ({{ collectiveLesson.steps?.length || 0 }})
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'extension' }"
              @click.stop="handleSelectItem('extension')"
            >
              <BranchesOutlined /> 教学延伸
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'reflection' }"
              @click.stop="handleSelectItem('reflection')"
            >
              <FileTextOutlined /> 教学反思
            </div>
          </div>
        </div>

        <!-- 五大领域课 -->
        <div
          v-for="lesson in domainLessons"
          :key="lesson.id"
          class="nav-lesson"
          :class="{ active: selectedLessonId === lesson.id }"
          @click="handleSelectLesson(lesson)"
        >
          <div class="lesson-header">
            <component :is="getLessonIcon(lesson.lessonType)" :size="16" :stroke-width="2" class="lesson-icon" />
            <span class="lesson-name">{{ getLessonTypeName(lesson.lessonType) }}</span>
            <span class="lesson-time">{{ lesson.duration }}分钟</span>
          </div>
          <div v-if="selectedLessonId === lesson.id" class="lesson-items">
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'resources' }"
              @click.stop="handleSelectItem('resources')"
            >
              <FolderOutlined /> 核心资源
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'objectives' }"
              @click.stop="handleSelectItem('objectives')"
            >
              <AimOutlined /> 教学目标
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'preparation' }"
              @click.stop="handleSelectItem('preparation')"
            >
              <ToolOutlined /> 教学准备
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'steps' }"
              @click.stop="handleSelectItem('steps')"
            >
              <OrderedListOutlined /> 教学过程 ({{ lesson.steps?.length || 0 }})
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'extension' }"
              @click.stop="handleSelectItem('extension')"
            >
              <BranchesOutlined /> 教学延伸
            </div>
            <div
              class="nav-item"
              :class="{ active: selectedItem === 'reflection' }"
              @click.stop="handleSelectItem('reflection')"
            >
              <FileTextOutlined /> 教学反思
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 备课笔记区域 -->
    <div class="notes-section">
      <div class="section-header">
        <FileEdit :size="18" :stroke-width="2" class="header-icon" />
        我的备课笔记
        <a-button type="link" size="small" @click="saveNotes" class="save-btn">
          <SaveOutlined /> 保存
        </a-button>
      </div>
      <div class="notes-body">
        <a-textarea
          v-model:value="myNotes"
          placeholder="在这里记录您的备课笔记、教学心得或需要特别注意的事项..."
          :auto-size="{ minRows: 6, maxRows: 10 }"
          class="notes-textarea"
        />
      </div>
      <div class="notes-footer">
        <a-button size="small" @click="clearNotes">
          <ClearOutlined /> 清除
        </a-button>
        <a-button size="small" @click="printNotes">
          <PrinterOutlined /> 打印素材清单
        </a-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import {
  AimOutlined, ToolOutlined, OrderedListOutlined, FileTextOutlined,
  FolderOutlined, BranchesOutlined, SaveOutlined, ClearOutlined,
  PrinterOutlined,
} from '@ant-design/icons-vue';
import {
  ClipboardList,
  BookOpen,
  FileEdit,
  Users,
  MessageCircle,
  Activity,
  Microscope,
  Handshake,
  Palette,
  Layers,
} from 'lucide-vue-next';
import { message } from 'ant-design-vue';

const props = defineProps<{
  course: any;
  lessons: any[];
  selectedSection: 'overview' | 'lesson';
  selectedLessonId: number | string | null;
  selectedItem: string;
}>();

const emit = defineEmits<{
  'select-section': [section: 'overview' | 'lesson'];
  'select-lesson': [lesson: any];
  'select-item': [item: string];
}>();

// 按类型分组课程
const introductionLesson = computed(() =>
  props.lessons?.find((l: any) => l.lessonType === 'INTRO')
);

const collectiveLesson = computed(() =>
  props.lessons?.find((l: any) => l.lessonType === 'COLLECTIVE')
);

const domainLessons = computed(() =>
  props.lessons?.filter((l: any) =>
    ['DOMAIN_LANGUAGE', 'DOMAIN_HEALTH', 'DOMAIN_SCIENCE', 'DOMAIN_SOCIAL', 'DOMAIN_ART'].includes(l.lessonType)
  ) || []
);

// 备课笔记
const myNotes = ref('');
const courseId = computed(() => props.course?.id);

// 保存/加载笔记
watch(() => props.course?.id, (newId) => {
  if (newId) {
    const saved = localStorage.getItem(`notes_${newId}`);
    if (saved) {
      myNotes.value = saved;
    }
  }
}, { immediate: true });

const saveNotes = () => {
  if (courseId.value) {
    localStorage.setItem(`notes_${courseId.value}`, myNotes.value);
    message.success('笔记已保存');
  }
  emit('select-item', 'notes');
};

const clearNotes = () => {
  myNotes.value = '';
  message.info('笔记已清除');
};

const printNotes = () => {
  message.info('打印功能开发中');
};

const handleSelectSection = (section: 'overview' | 'lesson') => {
  emit('select-section', section);
};

const handleSelectLesson = (lesson: any) => {
  emit('select-lesson', lesson);
};

const handleSelectItem = (item: string) => {
  emit('select-item', item);
};

const getLessonIcon = (type: string): any => {
  const iconMap: Record<string, any> = {
    'INTRO': BookOpen,
    'COLLECTIVE': Users,
    'DOMAIN_LANGUAGE': MessageCircle,
    'DOMAIN_HEALTH': Activity,
    'DOMAIN_SCIENCE': Microscope,
    'DOMAIN_SOCIAL': Handshake,
    'DOMAIN_ART': Palette,
  };
  return iconMap[type] || Layers;
};

const getLessonTypeName = (type: string): string => {
  const typeMap: Record<string, string> = {
    'INTRO': '导入课',
    'COLLECTIVE': '集体课',
    'DOMAIN_LANGUAGE': '语言领域课',
    'DOMAIN_HEALTH': '健康领域课',
    'DOMAIN_SCIENCE': '科学领域课',
    'DOMAIN_SOCIAL': '社会领域课',
    'DOMAIN_ART': '艺术领域课',
  };
  return typeMap[type] || type;
};
</script>

<style scoped>
.prepare-navigation {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.nav-section {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  overflow: hidden;
}

.nav-title {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  font-size: 15px;
  font-weight: 600;
  color: #333;
  cursor: pointer;
  transition: all 0.2s;
  border-bottom: 1px solid #f0f0f0;
}

.nav-title:hover {
  background: #F5F5F5;
}

.nav-title.active {
  background: linear-gradient(90deg, #FFF5EB 0%, #FFF 100%);
  color: #FF8C42;
}

.title-icon {
  color: #FF8C42;
  flex-shrink: 0;
}

.nav-items {
  padding: 8px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px 10px 28px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
}

.nav-item::before {
  content: '';
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #d9d9d9;
  transition: all 0.2s;
}

.nav-item:hover {
  background: #F5F5F5;
  color: #333;
}

.nav-item.active {
  background: linear-gradient(90deg, #FFF5EB 0%, #FFF 100%);
  color: #FF8C42;
  font-weight: 500;
}

.nav-item.active::before {
  background: #FF8C42;
}

.nav-lesson {
  margin-bottom: 8px;
}

.nav-lesson:last-child {
  margin-bottom: 0;
}

.lesson-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  cursor: pointer;
  transition: all 0.2s;
  border-bottom: 1px solid #f0f0f0;
}

.lesson-header:hover {
  background: #F9F9F9;
}

.lesson-header.active {
  background: linear-gradient(90deg, #FFF5EB 0%, #FFF 100%);
  color: #FF8C42;
  border-left: 3px solid #FF8C42;
}

.lesson-icon {
  color: #FF8C42;
  flex-shrink: 0;
}

.lesson-name {
  flex: 1;
}

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

.lesson-items {
  background: #FAFAFA;
}

.nav-item.lesson-item {
  padding-left: 40px;
  font-size: 13px;
}

/* 备课笔记区域 */
.notes-section {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  overflow: hidden;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  border-bottom: 1px solid #f0f0f0;
}

.header-icon {
  color: #FF8C42;
  flex-shrink: 0;
}

.save-btn {
  margin-left: auto;
  color: #FF8C42;
}

.notes-body {
  padding: 16px;
}

.notes-textarea {
  border: 1px solid #E8E8E8;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.6;
}

.notes-textarea:focus {
  border-color: #FF8C42;
  box-shadow: 0 0 0 2px rgba(255, 140, 66, 0.1);
}

.notes-footer {
  display: flex;
  justify-content: space-between;
  padding: 8px 16px;
  border-top: 1px solid #f0f0f0;
  background: #FAFAFA;
}
</style>
