<template>
    <view class="article-view">
        <view v-if="loading" class="article-view__state">
            <text>加载中...</text>
        </view>

        <view v-else-if="errorMsg" class="article-view__state">
            <text>{{ errorMsg }}</text>
        </view>

        <view v-else class="article-view__body">
            <view v-if="title" class="article-view__head">
                <text class="article-view__title">{{ title }}</text>
                <text v-if="createTime" class="article-view__time">{{ formatTime(createTime) }}</text>
            </view>
            <view class="article-view__content">
                <u-parse :html="content" />
            </view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import request from '@/utils/request'

const loading = ref(true)
const errorMsg = ref('')
const title = ref('')
const content = ref('')
const createTime = ref('')

const formatTime = (t: string) => {
    if (!t) return ''
    if (/^\d+$/.test(t)) {
        const d = new Date(Number(t) * 1000)
        return formatDate(d)
    }
    const d = new Date(t.replace(/-/g, '/'))
    if (isNaN(d.getTime())) return t
    return formatDate(d)
}

const formatDate = (d: Date) => {
    const pad = (n: number) => String(n).padStart(2, '0')
    return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}

const fetchData = async (type: string, id: string) => {
    loading.value = true
    errorMsg.value = ''
    try {
        const res: any = await request.get(
            { url: '/content/detail', data: { type, id } },
            { withToken: false }
        )
        title.value = res.title || ''
        content.value = res.content || ''
        createTime.value = res.create_time || ''
        if (title.value) {
            uni.setNavigationBarTitle({ title: title.value })
        }
    } catch (e: any) {
        errorMsg.value = String(e?.msg || e || '加载失败')
    } finally {
        loading.value = false
    }
}

onLoad((options: any) => {
    const type = options?.type || ''
    const id = options?.id || ''
    if (!type) {
        errorMsg.value = '参数错误：缺少 type'
        loading.value = false
        return
    }
    fetchData(type, id)
})
</script>

<style lang="scss" scoped>
.article-view {
    min-height: 100vh;
    background: #fff;
    padding: 32rpx 30rpx 60rpx;
    box-sizing: border-box;
}

.article-view__state {
    padding: 120rpx 0;
    text-align: center;

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

.article-view__head {
    padding-bottom: 24rpx;
    border-bottom: 1rpx solid #f0f0f0;
    margin-bottom: 24rpx;
    display: flex;
    flex-direction: column;
}

.article-view__title {
    font-size: 38rpx;
    font-weight: 700;
    color: #222;
    line-height: 1.5;
    margin-bottom: 16rpx;
}

.article-view__time {
    font-size: 24rpx;
    color: #999;
}

.article-view__content {
    font-size: 30rpx;
    line-height: 1.8;
    color: #333;
}
</style>
