<template>
	<div class="tools_container g_pl_10 g_pr_10 g_pb_12">
		<div class="g_flex_row_between u-skeleton">
			<div v-for="(item, index) in imgList" :key="index" class="g_flex_column_between flex_center g_radius_8 g_bg_f g_mt_10" style="width: calc((100% - 10px) / 2); overflow: hidden">
				<div class="g_h_240 g_w_all u-skeleton-rect g_position_rela" style="overflow: hidden">
					<u-lazy-load :image="item" threshold="-100" img-mode="widthFix" loadingImg="https://matripe-cms.oss-cn-beijing.aliyuncs.com/dailibaoming/loading.gif"></u-lazy-load>
				</div>
				<g-button btnText="生成海报" type="primary" size="mini" class="g_mb_12 g_mt_12 u-skeleton-rect" @clickBtn="getPoster(item)" />
			</div>
		</div>
		<u-popup v-model="posterShow" mode="center" :closeable="true" uZindex="9999" width="90%" height="87%" border-radius="12" :mask-close-able="true" @close="currentImg = ''">
			<div class="g_pt_36 g_pb_12 g_pl_12 g_pr_12 g_text_c" style="height: calc(100% - 56px); width: 100%">
				<image class="g_h_all" :show-menu-by-longpress="true" :src="currentImg" mode="aspectFit" lazy-load binderror="" bindload=""> </image>
			</div>
			<div class="g_flex_row_center">
				<g-button btnText="保存图片" @clickBtn="saveImg" type="primary" size="160" class="" />
				<!-- <g-button btnText="分享图片" size="small" class="" /> -->
			</div>
		</u-popup>
		<u-skeleton :loading="showPage" :animation="true" border-radius="12"></u-skeleton>
		<canvas type="2d" style="position: absolute; left: -2000px; width: 425px; height: 350px; background: #fff" id="canvasId" ref="canvasId"></canvas>
	</div>
</template>

<script>
export default {
	name: "tools",
	data() {
		return {
			showPage: true,
			imgList: [],
			currentImg: "",
			posterShow: false,
			miniAppInfo: uni.getStorageSync("miniApp-info"),
		};
	},
	onLoad() {},
	created() {
		setTimeout(() => {
			this.showPage = false;
		}, 2000);
		for (let i = 0; i < 12; i++) {
			this.imgList[i] = `https://matripe-cms.oss-cn-beijing.aliyuncs.com/dailibaoming/poster/a${i + 1}.png?chuo=` + new Date().getTime();
		}
		console.log(this.imgList);
		this.getUserQRCode();
	},
	props: {
		firstLoad: {
			type: Boolean,
			default: false,
		},
	},
	watch: {
		firstLoad(val) {
			console.log("firstLoad", val);
			if (val) {
				this.showPage = true;
				setTimeout(() => {
					this.showPage = false;
				}, 2000);
			}
		},
	},
	methods: {
		getUserQRCode() {
			let that = this;
			that.G.Get(that.api.user_getUserQRCode, {}, (res) => {
				console.log("res", res);
				that.miniAppInfo.qrCode = "https://daotian.matripe.com.cn" + res.qrcode;
			});
		},
		getPoster(e) {
			const that = this;
			uni.showLoading({
				title: "生成中...",
				mask: true,
			});
			that.getWXCanvas(e).then((res) => {
				console.log("res", res);
				that.currentImg = res.tempFilePath;
				that.posterShow = true;
				uni.hideLoading();
			});
		},
		// 微信端绘制
		getWXCanvas(sharelImg, type) {
			const that = this;
			return new Promise((resolve) => {
				var mycenter = 0; //文字左右居中显示
				var myheight = 0; //文字高度
				const query = uni.createSelectorQuery().in(this);
				query
					.select("#canvasId")
					.fields({
						node: true,
						size: true,
					})
					.exec((res) => {
						console.log("uni 获取节点信息：", res);
						// console.log("sharelImg", sharelImg);
						const canvas = res[0].node;
						const ctx = canvas.getContext("2d");
						new Promise(function (resolve) {
							// 绘制背景图片
							uni.getImageInfo({
								src: sharelImg, // 网络图片
								success(res) {
									console.log("res111", res);
									canvas.width = res.width;
									canvas.height = res.height;
									mycenter = canvas.width / 2;
									myheight = canvas.height;
									const img = canvas.createImage();
									img.src = res.path;
									console.log("img111", img);
									img.onload = (info) => {
										ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
										console.log("info111", info);
										resolve(true);
									};
								},
								fail(err) {},
							});
						}).then(() => {
							console.log("inthen");
							//使用：
							that
								.setShareJobImg(ctx, canvas, mycenter)
								.then(function (canvas1) {
									console.log("canvas1", canvas1);
									uni.canvasToTempFilePath({
										canvas: canvas1,
										success(res) {
											console.log("res222", res);
											resolve(res);
										},
										fail(res) {
											uni.hideLoading();
											uni.showToast({
												title: res.errMsg,
												icon: "error",
											});
										},
									});
								})
								.catch((err) => {
									console.log(err);
									uni.hideLoading();
									uni.showToast({
										title: "生成失败,请重试",
									});
								});
						});
					});
			});
		},
		// 设置分享布局
		setShareJobImg(ctx, canvas, mycenter, funObj) {
			let that = this;
			let labelLeft = 0;
			// ctx.arcTo()
			let baseTop, QRSize, baseLeft, titleFontSize, contentFontSize;
			baseTop = canvas.height - 412;
			// baseTop = 1470;
			QRSize = 144;
			baseLeft = 80;
			titleFontSize = 54;
			contentFontSize = 36;
			that.G.roundRect1(ctx, mycenter - (canvas.width - 60) / 2, baseTop, canvas.width - 60, 360, 48, "#fff");
			that.G.fillTextLineBreak(ctx, that.miniAppInfo.fullName || that.miniAppInfo.agencyName, baseLeft + QRSize + 20, baseTop + 172, 300, 64, "#000", titleFontSize, "500"); // 代理名字
			let slogan = that.miniAppInfo.slogan || "全心全意为老乡服务";
			// let slogan = '全新全意为老乡服务'
			that.G.fillTextLineBreak(ctx, slogan, baseLeft + QRSize + 20, baseTop + 178 + titleFontSize, 500, 64, "#666", contentFontSize, "500"); // slogan

			// miniAppInfo
			return new Promise((imgres) => {
				let p1 = new Promise((resolve) => {
					uni.getImageInfo({
						// src: "https://matripe-cms.oss-cn-beijing.aliyuncs.com/dailibaoming/poster/renhaiQRCode.png",
						src: that.miniAppInfo.qrCode,
						success(res) {
							console.log("res333", res);
							const img = canvas.createImage();
							img.src = res.path;
							img.onload = () => {
								console.log("ctx", ctx);
								console.log("canvas", canvas);
								console.log("that.miniAppInfo.logo", that.miniAppInfo.logo);
								// that.G.roundRect1(ctx, mycenter - 580 / 2, baseTop + 740, 580, 580, 0, "#fff");
								that.G.roundRect(ctx, img, canvas.width - 320, baseTop + 60, 240, 240, 0);
								resolve();
							};
						},
						fail(err) {
							console.log(err);
						},
					});
				});
				let p2 = new Promise((resolve) => {
					uni.downloadFile({
						url: that.miniAppInfo.logo || "https://matripe.oss-cn-beijing.aliyuncs.com/default.png",
						success(res) {
							console.log(res);
							const img = canvas.createImage();
							img.src = res.tempFilePath;
							img.onload = () => {
								that.G.roundRect(ctx, img, baseLeft, baseTop + 108, QRSize, QRSize, 0);
								resolve();
							};
						},
						fail(err) {
							console.log(err);
						},
					});
				});
				Promise.all([p1, p2]).then(() => {
					// setTimeout(() => {
						imgres(canvas);
					// }, 300);
				});
			});
		},
		saveImg() {
			let that = this;
			console.log("saveImg");
			uni.saveImageToPhotosAlbum({
				filePath: that.currentImg,
				success(res) {
					console.log(res);
					if (res.errMsg == "saveImageToPhotosAlbum:ok") {
						uni.showToast({
							title: "保存成功 ",
							icon: "none",
							duration: 1500,
						});
					}
				},
				fail(res) {
					console.log(res);
					uni.showToast({
						title: "图片保存失败",
						icon: "none",
						duration: 1500,
					});
				},
			});
		},
	},
};
</script>

<style>
.tools_container {
	min-height: 100vh;
	background-color: #f5f5f5;
}
.list-image {
	background-size: cover;
	background-position: top center;
	background-repeat: no-repeat;
}
</style>
