<template>
	<view :class="[AppTheme]" class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price text-neutral">{{ payprice }}</text>
		</view>

		<view class="pay-type-list">
			<view class="type-item b-b" @click="changePayType(1)">
				<text class="icon yticon icon-weixinzhifu"></text>
				<view class="con">
					<text class="tit">微信支付</text>
					<text>推荐使用微信支付</text>
				</view>
				<label class="radio">
					<radio value="" :color="primary" :checked="payType == 1" />
				</label>
			</view>
			<!-- 			<view class="type-item b-b" @click="changePayType(2)">
				<text class="icon yticon icon-alipay"></text>
				<view class="con">
					<text class="tit">支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="" color="#56AB2F" :checked='payType == 2' />
					</radio>
				</label>
			</view> -->
			<view class="type-item" @click="changePayType(2)">
				<text class="icon yticon icon-erjiye-yucunkuan"></text>
				<view class="con">
					<text class="tit">余额支付</text>
					<text>可用余额 ¥{{ money }}</text>
				</view>
				<label class="radio">
					<radio value="" :color="primary" :checked="payType == 2" />
				</label>
			</view>
		</view>

		<text class="mix-btn bg-linear-gradient" @click="confirm">确认支付</text>
		<u-keyboard default="" ref="uKeyboard" mode="number" :mask="true" :mask-close-able="false" :dot-enabled="false"
			v-model="show" :safe-area-inset-bottom="true" :tooltip="false" @change="onChange" @backspace="onBackspace">
			<view>
				<view class="u-text-center u-padding-20 money">
					<text>{{ payprice }}</text>
					<text class="u-font-20 u-padding-left-10">元</text>
					<view class="u-padding-10 close" data-flag="false" @tap="showPop(false)">
						<u-icon name="close" color="#333333" size="28"></u-icon>
					</view>
				</view>
				<view class="u-flex u-row-center">
					<u-code-input v-model="password" mode="box" :maxlength="6" :dot="true"></u-code-input>
				</view>
				<view class="u-text-center u-padding-top-10 u-padding-bottom-20 tips">支付键盘</view>
			</view>
		</u-keyboard>
	</view>
</template>

<script>
	import wallApi from '@/api/wall/index.js';
	import cart from '@/api/cart/index.js';
	import api from '@/api/mall/index.js';
	import adoptApi from '@/api/home/index.js';
	import mineapi from '@/api/mine/index.js';
	export default {
		data() {
			return {
				primary:this.$theme.primary,
				settingFile: getApp().globalData.siteinfo,
				payType: 1,
				orderInfo: {},
				money: '',
				show: false,
				password: '',
				payprice: '',
				orderid: '',
				djqid: '',
				oid: '',
				additionalpayment: false,
				orderidpayaga: 0,
				addseedonparms: false,
				paypricetext: '',
				params: {
					// 支付方式
					paytype: 1, //为1是微信支付 2是支付宝支付
					rid: '',
					systype: '', //h5 或 app
					money: 1
				}

			};
		},
		computed: {},
		onLoad(options) {
			let parms = options
			this.payprice = parms.payprice;
			this.params.money = Number(this.payprice)
			console.log(this.params.money);
			this.paypricetext = this.payprice + '元';
			// this.payType = parms.payType;
			console.log(this.payType);
			this.orderidpayaga = parms.orderidpayaga;
			if (parms.addseedon) {
				this.addseedonparms = parms;
			}
			if (parms.additionalpayment !== undefined && parms.additionalpayment !== null) {
				this.additionalpayment = parms.additionalpayment;
			}
			wallApi.income({}).then(res => {
				if (res.status == 200) {
					this.money = res.data.money; //余额
				}
			});
		},

		methods: {
			// #ifdef H5
			wxPay2(paydata) {
				console.log(111);
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						'appId': paydata.appId, //公众号id名称，由商户传入
						'timeStamp': paydata.timeStamp, //时间戳     
						'nonceStr': paydata.nonceStr, //随机串     
						'package': paydata.package,
						'signType': paydata.signType, //微信签名方式：     
						'paySign': paydata.paySign //微信签名 
					},
					function(res) {
						if (res.err_msg === 'get_brand_wcpay_request:ok') {
							uni.showToast({
								title: '购买会员成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1000)
						} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
							uni.showToast({
								icon: 'none',
								title: '已取消支付'
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '支付失败'
							})
						}
					})
			},
			// #endif
			//选择支付方式
			changePayType(type) {
				this.payType = type;
			},
			//确认支付
			confirm() {


				// // #ifdef H5
				//    // H5支付
				//    console.log('进来了');
				//    this.params.systype = 'h5'
				//    if (this.params.paytype === 1) {
				// 	   this.params.paytype = 'wxpay'
				//    	//paytype为1使用微信支付
				//    api.singleland(this.params).then(res => {
				// 		this.params.paytype = 1
				//    		if (res.status == 200) {
				// 			
				//    			this.wxPay(res.data)
				//    		} else {
				//    			uni.showToast({
				//    				icon: 'none',
				//    				title: res.msg
				//    			})
				//    		}
				//    	})
				//    } 
				// // #endif


				// // #ifdef APP-PLUS
				// if(this.payType == 1){
				// 	let orderInfo = JSON.stringify(this.params)
				// 	      this.params.systype = 'app'
				// 	      let payplatform = ''
				// 	      if (this.params.paytype === 1 || this.params.paytype === 2) {

				// 	      	//paytype为1使用微信支付，为2使用支付宝支付
				// 	      	payplatform = this.params.paytype === 1 ? 'wxpay' : 'alipay'
				// 			this.params.paytype = 'wxpay'
				// 			console.log(this.params);
				// 	      	wallApi.Wxpaycz(this.params).then(res => {
				// 				this.params.paytype = 1
				// 				
				// 	      		if (res.code === 1) {
				// 	      			uni.requestPayment({
				// 	      				provider: payplatform,
				// 	      				orderInfo: res.data,
				// 	      				//微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
				// 	      				success: function(res) {
				// 							
				// 	      					uni.showToast({
				// 	      						title: '购买会员成功',
				// 	      						icon: 'none'
				// 	      					})
				// 	      					setTimeout(() => {
				// 	      						uni.navigateBack({
				// 	      							delta: 1
				// 	      						})
				// 	      					}, 1000)
				// 	      				},
				// 	      				fail: function(err) {
				// 							console.log(err);
				// 	      					uni.showToast({
				// 	      						title: err.errMsg,
				// 	      						icon: 'none'
				// 	      					})
				// 	      				}
				// 	      			})
				// 	      		} else {
				// 	      			uni.showToast({
				// 	      				title: res.msg,
				// 	      				icon: 'none'
				// 	      			})
				// 	      		}
				// 	      	})
				// 	      } else {
				// 	      	// paytype为3,使用余额支付
				// 	      	// this.balancepayment(this.params)
				// 			console.log('错误');
				// 	      }
				// }

				// // #endif




				//判断后台是否开启实名认证，如果有则跳转去实名认证完毕后，否则跳转去设置支付密码
				api.getrecation().then(res => {
					if (res.data === 1) {
						//已开启实名认证,进行验证
						mineapi.vipIndex().then(res => {
							if (res.data.data.is_auth === 0) {
								//未认证，跳转认证页面
								uni.showToast({
									title: '未认证请先认证'
								});
								setTimeout(() => {
									uni.redirectTo({
										url: '/pagesD/pages/set/renzheng/renzheng'
									});
								}, 1000);
							} else if (res.data.data.is_auth === 1) {
								//审核中，显示一下
								uni.showToast({
									title: '资料正在审核中...'
								});
							} else if (res.data.data.is_auth === 2) {
								//审核通过，正常功能
								//开启支付键盘
								if (this.payType == 2) {
									this.show = true;
									this.password = '';
								} else {
									this.goumai(this.payType);
								}
							} else {
								//审核驳回，重新提交认证
								uni.showToast({
									title: '认证审核驳回'
								});
								setTimeout(() => {
									uni.redirectTo({
										url: '/pages/index/index'
									});
								}, 1000);
							}
						});
					} else {
						//开启支付键盘
						if (this.payType == 2) {
							this.show = true;
							this.password = '';
						} else {
							this.goumai(this.payType);
						}
					}
				});

			},
			onChange(val) {
				if (this.password.length < 6) {
					this.password += val;
				}
				if (this.password.length >= 6) {
					this.pay();
				}
			},
			onBackspace(e) {
				if (this.password.length > 0) {
					this.password = this.password.substring(0, this.password.length - 1);
				}
			},
			//支付键盘验证
			pay() {
				let that = this;
				uni.showLoading({
					title: '支付中'
				});
				cart.checkPayPassword({
					password: this.password
				}).then(res => {
					uni.hideLoading();
					if (res.status === 200) {
						this.goumai(this.payType);
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						});
						if (res.msg === '未设置支付密码，现在进行设置!') {
							setTimeout(() => {
								uni.navigateTo({
									url: "/pagesD/pages/set/changemima/changemima"
								})
							}, 1000);
						}
					}
				});
			},
			goumai(pay, password) {
				let that = this;
				let parms = {};
				//是否是追加种子支付
				if (that.addseedonparms.addseedon) {
					let parms = {
						orderid: that.addseedonparms.id,
						seeds: that.addseedonparms.seeds,
						payType: that.addseedonparms.payType,
						price: that.addseedonparms.payprice
					};
					//追加种子发起请求
					console.log(parms);
					return;
				}
				//判断是否是未付款补付
				if (this.additionalpayment) {
					parms = {
						id: that.orderidpayaga,
						type_pay: pay
					};
					if (pay === 1) {
						wxpay(parms);
					} else {
						money(parms);
					}
				} else {
					/* 参数 */
					parms = this.$store.state.updaordermess;
					console.log(parms);
					if (parms.seeds[0].seeditem !== null && parms.seeds[0].seeditem !== undefined) {
						let seeds = [];
						parms.seeds.forEach(item => {
							let seeditem = {
								seed_id: 0,
								seed_num: 0
							};
							seeditem.seed_id = item.seeditem.id;
							seeditem.seed_num = item.seednum;
							seeds.push(seeditem);
						});
						parms.seeds = JSON.stringify(seeds);
					}
					parms.type_pay = pay;
					if (pay === 1) {
						wxpay(parms);
					} else {
						money(parms);
					}
				}

				function wxpay(parms) {
					console.log(parms);
					// #ifdef H5
					parms['systype'] = 'h5'
					// #endif
					// #ifdef APP-PLUS
					parms['systype'] = 'app'
					// #endif




					// // #ifdef APP-PLUS
					// if(this.payType == 1){
					// 	// let orderInfo = JSON.stringify(this.params)
					// 	      // this.params.systype = 'app'
					// 	      let payplatform = ''
					// 	      if (this.params.paytype === 1 || this.params.paytype === 2) {
					// 			  console.log('222');
					// 	      	//paytype为1使用微信支付，为2使用支付宝支付
					// 	      	payplatform = this.params.paytype === 1 ? 'wxpay' : 'alipay'
					// 			// this.params.paytype = 'wxpay'
					// 			console.log(this.params);
					// 	      	wallApi.Wxpaycz(parms).then(res => {
					// 				
					// 				this.params.paytype = 1
					// 				
					// 	      		if (res.code === 1) {
					// 	      			uni.requestPayment({
					// 	      				provider: payplatform,
					// 	      				orderInfo: res.data,
					// 	      				//微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
					// 	      				success: function(res) {
					// 							
					// 	      					uni.showToast({
					// 	      						title: '购买会员成功',
					// 	      						icon: 'none'
					// 	      					})
					// 	      					setTimeout(() => {
					// 	      						uni.navigateBack({
					// 	      							delta: 1
					// 	      						})
					// 	      					}, 1000)
					// 	      				},
					// 	      				fail: function(err) {
					// 							console.log(err);
					// 	      					uni.showToast({
					// 	      						title: err.errMsg,
					// 	      						icon: 'none'
					// 	      					})
					// 	      				}
					// 	      			})
					// 	      		} else {
					// 	      			uni.showToast({
					// 	      				title: res.msg,
					// 	      				icon: 'none'
					// 	      			})
					// 	      		}
					// 	      	})
					// 	      } else {
					// 	      	// paytype为3,使用余额支付
					// 	      	// this.balancepayment(this.params)
					// 			console.log('错误');
					// 	      }
					// }

					// // #endif




					api.singleland(parms).then(res => {
						if (res == '此商家的收款功能已被限制，暂无法支付。商家可以登录微信商户平台/微信支付商家助手小程序查看原因和解决方案。') {
							uni.showToast({
								title: res,
								icon: 'error'
							}) //商户关闭提示
						}
						
						if (res.code == 0) {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						}
						// #ifdef H5
						that.wxPay2(res.data)
						// #endif


						// #ifdef APP-PLUS
						var payplatform = 'wxpay'
						if (res.code === 1) {
							uni.requestPayment({
								provider: payplatform,
								orderInfo: res.data,
								//微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
								success: function(res) {
									
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										})
									}, 1000)
								},
								fail: function(err) {
									console.log(err);
									uni.showToast({
										title: err.errMsg,
										icon: 'none'
									})
								}
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
						// #endif


						// #ifdef MP-WEIXIN
						if (res.appId !== null || res.appId !== undefined) {
							/* 微信支付 */
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.timeStamp,
								nonceStr: res.nonceStr,
								package: res.package,
								signType: res.signType,
								paySign: res.paySign,
								success: function(res) {
									uni.showToast({
										title: '微信支付成功',
										icon: 'success',
										duration: 1000
									});
									uni.redirectTo({
										url: '/pagesB/pages/creatorder/landpaysuccess/landpaysuccess'
									});
								},
								fail: function(err) {
									uni.showToast({
										title: '支付失败',
										icon: 'error'
									});
									setTimeout(function() {
										uni.redirectTo({
											url: '/pagesB/pages/myland/myland'
										});
									}, 1000);
								}
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							});
						}
						// #endif
					});

				}

				function money(parms) {
					let that = this;
					api.singleland(parms).then(res => {
						if (res == '此商家的收款功能已被限制，暂无法支付。商家可以登录微信商户平台/微信支付商家助手小程序查看原因和解决方案。') {
							uni.showToast({
								title: res,
								icon: 'error'
							}) //商户关闭提示
						}
						if (res.status === 1) {
							let parms2 = {
								order_info: JSON.stringify(res.data)
							};
							api.paybyinfo(parms2).then(res => {
								if (res.status === 1) {
									uni.showToast({
										title: '支付成功',
										icon: 'success',
										duration: 1000
									});
									uni.redirectTo({
										url: '/pagesB/pages/creatorder/landpaysuccess/landpaysuccess'
									});
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'error'
									});
									setTimeout(function() {
										uni.redirectTo({
											url: '/pagesB/pages/myland/myland'
										});
									}, 1000);
								}
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							});
							setTimeout(function() {
								uni.redirectTo({
									url: '/pagesB/pages/myland/myland'
								});
							}, 1000);
						}
					});
				}
			},
			showPop(flag = true) {
				this.password = '';
				this.show = flag;
			}
		}
	};
</script>

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

	.u-tooltip-tips {
		font-size: 50rpx !important;
		padding: 25rpx 10rpx !important;
	}

	.u-tooltip {
		align-items: center;
	}

	.u-tooltips-submit {
		font-size: 35rpx !important;
	}

	.u-tooltip-cancel {
		font-size: 35rpx !important;
	}

	.money {
		font-size: 65rpx;
		position: relative;
		text-align: center;
		margin: 25rpx 0;

		.close {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
			line-height: 28rpx;
			font-size: 28rpx;
		}
	}

	.tips {
		color: #909399;
		text-align: center;
		margin: 15rpx 0;
	}

	.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;
			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;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			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: $tab-color;
		border-radius: 10upx;
		// box-shadow: 1px 2px 5px $tab-color-2;
	}
</style>
