<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="orderinfo">
					<!-- <view class="row">
						<view class="nominal">订单名称:</view>
						<view class="text">{{orderName}}</view>
					</view> -->
					<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}} 个金币 (当前金币:{{user.gold}})</view>
						<view class="text" v-if="pryType == 3">{{amount}}元(当前金币:{{user.gold}})</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="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNjE2ODUyMjc2NDc1IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjI0MDMiIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGRlZnM+PHN0eWxlIHR5cGU9InRleHQvY3NzIj48L3N0eWxlPjwvZGVmcz48cGF0aCBkPSJNNTEyIDUwOS4xbS00NDQuNCAwYTQ0NC40IDQ0NC40IDAgMSAwIDg4OC44IDAgNDQ0LjQgNDQ0LjQgMCAxIDAtODg4LjggMFoiIGZpbGw9IiNGQUIwMDEiIHAtaWQ9IjI0MDQiPjwvcGF0aD48cGF0aCBkPSJNODg3LjcgNDU5LjFjM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TEuNi0yNS45LTI1LjktMjUuOWgtMzNjLTE0LjMgMC0yNS45IDExLjYtMjUuOSAyNS45djE0MS42YzAgMTQuMy0xMS42IDI1LjktMjUuOSAyNS45aC03LjNjLTE0LjMgMC0yNS45LTExLjYtMjUuOS0yNS45VjQ1OGMwLTE0LjMgMTEuNi0yNS45IDI1LjktMjUuOWg5Mi4yYzE0LjMgMCAyNS45LTExLjYgMjUuOS0yNS45di0xMC45YzAtMTQuOC0xMi40LTI2LjYtMjcuMi0yNS45LTM4LjggMS43LTc3LjUgMi43LTExNC42IDMtMTIuMSAwLjEtMjIuOS04LjMtMjUuNC0yMC4xIDAtMC4yLTAuMS0wLjQtMC4xLTAuNi0zLjQtMTYuMSA4LjMtMzEuNCAyNC44LTMxLjggMTE4LTIuNyAyNDUuOS0xMC42IDMzOC4zLTIyLjggMTEuNi0xLjUgMjIuNyA0LjkgMjcuMiAxNS43bDEuMSAyLjZjNi42IDE1LjgtMy40IDMzLjYtMjAuNCAzNS44LTM2LjkgNC45LTc3LjIgOC43LTExOS4zIDExLjktMTMuNSAxLTIzLjkgMTIuMy0yMy45IDI1Ljl2MTcuM2MwIDE0LjMgMTEuNiAyNS45IDI1LjkgMjUuOWg5N2MxNC4zIDAgMjUuOSAxMS42IDI1LjkgMjUuOXYxNTguM3oiIGZpbGw9IiNGRkZGRkYiIHAtaWQ9IjI0MDciPjwvcGF0aD48L3N2Zz4=">
							</image>
						</view>
						<view class="center">
							金币支付
						</view>
						<view class="right">
							<radio :checked="paytype=='jinbi'" color="#f06c7a" />
						</view>
					</view>
					<view class="row" @tap="paytype='wxpay'" v-if="pryType == 1 || pryType==3 ">
						<view class="left">
							<image src="/static/img/wxpay.png"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay'" color="#f06c7a" />
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="pay">
			<view class="btn" @tap="orderToPay">立即支付</view>
			<view class="tis">
				点击立即支付，即代表您同意<view class="terms">
					《条款协议》
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		orderToPay,
		orderToGoldPay
	} from '../../../Utils/Api.js'
	let app = getApp()
	export default {
		data() {
			return {
				amount: 0,
				orderName: '',
				pryType: '',
				paytype: '', //支付类型
				orderNo: '',
				user: '',
				couponId : ''
			};
		},
		onLoad(e) {
			this.pryType = e.paytype;
			this.orderNo = e.orderId;
			this.couponId = e.couponId;
			this.user = app.globalData.userInfo;
			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);
			uni.getStorage({
				key: 'paymentOrder',
				success: (e) => {
					if (e.data.length > 1) {
						this.orderName = '多商品合并支付'
					} else {
						this.orderName = e.data[0].name;
					}
					uni.removeStorage({
						key: 'paymentOrder'
					})
				}
			})
		},
		methods: {
			//获取订单数据
			orderToPay() {
				let that = this;
				if (that.paytype == 'wxpay') {
					uni.showLoading({
						title: '支付中...'
					})
					let params = {
						"orderId": that.orderNo,
						"openId": app.globalData.openId,
						"orderScene": "GOODS_ORDER"
					}
					orderToPay(params).then(res => {
						if (res.return_code == '000000') {
						
							uni.pay
							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: '支付成功'
									})
									uni.reLaunch({
										url: '../success/success?id=' + that.couponId
									});
								},
								fail: function(err) {
									uni.hideLoading();
									console.log('fail:' + JSON.stringify(err));
								}
							});
						} else {
							uni.showToast({
								title: res.return_msg,
								icon: 'none'
							})
						}
					})
				} else if (that.paytype == 'jinbi') {
					uni.showModal({
						title: '金币支付',
						content: '确认是否用金币支付？',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '支付中...'
								})
								let params = {
									"orderId": that.orderNo
								}
								orderToGoldPay(params).then(res => {
									if (res.return_code == '000000') {
										uni.hideLoading();
										uni.showToast({
											title: '支付成功'
										})
										uni.reLaunch({
											url: '../success/success?id=' + that.couponId
										});

									} else {
										uni.showToast({
											title: res.return_msg,
											icon: 'none'
										})
									}
								})
							} else if (res.cancel) {}
						}
					});
				} else {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'none',
						duration: 2000
					})
				}
			},
		}
	}
</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: #f06c7a;
			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>
