<template>
	<view class="page-container">
		<view class="exchange-header-bg">
			<view class="header-bg-upper"></view>
		</view>
		<view class="status-bar"></view>
		<view class="page-header">金币兑换</view>
		<view class="exchange-header-content justify-between">
			<view>
				<view class="coin-num">{{data.user_coin || 0}}</view>
				<view class="sub-text">当前拥有金币</view>
			</view>
			<image src="/static/images/coin-exchange-bg.png">
		</view>
		<view class="exchange-box">
			<view class="package justify-center">
				<view class="package-item">
					<view class="packages-box" @click="onSelect('points')">
						<view class="package-title">
							<view class="">
								兑换<text style="color:#D9876D;">播放</text>次数
							</view>
							<view class="package-text">{{data.exchange_points}}金币兑换1次</view>
						</view>
						<view class="exchange-input justify-center">
							<view class="reduce-button" @click="onReduce('points_')">-</view>
							<view class="input-box justify-center">
								<input type="number" v-model="points_num" />
							</view>
							<view class="add-button" @click="onAdd('points_')">+</view>
						</view>
						<view class="exchange-cisu" @click="onAll('points_')">您还可以兑换<text style="color:#D9876D;">{{points_maxNum}}</text>次</view>
						<view class="exchange-all" @click="onAll('points_')">全部兑换</view>
					</view>
				</view>
				<view class="package-item">
					<view class="packages-box" @click="onSelect('down_points')">
						<view class="package-title2">
							<view class="">
								兑换<text style="color:#D9876D;">下载</text>次数
							</view>
							<view class="package-text">{{data.exchange_down_points}}金币兑换1次</view>
						</view>
						<view class="exchange-input justify-center">
							<view class="reduce-button" @click="onReduce('down_')">-</view>
							<view class="input-box justify-center">
								<input type="number" v-model="down_num" />
							</view>
							<view class="add-button" @click="onAdd('down_')">+</view>
						</view>
						<view class="exchange-cisu" @click="onAll('down_')">您还可以兑换<text>{{down_maxNum}}</text>次</view>
						<view class="exchange-all" @click="onAll('down_')">全部兑换</view>
					</view>

				</view>
			</view>

		</view>
		<u-button shape="circle" :disabled="loading" :loading="loading"
			color="linear-gradient(90deg, #DA2538 0%, #F75F68 100%)" text="确认兑换" class="exchange-button"
			@click="exchange"></u-button>
	</view>
</template>

<script>
	import {
		user
	} from '@/utils/api'

	export default {
		data() {
			return {
				data: {},
				type: 'points',
				points_num: 0,
				down_num:0,
				points_maxNum: 0,
				down_maxNum: 0,
				loading: false
			}
		},
		async onLoad() {
			await this.$onLaunched
			const token = getApp().globalData.userToken
			if (!token) {
				uni.redirectTo({
					url: '/pages/user/login'
				})
			} else {
				this.getData()
			}
		},
		methods: {
			getData() {
				user.exchange().then(res => {
					if (res.code == 1) {
						this.data = res.data
						this.points_maxNum = Math.floor(this.data.user_coin / this.data.exchange_points)
						this.down_maxNum = Math.floor(this.data.user_coin / this.data.exchange_down_points)
					}
				})
			},
			async exchange() {
				let type=this.type=='down_points' ? 'down_' : 'points_'
				console.log(this.type, this[type+'num']);
				if (this[type+'num'] === 0) {
					uni.showToast({
						icon: 'none',
						title: '请添加兑换次数~!'
					})
					return
				}
				if (this[type+'num'] > this[type+'maxNum']) {
					this[type+'num'] = 0
					uni.showToast({
						icon: 'none',
						title: '您当前的金币不足~!'
					})
					return
				}
				this.loading = true
				
				await user.exchangePoints(this.type, this[type+'num']).then(res => {
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 3000
					})
					if (res.code === 1) {
						this.data.user_coin = res.data.user_coin
					}
				}).catch(e => {})
				this.loading = false
			},
			onSelect(value) {
				this.type = value
			},
			onReduce(type) {
				if (this[type+'num'] > 0) {
					this[type+'num']--;
				}
			},
			onAdd(type) {
				
				if (this[type+'num'] < this[type+'maxNum']) {
					this[type+'num']++;
				}
			},
			onAll(type) {
				this[type+'num'] = this[type+'maxNum']
			}
		}
	}
</script>

<style lang="scss">
	.page-container {
		padding-bottom: 20px;
		background-color: $uni-bg-color;
		position: relative;

		.exchange-header-bg {
			width: 100%;
			text-align: right;
			
background: linear-gradient(90deg, #DA2538 0%, #F75F68 100%);
			position: absolute;
			left: 0;
			top: 0;
			
			height: 287px;
			.header-bg-upper {
				position: absolute;
				bottom: 0;
				width: 100%;
				height: 30%;
				// background: linear-gradient(180deg,rgba(13,173,111,.1),#fff);
			}
		}

		.page-header {
			height: 44px;
			line-height: 44px;
			font-size: 18px;
			font-weight: 700;
			color: #fff;
			text-align: center;
			position: relative;
			z-index: 1;
		}

		.exchange-header-content {
			padding: 0px 10px 0px 30px;
			position: relative;
			z-index: 99;
			align-items: center;
			uni-image {
				width: 175px;
				height: 176px;
			}
			.coin-num {
				font-size: 34px;
				font-weight: 600;
				color: #F7EDD3;
			}
					
			.sub-text {
				font-size: 14px;
				font-weight: 400;
				color: #FFFFFF;
			}
		}

		.exchange-box {
			width: 100%;
			border-radius: 10px;
			margin-top: 10rpx;
			padding: 15px;
			background-color: $uni-bg-color;
			position: relative;
			z-index:99;
			min-height: 270px;
			.package {
				margin-top: 10px;
				flex-wrap: wrap;

				.package-item {
					width: 50%;
					height: 500rpx;
					padding: 10px;

					.packages-box {
						border: 1px solid #555353;
						position: relative;
						border-radius: 10px;

						.package-title {
							border-top-right-radius: 10px;
							border-top-left-radius: 10px;
							width: 100%;
							height: 100px;
							line-height: 60px;
							text-align: center;
							background: #434140;
							font-size: 18px;
							font-weight: 500;
							color: #fff;
						}

						.package-title2 {
							border-top-right-radius: 10px;
							border-top-left-radius: 10px;
							width: 100%;
							height: 100px;
							line-height: 60px;
							text-align: center;
							background: #434140;
							font-size: 18px;
							font-weight: 500;
							color: #fff;
						}

						.package-text {
							height: 25px;
							line-height: 25px;
							font-size: 12px;
							text-align: center;
							border-radius: 0 0 4px 4px;
						}

						&.selected {
							&::after {
								content: '';
								width: 0;
								height: 0;
								border: 10px solid transparent;
								border-top-color: $uni-color-primary;
								position: absolute;
								bottom: -20px;
								left: 50%;
								margin-left: -10px;
							}

							background-color: #ecfcf1;

							.package-title {
								color: $uni-color-primary;
							}

							.package-text {
								color: #fff;
								background-color: $uni-color-primary;
							}
						}
					}
				}
			}

			.exchange-input {
				width: 100%;
				max-width: 150px;
				height: 40px;
				margin: 20px auto 0;
				border: 1px solid $uni-color-primary;
				border-radius: 4px;
				overflow: hidden;

				.reduce-button,
				.add-button {
					width: 40px;
					height: 40px;
					line-height: 35px;
					text-align: center;
					background-color: #F8864E;
					color: #fff;
					font-size: 30px;
				}

				.input-box {
					flex: 1 1 0%;
					padding: 0 10px;
					text-align: center;

					color: #fff;

					uni-input {
						max-width: 80px;
						height: 40px;
						line-height: 35px;
						margin-right: 5px;
						font-size: 16px;
						font-weight: 500;
					}

					.unit {
						height: 38px;
						line-height: 38px;
						font-size: 12px;
						font-weight: 400;
					}
				}
			}

			.exchange-cisu {
				width: 100px;
				height: 30px;
				line-height: 30px;
				text-align: center;
				margin: 4px auto 0;
				font-size: 10px;
				font-weight: 400;
				color: #fff;
				uni-text {
					color: #D9876D;
				}
			}

			.exchange-all {
				width: 100px;
				height: 30px;
				line-height: 30px;
				text-align: center;
				margin: 4px auto 0;
				font-size: 14px;
				font-weight: 400;
				color: #F8864E;
			}
		}

		.exchange-button {
			width: auto;
			margin: 40px 15px 0;
			height: 44px;
			line-height: 44px;
		}
	}
</style>