<template>
	<view>
		<view v-if="!showSignature">
			<view v-if="!url">
				<view @click="openImg()">
					<l-painter ref="painter">
						<l-painter-image src="/static/img/1.jpg" css="width: 100%; height: 100%" />
						<l-painter-image :src="signatureBase64"
							css="width: 20%;position:absolute;bottom:20rpx;left:100rpx" />
					</l-painter>
				</view>
				<view>
					<button @click="showSignature = true"
						style="margin-top: 20px;background-color:#0066FF;border: #0066FF;color: #fff;width: 80%;">签名确认</button>
					<!-- <button @click="insImg()"
						style="margin-top: 20px;background-color:#0066FF;border: #0066FF;color: #fff;width: 80%;">图片上传</button> -->
				</view>
			</view>
			<view v-if="url">
				<image :src="url" style="width: 100%;" mode="widthFix"></image>
				<view style="text-align: center;margin-top: 10rpx;font-size: 20px;font-weight: 600;">↑↑↑长按图片保存↑↑↑</view>
			</view>
		</view>

		<view class="wrapper" v-if="showSignature">
			<view class="handBtn">
				<button @click="fanhui()" class="fanBtn">返回</button>
				<button @click="clear()" class="delBtn">清空</button>
				<button @click="subCanvas()" class="previewBtn">完成</button>
			</view>
			<view class="handCenter">
				<l-signature :penSize="10" :maxLineWidth="10" :minLineWidth="5" :openSmooth="true" ref="signatureRef"
					landscape></l-signature>
			</view>
			<view class="handRight">
				<view class="handTitle">请签名</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				signatureBase64: '',
				showSignature: false,
				url: '',
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: '新的标题'
			});
		},
		methods: {
			insImg() {
				this.$refs.painter.canvasToTempFilePathSync({
					// 在nvue里是jpeg
					fileType: "jpg",
					quality: 1,
					success: (res) => {
						console.log(res.tempFilePath);
						this.url = res.tempFilePath;
					},
					complete: () => {
						uni.hideLoading();
					},
				});
			},
			subCanvas() {
				this.$refs.signatureRef.canvasToTempFilePath({
					success: (res) => {
						this.signatureBase64 = res.tempFilePath;
						console.log(res);
						this.showSignature = false;
						uni.showLoading({
							title: '签名生成中...',
							mask: true
						});
						setTimeout(() => {
							this.insImg();
						}, 1000)
					}
				})
			},
			fanhui() {
				this.showSignature = false;
				this.$refs.signatureRef.clear();
			},
			clear() {
				this.$refs.signatureRef.clear();
			},
			openImg() {
				uni.previewImage({
					urls: ['/static/img/1.jpg'],
					current: 0
				});
			},
		}
	}
</script>

<style>
	page {
		background: #fbfbfb;
		height: auto;
		overflow: hidden;
	}

	.wrapper {
		width: 100%;
		height: 95vh;
		margin: 40rpx 0;
		overflow: hidden;
		display: flex;
		align-content: center;
		flex-direction: row;
		justify-content: center;
		font-size: 28rpx;
		z-index: 1000000000;
	}

	.handWriting {
		background: #fff;
		width: 100%;
		height: 95vh;
	}

	.handRight {
		display: inline-flex;
		align-items: center;
	}

	.handCenter {
		border: 4rpx dashed #e9e9e9;
		flex: 5;
		overflow: hidden;
		box-sizing: border-box;
	}

	.handTitle {
		transform: rotate(90deg);
		flex: 1;
		color: #666;
	}

	.handBtn button {
		font-size: 28rpx;
	}

	.handBtn {
		height: 95vh;
		display: inline-flex;
		flex-direction: column;
		justify-content: space-between;
		align-content: space-between;
		flex: 1;
	}

	.delBtn {
		position: absolute;
		top: 250rpx;
		left: 0rpx;
		transform: rotate(90deg);
		color: #666;
	}

	.fanBtn {
		position: absolute;
		top: 100rpx;
		left: 0rpx;
		transform: rotate(90deg);
		color: #666;
	}

	.previewBtn {
		position: absolute;
		bottom: 100rpx;
		left: 0rpx;
		background: #008ef6;
		transform: rotate(90deg);
		color: #ffffff;
	}
</style>