<template>
	<!-- 客服二维码弹窗 -->
	<view v-if="showModal" class="qrcode-modal-mask" @click="closeModal">
		<view class="qrcode-modal-content" @click.stop>
			<view class="qrcode-modal-header">
				<text class="qrcode-modal-title">联系客服</text>
				<view class="qrcode-modal-close" @click="closeModal">
					<uni-icons type="close" size="24" color="#999"></uni-icons>
				</view>
			</view>
			<view class="qrcode-modal-body">
				<image 
					v-if="serviceQrcodeUrl" 
					:src="serviceQrcodeUrl" 
					mode="aspectFit" 
					class="qrcode-modal-img"
					:show-menu-by-longpress="true"
					@longpress="onQrcodeLongPress"
				></image>
				<view v-else class="qrcode-placeholder">
					<text>二维码加载中...</text>
				</view>
				<text class="qrcode-modal-tip">长按识别二维码添加客服微信</text>
				<!-- <view class="qrcode-actions">
					<button class="save-btn" @click="saveQrcodeToAlbum" v-if="serviceQrcodeUrl">
						保存到相册
					</button>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'ServiceQrcode',
	data() {
		return {
			showModal: false
		}
	},
	computed: {
		serviceQrcodeUrl() {
			const item = getApp().globalData.configData.find(i => i.keyName === 'kefu_code')
			return item ? item.keyContent : ''
		}
	},
	methods: {
		// 打开客服二维码弹窗
		open() {
			this.showModal = true
		},
		
		// 关闭客服二维码弹窗
		close() {
			this.showModal = false
			this.$emit('close')
		},
		
		// 关闭二维码弹窗（内部调用）
		closeModal() {
			this.close()
		},
		
		// 处理二维码长按事件
		onQrcodeLongPress() {
			console.log('长按二维码')
			// 在微信小程序中，show-menu-by-longpress="true" 会自动处理长按识别
			// 这里可以添加一些反馈提示
			uni.showToast({
				title: '长按识别二维码',
				icon: 'none',
				duration: 1500
			})
		},
		
		// 保存二维码到相册
		saveQrcodeToAlbum() {
			if (!this.serviceQrcodeUrl) {
				uni.showToast({
					title: '二维码还未加载完成',
					icon: 'none'
				})
				return
			}
			
			// 先授权相册权限
			uni.getSetting({
				success: (res) => {
					if (!res.authSetting['scope.writePhotosAlbum']) {
						// 没有权限，申请权限
						uni.authorize({
							scope: 'scope.writePhotosAlbum',
							success: () => {
								this.doSaveQrcodeImage()
							},
							fail: () => {
								// 权限被拒绝，引导用户手动开启
								uni.showModal({
									title: '提示',
									content: '需要您授权保存相册权限',
									showCancel: false,
									success: () => {
										uni.openSetting()
									}
								})
							}
						})
					} else {
						// 已有权限，直接保存
						this.doSaveQrcodeImage()
					}
				}
			})
		},
		
		// 执行保存二维码图片
		doSaveQrcodeImage() {
			uni.downloadFile({
				url: this.serviceQrcodeUrl,
				success: (res) => {
					if (res.statusCode === 200) {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: () => {
								uni.showToast({
									title: '保存成功',
									icon: 'success'
								})
							},
							fail: (err) => {
								console.log('保存失败', err)
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								})
							}
						})
					}
				},
				fail: (err) => {
					console.log('下载失败', err)
					uni.showToast({
						title: '下载失败',
						icon: 'none'
					})
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
	// 客服二维码弹窗样式
	.qrcode-modal-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 9999;
		backdrop-filter: blur(5rpx);
	}

	.qrcode-modal-content {
		background: #fff;
		border-radius: 24rpx;
		width: 600rpx;
		max-width: 90vw;
		animation: fadeInScale 0.3s ease;
		overflow: hidden;
	}

	.qrcode-modal-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx 40rpx 20rpx 40rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.qrcode-modal-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.qrcode-modal-close {
		padding: 10rpx;
		margin: -10rpx;
	}

	.qrcode-modal-body {
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.qrcode-modal-img {
		width: 400rpx;
		height: 400rpx;
		border-radius: 16rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.qrcode-placeholder {
		width: 400rpx;
		height: 400rpx;
		border-radius: 16rpx;
		margin-bottom: 30rpx;
		background: #f5f5f5;
		display: flex;
		justify-content: center;
		align-items: center;
		
		text {
			color: #999;
			font-size: 28rpx;
		}
	}

	.qrcode-modal-tip {
		font-size: 28rpx;
		color: #666;
		text-align: center;
		line-height: 1.4;
		margin-bottom: 20rpx;
	}

	.qrcode-actions {
		display: flex;
		justify-content: center;
		margin-top: 20rpx;
	}

	.save-btn {
		background: linear-gradient(90deg, #ff8917, #ffd01e);
		color: #fff;
		border: none;
		border-radius: 25rpx;
		padding: 16rpx 40rpx;
		font-size: 28rpx;
		font-weight: bold;
		
		&::after {
			border: none;
		}
		
		&:active {
			opacity: 0.8;
		}
	}

	@keyframes fadeInScale {
		from {
			opacity: 0;
			transform: scale(0.8);
		}

		to {
			opacity: 1;
			transform: scale(1);
		}
	}
</style>