<template>
	<view class="content">
		 <view class="points-header">
			<text class="points-count">{{userInfo.score || '0'}}</text>
			<text class="points-label">我的积分</text>
		</view>
	
		<view class="listBox">
			<view class="list flex">
				<view @click="changePoints(index)" :class="{action:index==actionIndex}" class="item flex" 
					v-for="(item,index) in pointsList">
					<image class="icon" src="/static/icon/jf.png" mode="widthFix"></image>
					<view class="info">
						<text class="points">{{item.points}}积分</text>
						<text class="price">¥{{item.price}}元</text>
					</view>
				</view>
			</view>
			
			<view class="amount">
				<text class="label">应付金额(活动随机立减0.10~1.00元)</text>
				<text class="value">¥{{payMoney}}元</text>
			</view>
		</view>
		<view class="payment" @click="selectPayment('wxpay')">
			<view class="left flex">
				<image class="icon" src="/static/icon/weipay.png" mode="widthFix"></image>
				<text>微信支付</text>
			</view>
			<view class="right">
				<image v-if="paymentType=='wxpay'" class="checked" src="/static/icon/addressIconXz.png" mode="widthFix"></image>
				<view v-else class="no-checked"></view>
			</view>
		</view>
		
		<view class="recharge-btn" @click="recharge">充值</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import { jfcz, getUserInfo, getJfBl } from '@/api/user.js'
	export default {
		data() {
			return {
				bl: 0,
				loading: false,
				actionIndex: 0, //当前选中的积分选项索引
				paymentType: 'wxpay', //支付方式
				pointsList: [
					{ points: 0, price: 10 },
					{ points: 0, price: 20 },
					{ points: 0, price: 50 },
					{ points: 0, price: 80 },
					{ points: 0, price: 100 },
					{ points: 0, price: 200 }
				],
				reduction: 0.1, //活动立减金额
			};
		},
		onShow() {
			this.getJfBl()
		},
		computed: {
			payMoney() {
				const selected = this.pointsList[this.actionIndex];
				return selected.price
			},
			...mapState('user', ['userInfo', 'orderInfo', 'hasLogin']),
		},
		methods: {
			...mapMutations('user',['setUserInfo']),
			getJfBl() {
				let that = this
				getJfBl().then(res => {
					that.bl = res.data.points_transformation
					that.pointsList = this.pointsList.map(item => {
						item.points = item.price * that.bl
						return item
					})
				})
			},
			getUserInfo() {
				getUserInfo().then(res => {
					this.setUserInfo(res.data)
				})
			},
			// 返回
			navBack() {
				uni.navigateBack();
			},
			// 选择积分选项
			changePoints(ind) {
				this.actionIndex = ind;
			},
			// 选择支付方式
			selectPayment(type) {
				this.paymentType = type;
			},
			// 充值
			recharge() {
				let that = this;
				if(that.loading) return;
				that.loading = true
				jfcz({
					price: that.payMoney,
					pay_type: that.paymentType
				}).then(res => {
					let da = res.data.jsApiParameters
					let data = {
						appId: da.appId,
						timeStamp: da.timeStamp + '',
						nonceStr: da.nonceStr,
						package: da.package,
						signType: da.signType,
						paySign: da.paySign,
						success: function(res) {
							that.getUserInfo()
							that.loading = false
							uni.showModal({
								title: '提示',
								content: '支付成功！',
								cancelText: '关闭',
								confirmText: '查看我的积分',
								success: () => {},
								fail: () => {},
								complete(res) {
									if (res.confirm) {
										uni.reLaunch({
											url: '/pages/user/jf/bill'
										});
									}
								}
							});
						},
						fail: (err) => {
							console.log(err)
							that.loading = false
							// alert('支付失败')
							// uni.navigateTo({
							// 	url: '/pages/order/order?state=0'
							// });
						}
					};
					wx.requestPayment(data)
					
				}).catch(err => {
					that.loading = false
				})
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #F5F5F5;
	}

	.content {
		min-height: 100%;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		height: 80rpx;
		background-color: #FFFFFF;
		
		.back {
			width: 40rpx;
			height: 40rpx;
			
			image {
				width: 100%;
				height: 100%;
			}
		}
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
		}
		
		.right {
			width: 40rpx;
		}
	}

	.points-header {
		height: 200rpx;
		background-color: #f65486;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #FFFFFF;
		
		.points-count {
			font-size: 60rpx;
			font-weight: bold;
			margin-bottom: 10rpx;
		}
		
		.points-label {
			font-size: 28rpx;
		}
	}

	.listBox {
		margin: 30rpx;
		margin-top: -30rpx;
		border-radius: 20rpx;
		padding: 30rpx;
		background-color: #FFFFFF;
		position: relative;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.1);
	}

	.list {
		flex-wrap: wrap;
		
		.item {
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 30%;
			height: 150rpx;
			margin-bottom: 20rpx;
			margin-right: 5%;
			background-color: #F5F5F5;
			border-radius: 15rpx;
			padding: 20rpx;
			
			&:nth-child(3n) {
				margin-right: 0;
			}
			
			&.action {
				background-color: #FFF0F0;
				border: 2rpx solid #f65486;
			}
			
			.icon {
				width: 40rpx;
				height: 40rpx;
				margin-bottom: 10rpx;
			}
			
			.info {
				display: flex;
				flex-direction: column;
				align-items: center;
				
				.points {
					font-size: 26rpx;
					color: #333333;
					font-weight: bold;
					margin-bottom: 5rpx;
				}
				
				.price {
					font-size: 22rpx;
					color: #999999;
				}
			}
		}
	}

	.amount {
		margin-top: 30rpx;
		padding-top: 30rpx;
		border-top: 1rpx solid #EEEEEE;
		display: flex;
		align-items: center;
		justify-content: space-between;
		
		.label {
			font-size: 26rpx;
			color: #666666;
		}
		
		.value {
			font-size: 32rpx;
			font-weight: bold;
			color: #f65486;
		}
	}

	.payment {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		background-color: #FFFFFF;
		margin: 0 30rpx;
		margin-top: 20rpx;
		border-radius: 15rpx;
		
		.left {
			align-items: center;
			
			.icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 15rpx;
			}
			
			text {
				font-size: 28rpx;
				color: #333333;
			}
		}
		
		.right {
			width: 36rpx;
			height: 36rpx;
			
			.checked {
				width: 100%;
				height: 100%;
			}
			
			.no-checked {
				width: 100%;
				height: 100%;
				border: 1rpx solid #DDDDDD;
				border-radius: 50%;
			}
		}
	}

	.recharge-btn {
		margin: 50rpx 30rpx;
		height: 90rpx;
		background-color: #f65486;
		border-radius: 45rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>