<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 flex" @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 && !loading">
				<view class="center">
					<image class="center-img" src="https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/nodata_4.png" mode=""></image>
					<view class="center-font">还没有记录</view>
				</view>
			
			</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 16rpx;
			}
			
			&-title {
				font-size: 26rpx;
				line-height: 26rpx;
				margin-bottom: 24rpx;
				font-weight: 500;
				color: #FFFFFF;
				opacity: 0.72;
			}
			
			&-btn {
				width: 200rpx;
				height: 70rpx;
				line-height: 70rpx;
				font-size: 30rpx;
				text-align: center;
				background: #fff;
				border-radius: 16rpx;
				
				image {
					width: 56rpx;
					height: 56rpx;
					margin-right: 10rpx;
				}
			}
		}

		// 盲豆流水
		&-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;
		.center {
			text-align: center;
			
			&-img {
				width: 228rpx;
				height: 320rpx;
			}
			
			&-font {
				font-size: 30rpx;
				font-weight: 400;
				color: #999999;
				margin-bottom: 250rpx;
			}
		}
	}
</style>
