<template>
    <div class="article-detail-wrapper">
        <div class="main-container">

            <!-- 左侧核心内容区 -->
            <div class="article-left-content">
                <!-- 顶部返回按钮 -->
                <div class="back-btn-box">
                    <div class="back-btn" @click="handleBack">返回列表</div>
                </div>

                <!-- 主文章卡片 -->
                <div class="article-main-card">
                    <!-- 标题 -->
                    <h1 class="article-title">{{ currentArticle.title }}</h1>

                    <!-- 发布日期与浏览量 -->
                    <div class="article-meta">
                        <div class="meta-item">
                            <img class="meta-icon" src="@/public/article/date.png" alt="日期">
                            <span>{{ currentArticle.date }}</span>
                        </div>
                        <div class="meta-item">
                            <img class="meta-icon" src="@/public/article/eye.png" alt="浏览数">
                            <span>{{ currentArticle.views }}</span>
                        </div>
                    </div>

                    <!-- 文章导读区 -->
                    <div class="article-intro-box">
                        <div class="intro-header">
                            <img class="star-icon" src="@/public/article/start.png" />
                            <span>文章导读</span>
                        </div>
                        <p class="intro-content">{{ currentArticle.intro || currentArticle.description }}</p>
                    </div>

                    <!-- 正文区块 -->
                    <div class="article-body-content">
                        <template v-if="currentArticle.sections && currentArticle.sections.length">
                            <div v-for="(sec, idx) in currentArticle.sections" :key="idx" class="section-block">
                                <h3 class="section-title">{{ sec.heading }}</h3>
                                <p class="section-text">{{ sec.content }}</p>
                            </div>
                        </template>
                        <template v-else>
                            <div class="section-block">
                                <h3 class="section-title">一、功能介绍</h3>
                                <p class="section-text">
                                    我们不做“假图”，我们做的是“比真图更真的模拟器”。无论是你需要一张转账截图、一套朋友圈互动记录，还是一整份零钱账单流水，这款模拟器都能帮你瞬间完成。
                                </p>
                            </div>
                        </template>

                        <!-- 文章插图 -->
                        <div class="article-image-box">
                            <img v-if="currentArticle.image" :src="currentArticle.image" alt="文章插图"
                                class="article-img" />
                            <img v-else src="https://picsum.photos/800/320?random=1" alt="演示图" class="article-img" />
                        </div>
                    </div>

                    <!-- 上一篇 / 下一篇 导航卡片 -->
                    <div class="article-nav-section">
                        <div class="nav-card prev-card" @click="handleSwitchArticle(prevArticle)">
                            <div class="nav-arrow left-arrow">
                                <img style="width:100%;height:100%;" src="@/public/article/left.png" alt="">
                            </div>
                            <div class="nav-info">
                                <span class="nav-label">上一篇</span>
                                <span class="nav-title" :title="prevArticle?.title">{{ prevArticle?.title || '没有上一篇了'
                                    }}</span>
                            </div>
                        </div>

                        <div class="nav-card next-card" @click="handleSwitchArticle(nextArticle)">
                            <div class="nav-info align-right">
                                <span class="nav-label">下一篇</span>
                                <span class="nav-title" :title="nextArticle?.title">{{ nextArticle?.title || '没有下一篇了'
                                    }}</span>
                            </div>
                            <div class="nav-arrow right-arrow">
                                <img style="width:100%;height:100%;" src="@/public/article/right.png" alt="">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 右侧相关推荐侧边栏 -->
            <div class="recommend-sidebar">
                <div class="sidebar-card">
                    <div class="sidebar-header">
                        <img src="@/public/article/tuijian.png" class="recommend-icon" />
                        <span class="sidebar-title">相关推荐</span>
                    </div>

                    <div class="recommend-list">
                        <div v-for="(rec, index) in recommendList" :key="rec.id" class="recommend-item"
                            @click="handleSwitchArticle(rec)">
                            <div class="rec-rank" :class="{ 'top-rank': index < 3, 'four-rank': index == 3 }">{{ index +
                                1
                                }}</div>
                            <div class="rec-content">
                                <h4 class="rec-title" :title="rec.title">{{ rec.title }}</h4>
                                <div class="rec-meta">
                                    <div class="meta-sub">
                                        <img class="meta-sub-icon" src="@/public/article/date.png" alt="日期">
                                        <span>{{ rec.date }}</span>
                                    </div>
                                    <div class="meta-sub">
                                        <img class="meta-sub-icon" src="@/public/article/eye.png" alt="浏览">
                                        <span>{{ rec.views }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <!-- 底部警告组件 -->
        <WarningFooter />
    </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import WarningFooter from '@/components/common/WarningFooter.vue'

const router = useRouter()
const route = useRoute()

// 当前文章 ID
const currentId = ref(Number(route.query.id) || 1)

watch(() => route.query.id, (newId) => {
    if (newId) {
        currentId.value = Number(newId)
    }
})

// Mock 完整文章数据库
const articleDatabase = ref([
    {
        id: 1,
        title: '微商模拟神器-节假日活动必备玩法',
        date: '2026-7-20',
        views: '11.5K',
        intro: '我们不做“假图”，我们做的是“比真图更真的模拟器”。无论是你需要一张转账截图、一套朋友圈互动记录，还是一整份零钱账单流水，这款模拟器都能帮你瞬间完成。',
        sections: [
            {
                heading: '一、功能介绍',
                content: '我们不做“假图”，我们做的是“比真图更真的模拟器”。无论是你需要一张转账截图、一套朋友圈互动记录，还是一整份零钱账单流水，这款模拟器都能帮你瞬间完成。'
            }
        ],
        image: 'https://picsum.photos/800/320?random=10'
    },
    {
        id: 2,
        title: '微商模拟神器-节假日活动必备玩法超超超推荐',
        date: '2026-7-20',
        views: '11.5K',
        intro: '全网首创的高逼真恶搞模拟工具，支持自定义头像、对话内容、账单金额与交易状态，各种场景真实不穿帮。',
        sections: [
            {
                heading: '一、核心亮点',
                content: '一键生成高清对话截图，像素级细节还原，支持无限修改，帮助你轻松制作搞笑段子与社交互动图。'
            }
        ],
        image: 'https://picsum.photos/800/320?random=11'
    },
    {
        id: 3,
        title: '微商模拟神器-节假日活动必备玩法超超超推荐HHHHH...',
        date: '2026-7-20',
        views: '11.5K',
        intro: '无论是发朋友圈互动还是朋友间轻松恶搞，装样大师都能提供全套的模拟支持。',
        sections: [
            {
                heading: '一、快速上手',
                content: '无需复杂的操作步骤，选择模板填入文字即可一键导出无水印图片。'
            }
        ],
        image: 'https://picsum.photos/800/320?random=12'
    },
    {
        id: 4,
        title: '微信神器转账支付怎么制作？装样大师新手教程',
        date: '2026-7-20',
        views: '11.5K',
        intro: '新手必看！如何三步快速生成高清逼真的转账与零钱余额截图。',
        sections: [
            {
                heading: '一、教程步骤',
                content: '打开微信模拟功能，输入期望金额与收付款人信息，点击保存按钮导出图片。'
            }
        ],
        image: 'https://picsum.photos/800/320?random=13'
    },
    {
        id: 5,
        title: '工资单与机票模拟生成器全面上线说明',
        date: '2026-7-20',
        views: '11.5K',
        intro: '全新升级娱乐模拟玩法，支持模拟机票、高铁票及个人工资单记录。',
        sections: [
            {
                heading: '一、更新公告',
                content: '优化底纹防伪效果，提升了图片生成的画质与加载速度。'
            }
        ],
        image: 'https://picsum.photos/800/320?random=14'
    }
])

// 匹配当前 ID 对应的文章
const currentArticle = computed(() => {
    return articleDatabase.value.find(item => item.id === currentId.value) || articleDatabase.value[0]
})

// 计算上一篇与下一篇
const currentIndex = computed(() => {
    return articleDatabase.value.findIndex(item => item.id === currentArticle.value.id)
})

const prevArticle = computed(() => {
    if (currentIndex.value > 0) {
        return articleDatabase.value[currentIndex.value - 1]
    }
    return null
})

const nextArticle = computed(() => {
    if (currentIndex.value < articleDatabase.value.length - 1) {
        return articleDatabase.value[currentIndex.value + 1]
    }
    return null
})

// 相关推荐列表（包含 5 条数据）
const recommendList = computed(() => articleDatabase.value)

function handleBack() {
    router.push('/article')
}

function handleSwitchArticle(articleItem) {
    if (!articleItem) return
    router.push({
        path: '/article-detail',
        query: { id: articleItem.id }
    })
}
</script>

<style lang="scss" scoped>
.article-detail-wrapper {
    position: relative;
    min-height: calc(100vh - 112px);
    background-color: #F8F9FB;
    padding: 36px 20px 80px;
    box-sizing: border-box;
}

.main-container {
    width: 800px;
    position: relative;
    margin: 0 auto;
}

/* 左侧主体内容：相对于整个页面水平居中 */
.article-left-content {
    width: 800px;
}

/* 右侧侧边栏：固定在居中主卡片的右侧，且不跟随页面滚动 */
.recommend-sidebar {
    width: 310px;
    position: fixed;
    top: 201px;
    left: calc(50% + 424px);
    /* 正好定位在 800px 居中卡片右侧 24px 处 */
    z-index: 10;
}

@media (max-width: 1480px) {
    .recommend-sidebar {
        left: auto;
        right: 20px;
    }
}

@media (max-width: 1180px) {
    .recommend-sidebar {
        display: none;
        /* 屏幕过窄时隐藏侧边栏避免挡住主卡片 */
    }
}

/* 返回按钮 */
.back-btn-box {
    margin-bottom: 18px;
}

.back-btn {
    width: 116px;
    height: 36px;
    background-image: url('/src/public/article/back-bg.png');
    background-size: 100% 100%;
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: right;
    padding-right: 18px;
    font-weight: 500;
    font-size: 16px;
    color: #FFFFFF;
    line-height: 36px;

    &:hover {
        opacity: 0.92;
        transform: translateY(-1px);
    }
}

/* 主卡片 */
.article-main-card {
    background: #FFFFFF;
    border-radius: 16px;
    padding: 34px 30px 20px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.04);
    box-sizing: border-box;
}

.article-title {
    margin: 0 0 18px 0;
    font-weight: 700;
    font-size: 18px;
    color: #1A1A1A;
    line-height: 20px;
}

.article-meta {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 16px;

    .meta-item {
        height: 20px;
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 18px;
        color: #AAAAAA;
        line-height: 18px;
        text-align: left;

        .meta-icon {
            width: 20px;
            height: 20px;
        }
    }
}

/* 导读区 */
.article-intro-box {
    background-color: #F8F6FF;
    border-radius: 12px;
    padding: 16px 16px 12px;
    margin-bottom: 12px;

    .intro-header {
        display: flex;
        align-items: center;
        font-size: 16px;
        color: #965DF8;
        line-height: 16px;
        font-weight: 700;
        margin-bottom: 6px;

        .star-icon {
            width: 16px;
            height: 16px;
            margin-right: 6px;
        }
    }

    .intro-content {
        margin: 0;
        font-size: 16px;
        color: #767676;
        line-height: 26px;
    }
}

/* 正文 */
.article-body-content {
    .section-block {
        margin-bottom: 30px;
    }

    .section-title {
        font-size: 16px;
        line-height: 26px;
        font-weight: 700;
        color: #1A1A1A;
    }

    .section-text {
        font-size: 16px;
        color: #1A1A1A;
        line-height: 26px;
    }

    .article-image-box {
        width: 100%;
        height: 320px;
        border-radius: 4px;
        overflow: hidden;
        margin-bottom: 30px;

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

/* 上一篇/下一篇 导航卡片 */
.article-nav-section {
    display: flex;
    gap: 20px;
    margin-top: 32px;

    .nav-card {
        flex: 1;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        padding: 8px 16px;
        border-radius: 12px;
        cursor: pointer;
        transition: all 0.2s ease;
        box-sizing: border-box;
        background: #FFFFFF;
        border: 1px solid #EAEAEA;

        &.prev-card {
            &:hover {
                background: #F6F3FD;
            }
        }

        &.next-card {
            &:hover {
                background: #F0EBFF;
            }
        }

        .nav-arrow {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: rgba(139, 112, 255, 0.12);
            color: #8B70FF;
            display: flex;
            align-items: center;
            justify-content: center;

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

        .nav-info {
            display: flex;
            flex-direction: column;
            flex: 1;
            min-width: 0;
            padding: 0 16px;

            &.align-right {
                text-align: right;
            }

            .nav-label {
                font-size: 16px;
                font-weight: 700;
                color: #1A1A1A;
            }

            .nav-title {
                font-size: 12px;
                color: #767676;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }
    }
}

/* 右侧侧边栏 */
.recommend-sidebar {
    width: 230px;
    flex-shrink: 0;
}

.sidebar-card {
    background: #FFFFFF;
    border-radius: 18px;
    padding: 0;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.04);
    box-sizing: border-box;
}

.sidebar-header {
    position: relative;
    height: 46px;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 12px;
    color: #8B70FF;

    &::after {
        content: '';
        position: absolute;
        width: 100%;
        left: 0;
        bottom: 0;
        height: 1px;
        background-color: #D8D8D8;
        transform: scaleY(0.5);
    }

    .recommend-icon {
        width: 22px;
        height: 22px;
    }

    .sidebar-title {
        font-weight: 700;
        font-size: 16px;
        color: #1A1A1A;
        line-height: 16px;
    }
}

.recommend-list {
    display: flex;
    flex-direction: column;
}

.recommend-item {
    height: 86px;
    padding-left: 18px;
    display: flex;
    gap: 8px;
    align-items: center;
    cursor: pointer;

    &:last-child {
        border-bottom: none;
        padding-bottom: 0;
        margin-bottom: 0;
    }

    &:hover {
        .rec-title {
            color: #8B70FF;
        }
    }

    .rec-rank {
        font-size: 18px;
        font-weight: 700;
        color: #D8D8D8;
        width: 18px;

        &.top-rank {
            color: #965DF8;
        }

        &.four-rank {
            color: #AAAAAA;
        }
    }

    .rec-content {
        display: flex;
        justify-content: center;
        height: 100%;
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        border-bottom: 1px solid #F0F0F0;
        padding-right: 10px;
        gap: 12px;
    }

    .rec-title {
        margin: 0;
        font-size: 14px;
        font-weight: 500;
        color: #1A1A1A;
        line-height: 1.4;
        overflow: hidden;
        text-overflow: ellipsis;
        transition: color 0.2s;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .rec-meta {
        display: flex;
        align-items: center;
        gap: 12px;

        .meta-sub {
            display: flex;
            align-items: center;
            gap: 4px;
            font-size: 12px;
            color: #AAAAAA;

            .meta-sub-icon {
                width: 12px;
                height: 12px;
            }
        }
    }
}

@media (max-width: 960px) {
    .main-container {
        flex-direction: column;
    }

    .recommend-sidebar {
        width: 100%;
    }
}
</style>