<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in dataList" :key='index'>
				<view class="item" @tap="toDetail(item.code)">
					<view class="num">{{item.code}}</view>
					<view class="date">兑换时间：{{item.userExchangeTime}}</view>
				</view>
			</block>
		</view>
		<no-data v-if="!dataList.length" :showText="'暂无数据'"></no-data>
		<loading-text v-if="dataList.length"  :loading="loading" :noMore="noMore" ></loading-text>
		
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	import EventBus from '@/utils/eventbus.js';
	
	export default {
		data() {
			return {
				pageNum: 1,
				pageSize: 8,
				dataList: [],
				noMore: false,
				loading: false,
			}
		},
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onLoad() {
			this.getList();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1;
			this.getList();
		},
		
		// 上拉加载
		onReachBottom() {
			this.getList(1);
		},
		
		methods: {
			// 获取列表
			getList(loadMore) {
				if(this.noMore && loadMore)return;
				this.noMore = false
				if(!loadMore){
					this.pageNum = 1;
				}else{
					this.loading = true;
				}
				this.$axios({
					url: '/promotion/luck/draw/list',
					method: 'post',
					params: {
						pageNum: this.pageNum,
						pageSize: this.pageSize
					},
					isLoading: !loadMore
				}).then(res => { 
					let _list = res.data.records;
					let pageTotal = res.data.pages;
					if(this.pageNum >= pageTotal){
						this.noMore = true;
					}
					if (_list.length) {
						this.pageNum += 1
					}
					if (loadMore) {
						this.dataList = this.dataList.concat(_list);
						this.loading = false;
					} else {
						this.dataList = _list;
					}
					
					uni.stopPullDownRefresh();
				})
			},
			
			toDetail(code) {
				uni.navigateTo({
					url: '/pages/mine/exchange/detail?code=' + code
				})
			}
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		padding: 20rpx;
		padding-bottom: 250rpx;
		box-sizing: border-box;
	}
	.list-container {
		.item {
			border-radius: 20rpx;
			background: #ffffff;
			margin-bottom: 20rpx;
			padding: 20rpx 30rpx;
			.num {
				font-size: 32rpx;
				color: #333333;
				font-weight: 500;
			}
			.date {
				font-size: 24rpx;
				color: #999999;
				margin-top: 10rpx;
			}
		}
	}
</style>
