<template>
	<view class="vip">
		<view style="height: 20rpx;"></view>
		<view class="topCard">
			<view class="vipInfo">
				<view class="left">
					<view class="ownVip">当前会员:<text>{{setMealName||'普通用户'}}</text></view>
					<view class="endTime" v-if="expireTime">会员到期时间: {{expireTime||'--'}}</view>
				</view>
				<view class="right">
					<image slot="icon"
						src="http://zswl-dev.oss-cn-chengdu.aliyuncs.com/63b7c68b71a69169d1b33f92/store/bdb/user/avatar/Jm8VtOGY6Bqg6cf0dd7cba21cff1cf57cd5a36effe8f.png/1.png"
						mode="widthFix"></image>
				</view>
			</view>
			<view class="inputBox">
				<input type="text" v-model="vipQuery.activationCode" @input="handleInput" @paste="handlePaste"
					placeholder="请输入激活码" placeholder-class="activationIpt" />
				<u-button type="error " :loading="redeemload" loadingSize="20" shape="circle" text="立即兑换"
					@click="confirmActivation"></u-button>
			</view>
		</view>
		<view class="listCard">
			<view class="listTitle">激活记录</view>
			<view class="listBox" v-if="activationList.length>0">
				<view class="list" v-for="(item,index) in activationList" :key="item.id">
					<view class="vipTitle">
						<view class="">{{item.planningName}}</view>
						<view class="">已激活</view>
					</view>
					<view class="listInfo">
						<view class="">激活码:<text>{{item.serialNumber}}</text></view>
						<view class="">激活时间: {{item.useTime}}</view>
					</view>
				</view>
			</view>
			<block v-else>
				<u-empty mode="data" textSize="20" iconSize="120" text="暂无激活记录"></u-empty>
			</block>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		mealList
	} from '@/api/combo.js'
	import {
		refreshVip
	} from '@/api/common.js'
	import {
		selectActivationCode,
		getActivationList
	} from '@/api/vipActivation.js'
	import {
		levelImgName
	} from '@/utils/config.js'
	import {
		timestampToDate
	} from '@/utils/tool.js'
	export default {
		data() {
			return {
				expireTime: '',
				redeemload: false,
				activationList: [],
				list: [],
				userInfo: '',
				setMealName: '',
				vipQuery: {
					phoneNum: '',
					activationCode: '',
				}
			}
		},
		onShow() {},
		onLoad() {
			this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
			this.vipQuery.phoneNum = this.userInfo.phoneNum
			this.mealList()
			this.get_activationList()
			this.get_userinfo()
		},

		methods: {
			// 修复android粘贴出现回车问题
			handleInput() {
				this.vipQuery.activationCode = this.vipQuery.activationCode.replace(/\s/g, '');
			},
			handlePaste(event) {
				let pasteData = event.clipboardData.getData('text');
				pasteData = pasteData.replace(/\r?\n|\r/g, '');
				this.vipQuery.activationCode = this.vipQuery.activationCode + pasteData;
			},

			// 用户信息
			get_userinfo() {
				refreshVip().then(res => {
					res.content.map((item) => {
						this.expireTime = timestampToDate(item.expirationTime)
					})
				})
			},

			// 激活列表
			get_activationList() {
				getActivationList().then(res => {
					if (res.state == 'Success') {
						res.content.content.map((item) => {
							item.useTime = timestampToDate(item.useTime)
						})
						this.activationList = res.content.content
					}
				})
			},

			// 二次确认
			confirmActivation() {
				if (this.vipQuery.activationCode == '') {
					uni.showToast({
						title: '请输入激活码',
						icon: 'none'
					})
					return
				}
				let that = this
				uni.showModal({
					title: '提示',
					content: '确认兑换当前激活码吗?',
					success(res) {
						if (res.confirm) {
							that.redeemload = true
							that.select_activationCode()
						}
					}
				})
			},

			// 兑换激活码
			select_activationCode() {
				selectActivationCode(this.vipQuery).then(res => {
					this.redeemload = false
					if (res.state == 'Success') {
						uni.showToast({
							title: '激活成功',
						});
						this.vipQuery.activationCode = ''
						this.get_activationList()
					} else {
						this.$refs.uToast.show({
							message: res.content
						})
					}
				})
			},

			// 获取套餐类型
			mealList() {
				mealList({
					currentPage: 1,
					pageSize: 99,
					status: 1
				}).then(res => {
					if (res.state == 'Success') {
						this.list = res.content.records
						// 判断当前会员类型
						this.list.map(item => {
							if (item.setMealCode == this.userInfo.setMealCode) {
								this.setMealName = item.setMealName
							}
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.vip {
		height: 100vh;
		padding: 0 24rpx;
		background: #F9F9F9;

		.topCard {
			background: #fff;
			border-radius: 8rpx;
			padding: 20rpx;

			.vipInfo {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.left {
					.ownVip {
						font-size: 28rpx;
						color: #181818;
						font-weight: bold;

						&>text {
							margin-left: 10rpx;
							font-weight: bold;
							font-size: 36rpx;
						}
					}

					.endTime {
						margin-top: 20rpx;
						font-weight: normal;
						font-size: 24rpx;
						color: #AAAAAA;
					}
				}

				.right {
					&>image {
						width: 100rpx;
					}
				}
			}

			.inputBox {
				display: flex;
				align-items: center;
				border: #F9F9F9 4rpx solid;
				border-radius: 50rpx;
				padding: 6rpx;
				margin-top: 20rpx;

				&>input {
					margin-left: 16rpx;
					width: 1600rpx;
				}

				.activationIpt {
					font-size: 28rpx;
					color: #AAAAAA;
				}
			}
		}

		.listCard {
			background: #fff;
			border-radius: 8rpx;
			padding: 20rpx;
			margin-top: 20rpx;

			.listTitle {
				font-weight: bold;
				font-size: 28rpx;
				color: #222222;
			}

			.listBox {
				.list {
					border-bottom: 1rpx solid #F0F0F0;

					.vipTitle {
						display: flex;
						align-items: center;
						justify-content: space-between;
						font-weight: bold;
						font-size: 32rpx;
						margin-top: 20rpx;

						&>view:first-child {
							color: #222222;
						}

						&>view:last-child {
							color: #FF4A39;
						}
					}

					.listInfo {
						margin-top: 20rpx;

						&>view:first-child {
							font-weight: normal;
							color: #222222;
							font-size: 24rpx;

							&>text {
								margin-left: 10rpx;
								color: #AAAAAA;
							}
						}

						&>view:last-child {
							height: 64rpx;
							line-height: 64rpx;
							font-size: 24rpx;
							font-weight: normal;
							color: #222222;
						}
					}
				}
			}
		}
	}
</style>