<template>
  <view class="book-container">
    <!-- 自定义顶部导航栏 -->
    <uv-status-bar></uv-status-bar>
    <view class="custom-navbar" :class="{ 'navbar-hidden': !showNavbar }">
      <uv-status-bar></uv-status-bar>
      <view class="navbar-content">
        <view class="navbar-left" @click="goBack">
          <uv-icon name="arrow-left" size="20" color="#262626"></uv-icon>
        </view>
        <view class="navbar-title">{{ currentPageTitle }}</view>

      </view>
    </view>

    <!-- Swiper内容区域 -->
    <swiper 
      class="content-swiper" 
      :current="currentPage - 1"
      :disable-touch="isAudioLoading"
      @change="onSwiperChange"
    >
      <swiper-item 
        v-for="(page, index) in bookPages" 
        :key="index"
        class="swiper-item"
      >
        <view class="content-area" @click="toggleNavbar">
          <!-- 图片卡片页面 -->
          <view v-for="(item, index) in page" :key="index">
            <view v-if="item.type === 'card'" class="card-content">
              <view class="card-line">
                <image src="/static/划重点图标.png" class="card-line-image" mode="aspectFill" />
                <text class="card-line-text">划线重点</text>
              </view>
              <image class="card-image" :src="item.image" mode="aspectFill"></image>
              <text class="english-text" user-select @longpress="showWordMeaning(item.wordMeaning)">{{ item.englishText }}</text>
              <text class="chinese-text" user-select>{{ item.chineseText }}</text>
            </view>
            
            <!-- 文本页面 -->
            <view v-else-if="item.type === 'text'" class="text-content" >
              <view :class="{ 'text-highlight': isTextHighlighted(page, index) }">
                <text 
                class="content-text" 
                user-select
                >
                {{ item.content }}
                </text>
              </view>
            </view>

            <!-- 文本页面 -->
            <view v-else-if="item.type === 'image'" class="image-container">
              <image class="content-image" :src="item.imageUrl" mode="aspectFill"></image>
            </view>

            <!-- 视频页面 -->
            <view v-else-if="item.type === 'video'" class="video-content">
              <video :src="item.video" class="video-player" controls :poster="item.poster"></video>
            </view>

            <!-- 会员限制页面 -->
            <view v-else-if="item.type === 'member'" class="member-content">
              <text class="member-title">{{ item.title }}</text>
              <view class="member-button" @click="unlockBook">
                <text class="member-button-text">{{ item.buttonText }}</text>
              </view>
            </view>
          </view>
        </view>
      </swiper-item>
    </swiper>

    
    <!-- 自定义底部控制栏 -->
    <view class="custom-tabbar" :class="{ 'tabbar-hidden': !showNavbar }">
      <!-- 音频控制栏 -->
      <!-- 获取音频按钮 -->
      <view v-if="!hasAudioData && !isAudioLoading && isTextPage" class="audio-get-button-container">
        <view class="get-audio-btn" @click="handleGetAudio">
          <uv-icon name="play-circle" size="24" color="#06DADC"></uv-icon>
          <text class="get-audio-text">获取第{{currentPage}}页音频</text>
        </view>
      </view>
      
      <!-- 音频加载状态 -->
      <view v-else-if="isAudioLoading && isTextPage" class="audio-loading-container">
        <uv-loading-icon mode="spinner" size="30" color="#06DADC"></uv-loading-icon>
        <text class="loading-text">正在加载第{{currentPage}}页音频...</text>
      </view>
      
      <!-- 正常音频控制栏 -->
      <view v-else-if="hasAudioData" class="audio-controls" :class="{ 'audio-hidden': !isTextPage }">
        <view class="audio-time">
          <text class="time-text">{{ formatTime(currentTime) }}</text>
          <view class="progress-container">
            <view class="progress-bar">
              <view class="progress-fill" :style="{ width: progressPercent + '%' }"></view>
              <view class="progress-thumb" :style="{ left: progressPercent + '%' }"></view>
            </view>
          </view>
          <text class="time-text">{{ formatTime(totalTime) }}</text>
        </view>
        
        <view class="audio-controls-row">
          <view class="control-btn" @click="toggleLoop">
            <uv-icon name="reload" size="20" :color="isLoop ? '#06DADC' : '#999'"></uv-icon>
            <text class="control-text">循环</text>
          </view>
          
          <view class="control-btn" @click="previousPage">
            <text class="control-text">上一页</text>
          </view>
          
          <view class="play-btn" @click="togglePlay">
            <uv-icon :name="isPlaying ? 'pause-circle-fill' : 'play-circle-fill'" size="40" color="#666"></uv-icon>
          </view>
          
          <view class="control-btn" @click="nextPage">
            <text class="control-text">下一页</text>
          </view>
          
          <view class="control-btn" @click="toggleSpeed">
            <text class="control-text">{{ playSpeed }}x</text>
          </view>
        </view>
      </view>
    
      
      <view style="background-color: #fff;position: relative;z-index: 100" >
        <view class="tabbar-content">
          <view class="tabbar-left">
            <view class="tab-button" @click="toggleCoursePopup">
              <image src="/static/课程图标.png" class="tab-icon" />
              <text class="tab-text">课程</text>
            </view>
            <view class="tab-button" @click="toggleSound">
              <image src="/static/音色切换图标.png" class="tab-icon" />
              <text class="tab-text">音色切换</text>
            </view>
          </view>
          
          <view class="tabbar-right">
            <view class="page-controls">
              <view class="page-numbers">
                <view 
                  v-for="(page, index) in bookPages" 
                  :key="index"
                  class="page-number"
                  :class="{ 'active': (index + 1) === currentPage }"
                  @click="goToPage(index + 1)"
                >
                  {{ index + 1 }}
                </view>
              </view>
            </view>
          </view>
        </view>
        <uv-safe-bottom></uv-safe-bottom>
      </view>
    </view>
    
    <!-- 课程选择弹出窗 -->
    <uv-popup 
      mode="bottom" 
      ref="coursePopup"
      round="32rpx"
      bg-color="#f8f8f8"
    >
      <view class="course-popup">
        <view class="popup-header">
          <view>
            <uv-icon name="arrow-down" color="black" size="20"></uv-icon>
          </view>
          <view class="popup-title">课程</view>
          <view class="popup-title" @click="toggleSort">
            倒序
          </view>
        </view>
        <view class="course-list">
          <view 
            v-for="(course, index) in displayCourseList" 
            :key="course.id"
            class="course-item"
            :class="{ 'active': course.id === currentCourse }"
            @click="selectCourse(course.id)"
          >
            <view class="course-number " :class="{ 'highlight': course.id === currentCourse }">{{ String(course.index).padStart(2, '0') }}</view>
            <view class="course-content">
              <view class="course-english" :class="{ 'highlight': course.id === currentCourse }">{{ course.english }}</view>
              <view class="course-chinese" :class="{ 'highlight': course.id === currentCourse }">{{ course.chinese }}</view>
            </view>
          </view>
        </view>
      </view>
    </uv-popup>
    
    <!-- 释义弹出窗 -->
    <uv-popup 
      mode="bottom" 
      ref="meaningPopup"
      round="32rpx"
      bg-color="#FFFFFF"
      :overlay="true"
    >
      <view class="meaning-popup" v-if="currentWordMeaning">
        <view class="meaning-header">
          <view class="close-btn" @click="closeMeaningPopup">
            <text class="close-text">关闭</text>
          </view>
          <view class="meaning-title">释义</view>
          <view style="width: 80rpx;"></view>
        </view>
        
        <view class="meaning-content">
          <image class="meaning-image" src="/static/默认图片.png" mode="aspectFill"></image>
          
          <view class="word-info">
            <view class="word-main">
              <text class="word-text">{{ currentWordMeaning.word }}</text>
            </view>
            <view class="phonetic-container">
                <uv-icon name="volume-fill" size="16" color="#000"></uv-icon>
                <text class="phonetic-text">{{ currentWordMeaning.phonetic }}</text>
            </view>
            <view class="word-meaning">
              <text class="part-of-speech">{{ currentWordMeaning.partOfSpeech }}</text>
              <text class="meaning-text">{{ currentWordMeaning.meaning }}</text>
            </view>
          </view>
          
          <view class="knowledge-gain">
            <view class="knowledge-header">
              <image src="/static/知识收获图标.png" class="knowledge-icon" mode="aspectFill" />
              <text class="knowledge-title">知识收获</text>
            </view>
            <text class="knowledge-content">{{ currentWordMeaning.knowledgeGain }}</text>
          </view>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      voiceId: '',
      courseId: '',
      showNavbar: true,
      currentPage: 1,
      currentCourse: 1, // 当前课程索引
      currentWordMeaning: null, // 当前显示的单词释义
      isReversed: false, // 是否倒序显示
      // 音频控制相关数据
      isPlaying: false,
      currentTime: 0,
      totalTime: 0,
      isLoop: false,
      playSpeed: 1.0,
      speedOptions: [1.0, 1.25, 1.5, 2.0],
      // 音频数组管理
      currentPageAudios: [], // 当前页面的音频数组
      currentAudioIndex: 0, // 当前播放的音频索引
      audioContext: null, // 音频上下文
      currentAudio: null, // 当前音频实例
      // 音频缓存管理
      audioCache: {}, // 页面音频缓存 {pageIndex: {audios: [], totalDuration: 0}}
      // 音频加载状态
      isAudioLoading: false, // 音频是否正在加载
      hasAudioData: false, // 当前页面是否已有音频数据
      // 文本高亮相关
      currentHighlightIndex: -1, // 当前高亮的文本索引
      courseIdList: [],
      bookTitle: '',
      courseList: [
     
      ],

      // 二维数组 代表每个页面
      bookPages: [
        
      ],
      // 存储每个页面的标题
      pageTitles: [],
    }
  },
  computed: {
    displayCourseList() {
      return this.isReversed ? [...this.courseList].reverse() : this.courseList;
    },
    // 判断当前页面是否为文字类型
    isTextPage() {
      const currentPageData = this.bookPages[this.currentPage - 1];
      // currentPageData是一个数组 其中的一个元素的type是text就会返回true
      return currentPageData && currentPageData.some(item => item.type === 'text');
    },
    // 计算音频播放进度百分比
    progressPercent() {
      return this.totalTime > 0 ? (this.currentTime / this.totalTime) * 100 : 0;
    },

    // 动态页面标题
    currentPageTitle() {
      return this.pageTitles[this.currentPage - 1] || this.bookTitle;
    }
  },
  methods: {
    // 获取当前页面的音频内容
    async getCurrentPageAudio() {
      // 检查缓存中是否已有当前页面的音频数据
      const cacheKey = `${this.courseId}_${this.currentPage}`;
      if (this.audioCache[cacheKey]) {
        console.log('从缓存加载音频数据:', cacheKey);
        // 从缓存加载音频数据
        this.currentPageAudios = this.audioCache[cacheKey].audios;
        this.totalTime = this.audioCache[cacheKey].totalDuration;
        this.currentAudioIndex = 0;
        this.isPlaying = false;
        this.currentTime = 0;
        return;
      }
      
      // 开始加载状态
      this.isAudioLoading = true;
      
      // 清空当前页面音频数组
      this.currentPageAudios = [];
      this.currentAudioIndex = 0;
      this.isPlaying = false;
      this.currentTime = 0;
      this.totalTime = 0;
      
      // 对着当前页面的每一个[]元素进行切割 如果是文本text类型则进行音频请求
      const currentPageData = this.bookPages[this.currentPage - 1];
      if (currentPageData) {
        // 收集所有text类型的元素
        const textItems = currentPageData.filter(item => item.type === 'text');
        
        if (textItems.length > 0) {
          // 并行发送所有音频请求
          const audioPromises = textItems.map(async (item, index) => {
            try {
              // 进行音频请求 - 修正字段名：使用content而不是text
              const radioRes = await this.$api.music.textToVoice({
                text: item.content,
                voiceId: this.voiceId,
              });
              console.log(`音频请求响应 ${index + 1}:`, radioRes);
              
              if(radioRes.code === 200){
                // API返回的是Base64编码的WAV音频数据，在uniapp环境中需要特殊处理
                const base64Data = radioRes.result;
                // 在uniapp中，可以直接使用base64数据作为音频源
                const audioUrl = `data:audio/wav;base64,${base64Data}`;
                
                // 同时保存到原始数据中以保持兼容性
                item.audioUrl = audioUrl;
                console.log(`音频URL设置成功 ${index + 1}:`, audioUrl);
                
                return {
                  url: audioUrl,
                  text: item.content,
                  duration: 0, // 音频时长，需要在加载后获取
                  index: index // 保持原始顺序
                };
              } else {
                console.error(`音频请求失败 ${index + 1}:`, radioRes);
                return null;
              }
            } catch (error) {
              console.error(`音频请求异常 ${index + 1}:`, error);
              return null;
            }
          });
          
          // 等待所有音频请求完成
          const audioResults = await Promise.all(audioPromises);
          
          // 按原始顺序添加到音频数组中，过滤掉失败的请求
          this.currentPageAudios = audioResults
            .filter(result => result !== null)
            .sort((a, b) => a.index - b.index)
            .map(result => ({
              url: result.url,
              text: result.text,
              duration: result.duration
            }));
          
          console.log('所有音频请求完成，共获取', this.currentPageAudios.length, '个音频');
        }
        
        // 如果有音频，计算总时长
        if (this.currentPageAudios.length > 0) {
          await this.calculateTotalDuration();
          
          // 将音频数据保存到缓存中
          const cacheKey = `${this.courseId}_${this.currentPage}`;
          this.audioCache[cacheKey] = {
            audios: [...this.currentPageAudios], // 深拷贝音频数组
            totalDuration: this.totalTime
          };
          console.log('音频数据已缓存:', cacheKey, this.audioCache[cacheKey]);
          
          // 限制缓存大小
          this.limitCacheSize(10);
        }
      }
      
      // 结束加载状态
      this.isAudioLoading = false;
      
      // 设置音频数据状态
      this.hasAudioData = this.currentPageAudios.length > 0;
    },
    
    // 重置音频状态
     resetAudioState() {
       // 检查当前页面是否已有缓存的音频数据
       const pageKey = `${this.courseId}_${this.currentPage}`;
       const cachedAudio = this.audioCache[pageKey];
       
       if (cachedAudio && cachedAudio.audios && cachedAudio.audios.length > 0) {
         // 如果有缓存的音频数据，恢复音频状态
         this.currentPageAudios = cachedAudio.audios;
         this.totalTime = cachedAudio.totalDuration || 0;
         this.hasAudioData = true;
       } else {
         // 如果没有缓存的音频数据，重置为初始状态
         this.currentPageAudios = [];
         this.totalTime = 0;
         this.hasAudioData = false;
       }
       
       // 重置播放状态
     this.currentAudioIndex = 0;
     this.isPlaying = false;
     this.currentTime = 0;
     this.isAudioLoading = false;
     this.currentHighlightIndex = -1;
     },
     
     // 手动获取音频
     async handleGetAudio() {
       // 检查是否有音色ID
       if (!this.voiceId) {
         uni.showToast({
           title: '音色未加载，请稍后重试',
           icon: 'none'
         });
         return;
       }
       
       // 检查当前页面是否有文本内容
       if (!this.isTextPage) {
         uni.showToast({
           title: '当前页面没有文本内容',
           icon: 'none'
         });
         return;
       }
       
       // 检查是否正在加载
       if (this.isAudioLoading) {
         return;
       }
       
       // 调用获取音频方法
       await this.getCurrentPageAudio();
     },
    // 获取音色列表 拿第一个做默认的音色id
    async getVoiceList() {
      const voiceRes = await this.$api.music.list()
      if(voiceRes.code === 200){
        this.voiceId = voiceRes.result[0].id
        console.log('获取默认音色ID:', this.voiceId);
      }
    },
    toggleNavbar() {
      this.showNavbar = !this.showNavbar
    },
    goBack() {
      uni.navigateBack()
    },
    toggleCoursePopup() {
      if (this.$refs.coursePopup) {
        this.$refs.coursePopup.open()
      }
      // console.log('123123123');
      
    },
    toggleSort() {
      this.isReversed = !this.isReversed
    },
    selectCourse(courseId) {
      this.currentCourse = courseId
      if (this.$refs.coursePopup) {
        this.$refs.coursePopup.close()
      }
      // 这里可以添加切换课程的逻辑
      // console.log('选择课程:', courseId)
      this.getCourseList(courseId)
    },
    showWordMeaning(wordMeaning) {
      if (wordMeaning) {
        this.currentWordMeaning = wordMeaning
        if (this.$refs.meaningPopup) {
          this.$refs.meaningPopup.open()
        }
      }
    },
    closeMeaningPopup() {
      if (this.$refs.meaningPopup) {
        this.$refs.meaningPopup.close()
      }
      this.currentWordMeaning = null
    },
    // 计算音频总时长
    async calculateTotalDuration() {
      let totalDuration = 0;
      for (let i = 0; i < this.currentPageAudios.length; i++) {
        const audio = this.currentPageAudios[i];
        try {
          const duration = await this.getAudioDuration(audio.url);
          audio.duration = duration;
          totalDuration += duration;
        } catch (error) {
          console.error('获取音频时长失败:', error);
          // 如果无法获取时长，估算一个默认值（假设每100字符约5秒）
          const estimatedDuration = Math.max(5, audio.text.length / 20);
          audio.duration = estimatedDuration;
          totalDuration += estimatedDuration;
        }
      }
      this.totalTime = totalDuration;
      console.log('音频总时长:', totalDuration, '秒');
    },
    
    // 获取音频时长
    getAudioDuration(audioUrl) {
      return new Promise((resolve, reject) => {
        const audio = uni.createInnerAudioContext();
        audio.src = audioUrl;
        
        audio.onCanplay(() => {
          resolve(audio.duration || 5); // 如果无法获取时长，默认5秒
          audio.destroy();
        });
        
        audio.onError((error) => {
          console.error('音频加载失败:', error);
          reject(error);
          audio.destroy();
        });
        
        // 设置超时
        setTimeout(() => {
          reject(new Error('获取音频时长超时'));
          audio.destroy();
        }, 3000);
      });
    },
    
    // 音频控制方法
    togglePlay() {
      if (this.currentPageAudios.length === 0) {
        uni.showToast({
          title: '当前页面没有音频内容',
          icon: 'none'
        });
        return;
      }
      
      if (this.isPlaying) {
        this.pauseAudio();
      } else {
        this.playAudio();
      }
    },
    
    // 播放音频
    playAudio() {
      if (this.currentPageAudios.length === 0) return;
      
      // 如果没有当前音频实例或者需要切换音频
      if (!this.currentAudio || this.currentAudio.src !== this.currentPageAudios[this.currentAudioIndex].url) {
        this.createAudioInstance();
      }
      
      this.currentAudio.play();
      this.isPlaying = true;
      // 更新高亮状态
      this.updateHighlightIndex();
    },
    
    // 暂停音频
    pauseAudio() {
      if (this.currentAudio) {
        this.currentAudio.pause();
      }
      this.isPlaying = false;
      // 暂停时清除高亮
      this.currentHighlightIndex = -1;
    },
    
    // 创建音频实例
    createAudioInstance() {
      // 销毁之前的音频实例
      if (this.currentAudio) {
        this.currentAudio.destroy();
      }
      
      const audio = uni.createInnerAudioContext();
      audio.src = this.currentPageAudios[this.currentAudioIndex].url;
      audio.playbackRate = this.playSpeed;
      
      // 音频事件监听
      audio.onPlay(() => {
        console.log('音频开始播放');
        this.isPlaying = true;
      });
      
      audio.onPause(() => {
        console.log('音频暂停');
        this.isPlaying = false;
      });
      
      audio.onTimeUpdate(() => {
        this.updateCurrentTime();
      });
      
      audio.onEnded(() => {
        console.log('当前音频播放结束');
        this.onAudioEnded();
      });
      
      audio.onError((error) => {
        console.error('音频播放错误:', error);
        this.isPlaying = false;
        uni.showToast({
          title: '音频播放失败',
          icon: 'none'
        });
      });
      
      this.currentAudio = audio;
    },
    
    // 更新当前播放时间
    updateCurrentTime() {
      if (!this.currentAudio) return;
      
      let totalTime = 0;
      // 计算之前音频的总时长
      for (let i = 0; i < this.currentAudioIndex; i++) {
        totalTime += this.currentPageAudios[i].duration;
      }
      // 加上当前音频的播放时间
      totalTime += this.currentAudio.currentTime;
      
      this.currentTime = totalTime;
      
      // 更新当前高亮的文本索引
      this.updateHighlightIndex();
    },
    
    // 更新高亮文本索引
    updateHighlightIndex() {
      if (!this.isPlaying || this.currentPageAudios.length === 0) {
        this.currentHighlightIndex = -1;
        return;
      }
      
      // 根据当前播放的音频索引设置高亮
      this.currentHighlightIndex = this.currentAudioIndex;
      console.log('更新高亮索引:', this.currentHighlightIndex, '当前音频索引:', this.currentAudioIndex);
    },
    
    // 判断当前文本是否应该高亮
    isTextHighlighted(page, index) {
      // 只有当前页面且是文本类型才可能高亮
      if (page !== this.bookPages[this.currentPage - 1]) return false;
      
      // 计算当前页面中text类型元素的索引
      let textIndex = 0;
      for (let i = 0; i <= index; i++) {
        if (page[i].type === 'text') {
          if (i === index) {
            const shouldHighlight = textIndex === this.currentHighlightIndex;
            if (shouldHighlight) {
              console.log('高亮文本:', textIndex, '当前高亮索引:', this.currentHighlightIndex);
            }
            return shouldHighlight;
          }
          textIndex++;
        }
      }
      return false;
    },
    
    // 音频播放结束处理
    onAudioEnded() {
      if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
        // 播放下一个音频
        this.currentAudioIndex++;
        this.playAudio();
      } else {
        // 所有音频播放完毕
        if (this.isLoop) {
          // 循环播放
          this.currentAudioIndex = 0;
          this.playAudio();
        } else {
          // 停止播放
          this.isPlaying = false;
          this.currentTime = this.totalTime;
          this.currentHighlightIndex = -1;
        }
      }
    },
    toggleLoop() {
      this.isLoop = !this.isLoop;
    },
    
    toggleSpeed() {
      const currentIndex = this.speedOptions.indexOf(this.playSpeed);
      const nextIndex = (currentIndex + 1) % this.speedOptions.length;
      this.playSpeed = this.speedOptions[nextIndex];
      
      // 如果当前有音频在播放，更新播放速度
      if (this.currentAudio) {
        this.currentAudio.playbackRate = this.playSpeed;
      }
    },
    
    // 上一个音频
    previousAudio() {
      // 如果正在加载音频，禁止切换
      if (this.isAudioLoading) {
        return;
      }
      
      if (this.currentPageAudios.length === 0) return;
      
      if (this.currentAudioIndex > 0) {
        this.currentAudioIndex--;
        if (this.isPlaying) {
          this.playAudio();
        }
      }
    },
    
    // 下一个音频
    nextAudio() {
      // 如果正在加载音频，禁止切换
      if (this.isAudioLoading) {
        return;
      }
      
      if (this.currentPageAudios.length === 0) return;
      
      if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
        this.currentAudioIndex++;
        if (this.isPlaying) {
          this.playAudio();
        }
      }
    },
    async previousPage() {
        // 如果正在加载音频，禁止翻页
        if (this.isAudioLoading) {
          return;
        }
        
        if (this.currentPage > 1) {
          // 停止当前音频播放
          this.pauseAudio();
          
          this.currentPage--;
          // 获取对应页面的数据（如果还没有获取过）
          if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
            await this.getBookPages(this.courseIdList[this.currentPage - 1]);
          }
          
          // 清空当前音频状态
          this.resetAudioState();
        }
      },
      async nextPage() {
        // 如果正在加载音频，禁止翻页
        if (this.isAudioLoading) {
          return;
        }
        
        if (this.currentPage < this.bookPages.length) {
          // 停止当前音频播放
          this.pauseAudio();
          
          this.currentPage++;
          // 获取对应页面的数据（如果还没有获取过）
          if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
            await this.getBookPages(this.courseIdList[this.currentPage - 1]);
          }
          
          // 清空当前音频状态
          this.resetAudioState();
        }
      },
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60);
      const secs = Math.floor(seconds % 60);
      return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
    },
    toggleSound() {
      console.log('音色切换')
      uni.navigateTo({
        url: '/subPages/home/music'
      })
    },
    unlockBook() {
      console.log('解锁全书')
      // 这里可以跳转到会员页面或者调用解锁接口
      uni.navigateTo({
        url: '/pages/index/member'
      })
    },
    async goToPage(page) {
      // 如果正在加载音频，禁止翻页
      if (this.isAudioLoading) {
        return;
      }
      
      // 停止当前音频播放
      this.pauseAudio();
      
      this.currentPage = page
      console.log('跳转到页面:', page)
      // 获取对应页面的数据（如果还没有获取过）
      if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
        await this.getBookPages(this.courseIdList[this.currentPage - 1]);
      }
      
      // 清空当前音频状态
      this.resetAudioState();
    },
    async onSwiperChange(e) {
      // 如果正在加载音频，禁止翻页
      if (this.isAudioLoading) {
        return;
      }
      
      // 停止当前音频播放
      this.pauseAudio();
      
      this.currentPage = e.detail.current + 1
      // 获取对应页面的数据（如果还没有获取过）
      if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
        await this.getBookPages(this.courseIdList[this.currentPage - 1]);
      }
      
      // 清空当前音频状态
      this.resetAudioState();
    },
    async getCourseList(id) {
      const res = await this.$api.book.coursePage({
        id: id
      })
      if (res.code === 200) {
        this.courseIdList = res.result.map(item => item.id)
        // 初始化二维数组 换一种方式
        this.bookPages = this.courseIdList.map(() => [])
        // 初始化标题数组
        this.pageTitles = this.courseIdList.map(() => '')
        // 重置音频状态
        this.resetAudioState()
        // 初始化第一页
        if (this.courseIdList.length > 0) {
          await this.getBookPages(this.courseIdList[0])
        }
      }
    },
    async getBookPages(id) {
      const res = await this.$api.book.coursesPageDetail({
        id: id
      })
      if (res.code === 200) {
        // 使用$set确保响应式更新
        // 确保当前页面存在
        if (this.currentPage - 1 < this.bookPages.length) {
          this.$set(this.bookPages, this.currentPage - 1, JSON.parse(res.result.content))
          // 保存页面标题
          this.$set(this.pageTitles, this.currentPage - 1, res.result.title || '')
        }
      }
    },
    // 获取课程列表
    async getCoursePageList (bookId) {
      const res = await this.$api.book.course({
        id: bookId
      })
      if (res.code === 200) {
        this.courseList = res.result.records
        // 打上序列号
        this.courseList = this.courseList.map((item, index) => ({
          ...item,
          index,
        }))
      }
    },
    
    // 清理音频缓存
    clearAudioCache() {
      this.audioCache = {};
      console.log('音频缓存已清理');
    },
    
    // 限制缓存大小，保留最近访问的页面
    limitCacheSize(maxSize = 10) {
      const cacheKeys = Object.keys(this.audioCache);
      if (cacheKeys.length > maxSize) {
        // 删除最旧的缓存项
        const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
        keysToDelete.forEach(key => {
          delete this.audioCache[key];
        });
        console.log('缓存大小已限制，删除了', keysToDelete.length, '个缓存项');
      }
    }
  },
  async onLoad(args) {
    this.courseId = args.courseId
    // 重置音频状态
    this.resetAudioState()
    // 先获取点进来的课程的页面列表
    await Promise.all([this.getCourseList(this.courseId), this.getCoursePageList(args.bookId)])
    await this.getVoiceList()
    
  },
  
  // 页面卸载时清理音频资源
  onUnload() {
    if (this.currentAudio) {
      this.currentAudio.destroy();
      this.currentAudio = null;
    }
    this.isPlaying = false;
    // 清理音频缓存
    this.clearAudioCache();
  },
  
  // 页面隐藏时暂停音频
  onHide() {
    this.pauseAudio();
  }
}
</script>

<style lang="scss" scoped>
.book-container {
  width: 100%;
  height: 100vh;
  background-color: #F8F8F8;
  position: relative;
  overflow: hidden;
}

.custom-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: #F8F8F8;
  z-index: 1000;
  transition: transform 0.3s ease;
  
  &.navbar-hidden {
    transform: translateY(-100%);
  }
}

.navbar-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 32rpx;
  // padding-top: calc(20rpx + var(--status-bar-height, 0));
  height: 60rpx;
}

.navbar-left,
.navbar-right {
  width: 80rpx;
  display: flex;
  align-items: center;
}

.navbar-right {
  
  justify-content: flex-end;
  flex: 1;
}

.navbar-title {
  transform: translateX(-50rpx);
  flex: 1;
  text-align: center;
  font-family: PingFang SC;
  font-weight: 500;
  font-size: 32rpx;
  color: #262626;
  line-height: 48rpx;
}

.content-swiper {
  flex: 1;
  height: calc(100vh - 100rpx);
  // margin-top: 100rpx;
  margin-bottom: 100rpx;
}

.swiper-item {
  height: 100%;
}

.content-area {
  flex: 1;
  padding: 0 40rpx;
  padding-top: 100rpx;
  // background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  height: 100%;
  box-sizing: border-box;
  overflow-y: auto;
}

.card-content {
  background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  display: flex;
  flex-direction: column;
  gap: 32rpx;
  height: 1172rpx;
  margin-top: 20rpx;
  border-radius: 32rpx;
    // height: 100%;
    padding: 40rpx;
    // margin: 0 
    border: 1px solid #FFFFFF;
    box-sizing: border-box;
    .card-line {
      display: flex;
      align-items: center;
      // margin-bottom: 20rpx;
    }
  
    .card-line-image {
      width: 48rpx;
      height: 48rpx;
      margin-right: 16rpx;
    }
  
    .card-line-text {
      font-family: PingFang SC;
      font-weight: 600;
      font-size: 30rpx;
      line-height: 48rpx;
      color: #3B3D3D;
    }
  
    .card-image {
      width: 590rpx;
      height: 268rpx;
      border-radius: 24rpx;
      // margin-bottom: 20rpx;
    }
    .english-text {
      display: block;
      font-family: PingFang SC;
      font-weight: 600;
      font-size: 32rpx;
      line-height: 48rpx;
      color: #3B3D3D;
      // margin-bottom: 16rpx;
    }

    .chinese-text {
      display: block;
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 28rpx;
      line-height: 48rpx;
      color: #4F4F4F;
    }
  }

/* 会员限制页面样式 */
.member-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 90%;
  background-color: #F8F8F8;
  padding: 40rpx;
  margin: -40rpx;
  box-sizing: border-box;
}

.member-title {
  font-family: PingFang SC;
  font-weight: 500;
  font-size: 40rpx;
  line-height: 1;
  color: #6f6f6f;
  text-align: center;
  margin-bottom: 48rpx;
}

.member-button {
  width: 670rpx;
  height: 72rpx;
  background: #06DADC;
  border-radius: 200rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.member-button-text {
  font-family: PingFang SC;
  font-weight: 400;
  font-size: 30rpx;
  color: #FFFFFF;
}

.video-content {
  width: 100vw;
  margin: 200rpx -40rpx 0;
  height: 500rpx;
  background-color: #FFFFFF;
  // padding: 40rpx;
  border-radius: 24rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  .video-player{
    width: 670rpx;
    margin: 0 auto;
    height: 376rpx;
  }
}



.text-content {
  width: 100vw;
  background-color: #F6F6F6;
  height: 100%;
  padding: 40rpx;
  margin: -40rpx;
  box-sizing: border-box;
}

.content-text {
  font-family: PingFang SC;
  // font-weight: 400;
  font-size: 28rpx;
  color: #3B3D3D;
  line-height: 48rpx;
  letter-spacing: 0;
  text-align: justify;
  word-break: break-all;
  transition: all 0.3s ease;
}

.text-highlight {
  background-color: $primary-color;
  // color: #fff;
  // padding: 8rpx 16rpx;
  // border-radius: 8rpx;
  // box-shadow: 0 2rpx 8rpx rgba(6, 218, 220, 0.3);
}

.custom-tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  // background-color: #fff;
  border-top: 1rpx solid #EEEEEE;
  z-index: 1000;
  transition: transform 0.3s ease;
  
  &.tabbar-hidden {
    transform: translateY(100%);
  }
}

.tabbar-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24rpx 62rpx;
  // z-index: 100;
  // position: relative;
  // background-color: #fff;
  // padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  height: 88rpx;
}

.tabbar-left {
  display: flex;
  align-items: center;
  gap: 35rpx;
}

.tab-button {
  display: flex;
  align-items: center;
  flex-direction: column;
  gap: 8rpx;
}

.tab-icon {
  width: 52rpx;
  height: 52rpx;
}

.tab-text {
  font-family: PingFang SC;
  // font-weight: 400;
  font-size: 22rpx;
  color: #999;
  line-height: 24rpx;
}

.tabbar-right {
  flex: 1;
  display: flex;
  justify-content: flex-end;
}

.page-controls {
  display: flex;
  align-items: center;
}

.page-numbers {
  display: flex;
  align-items: center;
  gap: 8rpx;
  overflow-x: auto;
  max-width: 400rpx;
  
  &::-webkit-scrollbar {
    display: none;
  }
}

.page-number {
  min-width: 84rpx;
  height: 58rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 100rpx;
  font-family: PingFang SC;
  // font-weight: 400;
  font-size: 30rpx;
  color: #3B3D3D;
  background-color: transparent;
  border: 1px solid #3B3D3D;
  transition: all 0.3s ease;
  
  &.active {
    border: 1px solid $primary-color;
    color: $primary-color;
  }
}

/* 课程弹出窗样式 */
.course-popup {
  padding: 0 32rpx;
  max-height: 80vh;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 2rpx solid #EEEEEE
  // margin-bottom: 40rpx;
}

.popup-title {
  font-family: PingFang SC;
  font-weight: 500;
  font-size: 34rpx;
  color: #181818;
}


.course-list {
  max-height: 60vh;
  overflow-y: auto;
}

.course-item {
  display: flex;
  align-items: center;
  gap: 24rpx;
  padding-top: 24rpx;
  padding-right: 8rpx;
  padding-bottom: 24rpx;
  padding-left: 8rpx;

  border-bottom: 1px solid #EEEEEE;
  cursor: pointer;
  
  &:last-child {
    border-bottom: none;
  }
}

.course-number {
  width: 80rpx;
  font-family: PingFang SC;
  // font-weight: 400;
  font-size: 36rpx;
  color: #999;
  &.highlight {
    color: $primary-color;
  }
  // margin-right: 24rpx;
}

.course-content {
  flex: 1;
}

.course-english {
  font-family: PingFang SC;
  font-weight: 600;
  font-size: 36rpx;
  line-height: 44rpx;
  color: #252545;
  margin-bottom: 8rpx;
  
  &.highlight {
    color: $primary-color;
  }
}

.course-chinese {
  font-size: 28rpx;
  line-height: 48rpx;
  color: #3B3D3D;
  &.highlight {
    color: $primary-color;
  }
}

/* 释义弹窗样式 */
.meaning-popup {
  // width: 670rpx;
  background-color: #FFFFFF;
  // border-radius: 32rpx;
  overflow: hidden;
}

.meaning-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 32rpx;
  border-bottom: 2rpx solid #EEEEEE;
}

.close-btn {
  width: 80rpx;
}

.close-text {
  font-family: PingFang SC;
  font-size: 32rpx;
  color: #8b8b8b;
}

.meaning-title {
  font-family: PingFang SC;
  font-weight: 500;
  font-size: 34rpx;
  color: #181818;
}

.meaning-content {
  padding: 32rpx;
}

.meaning-image {
  width: 670rpx;
  height: 268rpx;
  border-radius: 24rpx;
  margin-bottom: 32rpx;
}

.word-info {
  margin-bottom: 32rpx;
}

.word-main {
  display: flex;
  align-items: center;
  gap: 16rpx;
  margin-bottom: 8rpx;
}

.word-text {
  font-family: PingFang SC;
  font-weight: 500;
  font-size: 40rpx;
  color: #181818;
}

.phonetic-container{
  display: flex;
  gap: 24rpx;
  .phonetic-text {
    font-family: PingFang SC;
    font-size: 28rpx;
    color: #262626;
    margin-bottom: 16rpx;
  }
}

.word-meaning {
  display: flex;
  gap: 16rpx;
}

.part-of-speech {
  font-family: PingFang SC;
  font-size: 28rpx;
  color: #262626;
}

.meaning-text {
  font-family: PingFang SC;
  font-size: 28rpx;
  color: #181818;
  flex: 1;
}

.knowledge-gain {
  background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  border-radius: 24rpx;
  padding: 32rpx 40rpx;
}

.knowledge-header {
  display: flex;
  align-items: center;
  gap: 16rpx;
  margin-bottom: 20rpx;
}

.knowledge-icon {
  width: 48rpx;
  height: 48rpx;
}

.knowledge-title {
  font-family: PingFang SC;
  font-weight: 600;
  font-size: 30rpx;
  color: #3B3D3D;
}

.knowledge-content {
  font-family: PingFang SC;
  font-size: 28rpx;
  color: #4f4f4f;
  line-height: 48rpx;
}

/* 音频控制栏样式 */
.audio-controls {
  background: #fff;
  padding: 20rpx 40rpx;
  border-bottom: 1rpx solid #eee;
  transition: transform 0.3s ease;
  position: relative;
  z-index: 10;
  &.audio-hidden {
    transform: translateY(100%);
    
  }
}

.audio-time {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}

.time-text {
  font-size: 28rpx;
  color: #999;
  min-width: 80rpx;
}

.progress-container {
  flex: 1;
  margin: 0 20rpx;
}

.progress-bar {
  position: relative;
  height: 6rpx;
  background: #f0f0f0;
  border-radius: 3rpx;
}

.progress-fill {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: #06DADC;
  border-radius: 3rpx;
  transition: width 0.1s ease;
}

.progress-thumb {
  position: absolute;
  top: 50%;
  width: 16rpx;
  height: 16rpx;
  background: #06DADC;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: left 0.1s ease;
}

.audio-controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.control-btn {
  display: flex;
  // flex-direction: column;
  align-items: center;

  padding: 10rpx;
  gap: 8rpx;
}

.control-text {
  font-size: 28rpx;
  color: #4A4A4A;
  // margin-top: 8rpx;
}

.play-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10rpx;
}

/* 音频加载状态样式 */
.audio-loading-container {
  background: #fff;
  padding: 40rpx;
  border-bottom: 1rpx solid #eee;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20rpx;
  position: relative;
  z-index: 10;
}

.loading-text {
  font-size: 28rpx;
  color: #999;
}

/* 获取音频按钮样式 */
.audio-get-button-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 30rpx;
  border-radius: 20rpx;
  border: 2rpx solid #E5E5E5;
  transition: all 0.3s ease;
  position: relative;
  z-index: 10;
}

.get-audio-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16rpx;
  padding: 20rpx 40rpx;
  background: linear-gradient(135deg, #06DADC 0%, #04B8BA 100%);
  border-radius: 50rpx;
  box-shadow: 0 8rpx 20rpx rgba(6, 218, 220, 0.3);
  transition: all 0.3s ease;
}

.get-audio-btn:active {
  transform: scale(0.95);
  box-shadow: 0 4rpx 10rpx rgba(6, 218, 220, 0.2);
}

.get-audio-text {
  font-size: 32rpx;
  color: #FFFFFF;
  font-weight: 500;
}
</style>