<template>
	<view class="sealWrapBox">
		<view class="wrapper" v-show="showCanvas">
			<view class="handBtn">
				<view class="lookBtn" @click="look">图例示范</view>
				<view class="delBtn" @click="clear">清除</view>
				<view v-if="paintBrush" class="saveBtn saveBtnActive" @click="finish">下一步</view>
				<view v-else class="saveBtn">下一步</view>
				<!-- <view class="previewBtn" @click="close">关闭</view> -->
			</view>

			<view class="handCenter">
				<canvas v-show="!maskShow" canvas-id="canvasid" id="test" class="handWriting" disable-scroll="true" @touchstart="touchstart"
				 @touchmove="touchmove" @touchend="touchend"></canvas>
			</view>
			<view class="handRight">
				<image src="../../pageMy/static/img/seal/tips.png" mode=""></image>
				<view class="handTitle">手写签名需与本人姓名保持一致</view>
			</view>
		</view>
		<uniMask :maskShow="maskShow">
			<view class="maskBody" :style="maskBodyStyle">
				<view class="closeMask" @click.stop="closeMask">关闭</view>
			</view>
		</uniMask>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/config/env.js'
	import {
		getStore
	} from '@/untils/store.js'
	import uniMask from '@/components/uni-mask/mask.vue'
	var x = 20;
	var y = 20;
	export default {
		data() {
			return {
				oc: "",
				src: "",
				//路径点集合
				points: [],
				maskBodyStyle: '',
				showCanvas: true,
				maskShow: false,
				paintBrush: false

			};
		},
		components: {
			uniMask
		},
		mounted() {
			this.initCanvas()
		},
		methods: {
			look() {
				uni.getSystemInfo({
					success: res => {
						var scrollH = res.windowHeight
						var scrollW = res.windowWidth
						// #ifdef H5
						this.maskBodyStyle =
							`width:${scrollH-144}px;height:${scrollW-100}px;top:${(scrollW/2)+94}px;left:-${(scrollW-100)/2}px`
						// #endif
						// #ifdef MP-WEIXIN
						this.maskBodyStyle =
							`width:${scrollH-144}px;height:${scrollW-100}px;top:${(scrollW/2)}px;left:-${(scrollW-100)/2}px`
						// #endif
					}
				});
				this.maskShow = true
			},
			closeMask() {
				this.maskShow = false
			},
			imgFile() {
				var token = getStore({
					name: 'token'
				})
				const that = this
				uni.uploadFile({
					url: `${baseUrl}/api/jobslink-api/resource/oss/endpoint/put-file?Jobslink-Auth=${token}`,
					filePath: that.src,
					name: 'file',
					success: (uploadFileRes) => {
						that.$emit('finish', uploadFileRes)
						that.paintBrush = true
					}
				});
			},
			finish() {
				const that = this
				this.paintBrush = false
				this.oc.draw(true, uni.canvasToTempFilePath({
					destWidth: 300,
					destHeight: 300,
					quality: 1,
					canvasId: 'canvasid',
					fileType: 'png',
					success: function(res) {
						that.src = res.tempFilePath
						that.imgFile()
					},
					fail: function(err) {
						console.log(err)
					}
				}, this))
			},
			close: function() {
				this.showCanvas = false;
				this.clear();
			},
			initCanvas() {
				this.showCanvas = true;
				this.oc = uni.createCanvasContext('canvasid', this);
				//设置画笔样式
				this.oc.lineWidth = 10;
				this.oc.lineCap = "round";
				this.oc.lineJoin = "round";
				// 画笔颜色
				// this.oc.setStrokeStyle("#ffffff");

			},
			//触摸开始，获取到起点
			touchstart(e) {
				const startX = e.changedTouches[0].x;
				const startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};
				this.points.push(startPoint);
				//每次触摸开始，开启新的路径
				this.oc.beginPath();
			},
			//触摸移动，获取到路径点
			touchmove(e) {
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}

			},

			// 触摸结束，将未绘制的点清空防止对后续路径产生干扰
			touchend() {
				this.points = [];
				this.paintBrush = true
			},

			/*
			#   绘制笔迹
			#   1.为保证笔迹实时显示，必须在移动的同时绘制笔迹
			#   2.为保证笔迹连续，每次从路径集合中区两个点作为起点（moveTo）和终点(lineTo)
			#   3.将上一次的终点作为下一次绘制的起点（即清除第一个点）
			*/
			draw() {
				let point1 = this.points[0]
				let point2 = this.points[1]
				this.points.shift()
				this.oc.moveTo(point1.X, point1.Y)
				this.oc.lineTo(point2.X, point2.Y)
				this.oc.stroke()
				this.oc.draw(true)
			},
			//清空画布
			clear() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						that.paintBrush = false
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.oc.clearRect(0, 0, canvasw, canvash);
						that.oc.draw(true);
					}
				})
			}
		}
	};
</script>
<style>
	page {
		background: #fbfbfb;
		height: auto;
	}

	.maskBody {
		position: fixed;
		transform: rotate(90deg);
		background: url(../../static/img/anli.png) no-repeat;
		background-size: contain;
	}

	.maskBody .closeMask {
		background-color: #FFFFFF;
		position: fixed;
		right: 16rpx;
		top: 12rpx;
		width: 108rpx;
		height: 45rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #999999;
		line-height: 45rpx;
		text-align: right;
	}

	.sealWrapBox {
		background: #FFFFFF;
		padding: 15px;
		height: -webkit-fill-available;
	}

	.wrapper {
		width: 100%;
		overflow: hidden;
		display: flex;
		align-content: center;
		flex-direction: row;
		justify-content: center;
		font-size: 28rpx;
	}

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

	.handRight {
		display: inline-flex;
		align-items: space-between;
		width: 100%;
		height: 50rpx;
		transform: rotate(90deg);
		position: absolute;
		left: 300rpx;
		bottom: 40%;

	}

	.handRight image {
		width: 40rpx;
		height: 40rpx;

	}

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

	.handTitle {
		flex: 1;
		color: #666;
		width: 100%;
		height: 40rpx;
		line-height: 40rpx;

	}

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

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

	.lookBtn {
		position: absolute;
		top: 40px;
		left: 0;
		transform: rotate(90deg);
		width: 128rpx;
		height: 45rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #1C66FF;
		line-height: 45rpx;
	}

	.delBtn {
		position: absolute;
		bottom: 90px;
		left: 0rpx;
		transform: rotate(90deg);
		color: #979797;
		width: 120rpx;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
	}

	.delBtn image {
		position: absolute;
		top: 13rpx;
		left: 25rpx;
	}

	.saveBtnActive {
		background-color: #1C66FF !important;
		color: #FFFFFF !important;
	}

	.saveBtn {
		position: absolute;
		bottom: 37px;
		left: 0rpx;
		transform: rotate(90deg);
		color: #979797;
		width: 120rpx;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		background: #D6D6D6;
		border-radius: 8rpx;
	}

	.previewBtn {
		position: absolute;
		top: 500rpx;
		left: 0rpx;
		transform: rotate(90deg);
		color: #666;
	}
</style>
