<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲豆"></u-navbar>
		<view class="bean">
			<view class="flex bean-balance">
				<view class="bean-balance-img">
					<image src="../../static/icon/bean.png" mode="aspectFill"></image>
				</view>
				
				<view class="bean-balance-num">{{ initData.coinNum }}</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="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()
			},
			
			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: 20rpx 10rpx;

		&-balance {
			justify-content: flex-start;
			background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/md-bkgd.png) center center;
			padding-left: 34rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
			color: #FFFFFF;

			image {
				width: 130rpx;
				height: 130rpx;
				margin-right: 16rpx;
			}
			
			&-img{
				background-color: rgba(255,255,255,0.7);
				border-radius: 100rpx;
				width: 65px;
				height: 65px;
				
				image {
					width: 57px;
					height: 57px;
					margin:7rpx 7rpx ;
				}
			}

			&-num {
				height: 230rpx;
				line-height: 230rpx;
				border-radius: 10rpx;
				font-size: 72rpx;
				margin-left: 25rpx;
			}
			
			&-btn{
				font-size: 28rpx;
				height: 54rpx;
				width: 160rpx;
				line-height: 51rpx;
				background-color: rgba(255,255,255,0.4);
				color: #FFFFFF;
				border: 2rpx solid #FFFFFF;
				margin-left: 170rpx;
				margin-top: 10rpx;
				border-radius: 27rpx;
				text-align: center;
			}
			&-btn:hover{
				opacity: 0.8;
			}
		}

		&-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>
