<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="orderinfo">
					<view class="row">
						<view class="nominal">支付金额:</view>
						<view class="text">{{payPrice}} 元</view>
					</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				选择支付方式
			</view>
			<view class="content">
				<view class="pay-list">
					<view class="width100" v-for="(item,index) in paytypeList" :key="index">
						<view class="row" v-if="item == 3" @tap="paytype='3'">
							<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=='3'" @click="changeRiado()" color="#0083f5" />
							</view>
						</view>
						<view class="row" v-if="item == 4" @tap="paytype='4'">
							<view class="left">
								<image src="../../../static/img/unionpay.png"></image>
							</view>
							<view class="center">
								银联支付
							</view>
							<view class="right">
								<radio :checked="paytype=='4'" color="#0083f5" />
							</view>
						</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 {
		getHuiLianTongCardBalance,
		hltUnionCardPayghk,
		findUser,
		findById,
		orderToUnionPay
	} from '../../../Utils/Api.js'
	import ssPaymentPassword from '@/components/sanshui-payment-password/index.vue';
	let app = getApp();
	export default {
		components: {
			ssPaymentPassword
		},
		data() {
			return {
				amount: 0,
				imagewxUrl: app.globalData.imageWxImg,
				orderNo: '',
				goodsid: '', //商品id
				user: '',
				PaymentPassword: '',
				tongCardPrice: 0,
				payPrice: '', //支付价格
				oilPirce: 0, //油卡金额
				paytype: '', //支付方式
				paytypeList: [], //支付方式
				rechargeDes: '', // 话费详情
			};
		},
		onLoad(e) {
			this.orderNo = e.orderId;
			this.goodsid = e.goodsId;
			this.payPrice = e.payPrice;
			if (!app.globalData.userInfo) {
				this.findUser();
			}
		},
		onShow() {
			let that = this;
			that.user = app.globalData.userInfo;
			if (this.user.hltCardNo) {
				that.getHuiLianTongCardBalance();
			}
			that.findById();
			if (app.globalData.userInfo) {
				that.findUser();
			}
		},
		methods: {
			//查询详情
			findById() {
				uni.showLoading({
					title: '加载中'
				})
				let datas = {
					platformId: 3,
					id: this.goodsid
				}
				findById(datas).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {
						this.rechargeDes = res.return_data;
						this.paytypeList = res.return_data.productPayTypeString.split(',');
						this.paytypeList = this.paytypeList.slice(0, this.paytypeList.length - 1);
					}
				});
			},
			//查询我的信息
			findUser() {
				let params;
				findUser(params).then(res => {
					if (res.return_code == '000000') {
						app.globalData.userInfo = res.return_data;
						this.user = res.return_data;
						if (res.return_data.oilCard) {
							this.oilPirce = res.return_data.oilCard.amount;
						}
						uni.setStorage({
							key: "user",
							data: res.return_data
						})
					}
				});
			},
			//获取选择支付方式
			changeRiado() {
				if (!this.user.isSetHltCard) {
					uni.showToast({
						icon: 'none',
						title: '当前账号还未绑定,前往绑定',
						duration: 2000,
						success() {
							setTimeout(() => {
								uni.navigateTo({
									url: '../../pages/user/bindingCard/bindingCard'
								})
							}, 1000)
						}
					});
					return;
				}
			},
			//获取订单信息
			orderToUnionPay() {
				let params = {
					"orderId": this.orderNo
				}
				orderToUnionPay(params).then(res => {
					if (res.return_code == '000000') {
						this.uniontopay(res.return_data.prepayid);
					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none'
						})
					}
				})
			},
			//银联支付
			uniontopay(item) {
				let that = this;
				upsdk.pluginReady(function() {
					upsdk.pay({
						tn: item,
						success: function(res) {
							uni.showToast({
								title: '支付成功'
							})
							uni.reLaunch({
								url: '../Phone-recharge-details/Phone-recharge-details?id=' +
									that.orderId
							})
						},
						fail: function(err) {
							uni.navigateBack({})
							uni.showToast({
								title: err.msg,
								icon: 'none',
								duration: 2000
							})
						}
					});
				});
			},
			//查询工会卡余额
			//查询详情
			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 == '3') {
					if (that.tongCardPrice < that.payPrice) {
						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');
					return;
				}
				if (that.paytype == '4') {
					that.orderToUnionPay();
					return;
				}
				uni.showToast({
					title: '请选择支付方式',
					icon: 'none',
					duration: 2000
				})
			},
			submitHandle(e) {
				this.PaymentPassword = e.value;
				uni.showLoading({
					title: '支付中...'
				})
				if (this.paytype == '3') {
					let params = {
						"orderId": this.orderNo,
						"cardNo": this.user.hltCardNo.cardNo,
						"password": this.PaymentPassword
					}
					hltUnionCardPayghk(params).then(res => {
						uni.hideLoading();
						if (res.return_code == '000000') {
							uni.showToast({
								title: '支付成功'
							})
							uni.reLaunch({
								url: '../Phone-recharge-details/Phone-recharge-details?id=' +
									that.orderNo
							})
							return;
						}
						if (res.return_code == '102130') {
							uni.navigateTo({
								url: '../../pages/login/updatePas/updatePas'
							})
							return;
						}
						uni.showToast({
							title: res.return_msg,
							icon: 'none'
						})
					})
					return;
				}
			}
		}
	}
</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>
