<template>
	<view class="content">
		<view class="tip" v-if="type == 3">
			阅读积分兑换余额宝手续费为{{pUser.fee_ratio}}%
		</view>
		<view class="tip" v-if="type == 0">
			通证兑换阅读积分比例为{{pUser.points_transaction}}:1
		</view>
		<view class="item flex">
			<view class="tit">
				可兑换{{type == 1 ? '商城积分': (type== 3?'阅读积分':(type == 2? '余额宝': '通证积分'))}}
			</view>
			<input type="text" placeholder="" v-model="allNum" disabled />
		</view>
		<view class="item flex">
			<view class="tit">
				申请兑换数量
			</view>
			<input type="text" placeholder="请输入申请兑换数量" v-model="num" />
		</view>
		<view class="all" @click="getAll">
			全部兑换
		</view>
		<view class="btn" @click="tradeMoney">
			立即兑换
		</view>
	</view>
</template>

<script>
	// tradeMoney
	import {
		tradeMoney,
		passUser
	} from '@/api/zero.js'
	export default {
		data() {
			return {
				type: 0, // 0 通证-》阅读 1 商城-》复投
				num: '',
				allNum: 0,
				pUser: {}
			}
		},
		onLoad(opt) {
			this.type = opt.type
			if (this.type == 1) {
				uni.setNavigationBarTitle({
					title: '兑换复投积分'
				})
			} else if (this.type == 3) {
				uni.setNavigationBarTitle({
					title: '兑换余额宝'
				})
			} else if (this.type == 2) {
				uni.setNavigationBarTitle({
					title: '兑换阅读积分'
				})
			}
			this.passUser()
		},
		onShow() {

		},
		onReachBottom() {

		},
		onReady() {

		},
		methods: {
			passUser() {
				passUser().then(res => {
					this.loading = false
					this.pUser = res.data
					if (this.type == 1) {
						this.allNum = res.data.integral * 1
					} else if (this.type == 3) {
						this.allNum = res.data.points * 1
					} else if (this.type == 2) {
						this.allNum = res.data.freeze_points * 1
					} else {
						this.allNum = res.data.pass ? res.data.pass : 0
					}
				})
			},
			getAll() {
				this.num = this.allNum
			},
			tradeMoney() {
				// 
				let that = this
				if (!that.num) {
					return that.$api.msg('请输入兑换数量')
				}
				if (that.num > that.allNum) {
					return that.$api.msg('兑换数量超出可兑换数量')
				}
				that.loading = true
				tradeMoney({
					num: that.num,
					type: that.type
				}).then(res => {
					uni.showToast({
						title: '兑换成功',
						duration: 2000
					});
					this.num = ''
					this.passUser()

				}).catch(err => {
					that.loading = false
				})
			}
		}
	}
</script>

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

	.item {
		width: 710rpx;
		margin: auto;
		background-color: #fff;
		height: 110rpx;
		border-bottom: 1px solid #E6E6E6;

		.tit {
			line-height: 110rpx;
			font-size: 32rpx;
		}

		input {
			text-align: right;
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
		}
	}

	.all {
		padding: 20rpx;
		text-align: right;
		font-size: 26rpx;
		font-weight: 500;
		color: #FF4C4C;
	}

	.btn {
		width: 670rpx;
		line-height: 88rpx;
		background: #FF4C4C;
		border-radius: 10rpx;

		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
		text-align: center;
		margin: 60rpx auto;
	}

	.tip {
		padding: 5rpx;
		padding-left: 20rpx;
		background-color: #f2c3d1;
		color: #f21f5d;
	}
</style>