<template>
	<view class="content">
		<!-- <u-image width="100vw" height="100vh" :src="contractInfo.url"></u-image> -->
		<web-view :src="contractInfo.url" class="web-view"></web-view>
		<view class="canvas-container">
			<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>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="toPay()">确定支付</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: '',
				},
				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.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)
					}
				})
			},
			//  支付
			toPay() {
				NET.request(API.getPayParams, {
					...this.orderInfo
				}, 'POST').then(res => {
					if (this.memberInfo.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>
