<template>
	<view class="detail-page">
		<!-- 加载中状态 -->
		<view v-if="loading" class="loading">
			<view class="loading-icon"></view>
			<text>加载中...</text>
		</view>
		
		<!-- PDF 预览 -->
		<view v-else-if="fileType === 'pdf' && fileUrl" class="pdf-wrapper">
			<web-view 
				:src="fileUrl"
				class="web-view"
				@load="onWebViewLoad"
				@error="onWebViewError"
			></web-view>
		</view>

		<!-- 图片预览 -->
		 
		<scroll-view v-else-if="(fileType === 'png' || fileType === 'jpg' || fileType === 'jpeg') && fileUrl" scroll-y class="image-scroll">
			<image class="contract-image" :src="fileUrl" mode="widthFix" @click="previewImage" />
		</scroll-view>

		<!-- 空状态 -->
		<view v-else class="empty">
			<text class="empty-text">{{ !fileUrl ? '暂无合同文件' : '文件加载失败' }}</text>
			<view v-if="fileUrl" class="empty-tip">请检查文件链接是否有效</view>
			<view @click="goBack">返回</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			fileType: '',
			fileUrl: '',
			loading: true,
			webViewError: false,
		}
	},
	onLoad(query) {
		console.log('detail page query:', query) // 调试日志
		
		this.fileType = query.fileType || 'pdf'
		this.fileUrl = query.fileUrl ? decodeURIComponent(query.fileUrl) : ''
		
		// 验证 URL 是否有效
		if (this.fileUrl && !this.isValidUrl(this.fileUrl)) {
			console.error('无效的文件URL:', this.fileUrl)
			this.fileUrl = ''
		}
		
		// 延迟显示，避免闪烁
		setTimeout(() => {
			this.loading = false
		}, 500)
		
		if (query.title) {
			uni.setNavigationBarTitle({
				title: decodeURIComponent(query.title),
			})
		}
	},
	methods: {
		isValidUrl(string) {
			try {
				const url = new URL(string)
				return url.protocol === 'http:' || url.protocol === 'https:'
			} catch (_) {
				return false
			}
		},
		
		previewImage() {
			if (!this.fileUrl) return
			uni.previewImage({
				urls: [this.fileUrl],
				current: this.fileUrl,
			})
		},
		
		onWebViewLoad() {
			console.log('web-view 加载成功')
		},
		
		onWebViewError(e) {
			console.error('web-view 加载失败:', e)
			this.webViewError = true
			this.loading = false
		},
		
		goBack() {
			uni.navigateBack({
				fail: () => {
					uni.switchTab({ url: '/pages/index/index' })
				}
			})
		},
	},
}
</script>

<style lang="scss" scoped>
::v-deep #toolbar{
	display: hidden!important;
}
.detail-page {
	min-height: 100vh;
	height: 100vh;
	background: #f5f5f5;
	position: relative;
	overflow: hidden;
}

.loading {
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	text-align: center;
	z-index: 100;
	background: rgba(255,255,255,0.9);
	padding: 40rpx 60rpx;
	border-radius: 16rpx;
}

.loading-icon {
	width: 60rpx;
	height: 60rpx;
	border: 4rpx solid #e0e0e0;
	border-top-color: #07c160;
	border-radius: 50%;
	animation: spin 1s linear infinite;
	margin: 0 auto 20rpx;
}

@keyframes spin {
	to { transform: rotate(360deg); }
}

.pdf-wrapper {
	width: 100%;
	height: 100vh;
	position: relative;
}

.web-view {
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
}

.image-scroll {
	height: 100vh;
}

.contract-image {
	width: 100%;
	display: block;
}

.empty {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-height: 60vh;
	padding: 120rpx 40rpx;
}

.empty-text {
	font-size: 32rpx;
	color: #666;
	margin-bottom: 16rpx;
}

.empty-tip {
	font-size: 26rpx;
	color: #999;
	margin-bottom: 48rpx;
}

.back-btn {
	background: #07c160;
	color: #fff;
	padding: 24rpx 80rpx;
	border-radius: 44rpx;
	font-size: 30rpx;
}
</style>