<template>
	<view class="page">
		<view class="content-body" v-if="loaded">
			<mp-html :content="content" />
		</view>
		<view v-else class="loading">
			<u-loading-icon />
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { get } from '@/utils/request.js'
import mpHtml from '@/uni_modules/mp-html/components/mp-html/mp-html.vue'

const content = ref('')
const loaded = ref(false)

onLoad((options) => {
	const key = options?.key || ''
	if (key) {
		loadContent(key)
	}
})

const loadContent = async (key) => {
	try {
		const res = await get('/api/content', { key })
		if (res.data) {
			content.value = res.data.content || ''
			uni.setNavigationBarTitle({ title: res.data.title || '' })
		}
	} catch (e) {
		console.error('获取内容失败', e)
	} finally {
		loaded.value = true
	}
}
</script>

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

.content-body {
	padding: 32rpx;
}

.loading {
	display: flex;
	justify-content: center;
	padding: 100rpx 0;
}
</style>
