<template>
	<view>
		<view class="top">
			<uni-search-bar class="search" radius="30" placeholder="请输入课程名称" v-model="keyword" clearButton="auto" cancelButton="none" />
			
			<scroll-view scroll-x :scroll-left="scrollLeft" class="tabs">
				<view v-for="(tab, i) in tabs" :key="i" :class="['item', {active: curTab === tab.classificationId}]" @click="tabChange(tab.classificationId)">{{ tab.classificationName }}</view>
			</scroll-view>
		</view>
		
		<template v-if="list.length">
			<ul class="list">
				<li v-for="(item, i) in list" :key="i" @click="toDetail(item)">
					<image class="cover" :src="item.coverUrl"></image>
					<view class="name">{{ item.goodsName }}</view>
					<view class="entry">{{ !item.isInEffect ? '续费' : '进入课程' }}</view>
				</li>
			</ul>
			<uni-load-more :status="status" />
		</template>
		<empty v-else />
		
		<realName ref="realName" />
	</view>
</template>

<script>
	import { schoolCourse, recentUse, getSchoolCourseAuthority } from '@/apis/modules/course.js'
	export default {
		data() {
			return {
				curTab: -1,
				tabs: [],
				scrollLeft: 0,
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				sort: 0,
				keyword: '',
				list: [],
				page: 1,
				pageSize: 10,
				total: 0,
			}
		},
		watch: {
			keyword () {
				clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.initList()
				}, 500)
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		// 上拉加载
		onReachBottom() {
			if (this.reachBottom >= 0) {
				this.reachBottom = 1
				this.status = 'loading'
				this.getList()
			}
		},
		onShow() {
						// uni.setStorageSync('token', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjQ2NzQ0LCJyblN0ciI6IktVcVR5eWtSbGlVQzF0dmRjdDRuR3dWaGVBaTJwQ2lMIiwiYWNjb3VudElkIjo0Njc0NCwidXNlcklkIjo0Njc0Mywic2Nob29sSWQiOjI4NDYsInVzZXJOYW1lIjoiYWMiLCJwbGF0Zm9ybUlkIjoiMSJ9.zo6L0AYyZA5yfMFovV9oGQHuFJgs936fa4bzyvpiK4Y')
			this.$refs.realName.handleRealName()
			this.getTab()
		},
		methods: {
			// 课程列表
			async getList() {
				uni.showLoading({
					title: '加载中'
				})
				// 最近使用
				if (this.curTab === 0) {
					const res = await recentUse({
						courseType: 0,
					    pageNum: this.page,
					    pageSize: this.pageSize,
					    goodsName: this.keyword,
					})
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = res.list.records
					this.list = this.reachBottom > 0 ? [...this.list, ...list] : list
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === res.list.total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
				} else {
					const { data } = await schoolCourse({
					    authority: this.curTab === -1 ? '' : this.curTab,
					    goodsName: this.keyword,
						pageNum: this.page,
						pageSize: this.pageSize,
					})
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = data.records
					this.list = this.reachBottom > 0 ? [...this.list, ...list] : list
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === data.total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
				}
			},
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getList()
			},
			// 获取tab
			async getTab () {
			  const { data } = await getSchoolCourseAuthority()
			  this.tabs = [
				{
				  classificationId: -1,
				  classificationName: '全部',
				},
				{
				  classificationId: 0,
				  classificationName: '最近使用',
				},
				...data,
			  ]
			  this.initList()
			},
			// tab切换
			tabChange(id) {
				this.curTab = id
				this.initList()
			},
			// 跳转详情
			toDetail(item) {
				this.$util.to(!item.isInEffect ? `/course/customer/customer` : `/course/courseDetail/courseDetail?cid=${item.cid}&mallId=${item.mallId}`) // 过期了跳转到添加客服，否则跳详情
			},
		}
	}
</script>

<style scoped lang="scss">
.top {
	position: sticky;
	top: 0;
	left: 0;
	padding: 20rpx 20rpx 0;
	margin-bottom: 20rpx;
	background-color: #fff;
	.tabs {
		display: flex;
		margin-top: 10rpx;
		white-space: nowrap;
		.item {
			position: relative;
			display: inline-block;
			padding: 0 4rpx 16rpx;
			text-align: center;
			font-size: 28rpx;
			color: #333;
			&:not(:last-child) {
				margin-right: 40rpx;
			}
		}
		.active {
			border-bottom: 4rpx solid #007EFF;
		}
	}
}

.list {
	display: flex;
	flex-wrap: wrap;
	padding: 40rpx 30rpx 0;
	li {
		width: calc(50% - 16rpx);
		padding: 24rpx;
		margin: 0 30rpx 52rpx 0;
		border-radius: 16rpx;
		background-color: #fff;
		box-sizing: border-box;
		&:nth-child(2n) {
			margin-right: 0;
		}
	}
	.cover {
		width: 100%;
		height: 160rpx;
		margin-top: -50rpx;
		border-radius: 20rpx;
		object-fit: cover;
	}
	.name {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		text-overflow: ellipsis;
		overflow: hidden;
		height: 68rpx;
		margin: 10rpx 0 16rpx;
		font-size: 26rpx;
		color: #333;
	}
	.entry {
		font-size: 26rpx;
		line-height: 1.9;
		color: #333;
		text-align: center;
		border: 1px solid #dadada;
		border-radius: 40rpx;
	}
}
</style>
