<template>
  <section class="page source-detail-page" v-loading="loading">
    <template v-if="project">
      <div class="detail-hero">
        <el-button class="back-button" :icon="Back" plain @click="router.push('/source-store')">返回源码库</el-button>
        <div class="hero-main">
          <div class="hero-cover">{{ project.coverText }}</div>
          <div class="hero-copy">
            <div class="hero-title-row">
              <div>
                <p class="eyebrow">源码详情</p>
                <h1>{{ project.name }}</h1>
              </div>
              <strong>{{ project.price }}</strong>
            </div>
            <p class="muted">{{ project.summary }}</p>
            <div class="tag-row">
              <el-tag v-for="tag in project.tags" :key="tag" effect="plain">{{ tag }}</el-tag>
            </div>
            <div class="hero-actions">
              <el-button
                :icon="project.purchased ? Download : ShoppingCart"
                size="large"
                type="primary"
                @click="project.purchased ? handleDownload() : handlePurchase()"
              >
                {{ project.purchased ? '下载源码' : '购买源码' }}
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <div class="detail-layout">
        <article class="markdown-panel">
          <div class="panel-header">
            <h2 class="panel-title">项目描述</h2>
          </div>
          <div class="markdown-body" v-html="descriptionHtml"></div>
        </article>

        <aside class="summary-panel">
          <section>
            <h2>项目信息</h2>
            <dl class="info-list">
              <div>
                <dt>浏览量</dt>
                <dd>{{ formatViews(project.views) }}</dd>
              </div>
              <div>
                <dt>更新时间</dt>
                <dd>{{ project.updateTime }}</dd>
              </div>
            </dl>
          </section>

          <section>
            <h2>功能模块</h2>
            <div class="pill-list">
              <span v-for="module in project.modules" :key="module">{{ module }}</span>
            </div>
          </section>

          <section>
            <h2>适用场景</h2>
            <div class="pill-list quiet">
              <span v-for="scene in project.scenes" :key="scene">{{ scene }}</span>
            </div>
          </section>
        </aside>
      </div>
    </template>

    <el-empty v-else-if="!loading" description="未找到源码项目">
      <el-button type="primary" @click="router.push('/source-store')">返回源码库</el-button>
    </el-empty>
  </section>
</template>

<script setup>
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Back, Download, ShoppingCart } from '@element-plus/icons-vue'
import { getSourceDownloadInfo, getSourceProject, purchaseSourceProject } from '@/api/source'
import { getToken } from '@/utils/auth'
import { renderMarkdown } from '@/utils/markdown'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const project = ref(null)
const descriptionHtml = computed(() => renderMarkdown(project.value?.descriptionMd || ''))

onMounted(loadProject)

async function loadProject() {
  loading.value = true
  try {
    project.value = await getSourceProject(route.params.sourceId)
  } finally {
    loading.value = false
  }
}

function formatViews(views) {
  return `${Number(views || 0).toLocaleString('zh-CN')} 人浏览`
}

async function handlePurchase() {
  if (!getToken()) {
    ElMessage.warning('请先登录后购买源码')
    router.push({ path: '/login', query: { redirect: route.fullPath } })
    return
  }
  await purchaseSourceProject(project.value.projectId)
  ElMessage.success('购买成功，源码资源已加入你的账户')
  await loadProject()
}

async function handleDownload() {
  if (!getToken()) {
    ElMessage.warning('请先登录后下载源码')
    router.push({ path: '/login', query: { redirect: route.fullPath } })
    return
  }
  const info = await getSourceDownloadInfo(project.value.projectId)
  if (info?.resourceUrl) {
    window.open(info.resourceUrl, '_blank')
    return
  }
  ElMessage.warning('源码资源暂未配置')
}
</script>

<style scoped lang="scss">
.source-detail-page {
  display: grid;
  gap: 18px;
}

.detail-hero,
.markdown-panel,
.summary-panel {
  border: 1px solid #d9e0ea;
  border-radius: 8px;
  background: #ffffff;
}

.detail-hero {
  display: grid;
  gap: 18px;
  padding: 22px;
}

.back-button {
  justify-self: start;
}

.hero-main {
  display: grid;
  grid-template-columns: 156px minmax(0, 1fr);
  gap: 22px;
  align-items: stretch;
}

.hero-cover {
  display: grid;
  min-height: 156px;
  place-items: center;
  border-radius: 8px;
  color: #1e3a8a;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.18), rgba(20, 184, 166, 0.16)),
    #f8fafc;
  font-size: 28px;
  font-weight: 800;
}

.hero-copy {
  display: grid;
  gap: 12px;
  align-content: start;

  .eyebrow {
    margin: 0 0 6px;
    color: #1d4ed8;
    font-weight: 700;
  }

  h1 {
    margin: 0;
    color: #101828;
    font-size: 30px;
    line-height: 1.2;
    letter-spacing: 0;
  }

  .muted {
    max-width: 760px;
    margin: 0;
    font-size: 15px;
    line-height: 1.7;
  }
}

.hero-title-row {
  display: flex;
  justify-content: space-between;
  gap: 18px;

  strong {
    color: #dc2626;
    font-size: 22px;
    white-space: nowrap;
  }
}

.tag-row,
.hero-actions,
.pill-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 18px;
  align-items: start;
}

.markdown-panel {
  overflow: hidden;
}

.panel-header {
  padding: 18px 22px;
  border-bottom: 1px solid #e4e7ed;
}

.panel-title {
  margin: 0;
  color: #101828;
  font-size: 18px;
}

.markdown-body {
  padding: 24px;
  color: #344054;
  font-size: 15px;
  line-height: 1.8;
}

.markdown-body :deep(h1),
.markdown-body :deep(h2),
.markdown-body :deep(h3),
.markdown-body :deep(h4) {
  margin: 24px 0 12px;
  color: #101828;
  letter-spacing: 0;
}

.markdown-body :deep(h1:first-child),
.markdown-body :deep(h2:first-child) {
  margin-top: 0;
}

.markdown-body :deep(h1) {
  font-size: 26px;
}

.markdown-body :deep(h2) {
  font-size: 20px;
}

.markdown-body :deep(p),
.markdown-body :deep(ul),
.markdown-body :deep(blockquote),
.markdown-body :deep(table),
.markdown-body :deep(pre) {
  margin: 0 0 16px;
}

.markdown-body :deep(ul) {
  padding-left: 22px;
}

.markdown-body :deep(pre) {
  overflow: auto;
  padding: 14px;
  border-radius: 8px;
  color: #dbeafe;
  background: #172033;
  font-size: 13px;
}

.markdown-body :deep(code) {
  border-radius: 6px;
  color: #1d4ed8;
  background: #eff6ff;
  font-size: 13px;
}

.markdown-body :deep(pre code) {
  color: inherit;
  background: transparent;
}

.markdown-body :deep(table) {
  width: 100%;
  border-collapse: collapse;
}

.markdown-body :deep(th),
.markdown-body :deep(td) {
  padding: 10px 12px;
  border: 1px solid #e4e7ed;
  text-align: left;
}

.markdown-body :deep(blockquote) {
  padding: 10px 14px;
  border-left: 4px solid #3b82f6;
  color: #475467;
  background: #eff6ff;
}

.summary-panel {
  display: grid;
  gap: 22px;
  padding: 20px;

  h2 {
    margin: 0 0 12px;
    color: #101828;
    font-size: 17px;
  }
}

.info-list {
  margin: 0;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;

  div {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    padding: 12px;
    border-bottom: 1px solid #e4e7ed;
  }

  div:last-child {
    border-bottom: 0;
  }

  dt,
  dd {
    margin: 0;
  }

  dt {
    color: #667085;
  }

  dd {
    color: #101828;
    font-weight: 700;
  }
}

.pill-list span {
  padding: 6px 10px;
  border-radius: 999px;
  color: #1d4ed8;
  background: #eff6ff;
  font-size: 13px;
  font-weight: 700;
}

.pill-list.quiet span {
  color: #344054;
  background: #f2f4f7;
}

@media (max-width: 920px) {
  .hero-main,
  .detail-layout {
    grid-template-columns: 1fr;
  }

  .hero-cover {
    min-height: 132px;
  }

  .hero-title-row {
    flex-direction: column;
  }
}
</style>
