<template>
	<view class="content">
		<u-tabs-swiper ref="uTabs" :active-color="mainColor" :list="tabList" :current="current" @change="tabsChange"
		 :is-scroll="false"></u-tabs-swiper>
		<swiper :current="swiperCurrent" @transition="transition" @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="10px" borderRadius="40" v-for="(site, index2) in item.tableList"
					 :key="index2" class="card-box" @click="goToClassDetail(site)">
						<view class="card-content" slot="head" style="padding-top: 10px;">
							<view class="card-name">{{site.name}}</view>
						</view>
						<view class="card-content" slot="body">
							<view class="card-info-text">
								<u-icon name="clock"></u-icon>
								{{site.classStartDate}}&nbsp;&nbsp;{{site.classStartHours}}
							</view>
							<view class="card-info-text">
								<u-icon name="map"></u-icon>
								{{site.address}}
							</view>
						</view>
					</u-card>
					<u-divider v-if="item.isOver" bg-color="transparent">没有更多了</u-divider>
				</scroll-view>
			</swiper-item>
		</swiper>
		<u-icon name="plus-circle-fill" :color="mainColor" size="96" class="fix-add-icon" @click="goToAddClassForm()"></u-icon>
		<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',
			])
		},
		data() {
			return {
				triggered: false,
				tabList: [{
					name: '全部',
					status: 3,
					isOver: false,
					pageIndex: 1,
					tableList: [],
				}, {
					name: '已开班',
					status: 1,
					isOver: false,
					pageIndex: 1,
					tableList: [],
				}, {
					name: '未开班',
					status: 0,
					isOver: false,
					pageIndex: 1,
					tableList: [],
				}, {
					name: '审核中',
					status: 2,
					isOver: false,
					pageIndex: 1,
					tableList: [],
				}],
				current: 0,
				swiperCurrent: 0,
			}
		},
		onShow() {
			this.tabList.forEach((site, index) => {
				site.isOver = false
				site.pageIndex = 1
				site.tableList = []
				this.getTableList(index)
			})
		},
		methods: {
			//  tab页面切换
			tabsChange(index) {
				this.swiperCurrent = index;
			},
			//  swiper-item左右移动，通知tabs的滑块跟随移动
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			//  swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			//  下拉刷新
			onRefresh() {
				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() {
				this.triggered = 'restore'
				this.triggered = false
			},
			//  懒加载
			handleLoadMore() {
				if (!this.tabList[this.current].isOver) {
					this.tabList[this.current].pageIndex++
					this.getTableList(this.current)
				}
			},
			//  获取列表数据
			getTableList(index, refresh) {
				NET.request(API.getClassList, {
					status: this.tabList[index].status,
					page: this.tabList[index].pageIndex,
					size: 10,
				}, '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 != 10
				}).catch(error => {
					this.triggered = false
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  跳转新增班级表单
			goToAddClassForm() {
				uni.navigateTo({
					url: '/pagesClass/addClassForm'
				});
			},
			//  跳转班级详情
			goToClassDetail(site) {
				uni.navigateTo({
					url: '/pagesClass/classDetail?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;
		position: relative;

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

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

				.scroll-box {
					width: 100%;
					height: calc(100vh - 34px);

					.card-box {
						.card-content {
							padding: 5px 15px;
						}

						.card-name {
							height: 20px;
							display: inline-block;
							font-weight: bold;
							font-size: 14px;
							line-height: 20px;
						}

						.card-info-text {
							color: #999999;
							margin-bottom: 5px;

							/deep/.u-icon {
								margin-right: 5px;
								font-size: 14px;
								color: #000000;
							}

						}
					}
				}
			}
		}

		.fix-add-icon {
			position: fixed;
			bottom: 15px;
			right: 15px;
		}
	}
</style>
