<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: {},
				userInfo: {},
				isClick: true,
			};
		},
		onReady() {
			this.info = uni.getStorageSync("teamInfo")
			this.userInfo = uni.getStorageSync("userInfo")
		},
		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)
				console.log(category_id,this.info)
				const { code, data, msg } = await this.$api.createOrder({
					category_id
				})
				if (code == 200) {
					this.isClick = true
					this.toPay(data.order_sn)
				} 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
					})
				}
			},
			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>