<template>
	<view class="content">
		<rich-text v-for="(item,index) in contractInfo" :key="index" :nodes="item.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" style="height:100px">
			<u-button style="margin-bottom: 10px;display: block;" type="warning" shape="circle" :ripple="true" :disabled="!ifSign" :custom-style="customStyle" @click="submitContract0()">其他方式支付</u-button>
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" :disabled="!ifSign" @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: {
					cardIds: '',
					couponId: '',
					studentId: '',
				},
				contractInfo: [],
				ifSign: false,
				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 = {
				cardIds: options.memberCardId.split(','),
				couponId: options.couponId,
				studentId: options.studentId,
			}
			this.realPayAmount = options.realPayAmount
			console.log(this.realPayAmount);
			this.getContractInfo(options.parentName, options.sex)
			context = uni.createCanvasContext('canvas')
			context.setLineWidth(3)
			context.setStrokeStyle("#000000")
			this.reset()
		},
		onShow() {},
		methods: {
			//  获取数据
			getContractInfo(parentName, sex) {
				NET.request(API.getContractInfo, {
					cardIds: this.orderInfo.cardIds,
					studentId: this.orderInfo.studentId,
					parentName: parentName,
					sex: sex,
				}, 'POST').then(res => {
					this.contractInfo = res.data
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			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)
			},
			handleConfirm() {
				uni.canvasToTempFilePath({
					canvasId: 'canvas',
					success: res => {
						this.$emit('success', res.tempFilePath)
					}
				})
			},
			//  其他方式支付
			submitContract0() {
				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, {
									contractIds: that.contractInfo.map(item => { return item.id }),
									fileId: that.signUrl,
								}, 'POST').then(res => {
									that.toPayOther(res.data.map(item => { return item.id }))
								}).catch(error => {
									that.$refs.uTips.show({
										title: error.message,
										type: 'warning',
									})
								})
							}
						});
					}
				})
			},
			//  提交签字
			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, {
									contractIds: that.contractInfo.map(item => { return item.id }),
									fileId: that.signUrl,
								}, 'POST').then(res => {
									/*uni.redirectTo({
										url: '/pagesMain/payResultNo?id=' + res.data.oderNo
									});*/
									//this.goToPayResult(res.data.oderNo)
									that.toPay(res.data.map(item => { return item.id }))
								}).catch(error => {
									that.$refs.uTips.show({
										title: error.message,
										type: 'warning',
									})
								})
							}
						});
					}
				})
			},
			//  其他支付
			toPayOther(contractId) {
				NET.request(API.getPayParams0, {
					...this.orderInfo,
					contractId: contractId
				}, 'POST').then(res => {
					uni.redirectTo({
						url: '/pagesMain/payResultNo?id=' + res.data.oderNo
					});
				})
			},
			//  支付
			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 100px 15px;
		box-sizing: border-box;

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