<template>
	<view class="page flex-col">
		<view class="block_1 flex-col" :style="{backgroundImage: 'url('+bgImg+')',backgroundSize: 'cover'}">
			<view class="section_2 flex-col">
				<text class="text_3">姓名*</text>
				<input class="input" type="text" v-model="postData.realName"
					:style="{backgroundImage: 'url('+inputBg+')'}" />
				<text class="text_4">手机号码*</text>
				<input class="input" type="text" v-model="postData.phoneNumber"
					:style="{backgroundImage: 'url('+inputBg+')'}" />
				<text class="text_5">报名项目*</text>
				<u-radio-group v-model="postData.category" placement="column" @change="groupChange">
					<view class="block_5 flex-row justify-end">
						<u-radio shape="circle" size="38rpx" iconSize="34rpx" name="手动挡"></u-radio>
						<view class="text-wrapper_1 flex-col">
							<text class="text_6">C1手动挡</text>
						</view>
					</view>
					<view class="block_6 flex-row justify-end">
						<u-radio shape="circle" size="38rpx" iconSize="34rpx" name="自动挡"></u-radio>
						<view class="text-wrapper_2 flex-col">
							<text class="text_6">C2自动挡</text>
						</view>
					</view>
					<view class="block_7 flex-row justify-end">
						<u-radio shape="circle" size="38rpx" iconSize="34rpx" name="三轮车"></u-radio>
						<view class="text-wrapper_3 flex-col">
							<text class="text_6">三轮车</text>
						</view>
					</view>
				</u-radio-group>
				<view class="baiz">
					点击我要报名，支付300元定金，即可立即成为铂金会员
				</view>
				<image class="image_2" :src="imagesUrl+'baoming.png'" @click="addWx()" />
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				imagesUrl: getApp().globalData.config.imagesUrl,
				inputBg: getApp().globalData.config.imagesUrl + 'inputBg.png',
				bgImg: getApp().globalData.config.imagesUrl + 'bgImg.png',
				postData: {
					realName: '',
					phoneNumber: '',
					category: '',
				},
				constants: {},
			};
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			// 获取用户信息
			async getUserInfo() {
				const res = await this.$request({
					url: '/getInfo',
				})
				console.log(res);
				if (res.code == 200 && res.hasOwnProperty('user')) {
					this.postData.realName = res.user.realName
					this.postData.phoneNumber = res.user.phonenumber
				}
			},
			// 单选组切换事件
			groupChange(n) {
				this.postData.category = n
			},
			// 提交学车信息
			async addWx() {
				if (uni.$u.test.isEmpty(this.postData.realName)) {
					return uni.showToast({
						icon: 'error',
						title: '请填写昵称'
					})
				}
				if (uni.$u.test.isEmpty(this.postData.phoneNumber)) {
					return uni.showToast({
						icon: 'error',
						title: '请填写电话'
					})
				}
				if (uni.$u.test.isEmpty(this.postData.category)) {
					return uni.showToast({
						icon: 'error',
						title: '请选择报名项目'
					})
				}
				const res = await this.$request({
					url: '/system/schoolRegistrationInfo/addWx',
					method: 'POST',
					data: this.postData
				})
				console.log(res);
				if (res.code == 200) {
					uni.showToast({
						title: '报名成功'
					})
					console.log('开始支付');
					this.getpayment()
					// setTimeout(() => {
					// 	this.$tabs.navigateBack()
					// }, 1500)
				}
			},
			//获取支付页信息
			async getorder() {
				const res = await this.$request({
					url: '/orderApi/orderDetail?goodsId=' + this.id + '&type=jx',
				})
				console.log('获取支付页信息', res);
				this.arr = res.data
				//处理积分
				this.current = this.arr.balance / 100
				//页面处理
				if (res.data.userLevel == 0) {
					this.goodsPrice = this.arr.goodsPrice / 100
					this.preferential = 0
				}
				if (res.data.userLevel == 1) {
					this.goodsPrice = ((this.arr.goodsPrice / 1000) * this.arr.silver).toFixed(2)
					this.preferential = ((this.arr.goodsPrice / 100) * ((10 - this.arr.silver) / 10)).toFixed(2)
				}
				if (res.data.userLevel == 2) {
					this.goodsPrice = ((this.arr.goodsPrice / 1000) * this.arr.gold).toFixed(2)
					this.preferential = ((this.arr.goodsPrice / 100) * ((10 - this.arr.gold) / 10)).toFixed(2)
				}
				if (res.data.userLevel == 3) {
					this.goodsPrice = ((this.arr.goodsPrice / 1000) * this.arr.platinum).toFixed(2)
					this.preferential = ((this.arr.goodsPrice / 100) * ((10 - this.arr.platinum) / 10)).toFixed(2)
				}
				console.log(this.goodsPrice, this.preferential);
				if (res.data == 11111111) {
					uni.navigateTo({
						url: '/subInspectionPages/Paymentsuccessful/Paymentsuccessful'
					})
				}
			},


			// 创建订单 吊起支付
			async getpayment() {
				let that = this
				//先判断当前积分是否能抵用当前商品

				// if (this.current >= this.goodsPrice) {
				if (1 == 2) {

					let num = this.goodsPrice * 100
					const ress = await this.$request({
						method: 'post',
						url: '/orderApi/createOrder',
						data: {
							goodsId: this.id,
							goodsType: 'jc',
							balance: num
						}
					})

					console.log('不用付钱', ress);
					if (ress.data == 11111111) {
						uni.navigateTo({
							url: '/subInspectionPages/Paymentsuccessful/Paymentsuccessful'
						})
					}

				} else {
					console.log('正常掏钱');
					const res = await this.$request({
						method: 'post',
						url: '/orderApi/createSchoolOrder',
					})
					console.log(res);
					this.orderid = res.data
					const ress = await this.$request({
						url: '/payApi/prepayment?type=jsapi' + '&orderId=' + this.orderid,
					})
					console.log(ress);
					wx.requestPayment({
						timeStamp: ress.timeStamp, // 时间戳，从1970年1月1日00:00:00至今的秒数，即当前的时间
						nonceStr: ress.nonceStr, // 随机字符串，长度为32个字符以下。
						package: ress.package, // 统一下单接口返回的 prepay_id 参数值，格式如“prepay_id=*”
						signType: ress.signType, // 签名算法类型，默认为 MD5，支持RSA等其他加密算法
						paySign: ress.paySign, // 签名，详见签名生成算法
						success: function(res) {
							console.log('成功', res);
							if (res.errMsg = 'requestPayment:ok') {
								uni.showToast({
									title: '支付成功'
								})
								uni.navigateTo({
									url: '/subInspectionPages/Paymentsuccessful/Paymentsuccessful'
								})
							}
							// 支付成功后的回调函数， res.errMsg = 'requestPayment:ok'
						},
						fail: function(res) {
							console.log('执行失败1', res);
							that.cancelpay()
							// this.cancelpay()
							// const resx =  this.$request({
							//  url:'/orderApi/cancelPay'
							// })
							// uni.showToast({
							// 	title:'支付失败'
							// })
							// console.log(resx);
							// 支付失败或取消支付后的回调函数， res.errMsg = 'requestPayment:fail cancel' 取消支付；res.errMsg = 'requestPayment:fail (detail error message)'
						}
					})


					// wx.requestPayment(ress)
				}
			},
			async cancelpay() {
				console.log('执行失败2');
				const resx = await this.$request({
					method: 'post',
					url: '/orderApi/cancelPay',
					params: {
						orderId: this.orderid,
					}
				})
				uni.showToast({
					title: '支付失败'
				})
				console.log(resx);
			}

		},
	};
</script>
<style>
	@import './index.rpx.css';
	.baiz{
		width: 100%;
		text-align: center;
		color: white;
		margin: 20px auto;
	}
	.input {
		background-size: cover;
		width: 670rpx;
		height: 105rpx;
		border-radius: 10rpx;
		margin: 25rpx auto;
		padding-left: 25rpx;
		font-size: 38rpx;
		font-weight: bold;

		text-indent: 2em;
	}
</style>
