<template>
	<view>
		<view class="filter">
			<uni-search-bar class="search" radius="30" placeholder="请输入课程名称" v-model="keyword" clearButton="auto" cancelButton="none" />
		</view>
		
		<ul class="tab-wrap">
			<scroll-view scroll-x :scroll-left="scrollLeft" class="tab tab-scroll">
				<li v-for="(tab, i) in tabs" :key="i" :class="{active: curTab === tab.classificationId}" @click="tabChange(tab.classificationId)">{{ tab.classificationName }}</li>
			</scroll-view>
		</ul>
		
		<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">进入课程</view>
			</li>
		</ul>
		<empty v-else />
		
		<RealName />
	</view>
</template>

<script>
	import { schoolCourse, recentUse, getSchoolCourseAuthority } from '@/apis/modules/course.js'
	import RealName from '@/components/realName/realName.vue'
	export default {
		components: { RealName },
		data() {
			return {
				curTab: 0,
				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,
				realNameForm: {
					userName: '',
					workNumber: '',
				},
			}
		},
		watch: {
			keyword () {
				clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.initList()
				}, 500)
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		onShow() {
						uni.setStorageSync('token', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjM5NTc2LCJyblN0ciI6IlpFVDVjb0k3Y2VBVHRYb0RtaDNiaHc5TlF2OVhmYmNRIiwiYWNjb3VudElkIjozOTU3NiwidXNlcklkIjozOTU3NSwic2Nob29sSWQiOjI4NDYsInVzZXJOYW1lIjoiYWMiLCJwbGF0Zm9ybUlkIjoiMSJ9.2Sltl681nbYYPnE0YX5xbF630uOe8hbEcYZqX3mqwCM')
			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: 0,
				  classificationName: '最近使用',
				},
				{
				  classificationId: -1,
				  classificationName: '全部',
				},
				...data,
			  ]
			  this.getList()
			},
			// tab切换
			tabChange(id) {
				this.curTab = id
				this.initList()
			},
			// 跳转详情
			toDetail(item) {
				this.$util.to(`/course/courseDetail/courseDetail?cid=${item.cid}&mallId=${item.mallId}`)
			},
		}
	}
</script>

<style scoped lang="scss">
.tab-wrap {
	display: flex;
	.tab-scroll {
		white-space: nowrap;
		li {
			display: inline-block;
		}
	}
}
.list {
	display: flex;
	flex-wrap: wrap;
	padding: 40rpx 30rpx 30rpx;
	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: 76rpx;
		margin: 10rpx 0 16rpx;
		font-size: 28rpx;
		color: #333;
		
	}
	.entry {
		font-size: 28rpx;
		line-height: 1.9;
		color: #333;
		text-align: center;
		border: 1px solid #dadada;
		border-radius: 40rpx;
	}
}
</style>
