<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="orderinfo">
					<view class="row">
						<view class="nominal">订单金额:</view>
						<view class="text" v-if="pryType == 1">{{amount}} 元</view>
						<view class="text" v-if="pryType == 2">{{amount*100}} 个积分</view>
						<view class="text" v-if="pryType == 3">{{amount}}元</view>
					</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				选择支付方式
			</view>
			<view class="content">
				<view class="pay-list">
					<view class="row" @tap="paytype='jinbi'" v-if="pryType == 2 || pryType==3 ">
						<view class="left">
							<image src="../../../static/img/jfx.png"></image>
						</view>
						<view class="center">
							积分支付(积分:{{user.gold}})
						</view>
						<view class="right">
							<radio :checked="paytype=='jinbi'" color="#0083f5" />
						</view>
					</view>
					<view class="row" @tap="paytype='gonghuika'" v-if="goodsType != 2 && goodsType != 8">
						<view class="left">
							<image style="border-radius: 50%;" src="../../../static/img/ghkpay.png"></image>
						</view>
						<view class="center">
							汇联通工会卡(余额:{{tongCardPrice}}元)
						</view>
						<view class="right">
							<radio :checked="paytype=='gonghuika'" @click="changeRiado()" color="#0083f5" />
						</view>
					</view>
					<view class="row" @tap="paytype='wxpay'" v-if="pryType == 1 || pryType==3 ">
						<view class="left">
							<image :src="imagewxUrl+imgadres"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay'" color="#0083f5" />
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="pay">
			<view class="btn" @tap="orderToPay">立即支付</view>
			<view class="tis">
				点击立即支付，即代表您同意<view class="terms">
					《条款协议》
				</view>
			</view>
		</view>

		<ssPaymentPassword ref="paymentPassword" :mode="1" @submit="submitHandle" />
	</view>
</template>

<script>
	import {
		orderToPay,
		orderToGoldPay,
		getHuiLianTongCardBalance,
		hltUnionCardPay
	} from '../../../Utils/Api.js'
	import ssPaymentPassword from '../../../components/sanshui-payment-password'
	let app = getApp();
	// #ifdef H5
	var jweixin = require('jweixin-module');
	// #endif
	export default {
		components: {
			ssPaymentPassword
		},
		data() {
			return {
				amount: 0,
				imagewxUrl: app.globalData.imageWxImg,
				imgadres: 'wxpay.png',
				pryType: '',
				paytype: '', //支付类型
				orderNo: '',
				user: '',
				couponId: '',
				jumpType: '',
				PaymentPassword: '',
				tongCardPrice: 0,
				goodsType: ''
			};
		},
		onLoad(e) {
			this.pryType = e.paytype;
			this.orderNo = e.orderId;
			this.couponId = e.couponId;
			this.jumpType = e.typeaout;
			this.goodsType = e.goodsType;
			if (this.pryType == 1) {
				this.paytype = 'wxpay';
			} else if (this.pryType == 2) {
				this.paytype = 'jinbi'
			} else {
				this.paytype = ''
			}
			this.amount = parseFloat(e.amount).toFixed(2);
		},
		onShow() {
			let that = this;
			that.user = app.globalData.userInfo;
			that.getHuiLianTongCardBalance();
		},
		methods: {
			//获取选择支付方式
			changeRiado() {
				if (!this.user.isSetHltCard) {
					uni.showToast({
						icon: 'none',
						title: '当前账号还未绑定,前往绑定',
						duration: 2000,
						success() {
							setTimeout(() => {
								uni.navigateTo({
									url: '../../user/bindingCard/bindingCard'
								})
							}, 1000)
						}
					});
					return;
				}
			},
			//查询工会卡余额
			//查询详情
			getHuiLianTongCardBalance() {
				let params = {
					cardNo: this.user.hltCardNo.cardNo
				}
				getHuiLianTongCardBalance(params).then(res => {
					if (res.return_code == '000000') {
						this.tongCardPrice = res.return_data.balance;
					}

				});
			},
			//获取订单数据
			orderToPay() {
				let that = this;
				if (that.paytype == 'wxpay') {

					let params = {
						"orderId": that.orderNo,
						"openId": app.globalData.openId,
						"orderScene": "GOODS_ORDER"
					}
					orderToPay(params).then(res => {
						if (res.return_code == '000000') {
							// #ifdef MP
							uni.showLoading({
								title: '支付中...'
							})
							uni.requestPayment({
								"appId": res.return_data.appId,
								"nonceStr": res.return_data.nonceStr,
								"package": res.return_data.package,
								"paySign": res.return_data.sign,
								"signType": "MD5",
								"timeStamp": res.return_data.timeStamp,
								success: function(res) {
									uni.hideLoading();
									uni.showToast({
										title: '支付成功'
									})
									if (that.jumpType == 4) {
										uni.reLaunch({
											url: '/pages/tabBar/user/user'
										});
										return;
									}
									uni.reLaunch({
										url: '../success/success?id=' + that.couponId
									});
								},
								fail: function(err) {
									uni.hideLoading();
								},
							});
							// #endif

							//判断是否是公众号
							// #ifdef H5
							//判断微信浏览器
							that.payRequest(res);
							// #endif

						} else {
							uni.showToast({
								title: res.return_msg,
								icon: 'none'
							})
						}
					})
				} else if (that.paytype == 'jinbi') {
					if (that.user.gold < that.amount * 100) {
						uni.showToast({
							icon: 'none',
							title: '积分不足',
							duration: 2000,
							success() {
								setTimeout(() => {
									uni.navigateTo({
										url:'../../user/deposit/deposit'
									})
								}, 1000)
							}
						});
						return;
					}
					if (!that.user.isSetPayPwd) {
						uni.navigateTo({
							url: '../../login/updatePas/updatePas'
						})
						return;
					}
					that.$refs.paymentPassword.modalFun('show');
				} else if (that.paytype == 'gonghuika') {
					if (that.tongCardPrice < that.amount) {
						uni.showToast({
							icon: 'none',
							title: '工会卡余额不足',
							duration: 2000,
						});
						return;
					}
					if (!that.user.isSetPayPwd) {
						uni.navigateTo({
							url: '../../login/updatePas/updatePas'
						})
						return;
					}
					if (!that.user.isSetHltCard) {
						uni.showToast({
							icon: 'none',
							title: '当前账号还未绑定,前往绑定',
							duration: 2000,
							success() {
								setTimeout(() => {
									uni.navigateTo({
										url: '../../user/bindingCard/bindingCard'
									})
								}, 1000)
							}
						});
						return;
					}
					that.$refs.paymentPassword.modalFun('show');
				} else {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'none',
						duration: 2000
					})
				}
			},
			submitHandle(e) {
				this.PaymentPassword = e.value;
				uni.showLoading({
					title: '支付中...'
				})
				if (this.paytype == 'jinbi') {
					let params = {
						"orderId": this.orderNo,
						"password": this.PaymentPassword
					}
					orderToGoldPay(params).then(res => {
						uni.hideLoading();
						if (res.return_code == '000000') {
							uni.showToast({
								title: '支付成功'
							})
							if (this.jumpType == 4) {
								uni.reLaunch({
									url: '/pages/tabBar/user/user'
								});
								return;
							}
							uni.reLaunch({
								url: '../success/success?id=' + this.couponId
							});

						}
						if (res.return_code == '102130') {
							uni.navigateTo({
								url: '../../login/updatePas/updatePas'
							})
							return;
						}

						uni.showToast({
							title: res.return_msg,
							icon: 'none'
						})
					})
					return;
				}
				let params = {
					"orderId": this.orderNo,
					"cardNo": this.user.hltCardNo.cardNo,
					"password": this.PaymentPassword
				}
				hltUnionCardPay(params).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {
						uni.showToast({
							title: '支付成功'
						})
						uni.reLaunch({
							url: '../success/success?id=' + this.couponId
						});
						return;
					}
					if (res.return_code == '102130') {
						uni.navigateTo({
							url: '../../login/updatePas/updatePas'
						})
						return;
					}
					uni.showToast({
						title: res.return_msg,
						icon: 'none'
					})
				})
			},
			payRequest: function(self) {
				uni.showLoading({
					title: '支付中...'
				})
				jweixin.config({
					// debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: self.return_data.appId, // 必填，公众号的唯一标识
					timestamp: self.return_data.timeStamp, // 必填，生成签名的时间戳
					nonceStr: self.return_data.nonceStr, // 必填，生成签名的随机串
					signature: self.return_data.sign, // 必填，签名，见附录1
					jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				uni.hideLoading();
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
						success: function(res) {},
						fail: function(res) {}
					});
					jweixin.chooseWXPay({
						appId: self.return_data.appId,
						timestamp: self.return_data
							.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
						nonceStr: self.return_data.nonceStr, // 支付签名随机串，不长于 32 位
						package: self.return_data
							.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
						signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
						paySign: self.return_data.sign, // 支付签名
						success: function(res) {
							// 支付成功后的回调函数
							uni.showToast({
								title: '支付成功'
							})
							if (that.jumpType == 4) {
								uni.reLaunch({
									url: '/pages/tabBar/user/user'
								});
								return;
							}
							uni.reLaunch({
								url: '../success/success?id=' + that.couponId
							});
						},
						cancel: function(r) {},
						fail: function(res) {}
					});
				});

				jweixin.error(function(res) {
					uni.showToast({
						icon: 'none',
						title: '支付失败了',
						duration: 4000
					});
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					/*alert("config信息验证失败");*/
				});
			},

		}
	}
</script>

<style lang="scss">
	.block {
		width: 94%;
		padding: 0 3% 40upx 3%;

		.title {
			width: 100%;
			font-size: 34upx;
		}

		.content {
			.orderinfo {
				width: 100%;
				border-bottom: solid 1upx #eee;

				.row {
					width: 100%;
					height: 90upx;
					display: flex;
					align-items: center;

					.nominal {
						flex-shrink: 0;
						font-size: 32upx;
						color: #7d7d7d;
					}

					.text {
						width: 70%;
						margin-left: 10upx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						font-size: 32upx;
					}
				}
			}

			.pay-list {
				width: 100%;
				border-bottom: solid 1upx #eee;

				.row {
					width: 100%;
					height: 120upx;
					display: flex;
					align-items: center;

					.left {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						align-items: center;

						image {
							width: 80upx;
							height: 80upx;
						}
					}

					.center {
						width: 100%;
						font-size: 30upx;
					}

					.right {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						justify-content: flex-end;
					}
				}
			}
		}
	}

	.pay {
		margin-top: 20upx;
		width: 100%;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;

		.btn {
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			background-color: #0083f5;
			box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.2);
		}

		.tis {
			margin-top: 10upx;
			width: 100%;
			font-size: 24upx;
			display: flex;
			justify-content: center;
			align-items: baseline;
			color: #999;

			.terms {
				color: #5a9ef7;
			}
		}
	}
</style>
