<template>
  <div class="course-directory">
    <div class="section-header">
      <div class="header-line"></div>
      <h3 class="section-title">{{$t('common.kechengmulu')}} ({{selectedItemIndex + 1}}/{{list.length}})</h3>
    </div>
    
    <div class="chapter-list">
      <div 
        class="chapter-item" 
        :class="{ 'active': selectedItem.chapterId === item.chapterId }"
        @click="handleCheck(item)"
        v-for="(item, index) in list" 
        :key="index"
      >
        <div class="chapter-content">
          <div class="chapter-order">{{item.chapterOrderName}}</div>
          
          <div class="chapter-info">
            <img :src="muluIcon" alt="" class="chapter-icon" v-show="selectedItem.chapterId === item.chapterId">
            <span class="chapter-name">{{item.chapterName}}</span>
          </div>
        </div>
        
        <div class="chapter-duration">
          {{item.chapterTimeConvert}}
        </div>
        
        <div class="chapter-indicator" v-if="selectedItem.chapterId === item.chapterId"></div>
      </div>
    </div>
    
    <div v-if="list.length === 0" class="empty-state">
      <el-empty :image-size="120" :description="$t('common.noData')" />
    </div>
  </div>
</template>
<script setup>
import muluIcon from '@/assets/imgs/mulu.png'
import { getChaptersList } from '@/api/course.js'
import { ref, computed, onMounted, watch, onActivated } from 'vue'
import { useRouter } from 'vue-router'
const emit = defineEmits(['purchaseFn']);
const router = useRouter();
const props = defineProps({
  info: {
    type: Object,
    default: () => ({})
  },
  formPage: {
    type: String,
    default: ''
  },
  chapterId: {
    type: String,
    default: ''
  }
})
const selectedItem = ref({});//选中的章节

const selectedItemIndex = computed(() => {
  return list.value.findIndex(item => item.chapterId === selectedItem.value.chapterId);
});
watch(() => props.info.courseId, (newVal, oldVal) => {
  if(newVal !== oldVal){
    getChaptersListFn();
  }
})

onActivated(() => {
 if(props.info.courseId){
    getChaptersListFn();
  }
})

const list = ref([]);

const getChaptersListFn = () => {
   getChaptersList({id: props.info.courseId}).then(res => {
    if(res.code === 200){
      list.value = res.rows || [];
      if(list.value.length > 0){
        if(props.chapterId){
          selectedItem.value = list.value.find(item => item.chapterId == props.chapterId) || {};
        }else{
          selectedItem.value = list.value[0];
        }
        purchaseFn(selectedItem.value.chapterId, selectedItem.value);
      }
    }
  })
};


const handleCheck = (item) => {
  selectedItem.value = item;
  if(props.formPage === 'LearningSystemDetail'){
    //增加参数名称
    router.push({
      path: `/learning-system/detail/${item.courseId}/course/${item.courseId}`,
      query: {
        courseId: item.courseId,
        chapterId: item.chapterId,
        metaTitle: '课程详情'
      }
    })
  }else{
    purchaseFn(selectedItem.value.chapterId, selectedItem.value);
  }
}

const purchaseFn = (chapterId, chapterInfo={}) => {
  emit('purchaseFn', chapterId, chapterInfo);
}



</script>
<style scoped lang="scss">
.course-directory {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  padding: 24px;
  box-shadow: 
    0 8px 32px rgba(79, 70, 229, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  
  .section-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 24px;
    
    .header-line {
      width: 4px;
      height: 24px;
      background: linear-gradient(135deg, #4f46e5, #7c3aed);
      border-radius: 2px;
      box-shadow: 0 2px 8px rgba(79, 70, 229, 0.3);
    }
    
    .section-title {
      font-size: 18px;
      font-weight: 700;
      color: #1f2937;
      margin: 0;
      letter-spacing: -0.02em;
    }
  }
  
  .chapter-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  
  .chapter-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 18px;
    cursor: pointer;
    position: relative;
    border-radius: 12px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid transparent;
    
    &:not(.active) {
      &:hover {
        background: rgba(79, 70, 229, 0.04);
        transform: translateX(4px);
        
        .chapter-name {
          color: #4f46e5;
        }
      }
      
      &::after {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 3px;
        height: 0;
        background: linear-gradient(135deg, #4f46e5, #7c3aed);
        border-radius: 0 2px 2px 0;
        transition: height 0.3s ease;
      }
      
      &:hover::after {
        height: 60%;
      }
    }
    
    &.active {
      background: linear-gradient(135deg, rgba(79, 70, 229, 0.08), rgba(124, 58, 237, 0.08));
      border-color: rgba(79, 70, 229, 0.15);
      box-shadow: 
        0 4px 16px rgba(79, 70, 229, 0.12),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
      
      .chapter-order {
        color: #4f46e5;
        font-weight: 600;
      }
      
      .chapter-name {
        color: #4f46e5;
        font-weight: 600;
      }
      
      .chapter-duration {
        color: #4f46e5;
        font-weight: 500;
      }
      
      .chapter-indicator {
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 4px;
        height: 60%;
        background: linear-gradient(180deg, #4f46e5, #7c3aed);
        border-radius: 0 3px 3px 0;
        animation: pulse-glow 2s ease-in-out infinite;
      }
    }
    
    .chapter-content {
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
      gap: 6px;
      
      .chapter-order {
        font-size: 13px;
        font-weight: 500;
        color: #6b7280;
        transition: all 0.3s ease;
      }
      
      .chapter-info {
        display: flex;
        align-items: center;
        gap: 8px;
        
        .chapter-icon {
          width: 18px;
          height: 18px;
          object-fit: contain;
          filter: drop-shadow(0 2px 4px rgba(79, 70, 229, 0.3));
        }
        
        .chapter-name {
          font-size: 15px;
          font-weight: 500;
          color: #374151;
          transition: all 0.3s ease;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
    
    .chapter-duration {
      font-size: 13px;
      color: #9ca3af;
      font-weight: 400;
      margin-left: 16px;
      flex-shrink: 0;
      transition: all 0.3s ease;
    }
  }
  
  @keyframes pulse-glow {
    0%, 100% {
      opacity: 1;
      box-shadow: 0 0 8px rgba(79, 70, 229, 0.6);
    }
    50% {
      opacity: 0.8;
      box-shadow: 0 0 16px rgba(124, 58, 237, 0.8);
    }
  }
  
  .empty-state {
    padding: 40px 20px;
    text-align: center;
  }
}

@media (max-width: 768px) {
  .course-directory {
    padding: 20px;
    border-radius: 16px;
    
    .section-header {
      margin-bottom: 20px;
      
      .header-line {
        height: 20px;
      }
      
      .section-title {
        font-size: 16px;
      }
    }
    
    .chapter-list {
      gap: 2px;
    }
    
    .chapter-item {
      padding: 14px 16px;
      border-radius: 10px;
      
      .chapter-content {
        .chapter-order {
          font-size: 12px;
        }
        
        .chapter-info {
          .chapter-icon {
            width: 16px;
            height: 16px;
          }
          
          .chapter-name {
            font-size: 14px;
          }
        }
      }
      
      .chapter-duration {
        font-size: 12px;
        margin-left: 12px;
      }
    }
  }
}

@media (max-width: 480px) {
  .course-directory {
    padding: 16px;
    border-radius: 14px;
    
    .section-header {
      margin-bottom: 16px;
      
      .section-title {
        font-size: 15px;
      }
    }
    
    .chapter-item {
      padding: 12px 14px;
      flex-direction: column;
      align-items: flex-start;
      gap: 8px;
      
      .chapter-duration {
        margin-left: 0;
        font-size: 11px;
      }
    }
  }
}
</style>
