<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲豆"></u-navbar>
		<view class="bean">
			<!-- 盲豆数 -->
			<view class="bean-balance flex">
				<view class="bean-balance-num">{{ initData.coinNum }}</view>
				<view class="bean-balance-title">盲豆余额</view>
				<view class="bean-balance-btn" @click="toCore">立即兑换</view>
			</view>
			
			<!-- 盲豆流水 -->
			<view class="bean-list">
				<view class="flex bean-list-item" v-for="(item, index) in list" :key="index">
					<view class="flex left">
						<view class="date">{{ $parseTime(item.createdTime) }}</view>
						<view class="title">{{ item.logText }}</view>
					</view>
					<view class="flex right">
						<view class="amt">{{ item.logMoney > 0 ? '+' : '' }}{{ item.logMoney }}</view>
						<view class="balance">余额：{{ item.money }}</view>
					</view>
				</view>
			</view>
			<view class="flex empty" v-if="!list.length">
				<u-empty text="数据为空" mode="order" />
			</view>
		</view>
	</view>
</template>

<script>
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				initData: {},
				pageNum: 1,
				total: 0,
				list: [],
			};
		},
		onShow() {
			this.getBean()
			this.pageList()
		},
		methods: {
			getBean() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/mine/init', {}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.initData = res.data
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			getList() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post(`/api/v1/mp/user/mine/coin/log/list?pageNum=${this.pageNum}&pageSize=50`, {}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						res.rows.forEach(item => {
							item.type = item.type ? JSON.parse(item.type) : ''
						})
						this.total = res.total
						this.list = this.list.concat(res.rows)
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			pageList() {
				this.pageNum = 1
				this.total = 0
				this.list = []
				this.getList()
			},
			
			toCore(){
				uni.switchTab({
					url:"/pages/core/index"
				})
			}
		},

		onReachBottom() {
			// 判断是否有数据
			if (this.total > this.pageNum * 50) {
				setTimeout(() => {
					++this.pageNum
					this.getList()
				}, 500)
			} else {
				uni.$u.toast('已经到底了')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.bean {
		margin: 34rpx 34rpx;
		padding-bottom: 100rpx;

		// 盲豆数
		&-balance {
			height: 264rpx;
			flex-direction: column;
			justify-content: flex-start;
			background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/coin_bkg.png) center center no-repeat;
			background-size: calc(100vw - 68rpx) 264rpx;
			margin-bottom: 34rpx;
			
			&-num {
				font-size: 52rpx;
				line-height: 52rpx;
				font-weight: bold;
				color: #FFFFFF;
				margin: 36rpx 0 24rpx;
			}
			
			&-title {
				font-size: 26rpx;
				line-height: 26rpx;
				margin-bottom: 42rpx;
				font-weight: 500;
				color: #FFFFFF;
				opacity: 0.72;
			}
			
			&-btn {
				width: 200rpx;
				height: 70rpx;
				line-height: 70rpx;
				color: #FFFFFF;
				text-align: center;
				background: #AC1F12;
				border-radius: 16rpx;
			}
		}

		// 盲豆流水
		&-list {
			background-color: #FFFFFF;
			padding: 0 34rpx;
			border-radius: 22rpx;

			&-item {
				justify-content: space-between;
				padding: 40rpx 0;
				border-bottom: 1px solid #eee;

				.left {
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-start;
					
					.date {
						color: #AAAAAA;
						line-height: 24rpx;
						margin-bottom: 28rpx;
					}
					
					.title {
						font-size: 30rpx;
						line-height: 30rpx;
					}
				}

				.right {
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-end;
					
					.amt {
						color: #FF4208;
						font-size: 30rpx;
						line-height: 30rpx;
						margin-bottom: 28rpx;
					}
					
					.balance {
						color: #AAAAAA;
					}
				}
			}

			&-item:last-child {
				border-bottom: none;
			}
		}
	}

	.empty {
		height: 60vh;
	}
</style>
