<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的芒豆"></u-navbar>
		<view class="bean">
			<view class="flex bean-balance">
				<image src="../../static/icon/bean.png" mode="aspectFill"></image>
				<view class="bean-balance-num">{{ initData.coinNum }}</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="title">{{ item.logText }}</view>
						<view class="date">{{ $parseTime(item.createdTime) }}</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()
			}
		},
		
		onReachBottom() {
			// 判断是否有数据
			if (this.total > this.pageNum * 50) {
				setTimeout(() => {
					++this.pageNum
					this.getList()
				}, 500)
			} else {
				uni.$u.toast('没有更多数据了')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.bean {
		margin: 20rpx 10rpx;

		&-balance {
			justify-content: flex-start;
			height: 230rpx;
			line-height: 230rpx;
			border-radius: 10rpx;
			background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/md-bkgd.png) center center;
			font-size: 50rpx;
			padding-left: 180rpx;
			font-weight: bold;
			margin-bottom: 20rpx;

			image {
				width: 130rpx;
				height: 130rpx;
				margin-right: 16rpx;
			}
		}

		&-list {
			background-color: #FFFFFF;
			padding: 0 40rpx;

			&-item {
				justify-content: space-between;
				padding: 24rpx 0;
				border-bottom: 1px solid rgba(236, 236, 236, 100);

				.left {
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-start;
				}

				.right {
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-end;
				}

				.title {
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 32rpx;
				}

				.amt {
					margin-bottom: 32rpx;
				}

				.balance {
					color: #606060;
				}
			}

			&-item:last-child {
				border-bottom: none;
			}
		}
	}
	.empty{
		height: 60vh;
	}
</style>
