<template>
	<view class="content">
		<u-navbar back-text="返回" title="签字" back-icon-color="#ffffff" :back-text-style="{color: '#ffffff'}" title-color="#ffffff"
		 :background="{backgroundColor: mainColor}" :border-bottom="false"></u-navbar>
		<view class="canvas-container" slot="label">
			<canvas canvas-id="canvas" id="canvas" :disable-scroll="true" style="width: 100%; height: calc(100vh - 150px);background-color: #FFFFFF;"
			 @touchstart="handleTouchStart($event)" @touchmove="handleTouchMove($event)" @touchend="handleTouchEnd($event)"
			 @touchcancel="handleEnd($event)"></canvas>
		</view>
		<view class="handle-fix-box">
			<u-button type="primary" :ripple="true" :custom-style="customStyle" :disabled="!ifSign" @click="submitAutographForm()">提交</u-button>
		</view>
		<u-top-tips ref="uTips" :navbar-height="statusBarHeight + navbarHeight"></u-top-tips>
	</view>
</template>

<script>
	var context = null
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
			])
		},
		data() {
			return {
				taskId: '',
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				navbarHeight: 44,
				ifSign: false,
				canvasData: []
			}
		},
		watch: {
			canvasData() {
				context.moveTo(this.canvasData[0].x, this.canvasData[0].y)
				for (let i = 0; i < this.canvasData.length; i++) {
					context.lineTo(this.canvasData[i].x, this.canvasData[i].y)
				}
				context.stroke()
				context.draw(true)
			}
		},
		onLoad(options) {
			this.taskId = options.taskId
			context = uni.createCanvasContext('canvas')
			context.setLineWidth(3)
			context.setStrokeStyle("#000000")
			this.reset()
		},
		onShow() {},
		methods: {
			reset() {
				this.ifSign = false
				context.draw()
			},
			handleTouchStart(e) {
				this.ifSign = true
				this.canvasData = []
				const a = e.changedTouches[0]
				this.canvasData.push({
					x: a.x,
					y: a.y
				})
			},
			handleTouchMove(e) {
				const a = e.changedTouches[0]
				this.canvasData.push({
					x: a.x,
					y: a.y
				})
			},
			handleTouchEnd(e) {
				const a = e.changedTouches[0]
				this.canvasData.push({
					x: a.x,
					y: a.y
				})
			},
			handleEnd() {
				context.stroke()
				context.draw(true)
			},
			//  提交签字
			submitAutographForm() {
				let that = this
				uni.canvasToTempFilePath({
					canvasId: 'canvas',
					success: res => {
						uni.uploadFile({
							url: API.uploadFile,
							filePath: res.tempFilePath,
							name: 'file',
							header: {
								Authorization: 'Bearer ' + uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								NET.request(API.submitAutographForm, {
									taskId: this.taskId,
									fileId: JSON.parse(uploadFileRes.data).data[0].fileId,
									filePath: JSON.parse(uploadFileRes.data).data[0].filePath,
									fileType: JSON.parse(uploadFileRes.data).data[0].fileType,
								}, 'POST').then(res => {
									that.$refs.uTips.show({
										title: '提交成功',
										type: 'success',
									})
									setTimeout(() => {
										uni.navigateBack()
									}, 1000)
								}).catch(error => {
									that.$refs.uTips.show({
										title: error.message,
										type: 'warning',
									})
								})
							}
						});
					}
				})
			},
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;
		padding: 15px 15px 60px 15px;
		box-sizing: border-box;
	}
</style>
