<template>
	<view class="page">
		<!-- 活动信息卡片 -->
		<view class="activity-card">
			<image class="activity-image" :src="activityInfo.image" mode="aspectFill"></image>
			<view class="activity-info">
				<view class="title-row">
					<view class="activity-badge">
						<text class="badge-text">{{ activityInfo.score }}分</text>
					</view>
					<text class="activity-title">{{ activityInfo.title }}</text>
				</view>
				<view class="activity-location">
					<uv-icon name="map-fill" size="14" color="#999"></uv-icon>
					<text class="location-text">{{ activityInfo.address }}</text>
				</view>
				<view class="activity-time">
					<uv-icon name="calendar" size="14" color="#999"></uv-icon>
					<text class="time-text">{{ activityInfo.activityTime }}</text>
				</view>
				<view class="activity-participants">
					<uv-icon name="account-fill" size="14" color="#999"></uv-icon>
					<text class="participants-text">{{ activityInfo.numActivity }}/{{ activityInfo.numLimit }}人已报名</text>
				</view>
			</view>
		</view>

		<!-- 二维码区域 -->
		<view class="qrcode-container">
			<view class="qrcode-wrapper">
				<!-- <uv-qrcode ref="qrcode" size="300px" value="https://h5.uvui.cn"></uv-qrcode> -->
				<uv-qrcode 
					ref="qrcode"
					:value="qrcodeValue" 
					size="500rpx"
					@complete="onQRCodeComplete"
				></uv-qrcode>
			</view>
		</view>

		<!-- 保存按钮 -->
		<view class="save-container">
			<uv-button 
				type="primary" 
				text="保存到手机" 
				size="large"
				shape="circle"
				@click="saveQRCode"
			></uv-button>
		</view>
	</view>
</template>

<script>
import utils from '@/utils/index'
export default {
	data() {
		return {
			activityInfo: {

			},
			qrcodeValue: 'https://h5.uvui.cn',
			qrCodeReady: false
		}
	},
	async onLoad(options) {
		// 获取传递的参数
		if (options.id) {
			await this.getActiviyDetail(options.id)
		} 

		// 生成签到码
		this.generateQRCode()
	},
	methods: {
		async getActiviyDetail(id) {
			const res = await this.$api.activity.queryActivityById({
				activityId: id
			})
			this.activityInfo = res.result
		},

		// 生成二维码内容
		generateQRCode() {
			// 生成包含活动信息的二维码内容
			const qrData = {
				activityId: this.activityInfo.id,
				activityTitle: this.activityInfo.title,
				type: 'checkin',
				timestamp: Date.now()
			}
			this.qrcodeValue = JSON.stringify(qrData)
		},
		
		// 二维码生成完成事件
		onQRCodeComplete(result) {
			if (result.success) {
				this.qrCodeReady = true
				console.log('二维码生成成功')
			} else {
				console.log('二维码生成失败')
			}
		},
		
		// 保存二维码到手机
		saveQRCode() {
			if (!this.qrCodeReady) {
				uni.showToast({
					title: '二维码还未生成完成',
					icon: 'none'
				})
				return
			}
			
			this.$refs.qrcode.save({
				success: (res) => {
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})
				},
				fail: (err) => {
					console.log('保存失败:', err)
					// 用uniapp获取保存权限
					utils.authoriza({
						scope: 'writePhotosAlbum',
						successfn: () => {
							// 保存二维码
							this.$refs.qrcode.save({
								success: (res) => {
									uni.showToast({
										title: '保存成功',
										icon: 'success'
									})
								},
								fail: (err) => {
									console.log('保存失败:', err)
									uni.showToast({
										title: '保存失败',
										icon: 'none'
									})
								}
							})
						},
						failfn: () => {
							uni.showToast({
								title: '保存失败',
								icon: 'none'
							})
						}
					})
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.page {
	background-color: #f5f5f5;
	min-height: 100vh;
	padding: 20rpx;
}

.activity-card {
	display: flex;
	background: #fff;
	border-radius: 12rpx;
	padding: 20rpx;
	margin-bottom: 40rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.activity-image {
	width: 180rpx;
	height: 180rpx;
	border-radius: 8rpx;
	margin-right: 20rpx;
}

.activity-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.title-row {
	display: flex;
	align-items: center;
	margin-bottom: 10rpx;
}

.activity-badge {
	width: 31px;
	height: 20px;
	background: #218cdd;
	border-radius: 3.5px;
	margin-right: 7rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.badge-text {
	font-size: 18rpx;
	color: #fff;
}

.activity-title {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	line-height: 1.4;
}

.activity-location, .activity-time, .activity-participants {
	display: flex;
	align-items: center;
	margin-bottom: 6rpx;
}

.location-text, .time-text, .participants-text {
	font-size: 24rpx;
	color: #999;
	margin-left: 6rpx;
}

.qrcode-container {
	display: flex;
	justify-content: center;
	margin-bottom: 60rpx;
}

.qrcode-wrapper {
	background: #fff;
	border-radius: 12rpx;
	padding: 40rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.save-container {
	padding: 0 40rpx;
}
</style>