<template>
  <div class="lsd-page container-height" v-if="!isChildRoute">
    <!-- ===== HERO SECTION ===== -->
    <section class="lsd-hero">
      <!-- Background layers -->
      <div class="lsd-hero-bg" aria-hidden="true">
        <div class="lsd-orb lsd-orb-1"></div>
        <div class="lsd-orb lsd-orb-2"></div>
        <div class="lsd-orb lsd-orb-3"></div>
        <div class="lsd-grid-overlay"></div>
        
        <!-- Blurred cover image background -->
        <div v-if="info.coverImageUrl" class="lsd-cover-blur" :style="{ backgroundImage: `url(${info.coverImageUrl})` }">
          <div class="lsd-cover-overlay"></div>
        </div>
      </div>

      <!-- Wave animation -->
      <canvas ref="waveCanvasRef" class="lsd-hero__wave-canvas"></canvas>

      <div class="container lsd-hero-inner">
        <!-- Breadcrumb -->
        <div class="lsd-breadcrumb-wrap">
          <Breadcrumb />
        </div>

        <!-- Hero content -->
        <div class="lsd-hero-content">
          <div class="lsd-hero-left">
            <div class="lsd-hero-badge">
              <span class="lsd-badge-dot"></span>
              <span>{{ $t('learningSystem.heroBadge') }}</span>
              <span class="lsd-badge-sep">|</span>
              <span class="lsd-badge-sub">{{ $t('learningSystem.courseDetail') }}</span>
            </div>

            <h1 class="lsd-hero-title">{{ info.courseTitle }}</h1>

            <p class="lsd-hero-desc">
              {{ info.courseIntro }}
            </p>

            <div class="lsd-tags-row">
              <span v-if="info.skillTagName" class="lsd-tag lsd-tag-primary">
                <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>
                {{ info.skillTagName }}
              </span>
              <span v-if="info.studyStageName" class="lsd-tag lsd-tag-stage">
                {{ info.studyStageName }}
              </span>
              <span v-if="info.courseCategoryName" class="lsd-tag lsd-tag-category">
                {{ info.courseCategoryName }}
              </span>
            </div>
          </div>

          <div class="lsd-hero-right">
            <div class="lsd-cover-card">
              <img :src="info.coverImageUrl" :alt="info.courseTitle" class="lsd-cover-img" />
              <!-- <div class="lsd-cover-overlay">
                <button class="lsd-play-btn" @click="goDetail(info)">
                  <svg width="24" height="24" viewBox="0 0 24 24" fill="white"><path d="M8 5v14l11-7z"/></svg>
                  <span>{{ $t('common.lijixuexi') }}</span>
                </button>
              </div> -->
            </div>
          </div>
        </div>

        <!-- Price & CTA Bar -->
        <div class="lsd-price-bar">
          <div class="lsd-price-left">
            <!-- <div class="lsd-price-label">{{ $t('learningSystem.coursePrice') }}</div> -->
            <div class="lsd-price-value">
              <span class="lsd-price-num">{{ info.coursePrice }}</span>
              <span class="lsd-price-unit">{{ $t('common.baomibi') }}</span>
            </div>
          </div>
          <div class="lsd-price-right">
            <button class="lsd-cta-btn" @click="goDetail(info)">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="white"><path d="M8 5v14l11-7z"/></svg>
              <span>{{ $t('common.lijixuexi') }}</span>
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="9 18 15 12 9 6"/></svg>
            </button>
          </div>
        </div>
      </div>
    </section>

    <!-- ===== MAIN CONTENT ===== -->
    <div class="container lsd-body">
      <div class="lsd-layout">

        <!-- Left: Tabs Content -->
        <div class="lsd-main-col">
          <div class="lsd-card lsd-tabs-card">
            <el-tabs v-model="activeName" class="lsd-tabs">
              <el-tab-pane :label="$t('common.kechengjieshao')" name="first">
                <CourseDescription :info="info" />
              </el-tab-pane>
              <el-tab-pane :label="$t('common.kechengmulu')" name="kechengmulu">
                <keep-alive>
                  <CourseDirectory 
                    :info="info" 
                    formPage="LearningSystemDetail" 
                    :chapterId="info.listTimeChapterId"
                  />
                </keep-alive>
              </el-tab-pane>
              <el-tab-pane :label="$t('common.pinglun')" name="pinglun">
                <keep-alive>
                  <Pinglun :info="info" />
                </keep-alive>
              </el-tab-pane>
              <el-tab-pane :label="$t('common.xuxibiji')" name="xuxibiji">
                <keep-alive>
                  <Xuxibiji :info="info" />
                </keep-alive>
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>

        <!-- Right: Sidebar -->
        <div class="lsd-sidebar">
          <div class="lsd-sidebar-sticky">
            <div class="lsd-sidebar-card">
              <!-- <div class="lsd-sidebar-header">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>
                <span>{{ $t('learningSystem.recommendedCourses') }}</span>
              </div> -->
              <OtherCourse :info="info"/>
            </div>
          </div>
        </div>

      </div>
    </div>

  </div>
    <router-view></router-view>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAppStore } from '@/pinia/appStore'
import { useI18n } from 'vue-i18n'

import Breadcrumb from '@/components/Breadcrumb.vue'
import OtherCourse from './components/OtherCourse.vue'
import CourseDescription from './components/CourseDescription.vue'
import CourseDirectory from './components/CourseDirectory.vue'
import Pinglun from './components/pinglun.vue'
import Xuxibiji from './components/xuxibiji.vue'

import { getCourseDetail } from '@/api/course.js'
import { confirmBuy } from '@/utils/util.js'

const router = useRouter()
const route = useRoute()
const appStore = useAppStore()
const { t } = useI18n()

const isChildRoute = computed(() => route.matched.length > 2)
const activeName = ref('first')
const courseId = ref(route.params.courseId || '')
const info = ref({})
const waveCanvasRef = ref(null)

let waveAnimId = null

function initWaveCanvas() {
  const canvas = waveCanvasRef.value
  if (!canvas) return
  const ctx = canvas.getContext('2d')
  let W = 0, H = 0
  
  function resize() {
    W = canvas.width = canvas.offsetWidth
    H = canvas.height = canvas.offsetHeight
  }
  
  resize()
  window.addEventListener('resize', resize)

  const waves = [
    { amp: 40, freq: 0.004, speed: 0.0012, phase: 0,   color: 'rgba(99,102,241,0.28)' },
    { amp: 28, freq: 0.006, speed: 0.0025, phase: 2.1, color: 'rgba(167,139,250,0.22)' },
    { amp: 18, freq: 0.008, speed: 0.004,  phase: 4.3, color: 'rgba(255,255,255,1)' },
  ]

  function draw() {
    ctx.clearRect(0, 0, W, H)
    waves.forEach(w => {
      w.phase += w.speed
      ctx.beginPath()
      ctx.moveTo(0, H)
      for (let x = 0; x <= W; x += 2) {
        const y = H * 0.60 + Math.sin(x * w.freq + w.phase) * w.amp
        ctx.lineTo(x, y)
      }
      ctx.lineTo(W, H)
      ctx.closePath()
      ctx.fillStyle = w.color
      ctx.fill()
    })
    waveAnimId = requestAnimationFrame(draw)
  }
  
  draw()
}

function destroyWaveCanvas() {
  if (waveAnimId) {
    cancelAnimationFrame(waveAnimId)
    waveAnimId = null
  }
}

watch(() => route.params.courseId, (newVal, oldVal) => {
  courseId.value = newVal || ''
  if (newVal !== oldVal) {
    getDetail()
  }
})

onMounted(() => {
  getDetail()
  initWaveCanvas()
})

onUnmounted(() => {
  destroyWaveCanvas()
})

const getDetail = async () => {
  getCourseDetail({ id: courseId.value }).then(res => {
    if (res.code === 200) {
      info.value = res.data || {}
    }
  })
}

const goDetail = (item) => {
  if (item.buyFlag == 0) {
    confirmBuy({
      callback: getDetail,
      appStore,
      router,
      price: item.coursePrice,
      t,
      productId: item.courseId,
      orderType: 'course_purchase',
      payMethod: 'BMI'
    })
    return
  }

  router.push({
    path: `/learning-system/detail/${item.courseId}/course/${item.courseId}`,
    query: {
      courseId: item.courseId,
      chapterId: item.listTimeChapterId || '',
      metaTitle: '课程详情'
    }
  })
}
</script>

<style scoped lang="scss">
.lsd-page {
  min-height: 100vh;
  background: #f8faff;
}

.container{
  min-width: auto;
  padding: 20px;
}

/* ===== HERO SECTION ===== */
.lsd-hero {
  position: relative;
  overflow: hidden;
  background: linear-gradient(160deg, #eef2ff 0%, #ede9fe 28%, #e8e4ff 52%, #f3e8ff 72%, #fce7f3 100%);
  padding-bottom: 80px;
}

.lsd-hero-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;

  .lsd-grid-overlay {
    position: absolute;
    inset: 0;
    background-image: radial-gradient(circle, rgba(99,102,241,0.05) 1px, transparent 1px);
    background-size: 36px 36px;
    opacity: 0.45;
  }

  .lsd-orb {
    position: absolute;
    border-radius: 50%;
    filter: blur(80px);
    opacity: 0.6;
    
    &-1 {
      width: 500px;
      height: 500px;
      background: rgba(99, 102, 241, 0.35);
      top: -150px;
      left: -100px;
      animation: floatOrb 12s ease-in-out infinite;
    }
    
    &-2 {
      width: 400px;
      height: 400px;
      background: rgba(167, 139, 250, 0.30);
      top: 50%;
      right: -80px;
      animation: floatOrb 15s ease-in-out infinite reverse;
    }
    
    &-3 {
      width: 300px;
      height: 300px;
      background: rgba(236, 72, 153, 0.25);
      bottom: -100px;
      left: 40%;
      animation: floatOrb 10s ease-in-out infinite 2s;
    }
  }

  .lsd-cover-blur {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center;
    filter: blur(80px);
    opacity: 0.35;

    .lsd-cover-overlay {
      position: absolute;
      inset: 0;
      background: linear-gradient(135deg, rgba(238, 242, 255, 0.6), rgba(248, 232, 255, 0.6));
    }
  }
}

@keyframes floatOrb {
  0%, 100% { transform: translate(0, 0); }
  33% { transform: translate(30px, -20px); }
  66% { transform: translate(-20px, 20px); }
}

.lsd-hero__wave-canvas {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 120px;
  pointer-events: none;
}

.lsd-hero-inner {
  position: relative;
  z-index: 2;
  padding-top: 24px;
}

.lsd-breadcrumb-wrap {
  margin-bottom: 32px;
}

.lsd-hero-content {
  display: grid;
  grid-template-columns: 1fr 420px;
  gap: 48px;
  align-items: center;
  margin-bottom: 32px;
}

.lsd-hero-left {
  .lsd-hero-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(12px);
    border-radius: 100px;
    font-size: 13px;
    color: #4f46e5;
    margin-bottom: 24px;
    box-shadow: 0 4px 16px rgba(79, 70, 229, 0.08);

    .lsd-badge-dot {
      width: 8px;
      height: 8px;
      background: linear-gradient(135deg, #22c55e, #16a34a);
      border-radius: 50%;
      animation: pulse 2s infinite;
    }

    .lsd-badge-sep {
      color: #d1d5db;
    }

    .lsd-badge-sub {
      color: #6b7280;
    }
  }

  .lsd-hero-title {
    font-size: clamp(28px, 4vw, 42px);
    font-weight: 800;
    line-height: 1.2;
    color: #111827;
    margin-bottom: 20px;
    letter-spacing: -0.02em;
  }

  .lsd-hero-desc {
    font-size: 16px;
    line-height: 1.7;
    color: #4b5563;
    max-width: 520px;
    margin-bottom: 24px;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .lsd-tags-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
  }

  .lsd-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 12px;
    font-size: 13px;
    font-weight: 600;
    transition: all 0.25s ease;

    svg {
      width: 12px;
      height: 12px;
    }

    &-primary {
      background: linear-gradient(135deg, #4f46e5, #7c3aed);
      color: white;
      box-shadow: 0 4px 16px rgba(79, 70, 229, 0.25);

      &:hover {
        transform: translateY(-1px);
        box-shadow: 0 6px 20px rgba(79, 70, 229, 0.35);
      }
    }

    &-stage {
      background: rgba(99, 102, 241, 0.1);
      color: #4f46e5;
      border: 1px solid rgba(99, 102, 241, 0.2);

      &:hover {
        background: rgba(99, 102, 241, 0.15);
      }
    }

    &-category {
      background: rgba(124, 58, 237, 0.1);
      color: #7c3aed;
      border: 1px solid rgba(124, 58, 237, 0.2);

      &:hover {
        background: rgba(124, 58, 237, 0.15);
      }
    }
  }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.lsd-hero-right {
  .lsd-cover-card {
    position: relative;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(79, 70, 229, 0.25);
    aspect-ratio: 16/10;
    background: #000;

    .lsd-cover-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .lsd-cover-overlay {
      position: absolute;
      inset: 0;
      background: rgba(0, 0, 0, 0.3);
      display: flex;
      align-items: center;
      justify-content: center;
      transition: background 0.3s;

      &:hover {
        background: rgba(0, 0, 0, 0.45);
      }
    }

    .lsd-play-btn {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 18px 32px;
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(12px);
      border-radius: 100px;
      border: none;
      cursor: pointer;
      color: #4f46e5;
      font-weight: 700;
      font-size: 15px;
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
      box-shadow: 0 8px 32px rgba(79, 70, 229, 0.3);

      &:hover {
        transform: scale(1.05);
        box-shadow: 0 12px 40px rgba(79, 70, 229, 0.4);
      }
    }
  }
}

/* Price Bar */
.lsd-price-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  padding: 24px 28px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(79, 70, 229, 0.08);

  .lsd-price-left {
    .lsd-price-label {
      font-size: 13px;
      color: #6b7280;
      margin-bottom: 6px;
      text-transform: uppercase;
      letter-spacing: 0.05em;
      font-weight: 600;
    }

    .lsd-price-value {
      display: flex;
      align-items: baseline;
      gap: 8px;

      .lsd-price-num {
        font-size: clamp(32px, 4vw, 42px);
        font-weight: 800;
        color: #dc2626;
        letter-spacing: -0.02em;
      }

      .lsd-price-unit {
        font-size: 16px;
        color: #6b7280;
        font-weight: 500;
      }
    }
  }

  .lsd-price-right {
    .lsd-cta-btn {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 16px 36px;
      background: linear-gradient(135deg, #4f46e5, #7c3aed);
      border: none;
      border-radius: 14px;
      cursor: pointer;
      color: white;
      font-weight: 700;
      font-size: 16px;
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
      box-shadow: 0 8px 24px rgba(79, 70, 229, 0.35);

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 12px 32px rgba(79, 70, 229, 0.45);
      }

      svg:first-child {
        color: white;
      }

      svg:last-child {
        margin-left: 4px;
      }
    }
  }
}

/* ===== MAIN BODY ===== */
.lsd-body {
  position: relative;
  z-index: 3;
  margin-top: -40px;
  padding-bottom: 60px;
}

.lsd-layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 28px;
  align-items: start;
}

/* ===== CARDS ===== */
.lsd-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(79, 70, 229, 0.08);
  overflow: hidden;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 12px 44px rgba(79, 70, 229, 0.14);
    transform: translateY(-2px);
  }
}

.lsd-tabs-card {
  padding: 8px;

  :deep(.el-tabs__header) {
    margin: 0;
    padding: 0 20px;
    background: linear-gradient(135deg, #f9fafb, #f3f4f6);
    border-bottom: 1px solid #e5e7eb;
    border-radius: 16px 16px 0 0;
  }

  :deep(.el-tabs__nav-wrap::after) {
    display: none;
  }

  :deep(.el-tabs__item) {
    padding: 16px 24px;
    font-size: 15px;
    font-weight: 600;
    color: #6b7280;
    transition: all 0.25s ease;

    &:hover {
      color: #4f46e5;
    }

    &.is-active {
      color: #4f46e5;
    }
  }

  :deep(.el-tabs__active-bar) {
    background: linear-gradient(90deg, #4f46e5, #7c3aed);
    height: 3px;
    border-radius: 3px;
  }

  :deep(.el-tab-pane) {
    padding: 28px;
  }
}

/* ===== SIDEBAR ===== */
.lsd-sidebar {
  position: relative;
}

.lsd-sidebar-sticky {
  position: sticky;
  top: 24px;
}

.lsd-sidebar-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(79, 70, 229, 0.08);
  overflow: hidden;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 12px 44px rgba(79, 70, 229, 0.14);
  }

  .lsd-sidebar-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 20px;
    background: linear-gradient(135deg, #f9fafb, #f3f4f6);
    border-bottom: 1px solid #e5e7eb;
    font-size: 15px;
    font-weight: 600;
    color: #374151;

    svg {
      color: #4f46e5;
    }
  }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 1200px) {
  .lsd-layout {
    grid-template-columns: 1fr 340px;
    gap: 24px;
  }

  .lsd-hero-content {
    grid-template-columns: 1fr 360px;
    gap: 36px;
  }
}

@media (max-width: 1024px) {
  .lsd-layout {
    grid-template-columns: 1fr;
  }

  .lsd-sidebar {
    order: -1;
  }

  .lsd-sidebar-sticky {
    position: static;
  }

  .lsd-hero-content {
    grid-template-columns: 1fr;
    text-align: center;
  }

  .lsd-hero-left {
    .lsd-hero-desc {
      max-width: 100%;
    }

    .lsd-tags-row {
      justify-content: center;
    }
  }

  .lsd-hero-right {
    max-width: 480px;
    margin: 0 auto;
  }

  .lsd-price-bar {
    flex-direction: column;
    text-align: center;
  }
}

@media (max-width: 768px) {
  .lsd-hero {
    padding-bottom: 60px;
  }

  .lsd-breadcrumb-wrap {
    margin-bottom: 20px;
  }

  .lsd-hero-left {
    .lsd-hero-title {
      font-size: 26px;
    }

    .lsd-hero-desc {
      font-size: 15px;
    }

    .lsd-tags-row {
      justify-content: center;
    }
  }

  .lsd-body {
    margin-top: -24px;
  }

  .lsd-card {
    border-radius: 16px;
  }

  .lsd-sidebar-card {
    border-radius: 16px;
  }

  .lsd-price-bar {
    padding: 20px;
    border-radius: 16px;
  }

  .lsd-cta-btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .lsd-hero-inner {
    padding-top: 16px;
  }

  .lsd-hero-left {
    .lsd-hero-badge {
      font-size: 12px;
      padding: 6px 12px;
    }

    .lsd-hero-title {
      font-size: 22px;
    }

    .lsd-tag {
      font-size: 12px;
      padding: 6px 12px;
    }
  }

  .lsd-price-left {
    .lsd-price-num {
      font-size: 28px;
    }
  }

  .lsd-cta-btn {
    padding: 14px 28px;
    font-size: 14px;
  }
}
</style>