<template>
	<view class="page">
		<cu-custom bgColor="bg-blue" :isBack="true"><block slot="backText">返回</block><block slot="content">签名确认</block></cu-custom>
		
		<view class="sign-container">
			<view class="sign-info">
				<text class="info-label">交接人：</text>
				<text class="info-value">{{handName}}</text>
			</view>
			
			<view class="canvas-wrapper">
				<canvas 
					class="sign-canvas" 
					canvas-id="signCanvas" 
					@touchstart="touchStart"
					@touchmove="touchMove"
					@touchend="touchEnd"
					@touchcancel="touchEnd"
					disable-scroll="true"
				></canvas>
			</view>
			
			<view class="btn-container">
				<button class="clear-btn" @click="clearCanvas">清除</button>
				<button class="confirm-btn" @click="confirmSign">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				handName: '',
				ctx: null,
				isDrawing: false,
				hasSigned: false
			}
		},
		onLoad(options) {
			console.log('接收到的参数：', options);
			this.id = decodeURIComponent(options.id || '');
			this.handName = decodeURIComponent(options.handName || '');
			console.log('id:', this.id, 'handName:', this.handName);
		},
		onReady() {
			this.initCanvas();
		},
		methods: {
			initCanvas() {
				const ctx = uni.createCanvasContext('signCanvas', this);
				this.ctx = ctx;
				ctx.setStrokeStyle('#000000');
				ctx.setLineWidth(4);
				ctx.setLineCap('round');
				ctx.setLineJoin('round');
			},
			touchStart(e) {
				this.isDrawing = true;
				this.hasSigned = true;
				const touch = e.touches[0];
				this.ctx.beginPath();
				this.ctx.moveTo(touch.x, touch.y);
			},
			touchMove(e) {
				if (!this.isDrawing) return;
				const touch = e.touches[0];
				this.ctx.lineTo(touch.x, touch.y);
				this.ctx.stroke();
			},
			touchEnd(e) {
				if (this.isDrawing) {
					this.ctx.stroke();
					this.ctx.closePath();
				}
				this.isDrawing = false;
			},
			clearCanvas() {
				uni.getSystemInfo({
					success: (res) => {
						this.ctx.clearRect(0, 0, res.windowWidth, 800);
					}
				});
				this.hasSigned = false;
			},
			confirmSign() {
				if (!this.hasSigned) {
					uni.showToast({
						title: '请先签名',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				
				uni.canvasToTempFilePath({
					canvasId: 'signCanvas',
					fileType: 'png',
					success: (res) => {
						console.log('签名图片路径：', res.tempFilePath);
						uni.showToast({
							title: '签名已确认',
							icon: 'success',
							duration: 2000
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 500);
					},
					fail: (err) => {
						console.error('保存签名失败', err);
						uni.showToast({
							title: '签名保存失败',
							icon: 'none',
							duration: 2000
						});
					}
				}, this);
			}
		}
	}
</script>

<style>
	.page {
		width: 100vw;
		height: 100vh;
		background: #f5f5f5;
	}
	
	/* 签名容器 */
	.sign-container {
		display: flex;
		flex-direction: column;
		height: calc(100vh - 100rpx);
		padding: 30rpx;
	}
	
	/* 签名信息 */
	.sign-info {
		display: flex;
		align-items: center;
		padding: 30rpx;
		background: #fff;
		border-radius: 20rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		flex-shrink: 0;
	}
	
	.info-label {
		font-size: 32rpx;
		color: #666;
		font-weight: 500;
		margin-right: 20rpx;
	}
	
	.info-value {
		font-size: 32rpx;
		color: #4a90e2;
		font-weight: 600;
	}
	
	/* 画布包裹层 */
	.canvas-wrapper {
		background: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		flex: 1;
		min-height: 0;
	}
	
	/* 签名画布 */
	.sign-canvas {
		width: 100%;
		height: 800rpx;
		border: 2px dashed #d0dbe6;
		border-radius: 16rpx;
		background: #fafafa;
	}
	
	/* 按钮容器 */
	.btn-container {
		display: flex;
		justify-content: space-around;
		padding: 30rpx;
		flex-shrink: 0;
	}
	
	/* 清除按钮 */
	.clear-btn {
		flex: 1;
		margin-right: 20rpx;
		background: linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%);
		color: #666;
		border: none;
		border-radius: 24rpx;
		padding: 28rpx 0;
		font-size: 32rpx;
		font-weight: 600;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
	}
	
	.clear-btn::after {
		border: none;
	}
	
	/* 确认按钮 */
	.confirm-btn {
		flex: 1;
		margin-left: 20rpx;
		background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
		color: #fff;
		border: none;
		border-radius: 24rpx;
		padding: 28rpx 0;
		font-size: 32rpx;
		font-weight: 600;
		box-shadow: 0 6rpx 24rpx rgba(74, 144, 226, 0.35);
	}
	
	.confirm-btn::after {
		border: none;
	}
</style>
