<template>
	<view>
		<view class="inner-box" v-if="order!=null">
			<!-- 时间、价格 -->
			<view class="time-price">
				<text class="time">支付金额</text>
				<view class="price-box">
					<text class="unit">¥</text>
					<text class="num"  >{{order.payMoney.toFixed(2)}}</text>
				</view>
			</view>
			<!-- 订单详情查看 -->
			<view class="order-info">
				<view class="title">订单信息</view>
				<view class="item">
					<text class="label">订单编号</text>
					<view class="sn-box">
						<text class="text">{{order.orderSn}}</text>
						<view class="copy-btn" @click="copyTest(order.orderSn)">复制</view>
					</view>
				</view>
				<view class="item">
					<text class="label">下单时间</text>
					<text class="text">{{order.createTime}}</text>
				</view>
			</view>
			<!-- 支付方式 -->
			<view class="order-info">
				<view class="title">选择支付方式</view>
				<!-- 支付宝微信选择 -->
				<radio-group @change="payTypeChange">
					<label class="item" style="justify-content: space-between;">
						<view class="sn-box" style="display: flex; align-items: center;">
							<image src="/static/image/course/vipBuy/wx.png" style="width: 48upx; height: 48upx; margin-right: 20upx;"></image>
							<text class="text">微信支付</text>
						</view>
						<radio value="1" :checked="payType=='1'" color="#FF5030" style="transform:scale(0.8)"/>
					</label>
				<!-- 	<label class="item" style="justify-content: space-between;">
						<view class="sn-box" style="display: flex; align-items: center;">
							<image src="/static/image/course/vipBuy/wx.png" style="width: 48upx; height: 48upx; margin-right: 20upx;"></image>
							<text class="text">微信支付备用</text>
						</view>
						<radio value="3" :checked="payType=='3'" color="#FF5030" style="transform:scale(0.8)"/>
					</label> -->
					<label class="item" style="justify-content: space-between;">
						<view class="sn-box" style="display: flex; align-items: center;">
							<image src="/static/image/course/vipBuy/zfb.png" style="width: 48upx; height: 48upx; margin-right: 20upx;"></image>
							<text class="text">支付宝支付</text>
						</view>
						<radio value="4" :checked="payType=='4'" color="#FF5030" style="transform:scale(0.8)"/>
					</label>
				</radio-group>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="btn-box">
			<view class="btn" v-if="order!=null" @click="payOrder()">立即支付</view>
		</view>
		<uni-popup ref="popTip" type="dialog">
		<uni-popup-dialog cancelText="支付失败" confirmText="支付成功" mode="base" content="是否已支付成功?" title="提示" :duration="2000" :before-close="true" @close="confirmTip" @confirm="confirmTip"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import{orderDetail,payment}from '@/api/courseAnswer.js'
	export default {
		data() {
			return {
				payType:'1',
				orderId:null,
				order:null,
				tzCashier:null,
				userInfo: null
			}
		},
		onLoad(option) {
			this.tzCashier=uni.requireNativePlugin("TZBank-Cashier");
			this.orderId=option.orderId;
			this.userInfo=JSON.parse(uni.getStorageSync("userInfo"));
			if(option.payType){
				this.payType=option.payType;
			}
			
		},
		onShow() {
			this.getOrderDetail();
		},
		 
		methods: {
			payTypeChange(e){
				this.payType = e.detail.value;
			},
			copyTest(text) {
				// 复制方法
				uni.setClipboardData({
					data:text,
					success:()=>{
						uni.showToast({
							title:'内容已成功复制到剪切板',
							icon:'none'
						})
					}
				});
			},
			getOrderDetail(type){
				var data={orderId:this.orderId};
				orderDetail(data).then(res => {
					if(res.code==200){
						this.order=res.data;
						if(type==1) {
							if(res.data.status==2){
								uni.showToast({title:"支付成功",icon:'success'});
								uni.navigateTo({
									url: '/pages/courseAnswer/orderPaySuccess?orderId='+this.orderId
								})
							}else{
								uni.showToast({title:"支付失败",icon:'error'});
							}
						}
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						 
					}
				});
			},
			confirmTip() {
				// this.getUserInfo();
				// this.getCourseInfo();
				this.$refs.popTip.close();
				uni.showLoading({title:"请稍侯...",mask:true,duration:3000});
				setTimeout(()=>{
					this.getOrderDetail(1);
				},2000);
			},
			payOrder(){
				if(this.payType==1&&this.order.payMoney>0) {
					this.doWxPay();
					return
				}
				const param = {
					orderId: this.order.orderId,
					payType: this.payType
				}
				uni.showLoading()
				payment(param).then(res=>{
					uni.hideLoading()
					if(res.code ==200) {
						console.log("res=======:",res);
						if(res.isPay==1) {
							uni.navigateTo({
								url: '/pages/courseAnswer/orderPaySuccess?orderId='+this.orderId
							})
						}else {
							if(this.payType==1){  //微信支付
								this.doWxPay();
							}else if(this.payType==4){   //支付宝
								this.doAlipay(res);
							}else if(this.payType==3){   //微信支付app
								this.doWxPay1(res);
							}
						}
					}else {
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			},
			doWxPay() {
				var that=this;
				plus.share.getServices(function(res){
				    var sweixin = null;  
				    for(var i=0;i<res.length;i++){  
				        var t = res[i];  
				        if(t.id == 'weixin'){  
				            sweixin = t;  
				        }  
				    }  
					if(sweixin){
						console.log('调起小程序')
						that.$refs.popTip.open()
						//唤起微信跳转小程序
						sweixin.launchMiniProgram({
							id:"gh_feb7753a310b",//gh_7a6a32e5ef61 御君方互医
							path:'/pages_courseAnswer/orderPay?orderId='+that.orderId+"&payMethod=app",
							type:0
						},function(){
							console.log("微信唤起成功");
							return true;
						},function(e){
							console.log("微信唤起失败",e);
							uni.showToast({
								title:'微信唤起失败，请检查是否有微信应用',
								icon:'none'
							})
							return false;
						})
						
					}else{
						uni.showToast({
							title:'微信唤起失败，请检查是否有微信应用',
							icon:'none',
							duration:3000
						})
						return false;
					}
				      
				},function(res){  
				    console.log(JSON.stringify(res));  
				});
			},
			doAlipay(res){
				this.$refs.popTip.open();
				if(res.code==200){
					if(res.type=="tz"){
						const match = res.data.body.url.match(/[\?&]businessCstNo=([^&]+)/);
						const businessCstNo = match ? match[1] : null;
				        console.log("qxj tzCashier:"+this.tzCashier+" businessCstNo:"+businessCstNo);
						this.tzCashier.pay({
							env:0,
							wxMiniProgramType:0,
							wxAppId:'wx703c4bd07bbd1695',
							wxUniversalLink:"https://yjf.runtzh.com/",
							orderFlowNo:res.data.body.orderFlowNo,
							businessCstNo:businessCstNo,
							platMerCstNo:res.data.body.platMerCstNo
						},(res)=>{
							    console.log("qxj 支付成功:"+JSON.stringify(res));
						  });
					}
					else if(res.type=='hf'){
						if (uni.getSystemInfoSync().platform == 'android') {
							var alipayScheme ='alipays://platformapi/startApp?&saId=10000007&qrcode=' + res.data.qr_code;
							
						}else{
						    var alipayScheme ='alipay://platformapi/startApp?&saId=10000007&qrcode=' + res.data.qr_code;	
						}
						console.log(alipayScheme)
						// 在uni-app中使用plus.runtime.openURL打开URL
						plus.runtime.openURL(alipayScheme, function(error) {
						  // console.error('打开支付宝失败: ' + error.message);
						  // 处理打开支付宝失败的情况，比如提示用户或者跳转到其他支付方式
						});
					}
				}
				else{
					uni.showToast({
						title:res.msg,
						icon:'none'
					});
				}
			},
			doWxPay1(res) {
				console.log("doWxPay1==",res)
				let that = this
				if(res.type=="tz"){
					console.log("qxj body",res.data.body);
					let businessCstNo=this.userInfo.userId;
					console.log("businessCstNo",res.data.body);
					if(!!res.data.body.url){
						const match = res.data.body.url.match(/[\?&]businessCstNo=([^&]+)/);
						console.log("match==",match);
						businessCstNo = match ? match[1] : null;
					}
					this.tzCashier.pay({
						env:0,
						wxMiniProgramType:0,
						wxAppId:"wx703c4bd07bbd1695",
						wxUniversalLink:"https://yjf.runtzh.com/",
						orderFlowNo:res.data.body.orderFlowNo,
						businessCstNo:businessCstNo,
						platMerCstNo:res.data.body.platMerCstNo
					},(res)=>{
							that.showPayTips=true;
					  });
				}
				else if(res.type=='hf'){
					that.showPayTips=true;
				}
			}
		}
	}
</script>

<style lang="scss">
	.inner-box{
		padding: 20upx 20upx 300upx;
		.time-price{
			box-sizing: border-box;
			padding: 50upx 0upx;
			background: #FFFFFF;
			border-radius: 16upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			.time{
				font-size: 32upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #222222;
				line-height: 1;
				text-align: center;
			}
			.desc{
				margin: 30upx 0upx 15upx;
				font-size: 26upx;
				font-family: PingFang SC;
				color: #999999;
				line-height: 1;
				text-align: center;
			}
			.price-box{
				display: flex;
				align-items: flex-end;
				margin-top: 28upx;
				.unit{
					font-size: 32upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FF6633;
					line-height: 1.3;
					margin-right: 10upx;
				}
				.num{
					font-size: 56upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FF6633;
					line-height: 1;
				}
			}
		}
		.order-info{
			margin-top: 20upx;
			background: #FFFFFF;
			border-radius: 16upx;
			padding: 40upx 30upx;
			.title{
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #222222;
				line-height: 1;
			}
			.item{
				margin-top: 40upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.label{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 1;
				}
				.text{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #222222;
					line-height: 32upx;
				}
				.cont-text{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					.bold{
						color: #111111;
					}
				}
				.sn-box{
					display: flex;
					align-items: center;
					.copy-btn{
						width: 58upx;
						height: 32upx;
						line-height: 32upx;
						text-align: center;
						font-size: 22upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #222222;
						background: #F5F5F5;
						border-radius: 4upx;
						margin-left: 24upx;
					}
				}
				 
			}
			.line{
				width: 100%;
				height: 1px;
				background: #F0F0F0;
				margin-top: 30upx;
			}
		}
	}
	
	
	.btn-box{
	 	z-index: 9999;
	 	width: 100%;
	 	padding: 30rpx 30upx 0rpx;
	 	position: fixed;
	 	bottom: 0;
	 	left: 0;
	 	box-sizing: border-box;
	 	background-color: #ffffff;
	 	display: flex;
		flex-direction: column;
	 	align-items: center;
	 	justify-content: center;
	 	.btn{
			margin-bottom: 20rpx;
	 		width: 100%;
	 		height: 88upx;
	 		line-height: 88upx;
	 		text-align: center;
	 		font-size: 34upx;
	 		font-family: PingFang SC;
	 		font-weight: 400;
	 		color: #FFFFFF;
	 		background: #FF5030;
	 		border-radius: 10upx;
			position: relative;
	 	}
		.btn1{
			margin-bottom: 20rpx;
			width: 100%;
			height: 88upx;
			line-height: 88upx;
			text-align: center;
			font-size: 34upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FF5030;
			border: 1rpx solid #FF5030;
			border-radius: 10upx;
			position: relative;
			.share-btn {
				top:0rpx;
				left:0rpx;
				position: absolute;
				width: 100%;
				height: 88upx;
				display: flex;
				opacity: 0;
			}
		}
	}
	 
</style>


