<!-- 活动红包 -->
<template>
	<view class="red-bag">
		<!-- 红包 -->
		<view class="message-content" @click="openCover">
			<view class="df aic">
				<image class="img mr10" src="/static/image/red-envelope.png" />
				<view class="red-packet-text">{{ dataItem.payload.data.name }}</view>
			</view>
			<!-- 分割线 -->
			<view class="line"></view>
			<view class="f20" style="color:#FBD3A4">{{ dataItem.payload.data.type == 1 ? '积分红包' : '余额红包' }}</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api/index'
	import {
		mapState
	} from 'vuex';
	export default {
		inject: ['util'],
		props: {
			dataItem: {
				type: Object,
				default: () => {
					{}
				}
			},
		},

		data() {
			// console.log('====================================');
			// console.log(this.dataItem);
			// console.log('====================================');
			let {
				userName,
				userPortrait
			} = uni.getStorageSync('toUser') || {
				userName: null,
				userPortrait: null
			};
			return {
				status: 'success',
				openTime: undefined,
				defConfig: {
					userImg: userPortrait,
					userName: userName,
					coverTitle: '恭喜发财',
					openTitle: '恭喜您获得',
					openTips: '已存入钱包，可直接提现',
					btnText: '查看我的钱包'
				}, // 默认配置
				rbagmodelshow: false, // 红包封面
				openrbagmodelshow: false, // 拆封红包
				openbrnanimation: {}, // 拆封动画
				digitalData: [], // 滚动数字数据
				redValue: null
			}
		},

		computed: {
			// 处理配置项
			config() {
				const result = Object.assign(this.defConfig)
				return result
			}
		},

		methods: {
			openCover() {
				// 开红包
				if (this.dataItem.from == this.userinfo.userId) {
					this.util.checkLink(`pages/news/red-envelope/index?bagId=${this.dataItem.ext.id}`)
				} else {
					this.rbagmodelshow = true
				}
			},
			// 红包封面 => 開红包按钮
			openBtn() {
				let duration = 1000
				let animation = uni.createAnimation({
					duration,
					timingFunction: 'ease'
				})
				this.openbrnanimation = animation
				animation.rotateY(360).step()
				this.openbrnanimation = animation.export()
				setTimeout(() => {
					this.rbagmodelshow = false
					this.openbrnanimation = undefined
					// 打开封面后回调
					this.$nextTick(() => {
						api.news.grabred({
							id: this.dataItem.ext.id
						}).then(res => {
							console.log(res);
							if (res.code == 500) {
								// 提示领取失败
								this.status = 'error'
								this.defConfig.coverTitle = '恭喜发财';
								this.defConfig.openTitle = '恭喜您获得';
								this.defConfig.openTips = res.msg;
								this.defConfig.btnText = '领取记录'
								this.openrbagmodelshow = true
							}
							if (res.code == 200) {
								let money = null
								this.redValue = res.data
								switch (res.data.type) {
									// 积分
									case 1:
										money = res.data.score
										break;
										// 余额
									case 2:
										money = res.data.balance
										break;
									default:
										break;
								}
								this.status = 'success'
								this.rbagmodelshow = false
								this.openrbagmodelshow = true
								this.openbrnanimation = {}
								this.setMoney(money)
							}
						})
					})
				}, duration)
			},

			// 确认红包
			onConfirm() {
				this.util.checkLink(`pages/news/red-envelope/index?bagId=${this.dataItem.ext.id}`)
				this.onClose()
			},

			// 隐藏红包
			onClose() {
				this.rbagmodelshow = false
				this.openrbagmodelshow = false
				this.$emit('onClose') // 关闭后回调
			},

			// 设置金额
			setMoney(money) {
				const digitalArr = String(money).split('')
				const dataList = []
				digitalArr.forEach((num) => {
					const obj = {
						num: isNaN(num) ? num : Number(num),
						style: ''
					}
					dataList.push(obj)
				})
				this.digitalData = dataList
			},
		}
	}
</script>

<style lang="scss" scoped>
	@keyframes bagAni {
		0% {
			transform: rotate(6deg);
		}

		100% {
			transform: rotate(36deg);
		}
	}

	.message-content {
		width: 360rpx;
		padding: 24rpx;
		background: rgb(248, 162, 60);
		color: #FCFEFD;
		border-radius: 10rpx;

		.img {
			width: 50rpx;
			height: 60rpx;
			margin-right: 10rpx;
		}

		.line {
			width: 100%;
			height: 1px;
			background: #EF9B45;
			margin: 10rpx 0;
			display: inline-block;
		}

	}

	.red-bag {
		.bag-btn {
			position: fixed;
			left: -46rpx;
			top: 360rpx;
			width: 150rpx;
			height: 100rpx;
			z-index: 999;
			animation-name: bagAni;
			animation-duration: 1.1s;
			animation-iteration-count: infinite;
			animation-direction: alternate;
		}

		// 红包封面
		.rbag-model {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
			background-color: rgba(0, 0, 0, 0.3);
			display: flex;
			align-items: center;
			justify-content: center;
			z-index: 1000;

			.rbag-con {
				position: relative;
				width: 80%;
				height: 840rpx;
				background-color: #da4d44;
				border-radius: 14rpx;
				box-shadow: 0rpx 0rpx 10rpx rgba(0, 0, 0, 0.2);

				.rbag-box {
					position: relative;
					width: 100%;
					height: 100%;
					border-radius: 14rpx;
					overflow: hidden;
				}

				.rbag_top {
					position: absolute;
					left: -20%;
					top: 0;
					width: 140%;
					height: 540rpx;
					background-color: #e0534a;
					border-radius: 0 0 50% 50%;
					box-shadow: 0 0 14rpx rgba(0, 0, 0, 0.4);
					z-index: 1001;

					.rbag_top_info {
						margin-top: 60rpx;

						.rbag_logo {
							width: 160rpx;
							height: 160rpx;
							border-radius: 50%;
							display: block;
							margin: 0 auto;
							overflow: hidden;
						}

						.app_name {
							font-size: 38rpx;
							color: #f6ac96;
							text-align: center;
							margin-top: 18rpx;
							letter-spacing: 1rpx;
						}

						.rbag_tips {
							font-size: 50rpx;
							color: #edddd3;
							text-align: center;
							margin-top: 34rpx;
							letter-spacing: 1rpx;
						}
					}
				}

				.open_rbag_btn {
					position: absolute;
					top: 450rpx;
					left: 0;
					right: 0;
					width: 180rpx;
					height: 180rpx;
					line-height: 180rpx;
					border-radius: 50%;
					margin: 0 auto;
					text-align: center;
					background-color: #ffd287;
					font-size: 55rpx;
					color: #fef5e8;
					box-shadow: 2rpx 2rpx 6rpx rgba(0, 0, 0, 0.2);
					z-index: 1002;
				}

				.hide_btn {
					position: absolute;
					bottom: -110rpx;
					left: 0;
					right: 0;
					width: 90rpx;
					height: 90rpx;
					margin: 0 auto;
				}
			}
		}

		// 打开红包
		.open_rbag_model {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
			background-color: rgba(0, 0, 0, 0.3);
			z-index: 1000;

			.rbag_conbg {
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				width: 80%;
				height: 840rpx;
				margin: auto;
				z-index: 1001;
			}

			.open_rbag_con {
				z-index: 1002;

				.open_title {
					height: 120rpx;
					line-height: 120rpx;
					text-align: center;
					font-size: 38rpx;
					letter-spacing: 2rpx;
					color: #e46965;
				}

				.rbag_detail {
					margin-top: 90rpx;

					.open_money {
						text-align: center;
						font-size: 80rpx;
						color: #c95948;
						font-weight: bold;
						display: flex;
						justify-content: center;

						.danwei {
							font-size: 30rpx;
							margin-left: 16rpx;
							margin-top: 24rpx;
						}
					}

					.open_tips {
						text-align: center;
						font-size: 30rpx;
						color: #d26762;
						margin-top: 30rpx;
					}
				}

				.lookbag_box {
					margin-top: 300rpx;
					display: flex;
					justify-content: center;

					.lookbag_btn {
						width: 70%;
						height: 90rpx;
						line-height: 90rpx;
						text-align: center;
						font-size: 32rpx;
						color: #c95948;
						letter-spacing: 2rpx;
						background-color: #ffd356;
						border-radius: 50rpx;
						box-shadow: 0rpx 0rpx 4rpx rgba(0, 0, 0, 0.2);
					}
				}

				.hide_btn {
					position: absolute;
					bottom: -110rpx;
					left: 0;
					right: 0;
					width: 80rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					margin: 0 auto;
				}
			}
		}
	}

	.digital-scroll {
		font-size: 28rpx;
		font-weight: bold;
		display: flex;
		align-items: center;

		.digital {
			display: flex;
			justify-content: center;
			width: 0.7em; // 0.7em 是为了让文本间隔没那么宽
			height: 1em;
			line-height: 1em;
			overflow: hidden;

			.scroll-num {
				// 文本竖直排列
				writing-mode: vertical-rl;
				text-orientation: upright;

				.tra-num {
					transition: all 1s;
				}
			}
		}

		.digitalStr {
			width: auto;
			line-height: 1em;
		}
	}
</style>