<template>
	<view class="content">
		<rich-text :nodes="contractInfo.url" class="web-view"></rich-text>
		<u-cell-group style="width: 100%; float: left;">
			<u-cell-item title="乙方签字" :arrow="false" :title-style="{width: '100%'}" required>
				<view class="canvas-container" slot="label">
					<canvas canvas-id="canvas" id="canvas" :disable-scroll="true" style="width: 100%; height: 200px;background-color: #FFFFFF;"
					 @touchstart="handleTouchStart($event)" @touchmove="handleTouchMove($event)" @touchend="handleTouchEnd($event)"
					 @touchcancel="handleEnd($event)"></canvas>
				</view>
			</u-cell-item>
		</u-cell-group>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitContract()">确定支付</u-button>
		</view>
		<u-top-tips ref="uTips"></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 {
				orderInfo: {
					id: '',
					couponId: '',
					studentId: '',
				},
				contractInfo: {
					id: '',
					url: '',
				},
				signUrl: '',
				realPayAmount: '',
				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.orderInfo = {
				id: options.memberCardId,
				couponId: options.couponId,
				studentId: options.studentId,
			}
			this.realPayAmount = options.realPayAmount
			this.getContractInfo()
			context = uni.createCanvasContext('canvas')
			context.setLineWidth(3)
			context.setStrokeStyle("#000000")
			this.reset()
		},
		onShow() {},
		methods: {
			//  获取数据
			getContractInfo() {
				NET.request(API.getContractInfo, {
					cardId: this.orderInfo.id
				}, 'POST').then(res => {
					this.contractInfo = res.data
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			reset() {
				context.draw()
			},
			handleTouchStart(e) {
				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)
			},
			handleConfirm() {
				uni.canvasToTempFilePath({
					canvasId: 'canvas',
					success: res => {
						this.$emit('success', res.tempFilePath)
					}
				})
			},
			//  提交签字
			submitContract() {
				let that = this
				uni.canvasToTempFilePath({
					canvasId: 'canvas',
					success: res => {
						uni.uploadFile({
							url: API.uploadFile,
							filePath: res.tempFilePath,
							name: 'file',
							header: {
								Authorization: uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								that.signUrl = JSON.parse(uploadFileRes.data).data.id
								NET.request(API.submitContractForm, {
									contractId: that.contractInfo.id,
									fileId: that.signUrl,
								}, 'POST').then(res => {
									that.toPay(res.data.id)
								}).catch(error => {
									that.$refs.uTips.show({
										title: error.message,
										type: 'warning',
									})
								})
							}
						});
					}
				})
			},
			//  支付
			toPay(contractId) {
				NET.request(API.getPayParams, {
					...this.orderInfo,
					contractId: contractId
				}, 'POST').then(res => {
					if (this.realPayAmount <= 0) {
						this.goToPayResult(res.data.oderNo)
						return false
					}
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.timeStamp,
						nonceStr: res.data.nonceStr,
						package: res.data.packageString,
						signType: res.data.signType,
						paySign: res.data.paySign,
						success: (payRes) => {
							this.goToPayResult(res.data.oderNo)
						},
						fail: (error) => {
							this.$refs.uTips.show({
								title: '支付未成功',
								type: 'warning',
							})
						}
					})
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  跳转支付结果
			goToPayResult(oderNo) {
				uni.redirectTo({
					url: '/pagesMain/payResult?id=' + oderNo
				});
			}
		},
	}
</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;

		.web-view {
			height: 200px !important;
		}
	}
</style>