<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{ money }}</text>
		</view>
		<view class="pay-type-list">
			<view class="type-item b-b" @click="changePayType(1)">
				<text class="icon iconfont iconweixin"></text>
				<view class="con">
					<text class="tit">微信支付</text>
					<text>推荐使用微信支付</text>
				</view>
				<label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 1"></radio></label>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="type-item b-b" @click="changePayType(2)">
				<text class="icon iconfont iconzhifubao"></text>
				<view class="con"><text class="tit">支付宝支付</text></view>
				<label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 2"></radio></label>
			</view>
			<!-- #endif -->
			<view class="type-item" @click="changePayType(3)">
				<text class="icon iconfont iconyue"></text>
				<view class="con">
					<text class="tit">余额支付</text>
					<text>可用余额 ¥{{ now_money }}</text>
				</view>
				<label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 3"></radio></label>
			</view>
		</view>
		<text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
	</view>
</template>

<script>
import { computedOrderkey, balance, createOrderkey, orderPay } from '@/api/money.js';
import { bvip } from '@/api/user.js';
import { mapState } from 'vuex';
export default {
	data() {
		return {
			payType: 1, //支付类型
			// #ifdef H5
			payName: 'weixin',
			// #endif
			// #ifdef MP-WEIXIN
			payName: 'weixin',
			// #endif
			orderInfo: {},
			money: 0.0, //订单金额
			now_money: 0.0, //余额
			orderKey: '',
			orderId: '', //保存订单id
			payLoding: false, //判断是否支付中
			type: '', //判断是否从订单中进入
			// #ifdef H5
			froms: '', //保存h5中数据来源对象
			// #endif
			pinkid: '', //保存拼团商品id
			typegoods:'',//区分普通和拼团商品
			level:'',//用户等级
			use_integral_pink: '',
			id: '', // 地址编号
			goodstype: '',	// 商品类型
		};
	},
	computed: {
		// #ifdef H5
		...mapState(['weichatObj','userInfo'])
		// #endif
	},
	onLoad(options) {
		console.log(options,99999)
		this.goodstype = options.goodsType;
		if (options.type == 1) {
			this.type = 1;
			this.orderId = options.ordid;
			this.money = options.money;
		} else {
			this.orderKey = options.key;
			this.id = options.id;
			console.log(this.orderKey,989)
			this.use_integral_pink = options.use_integral_pink;
			let prepage = this.$api.prePage();
			// console.log(prepage, 88);
			computedOrderkey({
				orderkey: this.orderKey,
				use_integral_pink: this.use_integral_pink,
				// couponId: prepage.couponChecked.id, //优惠券编号
				useShopping_card: prepage.shoppingPoints ? 1 : 0, //是否使用购物卡
				addressId: this.id, //地址编号
				useIntegral: prepage.checkedPoints ? 1 : 0
			},this.orderKey).then(({ data }) => {
				console.log(data)
				// 获取支付金额
				this.money = data.result.pay_price;
			});
		}
		// 保存pinkid
		if (options.pinkid) {
			this.pinkid = options.pinkid;
		}
		//区分普通商品和拼团商品
		if(options.typegoods){
			this.typegoods = options.typegoods;
		}
		//保存用户等级
		if(options.level){
			this.level = options.level;
		}
		
		// 载入余额
		balance({}).then(({ data }) => {
			// 获取余额
			this.now_money = data.now_money;
		});
	
	},
	methods: {
		//选择支付方式
		changePayType(type) {
			// console.log(type, 88);
			this.payType = type;
			if (this.payType == 1) {
				// #ifdef H5
				this.payName = 'weixin';
				// #endif
				// #ifdef MP-WEIXIN
				this.payName = 'weixin';
				// #endif
				// #ifdef APP-PLUS
				this.payName = 'weixin';
				// #endif
			}
			if (this.payType == 2) {
				this.payName = 'ali';
			}
			if (this.payType == 3) {
				this.payName = 'yue';
			}
		},
		// 支付金额
		orderMoneyPay() {
			console.log(55555555555555555)
			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.payName //支付类型  weixin-微信 yue-余额
			}).then(e => {
					// 判断是否为余额支付
					if (obj.payName == 'yue') {
						if (e.status == 200) {
							obj.paySuccessTo();
						} else {
							alert(e.msg);
							obj.$api.msg(e.msg);
						}
					}
					// #ifdef APP-PLUS
					if(obj.type == 'ali')
					{
						uni.requestPayment({
						    provider: 'alipay',
						    orderInfo: 'orderInfo', //微信、支付宝订单数据
						    success: function (res) {
						        console.log('success:' + JSON.stringify(res));
						    },
						    fail: function (err) {
						        console.log('fail:' + JSON.stringify(err));
						    }
						});
					}else{
						uni.requestPayment({
						    provider: 'weixin',
						    orderInfo: 'orderInfo', //微信、支付宝订单数据
						    success: function (res) {
						        console.log('success:' + JSON.stringify(res));
						    },
						    fail: function (err) {
						        console.log('fail:' + JSON.stringify(err));
						    }
						});
					}
					
					
					
					// #endif
					
					// #ifdef H5
					if (obj.payName == 'weixin') {
						let da = e.data.result.jsConfig;
						obj.weichatObj.chooseWXPay({
							timestamp: da.timestamp,
							nonceStr: da.nonceStr,
							package: da.package,
							signType: da.signType,
							paySign: da.paySign,
							success: function(res) {
								obj.paySuccessTo();
							},
							fail: function (res) {
								console.log(res,"失败")
								console.log(res.errMsg)	
							}
						});
					}
					// #endif
					// #ifdef MP-WEIXIN
					if (obj.payName == 'weixin') {
						let da = e.data.result.jsConfig;
						wx.requestPayment({
							timeStamp: da.timestamp,
							nonceStr: da.nonceStr,
							package: da.package,
							signType: da.signType,
							paySign: da.paySign,
							success: function(res) {
								obj.paySuccessTo();
							}
						});
					}
					// #endif
					obj.payLoding = false;
				})
				.catch(e => {
					// 支付完成
					alert(e.message+";file="+e.file+";line="+e.line);
					obj.payLoding = false;
					console.log(e.message)
					if(e.message == "该订单已支付!"){
						obj.paySuccessTo();
					}
				});
		},
		// 支付成功跳转
		paySuccessTo() {
			let url = '/pages/money/paySuccess?orderid=' + this.orderId + '&goodstype=' + this.goodstype + '&money=' + this.money
			console.log();
			if(!this.typegoods){
				url += "&newGoods=true"
			}
			uni.redirectTo({
				url
			});
		},
		//确认支付
		confirm: async function() {
			let obj = this;
			// 判断是否余额不足
			if (obj.payName == 'yue' && +obj.now_money < obj.money) {
				uni.showModal({
					title: '提示',
					content: '账户余额不足！',
					showCancel: true,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
				return;
			}
			// 支付中
			obj.payLoding = true;
			// #ifdef H5
			// 获取当前是否为微信浏览器
			obj.froms = uni.getStorageSync('weichatBrowser') || '';
			// #endif
			// 判断是否为未支付订单中跳转进入
			if (obj.type != 1) {
				// 初次生成订单
				obj.firstCreateOrder();
			} else {
				// 已经生成订单未支付
				obj.orderMoneyPay();
			}
		},
		// 初次订单创建
		firstCreateOrder() {
			let obj = this;
			// 获取下单页面数据
			let prepage = obj.$api.prePage();
			let data = {
				use_integral_pink: obj.use_integral_pink,
				real_name: prepage.addressData.real_name, //联系人名称
				phone: prepage.addressData.phone, //联系人号码
				couponId: prepage.couponChecked.id, //优惠券编号
				addressId: prepage.addressData.id, //支付地址id
				useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
				useShopping_card: prepage.shoppingPoints ? 1 : 0, //是否使用购物卡
				payType: obj.payName, //支付类型  weixin-微信 yue-余额
				mark: prepage.idcardName+prepage.idcardNum, //备注
				// #ifdef H5
				from: obj.froms ? 'weixin' : 'H5', //来源
				// #endif
				// #ifdef MP-WEIXIN
				from: 'routine', //来源
				// #endif
				// #ifdef APP-PLUS
				from: 'app', //来源
				// #endif
				shipping_type: 1, //提货方式 1 快递 2自提
			};
			// 判断是否拼团商品
			if (obj.typegoods == 6) {
				data.level = obj.level;
			}else{
				data.pinkId = obj.pinkid;
			}
			// 生成订单
			createOrderkey(data, obj.orderKey)
				.then(({ data, status, msg }) => {
					// 保存订单号
					obj.orderId = data.result.orderId;
					
					// 允许支付
					obj.payLoding = false;
					if (data.status == 'ORDER_EXIST') {
						obj.$api.msg(msg);
						return;
					}
					// 判断是否为余额支付
					if (obj.payName == 'yue') {
						if (status == 200&&data.status == 'SUCCESS') {
							obj.paySuccessTo();
						} else {
							alert(msg);
							obj.$api.msg(msg)							
						}
					} else {
						// 立即支付
						obj.orderMoneyPay();
					}
				})
				.catch(e => {
					alert(e.message);
					console.log(e);
				});
		}
	}
};
</script>

<style lang="scss">
.app {
	width: 100%;
}

.price-box {
	background-color: #fff;
	height: 265upx;
	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;
	}
}
.mix-btn {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 630upx;
	height: 80upx;
	margin: 80upx auto 30upx;
	font-size: $font-lg;
	color: #fff;
	background-color: $base-color;
	background:linear-gradient(0deg,rgba(250,39,64,1),rgba(254,85,68,1));
	border-radius: 10upx;
}

.clickbg {
	background-color: $color-gray !important;
}
</style>
