<template>
	<view class="buyTwo">
		<view class="buyTwo-con">
			<view class="a">
				{{info.name}}
			</view>
			<view class="b" v-if="info.type != 1">
				{{info.type == 2?info.children[info.idx].name:info.children[info.idx1].name}}
			</view>
			<view class="c">
				￥{{info.type == 1?info.price: info.type == 2?info.children[info.idx].price: info.children[info.idx1].price}}
			</view>
		</view>
		<view class="buyTwo-pay">
			<view class="l">支付方式</view>
			<!-- <picker mode="selector" @change="bindPickerChange" :value="tabIndex" :range="range">
				<view class="r">
					<text>{{tabIndex=="-1"?"请选择支付方式":range[tabIndex]}}</text><u-icon name="arrow-down" color="#333333" size="14"></u-icon>
				</view>
			</picker> -->
			<view class="r">
				<view class="item" :class="tabIndex == 0?'item-on':''" @click="bindPickerChange(0)">
					<image v-if="tabIndex == 0" :src="staticUrl('/static/icon-select-on.png')"></image>
					<image v-else :src="staticUrl('/static/icon-select.png')"></image>
					微信支付
				</view>
				<view class="item" :class="tabIndex == 1?'item-on':''" @click="bindPickerChange(1)">
					<image v-if="tabIndex == 1" :src="staticUrl('/static/icon-select-on.png')"></image>
					<image v-else :src="staticUrl('/static/icon-select.png')"></image>
					余额支付
				<text>({{'剩余：'+userInfo.balance+'元'}})</text></view>
			</view>
		</view>
		<view class="buyTwo-btn">
			<view class="btn" @click="toSubmit()">
				立即支付
			</view>
		</view>
		<!-- <u-action-sheet :show="isShow" @close="isShow = false" title="" cancelText="取消" :round="10">
			<view class="buyTwo-action">
				<view class="li">
					<image src="/static/pay-wx.png"></image>
				</view>
				<view class="li">
					<image src="/static/pay-zfb.png"></image>
				</view>
			</view>
		</u-action-sheet> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabIndex: 0,
				range: ['微信支付','余额支付'],
				info: {
					type: 1,
					children: []
				},
				userInfo: {},
				isClick: true,
			};
		},
		onReady() {
			this.info = uni.getStorageSync("teamInfo")
			this.userInfo = uni.getStorageSync("userInfo")
			console.log(this.info)
		},
		methods: {
			//去提交
			async toSubmit() {
				if(this.tabIndex == -1){
					uni.showToast({
						icon: "none",
						title: "请选择支付方式"
					})
					return ;
				}
				if(this.tabIndex == 1){
					let price = this.info.type == 1?this.info.price: 
					(this.info.type == 2?this.info.children[this.info.idx].price: 
					this.info.children[this.info.idx1].price)
					
					// if(price > wx.getStorageSync("userInfo").balance){
					// 	uni.showToast({
					// 		icon: "none",
					// 		title: "余额不足！"
					// 	})
					// 	return ;
					// }
					
				}
				if(this.isClick == false){
					return ;
				}
				this.isClick = false
				let category_id = (this.info.type == 1?this.info.id: this.info.type == 2?this.info.children[this.info.idx].id: this.info.children[this.info.idx1].id)
				
				const { code, data, msg } = await this.$api.createOrder({
					category_id
				})
				console.log(category_id,this.info, code)
				if (code == 200) {
					this.isClick = true
					// #ifdef MP-WEIXIN
					this.toPay(data.order_sn)
					console.log(11)
					// #endif
					// #ifdef H5
					this.toPay1(data.order_sn)
					// #endif
				} else {
					this.isClick = true
					uni.showToast({
						icon: "none",
						title: msg
					})
				}
			},
			//去获取支付信息
			async toPay(order_sn) {
				const { code, data, msg } = await this.$api.payOrder({
					is_balance: this.tabIndex == 1?1:"",
					order_sn
				})
				if (code == 200) {
					if(data.is_pay == 1){
						uni.removeStorageSync("teamInfo")
						wx.redirectTo({
							url: "/pages/question/buyThree"
						})
					}else{
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: data.pay_data.timeStamp,
							nonceStr:  data.pay_data.nonceStr,
							package:  data.pay_data.package,
							signType: data.pay_data.signType,
							paySign: data.pay_data.paySign,
						    success: function (res) {
								uni.removeStorageSync("teamInfo")
								uni.redirectTo({
									url: "/pages/question/buyThree"
								})
						    },
						    fail: function (err) {
								uni.showToast({
									icon: "none",
									title: '支付失败',
								})
								uni.removeStorageSync("teamInfo")
								setTimeout(()=>{
									uni.redirectTo({
										url: "/pages/question/list"
									})
								},2000)
						    }
						});
					}
				} else {
					uni.showToast({
						icon: "none",
						title: msg
					})
				}
			},
			//去获取支付信息
			async toPay1(order_sn) {
				const { code, data, msg } = await this.$api.payOrder({
					is_balance: this.tabIndex == 1?1:"",
					order_sn,
					is_public: 1
				})
				if (code == 200) {
					if(data.is_pay == 1){
						uni.removeStorageSync("teamInfo")
						wx.redirectTo({
							url: "/pages/question/buyThree"
						})
					}else{
						console.log(22333)
						function onBridgeReady(){
							console.log(data.pay_data)
							  WeixinJSBridge.invoke(
								"getBrandWCPayRequest",
								{
								  appId: data.pay_data.appId, //公众号ID，由商户传入
								  timeStamp: data.pay_data.timeStamp, //时间戳，自1970年以来的秒数
								  nonceStr: data.pay_data.nonceStr, //随机串
								  package: data.pay_data.package,
								  signType: data.pay_data.signType, //微信签名方式：
								  paySign: data.pay_data.paySign //微信签名
								},
								function (res) {
									console.log(res)
								  // 使用以上方式判断前端返回,微信团队郑重提示：
								  //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
								  if (res.err_msg == "get_brand_wcpay_request:ok") {
									uni.showToast({
										icon: "none",
										title: '支付成功',
									 })
									 setTimeout(()=>{
									  uni.removeStorageSync("teamInfo")
									  uni.redirectTo({
										url: "/pages/question/buyThree"
									  })
									},2000)
								  }
								  // 支付过程中用户取消
								  if (res.err_msg == "get_brand_wcpay_request:cancel") {
									  uni.showToast({
										icon: "none",
										title: '支付失败',
									  })
									  uni.removeStorageSync("teamInfo")
									  setTimeout(()=>{
										uni.redirectTo({
											url: "/pages/question/list"
										})
									  },2000)
								  }
								  // 支付失败
								  if (res.err_msg == "get_brand_wcpay_request:fail") {
									uni.showToast({
										icon: "none",
										title: '支付失败',
									})
									uni.removeStorageSync("teamInfo")
									setTimeout(()=>{
										uni.redirectTo({
											url: "/pages/question/list"
										})
									},2000)
								  }
								  /**
								   * 其它
								   * 1、请检查预支付会话标识prepay_id是否已失效
								   * 2、请求的appid与下单接口的appid是否一致
								   * */
								  if (res.err_msg == "调用支付JSAPI缺少参数：total_fee") {
									  uni.showToast({
										icon: "none",
										title: res.err_msg,
									  })
									  uni.removeStorageSync("teamInfo")
									  setTimeout(()=>{
										uni.redirectTo({
											url: "/pages/question/list"
										})
									  },2000)
								  }
								}
							  );
						  }
						 if (typeof WeixinJSBridge == "undefined") {
						   if (document.addEventListener) {
							 document.addEventListener("WeixinJSBridgeReady", onBridgeReady, false);
						   } else if (document.attachEvent) {
							 document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
							 document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
						   }
						 } else {
						   onBridgeReady();
						 }
						
					}
				} else {
					uni.showToast({
						icon: "none",
						title: msg
					})
				}
			},
			bindPickerChange(e){
				this.tabIndex = e;
			},
			openPage(){
				uni.navigateTo({
					url: "/pages/question/buyThree"
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	.buyTwo {
		width: 100%;
		padding: 0 25rpx 25rpx;
		overflow: hidden;
		box-sizing: border-box;

		&-action {
			padding: 30rpx;
			overflow: hidden;
			display: flex;
			align-items: center;

			.li {
				flex: 1;
				text-align: center;
				position: relative;
				font-size: 26rpx;
				font-weight: 400;
				color: #333333;

				image {
					width: 110rpx;
					height: 110rpx;
					display: block;
					margin: 0 auto;
					margin-bottom: 20rpx;
				}

				button {
					width: 100%;
					height: 100%;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 2;
					opacity: 0;
				}
			}
		}

		&-con {
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 50rpx 30rpx;
			text-align: center;

			.a {
				font-size: 30rpx;
				font-weight: bold;
				color: #222222;
				line-height: 2;
			}

			.b {
				font-size: 26rpx;
				color: #666666;
				line-height: 2;
			}

			.c {
				margin: 30rpx 0 10rpx;
				font-size: 60rpx;
				font-weight: bold;
				color: #F01B1B;
			}
		}

		&-pay {
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 50rpx 30rpx;
			.l {
				font-size: 30rpx;
				color: #222222;
			}

			.r {
				margin-top: 30rpx;
				.item{
					padding: 20rpx;
					display: flex;
					align-items: center;
					font-size: 30rpx;
					color: #222222;
					image{
						width: 40rpx;
						height: 40rpx;
						margin-right: 20rpx;
					}
					text {
						color: #999999;
						margin-left: 10rpx;
						font-size: 26rpx;
					}
					&-on{
						color: #38BCFF;
					}
				}
			}
		}

		&-btn {
			.btn {
				width: 320rpx;
				line-height: 98rpx;
				margin: 70rpx auto;
				font-size: 30rpx;
				color: #ffffff;
				text-align: center;
				background: linear-gradient(-90deg, #3399EA, #38BCFF);
				border-radius: 49rpx;
			}
		}
	}
</style>