<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>
		
		<ul v-if="list.length" 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>
		<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)
		},
		onShow() {
						// uni.setStorageSync('token', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjM5NTc2LCJyblN0ciI6ImtOaUd2TVI4dlA5eldNZTFlbUFKSmoycWRHMENaU1Q5IiwiYWNjb3VudElkIjozOTU3NiwidXNlcklkIjozOTU3NSwic2Nob29sSWQiOjI4NDYsInVzZXJOYW1lIjoiYWMiLCJwbGF0Zm9ybUlkIjoiMSJ9.JRcmjfyBUik35mG8UxtGTsD0hFGdQfY63cQKQMajq3I')
			this.$refs.realName.handleRealName()
			this.getTab()
		},
		methods: {
			// 课程列表
			async getList() {
				// 最近使用
				if (this.curTab === 0) {
					const { page } = await recentUse({
					  pageNum: 1,
					  pageSize: 100,
					  goodsName: this.keyword,
					})
					this.list = page.records
				} else {
					const { data } = await schoolCourse({
					  authority: this.curTab === -1 ? '' : this.curTab,
					  goodsName: this.keyword,
					})
					this.list = data
				}
			},
			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.getList()
			},
			// tab切换
			tabChange(id) {
				this.curTab = id
				this.initList()
			},
			// 跳转详情
			toDetail(item) {
				if (!item.isInEffect) {
					this.$util.errMsg('课程已到期，请联系课程负责老师！')
				} else {
					this.$util.to(`/course/courseDetail/courseDetail?cid=${item.cid}&mallId=${item.mallId}`)
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.top {
	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;
	max-height: calc(100vh - 262rpx);
	padding: 40rpx 30rpx 30rpx;
	overflow: auto;
	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>
