<template>
	<view class="index">
		<view class="">
			<u-sticky bgColor="#fff">
			    <u-tabs :list="list1" :scrollable='false' @click="clikeTab"></u-tabs>
			  </u-sticky>
		</view>
		<view class="index-img">
			<!-- <image style="width: 100%;height: 100%;" src="../../static/logo.png" mode=""></image> -->
			<template>
				<canvas class="jimfds" style="width: 100%;height: 100%;"
					canvas-id="myCanvas">
				</canvas>
			</template>
		</view>
		<view class="index-btm">
			<button open-type="share" class="btn">
				<view class="index-btm-lef">
					转发好友
				</view>
			</button>

			<view class="index-btm-rig"
				@click="downloadFile(downFile)">
				保存图片
			</view>
		</view>
	</view>
</template>

<script>
	import apiBaseConfig from '@/config/index.js';
	import {
		myPosterGET,
		myPosterSjGET,
		listByIds
	} from '@/api/workbench/index.js';
	export default {
		data() {
			return {
				imgBgUrl: apiBaseConfig.imgBgUrl,
				globalData: getApp().globalData,
				imgpath: '',
				screenHeight: '',
				screenWidth: '',
				lefWidth: '',
				topHeig: '',
				downFile:'',
				userInfo:{},
				list1:[{
					name: '邀请用户',
				},
				 {
					name: '邀请商家',
				 }
				]
			};
		},
		onShareAppMessage() { // 分享到微信好友
			// 更多参数配置,参考文档
			let self = this;
			return {
				title: '全屋定制管家-白名单小程序',
				path: `/pages/login/index?spread=${this.userInfo.userId}`,
			}
		},
		onLoad() {
			this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
			this.screenHeight = uni.getSystemInfoSync().windowHeight;
			this.screenWidth = uni.getSystemInfoSync().windowWidth - 30;
			this.lefWidth = (this.screenWidth + 60)/ 4
			this.topHeig = this.screenHeight - 350
			console.log(this.screenHeight, this.screenWidth, this.lefWidth, this.topHeig, 'screen_height');
			this.myPoster()
		},
		methods: {
			// tab
			clikeTab(e){
				console.log(e)
				if(e.index=='0'){
					//用户
					this.myPoster()
				}else{
					//商家
					this.myPosterSj()
				}
			},
			//用户
			myPoster() {
				myPosterGET().then((res) => {
					uni.showLoading({
						title:'加载中'
					})
					this.listBy(res.data.posterContent)
					this.getImage(res.data.qrCode)
				})
			},
			//商家
			myPosterSj() {
				myPosterSjGET().then((res) => {
					uni.showLoading({
						title:'加载中'
					})
					this.listBy(res.data.posterContentTwo)
					this.getImage(res.data.qrCode)
				})
			},
			
			listBy(posterContent) {
				listByIds(posterContent).then((res) => {
					this.getImageInfo(res.data[0].url)
				})
			},
			//获取背景图,将网络图片转成本地图片,
			getImageInfo(src) {
				let self = this
				wx.getImageInfo({
					src: src,
					success(res) {
						// console.log('3333333333')
						self.mergeImages(res.path)
					}
				})
			},
			// 获取二维码,将网络图片转成本地图片,
			getImage(src) {
				let self = this
				wx.getImageInfo({
					src: src,
					success(res) {
						self.imgpath = res.path
					}
				})
			},
			mergeImages(v) {
				let that = this
				// console.log('v', v)
				const ctx = uni.createCanvasContext('myCanvas');
				// 绘制第一张图片  
				ctx.drawImage(v, 0, 0, this.screenWidth, this.screenHeight);
				// 绘制第二张图片,调整位置和大小  
				ctx.drawImage(that.imgpath, this.lefWidth, this.topHeig, 150, 150);
				// 完成绘制,将结果导出为图片  
				ctx.draw(); // true表示导出时使用canvas的宽高,不进行缩放  
				uni.hideLoading()
				uni.canvasToTempFilePath({
					canvasId: 'myCanvas',
					success: function(res) {
						console.log('合并后的图片路径:', res.tempFilePath);
						setTimeout(()=>{
							that.setDatas(res.tempFilePath)
						},500)
						// 在这里你可以将合并后的图片保存到本地或进行其他处理  
					}
				});
			},
			setData(v) {
				this.downFile = v
				// console.log(v, '11111111111111222222')
				// this.parth.push(v)
			},
			downloadFile(url) {
				// this.uploadFilePr(this.ewmLink)
				uni.saveImageToPhotosAlbum({
					filePath: url,
					success: () => {
						uni.showToast({
							title: '保存成功!',
						})
					},
					fail: () => {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						})
					},
				})
				return
				uni.downloadFile({
					url,
					fail: function(res) {
						uni.showModal({
							title: '提示',
							content: '保存失败',
						})
					},
					success: function(res) {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: () => {
								uni.showToast({
									title: '保存成功!',
								})
							},
							fail: () => {
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								})
							},
						})
					},
				})
			},
			// 退出登录
			jump() {
				uni.clearStorageSync();
				uni.reLaunch({
					url: '/pages/login/index'
				});
			}
		}
	};
</script>

<style>
	page {
		background: #F7F8FA;
		height: 100%;
	}
</style>
<style lang="scss" scoped>
	.index {
		height: 100%;

		&-img {
			padding: 30rpx 30rpx 220rpx;
			height: calc(100% - 250rpx);
		}

		&-btm {
			position: fixed;
			bottom: 0rpx;
			background: #FFFFFF;
			width: calc(100% - 80rpx);
			padding: 24rpx 40rpx 62rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&-lef {
				width: 320rpx;
				height: 90rpx;
				border-radius: 45rpx;
				border: 2rpx solid #1572FF;
				font-weight: 500;
				font-size: 30rpx;
				color: #1572FF;
				text-align: center;
				line-height: 90rpx;
			}

			&-rig {
				width: 320rpx;
				height: 90rpx;
				background: #1572FF;
				border-radius: 45rpx;
				font-weight: 500;
				font-size: 30rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 90rpx;
			}
		}
	}

	.btn {
		background: #fff;
		border: none;
		padding: 0rpx;
		margin: 0rpx;
	}

	button::after {
		border: none
	}
</style>