<template>
	<view class="content">
		<u-subsection mode="subsection" :active-color="mainColor" :list="tabList" :current="current" @change="tabsChange"></u-subsection>
		<swiper :current="swiperCurrent" @animationfinish="animationfinish" class="swiper-box">
			<swiper-item class="swiper-item" v-for="(item, index1) in tabList" :key="index1">
				<scroll-view scroll-y class="scroll-box" @scrolltolower="handleLoadMore" :refresher-enabled="true"
				 :refresher-triggered="triggered" :refresher-threshold="100" refresher-background="white" @refresherrefresh="onRefresh"
				 @refresherrestore="onRestore">
					<u-card :head-border-bottom="false" :foot-border-top="false" padding="0px" margin="0" borderRadius="0" v-for="(site, index2) in item.tableList"
					 :key="index2" class="class-card" @click="goToStudent(site)">
						<view class="class-content" slot="body">
							<view class="class-info-img">
								<u-avatar :src="site.url" size="100"></u-avatar>
							</view>
							<view class="class-info-text">{{site.name}}&nbsp;&nbsp;{{index1 == 1 ? site.residue + '课时' :''}}</view>
							<u-icon name="arrow-right" color="#aaaaaa" size="36"></u-icon>
						</view>
					</u-card>
					<u-divider v-if="item.isOver" bg-color="transparent">没有更多了</u-divider>
				</scroll-view>
			</swiper-item>
		</swiper>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'handleCustomStyle',
				'handleDefaultCustomStyle',
			])
		},
		data() {
			return {
				triggered: false,
				tabList: [{
					name: '到期提醒',
					isOver: false,
					pageIndex: 1,
					tableList: [],
				}, {
					name: '到期余课',
					isOver: false,
					pageIndex: 1,
					tableList: [],
				}],
				// 请求数据size
				pageSize: 12,
				current: 0,
				swiperCurrent: 0,
			}
		},
		onLoad() {
			this.getTableList(0)
			this.getTableList(1)
		},
		onReady() {},
		methods: {
			//  tab页面切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			//  swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				this.swiperCurrent = current;
				this.current = current;
			},
			//  下拉刷新
			onRefresh() {
				console.log('下拉刷新');
				if (!this.triggered) {
					this.triggered = true
					this.tabList[this.current].isOver = false
					this.tabList[this.current].pageIndex = 1
					this.tabList[this.current].tableList = []
					this.getTableList(this.current, 'refresh')
				}
			},
			//  重置下拉刷新状态
			onRestore() {
				console.log('重置下拉刷新状态');
				this.triggered = 'restore'
				this.triggered = false
			},
			//  懒加载
			handleLoadMore() {
				console.log('懒加载');
				if (!this.tabList[this.current].isOver) {
					this.tabList[this.current].pageIndex++
					this.getTableList(this.current)
				}
			},
			//  获取列表数据
			getTableList(index, refresh) {
				NET.request(API.getRenewList, {
					type: index,
					page: this.tabList[index].pageIndex,
					size: this.pageSize,
				}, 'POST').then(res => {
					this.triggered = false
					this.tabList[index].tableList = this.tabList[index].tableList.concat(res.data.row)
					this.tabList[index].isOver = res.data.row.length != this.pageSize
				}).catch(error => {
					this.triggered = false
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  跳转学生详情
			goToStudent(site) {
				uni.navigateTo({
					url: '/pagesMain/studentInfo?type=2&id=' + site.id
				});
			}
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;

		.swiper-box {
			height: calc(100vh - 34px);

			.swiper-item {
				height: calc(100vh - 34px);

				.scroll-box {
					width: 100%;
					height: calc(100vh - 34px);
					box-sizing: border-box;
					padding-bottom: 10px;

					.class-card {
						border-bottom: 1px solid #cccccc;

						.class-content {
							padding: 5px 15px;
							display: flex;
							align-items: center;

							.class-info-img {
								width: 50px;
								height: 50px;
								margin-right: 5px;
								display: flex;
								align-items: center;
								justify-content: center;
							}

							.class-info-text {
								line-height: 20px;
								font-size: 14px;
								flex: 1;
							}
						}
					}

					.class-card:last-child {
						border-bottom: none;
					}
				}
			}
		}
	}
</style>
