<template>
	<view class="container">
		<view class="v-navbar">
			<u-navbar title="商户收款" :autoBack="true" :fixed="false" bgColor="transparent" leftIconColor="#fff"
				:titleStyle="{color:'#fff',fontWeight:'bold',fontSize:'32rpx'}">
			</u-navbar>
		</view>
		<view style="display:flex;justify-content:center;flex-direction:column;align-items:center;">
			<image src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/82347202403181619505958.png"
				style="width:350rpx;height:118rpx;" mode="widthFix" />
			<text class="shop_name">店铺:{{userInfo.mer_info.mer_name || '-'}}</text>
		</view>

		<view class="v-qrcode">
			<u-image width="490rpx" height="490rpx" :showLoading="true" :src="img" class="v-qrcode-img"></u-image>
		</view>

		<view class="v-btn" @click="handleSavePic">
			<text>保存二维码</text>
		</view>
	</view>
</template>

<script>
	import {
		qrcode
	} from "@/api/payment.js";
	export default {
		data() {
			return {
				img: '',
				userInfo: {}
			}
		},
		onLoad(opt) {
			let user = this.$store.state.app.userInfo;
			if (typeof user == 'string') user = JSON.parse(user);
			this.userInfo = user;
			this.getQrcode(user.service.mer_id)
		},
		methods: {
			// 获取二维码
			getQrcode(merid) {
				qrcode({
					mer_id: merid
				}).then(res => {
					if (!res.data.count || res.data.count == 0) {
						this.$util.Tips({
							title: "您还没有添加商品,请添加!"
						}, () => {
							uni.redirectTo({
								url: "/pages/product/addGood/addGood?mer_id=" + merid
							})
						})
					} else {
						this.img = res.data.url;
					}
				})
			},

			handleSavePic() {
				// 获取要保存的图片路径或URL
				let imageUrl = this.img; // 这里使用了网络上的图片作为示例

				// #ifdef H5
				var a = document.createElement("a");
				a.download = imageUrl;
				a.href = imageUrl;
				document.body.appendChild(a);
				a.click();
				a.remove();
				this.$util.Tips({
					title: '二维码保存成功!'
				})
				// #endif

				// #ifndef H5
				let that = this;
				uni.downloadFile({
					url: imageUrl,
					success(res) {
						if (res.statusCode === 200) {
							let tempFilePath = res.tempFilePath; // 临时文件路径
							uni.saveImageToPhotosAlbum({
								filePath: tempFilePath,
								success() {
									return that.$util.Tips({
										title: '图片已保存至相册!'
									});
								},
								fail(err) {
									console.error('保存失败', err);
								}
							});
						} else {
							console.error('下载失败', res.statusCode);
						}
					},
					fail(err) {
						console.error('下载失败', err);
					}
				});
				// #endif
			}
		}
	}
</script>
<style lang="scss">
	page {
		background-color: #40AE36;
	}

	.container {
		position: relative;
		height: 100vh;
		background-size: 100% auto;
		background-repeat: no-repeat;
		padding-top: var(--status-bar-height);

		.v-navbar {
			margin-bottom: 54rpx;
		}

		.v-qrcode {
			width: 650rpx;
			height: 650rpx;
			margin-top: 100rpx;
			margin: 100rpx auto 0;
			background-color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.shop_name {
			margin-top: 30rpx;
			font-size: 30rpx;
			color: #FFFFFF;
		}

		.v-btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 690rpx;
			height: 100rpx;
			margin: 70rpx auto 0;

			text {
				font-weight: 400;
				font-size: 32rpx;
				color: #fff;
			}
		}
	}
</style>