<template>
	<view>
		<lf-nav title="我的报名" :showIcon="true" bgColor="#fff" @changeHeight="e => nav_height = e"></lf-nav>
		<view v-if="tab_list.length">
			<u-tabs :list="tab_list" active-color="#15716E" inactive-color='#777777' :is-scroll="true" :current="tab_current" @change="tabChange"></u-tabs>
		</view>
		<swiper :style="{height: autoHeight}" :current="tab_current" @change="swiperChange">
			<swiper-item v-for="(tab, tabIndex) in tab_list" :key="tabIndex">
				<scroll-view :style="{height: autoHeight}" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="isRefresher"
					@scrolltolower="onScrolltolower" @refresherrefresh="onRefresherrefresh">
					<view class="scroll-content">
						<view class="card" v-for="(item, index) in tab.list" :key="index" @click="goDetails(item)">
							<view class="cover">
								<image class="img" :src="item.activity.image"></image>
								<view class="label" :class="'theme-'+ (index+1)" v-if="item.activity.is_expired==false && item.state =='待使用'">待使用</view>
								<view class="label lf-bg-gray" :class="'theme-'+ (index+1)" v-if="item.activity.is_expired==true">已过期</view>
								<view class="label" :class="'theme-'+ (index+1)" v-if="item.activity.is_expired==false">{{item.state}}</view>
							</view>
							<view class="info">
								<view class="title">{{item.activity.name}}</view>
								<view class="date">{{item.created_at}}</view>
							</view>
						</view>
						<!-- 空数据的情况 -->
						<view class="loading-more">
							<text v-if="tab.list.length"
								:class="{'loading-more-text': tab.loadingClass}">{{ tab.loadingText }}</text>
							<lf-nocontent src="/static/images/empty.png" v-else></lf-nocontent>
						</view>
					</view>
					<view style="height: 30rpx;"></view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data(){
			let _public = {
				page: 1,
				isPage: true,
				loadingClass: true,
				loadingText: '正在加载中'
			}
			return {
				tab_current: 0,
				tab_list: [{
					name: '全部',
					state: '',
					list: [],
					..._public
				},{
					name: '待参加',
					state: '待使用',
					list: [],
					..._public
				},{
					name: '已参加',
					state: '已使用',
					list: [],
					..._public
				},{
					name: '已过期',
					state: '已过期',
					list: [],
					..._public
				}],
				scrollH: 0,
				nav_height: 0,
				isRefresher: true,
				pageSize: 10
			}
		},
		computed: {
			autoHeight(){
				return `calc(${this.scrollH}px - ${this.nav_height}px - 86rpx)`;
			}
		},
		onLoad(){
			let info = uni.getSystemInfoSync();
			this.scrollH = info.screenHeight;
			this.getMyActivity()
		},
		methods: {
			goDetails(item) {
				if(item.activity.is_expired) {
					this.$msg('该活动已过期!');
				}else {
					this.$url('/pages/user/my/myregister?activity_id='+item.id);
				}
			},
			// 页面触底，加载下一页
			onScrolltolower(){
				let tab_item = this.tab_list[this.tab_current];
				if(tab_item.isPage){
					tab_item.page = tab_item.page + 1;
					this.getMyActivity();
				}
			},
			// 下拉刷新处理
			refreshFn(options){
				let tab_item = this.tab_list[this.tab_current];
				tab_item.page = 1;
				tab_item.isPage = true;
				tab_item.loadingClass = true;
				tab_item.list = []
				tab_item.loadingText = '正在加载中';
				this.getMyActivity(options);
			},
			// scroll-view 下拉刷新
			onRefresherrefresh(){
				this.isRefresher = true;
				this.refreshFn({type: 'scrollRefresh'});
			},
			tabChange(event){
				this.tab_current = event;
				this.getMyActivity();
			},
			swiperChange(event){
				this.tab_current = event.detail.current;
				if (event.detail.source == '') return; // 如果是被动出发，没有事件类型则不做处理
				this.getMyActivity();
			},
			getMyActivity(options = {}) {
				let tab_item = this.tab_list[this.tab_current];
				console.log(tab_item.state)
				this.$http
				    .get({
				        api: 'api/activity/my',
						data:{
							state: tab_item.state,
							page: tab_item.page,
							page_size: this.pageSize
						},
						header: {
						    Authorization: this.$cookieStorage.get('user_token')
						},
				    })
				    .then(res => {
				        if (res.data.code == 200) {
				            if (res.data.status) {
								let isPage = res.data.next_page_url == null?false:true;
								tab_item.isPage = isPage;
								if(!isPage) {
									tab_item.loadingClass = false;
									tab_item.loadingText = '没有更多数据啦~';
								}
								if(options.type == 'pageRefresh') {
									uni.stopPullDownRefresh();
								}else if(options.type == 'scrollRefresh') {
									this.isRefresher = false;
								}
								if(tab_item.page == 1) {
									tab_item.list = res.data.data.data;
								}else {
									tab_item.list.push(...res.data.data.data);
								}
				               console.log('数组列表',tab_item.list)
				            } else {
				                wx.showModal({
				                    content: res.data.message || '请下拉页面刷新重试',
				                    showCancel: false
				                });
				            }
				        } else {
				            wx.showModal({
				                content: res.data.message || '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				        wx.hideLoading();
				    })
				    .catch(() => {
				        wx.hideLoading();
				        wx.showModal({
				            content: '请求失败',
				            showCancel: false
				        });
				    });
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.scroll-content{
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		.card{
			width: 686rpx;
			height: max-content;
			background-color: #FFFFFF;
			box-shadow: 1rpx 1rpx 6rpx 3rpx #e5e5e5;
			margin-top: 30rpx;
			border-radius: 20rpx;
			overflow: hidden;
			.cover{
				width: 686rpx;
				height: 300rpx;
				position: relative;
				.img{
					width: 100%;
					height: 100%;
					background-color: #EEEEEE;
				}
				.label{
					position: absolute;
					top: 0;
					left: 0;
					width: 118rpx;
					height: 57rpx;
					border-radius: 20rpx 0rpx 20rpx 0rpx;
					text-align: center;
					line-height: 57rpx;
					color: #FFFFFF;
					font-size: 26rpx;
				}
			}
			.info{
				width: 686rpx;
				height: max-content;
				padding: 20rpx 30rpx;
				box-sizing: border-box;
				.title{
					font-size: 28rpx;
					color: #222222;
					font-weight: bold;
				}
				.date{
					margin-top: 20rpx;
					font-size: 24rpx;
					color: #555555;
				}
			}
		}
	}
	
	.theme-1{
		background-color: #15716E;
	}
	.theme-2{
		background-color: #1789E4;
	}
	.theme-3{
		background-color: #777777;
	}
	
	// tabs 样式修改
	/deep/.u-scroll-box {
		display: flex;
		justify-content: center;
		align-items: center;
		border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
	}
	/deep/.u-scroll-box .u-tab-bar {
		background-color: #15716E!important;
		width: 80rpx!important;
		position: absolute;
		left: 0;
		bottom: -12rpx;
	}
	
	/deep/.special_tab .u-tabs .u-scroll-box .u-tab-bar {
		background-color: #15716E!important;
		width: 56rpx!important;
		position: absolute;
		height: 5rpx!important;
		left: 8rpx;
		bottom: -4rpx;
	}
	
	/deep/ .u-tab-item {
		font-size: 28rpx!important;
	}
</style>
