<template>
	<view>
		<!-- #ifdef MP-ALIPAY -->
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲豆"
			leftIconSize="0"></u-navbar>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲豆"></u-navbar>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN || MP-ALIPAY -->
		<view v-if="pagesNum > 1">
			<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="我的盲豆" />
		</view>
		<view v-else>
			<u-navbar leftIconSize="0" :placeholder="true" bgColor="#fff" :border="true" title="我的盲豆">
				<view class="nav-left flex" slot="left" @click="$toIndex()">
					<u-icon name="arrow-left" size="20" color="#333"></u-icon>
				</view>
			</u-navbar>
		</view>
		<!-- #endif -->

		<view class="bean">
			<!-- 盲豆数 -->
			<view class="bean-balance flex">
				<view class="bean-balance-num">{{ initData.coinNum }}</view>
				<view class="bean-balance-title">盲豆余额</view>
				<view class="">
					<view class="bean-balance-btn flex" @click="toCore">立即兑换</view>
					<view class="bean-balance-btn flex" @click="onTransfer">盲豆转赠</view>
				</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.split('｜')[1] ? item.logText.split('｜')[0] : item.logText }}</view>
						<view class="beanNum" v-if="item.logText.split('｜')[1]">{{ item.logText.split('｜')[1] }}</view>
					</view>
					<view class="flex right">
						<view class="amt">{{ item.logMoney > 0 ? '+' : '' }}{{ item.logMoney }}</view>
						<view class="balance">余额：{{ item.money }}</view>
						<view class="beanNum" v-if="item.logText.split('｜')[1]">-</view>
					</view>
				</view>
			</view>
			<u-loadmore :line="true" v-if="list.length>7" :status="status" :loading-text="'努力加载中'" :nomore-text="'已经到底了'" />
			<view class="flex empty" v-if="!list.length">
				<view class="center">
					<image class="center-img" src="https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/nodata_6.png"
						mode="scaleToFill"></image>
					<view class="center-font">还没有记录</view>
				</view>
			</view>
		</view>
		<Transfer :popup-show="popupShow" @close="close" @confirm="confirm" :coin-num="coinNum"
					v-if="popupShow" />
	</view>
</template>

<script>
	import $http from '@/utils/request.js'
	import Transfer from './components/transfer.vue'
	export default {
		components: {
			Transfer
		},
		data() {
			return {
				status: 'nomore', //上拉刷新状态
				initData: {},
				pageNum: 1,
				total: 0,
				list: [],
				pagesNum: '',
				popupShow: false,
				coinNum: 0,
			};
		},
		onShow() {
			this.pagesNum = getCurrentPages().length
			this.getBean()
			this.pageList()
		},
		methods: {
			getBean() {
				$http.post('/api/v1/mp/user/mine/init', {}).then(res => {
					if (res.code == 0) {
						this.initData = res.data
						this.coinNum = this.initData.coinNum
					}
				}).catch(() => {})
			},

			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()
			},
			
			//盲票转赠
			onTransfer() {
				this.popupShow = true
			},
			
			close() {
				this.popupShow = false
			},
			
			confirm() {
				//刷新盲豆数量
				this.getBean()
				//刷新盲豆记录
				this.pageList()
			},

			toCore() {
				uni.switchTab({
					url: "/pages/core/index"
				})
			},
		},

		onReachBottom() {
			if (this.total < this.pageNum * 50) return;
			this.status = 'loading';
			++this.pageNum
			if (this.total < this.pageNum * 50) this.status = 'nomore';
			else this.status = 'loading';
			this.getList()
		},
	}
</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 {
				display: inline-block;
				margin: 0 20px;
				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;
					}
					
					.beanNum {
						margin-top: 20rpx;
						color: #AAAAAA;
					}
					
				}

				.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;
					}
					.beanNum{
						margin-top: 20rpx;
						color: #fff;
					}
				}
			}

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