<template>
	<view class="app">
		<view class="bg">
		</view>
		<view class="moneyBox">
			<view class="price-box">
				<text>支付金额</text>
				<text class="price">{{ money }}</text>
			</view>
			<view class="yt-list">
				<view class="yt-list-cell b-b" v-if="fx" @click="payType='weixin'">
					<view class="cell-tit flex">
						<image class="orderIcon" src="../../../static/icon/orderWx.png" mode="widthFix"></image>
						<text class="margin-l-10">微信支付</text>
					</view>
					<image class="checked" v-if="payType=='weixin'" src="../../../static/icon/addressIconXz.png"
						mode="widthFix"></image>
					<view v-else class="noChecked"></view>
				</view>
				<!-- #ifdef APP-PLUS -->
				<view class="yt-list-cell b-b" @click="payType='ali'">
					<view class="cell-tit flex">
						<image class="orderIcon" src="../../../static/icon/orderAli.png" mode="widthFix"></image>
						<text class="margin-l-10">支付宝</text>
					</view>
					<image class="checked" v-if="payType=='ali'" src="../../../static/icon/addressIconXz.png"
						mode="widthFix">
					</image>
					<view v-else class="noChecked"></view>
				</view>
				<!-- #endif -->
				<view class="yt-list-cell" @click="payType='yue'">
					<view class="cell-tit flex">
						<image class="orderIcon" src="../../../static/icon/ye.png" mode="widthFix"></image>
						<text class="margin-l-10">余额({{now_money}})</text>
					</view>
					<image class="checked" v-if="payType=='yue'" src="../../../static/icon/addressIconXz.png"
						mode="widthFix">
					</image>
					<view v-else class="noChecked"></view>
				</view>
			</view>
		</view>
		<view class="base-buttom" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		balance
	} from '@/api/wallet.js';
	import {
		orderPay
	} from '@/api/order.js';
	// #ifdef H5
	import weixinObj from "@/plugin/jweixin-module/index.js";
	// #endif
	export default {
		data() {
			return {
				// #ifdef H5
				payType: 'ali',
				// #endif
				// #ifdef MP-WEIXIN
				payType: 'ali',
				// #endif
				money: 0.0, //订单金额
				now_money: 0.0, //余额
				orderId: '', //保存订单id
				payLoding: false, //判断是否支付中
				payType: 'ali',
				// #ifdef H5
				froms: '', //保存h5中数据来源对象
				// #endif
			};
		},
		onLoad(options) {
			// 判断是否为已创建单号订单
			this.type = 1;
			this.orderId = options.ordid;
			this.money = options.money;
			// 载入余额
			balance({}).then(({
				data
			}) => {
				// 获取余额
				this.now_money = data.now_money;
			});
		},
		computed:{
			...mapState(['fx'])
		},
		methods: {
			// 支付金额
			orderMoneyPay() {
				let obj = this;
				orderPay({
						uni: obj.orderId,
						// #ifdef H5
						from: obj.froms ? 'weixin' : 'H5', //来源
						// #endif
						// #ifdef MP-WEIXIN
						from: 'routine', //来源
						// #endif
						// #ifdef APP-PLUS
						from: 'app', //来源
						// #endif
						paytype: obj.payType //支付类型  weixin-微信 yue-余额
					})
					.then(e => {
						// 判断是否为余额支付
						if (obj.payType == 'yue' && e.data.status == 'SUCCESS') {
							if (e.status == 200) {
								obj.paySuccessTo();
							} else {
								obj.$api.msg(msg);
							}
						}
						if (obj.payType == 'weixin') {
							let da = e.data.result.jsConfig;
							let data = {
								// #ifdef H5
								timestamp: da.timestamp,
								// #endif
								// #ifdef MP
								timeStamp: da.timestamp,
								// #endif
								nonceStr: da.nonceStr,
								package: da.package,
								signType: da.signType,
								paySign: da.paySign,
								success: function(res) {
									obj.paySuccessTo();
								},
								fail: () => {
									uni.navigateTo({
										url: '/pages/order/order?state=0'
									});
								}
							};
							// #ifdef H5
							if (obj.payType == 'weixin') {
								weixinObj.chooseWXPay(data);
							}
							// #endif
							// #ifdef MP-WEIXIN
							if (obj.payType == 'weixin') {
								wx.requestPayment(data);
							}
							// #endif
						}
						uni.hideLoading();
						obj.payLoding = false;
					})
					.catch(e => {
						// 支付完成
						uni.hideLoading();
						obj.payLoding = false;
						console.log(e);
					});
			},
			// 支付成功跳转
			paySuccessTo() {
				uni.hideLoading();
				uni.redirectTo({
					url: './paySuccess?orderid=' + this.orderId
				});
			},
			//确认支付
			confirm: async function() {
				let obj = this;
				uni.showLoading({
					title: '支付中',
					mask: true
				})
				// 判断是否余额不足
				if (obj.payType == 'yue' && +obj.now_money < obj.money) {
					uni.showModal({
						title: '提示',
						content: '账户余额不足！',
						showCancel: false,
						success: res => {},
						fail: () => {},
						complete: () => {}
					});
					return;
				}
				// 支付中
				obj.payLoding = true;
				// #ifdef H5
				// 获取当前是否为微信浏览器
				obj.froms = uni.getStorageSync('weichatBrowser') || '';
				// #endif
				// 判断是否为未支付订单中跳转进入
				obj.orderMoneyPay();
			}
		}
	};
</script>

<style lang="scss">
	.app {
		width: 100%;
		padding-top: 30rpx;
	}

	.moneyBox {
		position: relative;
		margin: 0 $page-row-spacing;
		border-radius: 20rpx;
		overflow: hidden;
		background-color: #FFFFFF;
	}

	.bg {
		background-color: $color-green;
		width: 950rpx;
		position: absolute;
		left: -100rpx;
		border-bottom-left-radius: 1000rpx;
		border-bottom-right-radius: 1000rpx;
		top: 0rpx;
		height: 300rpx;
	}

	.price-box {
		background-color: #fff;
		height: 200rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.iconyue {
			color: #fe8e2e;
		}

		.iconweixin {
			color: #36cb59;
		}

		.iconzhifubao {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.clickbg {
		background-color: $color-gray !important;
	}

	.yt-list {
		background: #fff;
		margin: 0 $page-row-spacing;
		border-radius: 20rpx;
		padding-bottom: 20rpx;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10rpx 30rpx 10rpx 40rpx;
		line-height: 70rpx;
		position: relative;

		.checked,
		.noChecked {
			width: 36rpx;
			height: 36rpx;
		}

		.noChecked {
			border: 1px solid $font-color-light;
			border-radius: 100rpx;
		}

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30rpx;
		}

		.cell-icon {
			height: 32rpx;
			width: 32rpx;
			font-size: 22rpx;
			color: #fff;
			text-align: center;
			line-height: 32rpx;
			background: #f85e52;
			border-radius: 4rpx;
			margin-right: 12rpx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}
		}

		.cell-more {
			align-self: center;
			font-size: 24rpx;
			color: $font-color-light;
			margin-left: 8rpx;
			margin-right: -10rpx;
		}

		.cell-tit {
			font-size: 26rpx;
			color: $font-color-light;
			margin-right: 10rpx;

			.orderIcon {
				width: 48rpx;
			}
		}

		.cell-tip {
			font-size: 26rpx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90rpx;
			}
		}

		.desc {
			text-align: right;
			font-size: $font-base;
			color: $font-color-light;
		}
	}
</style>
