<template>
	<!-- 头部 -->
	<nav-bar height="312" backPath="workspace" title="学生学习" :filterPickerData="state.filterPickerData" :tabList="state.tabList" :onlyTitle="false" :showFilterPicker="true" :showTabs="true" rightWidth="0rpx" @CommonFilterData="CommonFilterData"></nav-bar>
	<view class="page_content">
		<scroll-view
		    class="scroll_view_y"
			:scroll-top="state.scrollTop"
			scroll-y="true"
			:refresher-enabled="false" 
			:enable-back-to-top="true"
			:show-scrollbar="false" 
			:scroll-with-animation="true" 
			refresher-default-style="none"
			refresher-background="#F8F9FD" 
			:refresher-triggered="state.isRefreshing" 
			@scrolltolower="OnLoadMore"
			@refresherrefresh="OnRefresh"
			@scroll="OnScroll">
			<view class="refresh_loading" v-if="state.isRefreshing"><uni-load-more status="loading" /></view>
			<view class="page_list">
				<view class="list_item" v-for="item in state.pageList" :key="item.id" @click="GoVideoDetails(item.id)">
					<view class="item_img_box">
						<image class="item_img" mode="scaleToFill" :src="item.videoCoverUrl"></image>
						<view v-if="(item.learnStatus == 3 || item.learnStatus == 4 || item.learnStatus == 1) && state.courseType == '0'" :class="['remaining_days',{wait:item.learnStatus == 3,finish:item.learnStatus == 4,overtime:item.learnStatus == 1}]">
							<template v-if="item.learnStatus == 3">剩余{{item.remainingTime}}</template>
							<template v-if="item.learnStatus == 4">已完成</template>
							<template v-if="item.learnStatus == 1">已结束</template>
						</view>
						<view class="progress"><view class="done" :style="{width:item.progress}"></view></view>
					</view>
					<view class="item_title">{{item.courseName}}</view>
					<view class="item_time">
						<image class="img" src="@/static/image/icon/time2x.png"></image>
						<text class="time">{{item.courseStartDatetime}}</text>
					</view>
				</view>
			</view>
			<view class="no_data" v-if="!state.isLoading && state.pageList.length == 0">
				<image class="no_data_img" src="@/static/image/bg/no_data.png"></image>
				<text class="no_data_text">暂无数据</text>
			</view>
			<uni-load-more v-if="state.pageList.length > 0" :status="state.loadStatus" />
		</scroll-view>
	</view>
</template>

<script setup>
	import navBar from '@/components/navBar.vue';
	import {queryTeacherCourseTypeList,teacherRecordPageListData} from '@/reqApi/studentStudy.js';
	import {
		reactive,
		ref,
		onMounted,
		nextTick
	} from 'vue';
	const state = reactive({
		courseTypeData:[],//顶部查询数据 
		filterPickerData: [{
			valueIndex:0,//默认值索引
			defaultValue:'0',//默认值
			list:[{
				label:'必修任务',
				value:'0',
			},{
				label:'自主研修',
				value:'1',
			}]
		},{
			valueIndex:0,//默认值索引
			defaultValue:'',//默认值
			list:[]
		},{
			valueIndex:0,//默认值索引
			defaultValue:'',//默认值
			list:[]
		}],//必修任务tab
		tabList:[],
		mustTabList: [{
			label: '全部',
			value: ''
		}, {
			label: '待完成',
			value: '3'
		}, {
			label: '已完成',
			value: '4'
		}, {
			label: '已结束',
			value: '1'
		}],
		selftabList: [{
			label: '学习中',
			value: '3'
		}, {
			label: '已完成',
			value: '4'
		}, {
			label: '已关注',
			value: '5'
		}],//自主研修tab
		pageList:[],//列表数据
		queryStr: '',
		courseType:'0',//默认必修任务
		studCourseTypeId: '',//内容领域	
		studCourseTypeClassId: '',//学习专题
		learnStatus: '',//tab选中值
		pageSize: 30,
		pageNum: 1,
		scrollTop:0,
		oldScrollTop:0,
		isRefreshing:false,//设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
		isLoading:true,//加载中
		noMoreData:false,//没有更多数据了
		loadStatus:'more',//loading状态
	})
	onMounted(() => {
		state.tabList = [...state.mustTabList];
		getCourseTypeDataList();
		OnLoadData(true);
	});
	//获取顶部查询项
	const getCourseTypeDataList = () => {
		queryTeacherCourseTypeList().then(res => {
			if(res.code == 200){
				const defaultData = [{
					label:'内容领域',
					id:'',
					value:''
				}];
				const resData = res?.data || [];
				state.courseTypeData = defaultData.concat(resData);
				//内容领域
				state.filterPickerData[1].list = state.courseTypeData.map(item=>({
					...item,
					value:item.id
				}))
			}
		})
	}
	//切换条件选择器
	const CommonFilterData = (filterOptions,type) => {
		const {searchWord,filterOpts,tabsSelected} = filterOptions;
		const {valueIndex, index,filterPickerValue} = filterOpts;
		state.queryStr = searchWord;
		state.courseType = filterPickerValue[0];
		state.studCourseTypeId = filterPickerValue[1];
		state.studCourseTypeClassId = filterPickerValue[2];
		state.learnStatus = tabsSelected;
		if(type == 'filterPicker'){
			state.filterPickerData[index].valueIndex = valueIndex;//学习场景、内容领域、学习专题选中项的索引
			if(index==1){//切换内容领域选中项
				//学习专题默认切换到全部 同时获取学习专题所有选项
				state.filterPickerData[2].valueIndex = 0;
				const childrenData = state.courseTypeData?.[valueIndex]?.children || [];
				const childrenList = childrenData.map(item=>({
					...item,
					value:item?.id ?? ''
				}))
				if(childrenList.length){
					childrenList.unshift({
						label:'学习专题',
						id:'',
						value:''
					})
				}
				state.filterPickerData[2].list = childrenList;
			}else{
				if(index == 0){
					state.filterPickerData[1].valueIndex = 0;//内容领域
					state.filterPickerData[2].valueIndex = 0;//学习专题
					state.filterPickerData[2].list = [];//学习专题选项
					state.tabList = valueIndex == 0 ? [...state.mustTabList] : [...state.selftabList];
					state.learnStatus = valueIndex == 0 ? '' : '3';
				}
			}
		}
		OnLoadData(true)
	}
	/*
	*获取数据
	* initPage:初始分页 从第一开始
	*/
	const OnLoadData = (initPage) => {
		// uni.showLoading({
		// 	title: '加载中'
		// });
		if (initPage) state.pageNum = 1;//如果是下拉刷新、重新查询 默认加载第一页
		state.isLoading = true;
		state.loadStatus = 'loading';
		teacherRecordPageListData({
			queryStr:state.queryStr,
			courseType:state.courseType,
			studCourseTypeId:state.studCourseTypeId,
			studCourseTypeClassId:state.studCourseTypeClassId,
			learnStatus:state.learnStatus,
			pageSize:state.pageSize,
			pageNum:state.pageNum
		}).then(res=>{
			if(res.code == 200){
				const total = Number(res?.data?.dateList?.total) || 0;//总数
				const pages = Math.ceil(total / state.pageSize);//总页数
				const recordsData = res?.data?.dateList?.records || [];
				state.pageList = initPage ? recordsData : [...state.pageList,...recordsData];
				state.noMoreData = state.pageNum == pages;
				state.pageNum++;
			}
		}).finally(()=>{
			// uni.hideLoading();
			state.isLoading = false;
			state.isRefreshing = false;//下拉刷新未被触发
			if(state.noMoreData){
				state.loadStatus = 'no-more';
			}else{
				state.loadStatus = 'more';
			}
			//返回到页面顶部
			if(initPage){
				GoTop();
			}
		})
	}
	// 自定义下拉刷新被触发	 下拉刷新
	const OnRefresh = () =>{
		state.isRefreshing = true;//下拉刷新已经被触发
		OnLoadData(true);
	}
	//滚动到底部/右边 触发上拉刷新
	const OnLoadMore = () => {
		if (state.isLoading || state.noMoreData) return;
		OnLoadData(false);
	}
	//	滚动时触发
	const OnScroll = (e) => {
		state.oldScrollTop = e.detail.scrollTop;
	}
	//返回到顶部
	const GoTop = () => {
		// 解决view层不同步的问题
		state.scrollTop = state.oldScrollTop;
		nextTick(() => {
			state.scrollTop = 0
		});
	}
	//视频详情
	const GoVideoDetails = (id) => {
		uni.navigateTo({
			url: `/pages/studentStudy/videoDetails?id=${id}`
		});
	}
</script>

<style lang="scss" scoped>
	.page_content{
		height: calc(100% - 448rpx);
		.scroll_view_y{
			height: 100%;
			.refresh_loading{
				padding: 10rpx 0;
			}
		}
		.page_list{
			display: flex;
			width: 100%;
			flex-wrap: wrap;
			gap:32rpx;
			.list_item{
				&:nth-child(1){
					margin-top: 16rpx;
				}
				&:nth-child(2){
					margin-top: 16rpx;
				}
				width: calc((100% - 32rpx) / 2);
				overflow: hidden;
				display: flex;
				flex-direction: column;
				.item_img_box{
					width: 100%;
					height: 220rpx;
					border-radius: 8rpx;
					overflow: hidden;
					position: relative;
					.item_img{
						width: 100%;
						height: 100%;
						border-radius: 8rpx;
					}
					.remaining_days{
						position: absolute;
						top:0;
						right: 0;
						border-radius: 0px 8px 0px 8px;
						font-weight: 400;
						font-size: 28rpx;
						color: #FFFFFF;
						text-align: center;
						height: 48rpx;
						line-height: 48rpx;
						&.wait{
							min-width: 134rpx;
							padding:0 16rpx;
							box-sizing: border-box;
							background-color: #2E64FA;
						}
						&.finish{
							width: 116rpx;
							background-color: #90CB75;
						}
						&.overtime{
							width: 116rpx;
							background-color: #F56C6C;
						}
					}
					.progress{
						width: 100%;
						height: 12rpx;
						position: absolute;
						left: 0;
						bottom: 0;
						.done{
							height: 12rpx;
							background-color: #2E64FA;
							display: flex;
							width: 70%;
						}
					}
				}
				
				.item_title{
					display: flex;
					width: 100%;
					margin:8rpx 0;
					font-weight: 400;
					font-size: 24rpx;
					color: #333333;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.item_time{
					display: flex;
					align-items: center;
					width: 100%;
					.img{
						width: 28rpx;
						height: 28rpx;
						margin-right: 8rpx;
					}
					.time{
						font-weight: 400;
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}
		
	}
</style>