<template>
	<!-- 头部 -->
	<nav-bar height="224" backPath="workspace" title="教师专业发展" :tabList="state.tabList" :onlyTitle="false" :showFilterPicker="false" :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.teacherId" @click="GoHonorTeacherDetail(item.teacherId,item.teacherName)">
					<view class="teacher_name">
						<image class="icon" src="@/static/image/icon/person.png"></image>
						<text class="name">{{item.teacherName}}</text>
					</view>
					<view class="honor_count">共<text class="count">{{item.honorCount}}</text>个奖项</view>
					<view class="honor_desc">{{item.honorName}}</view>
					<view class="honor_date">更新时间：{{item?.createdAt?.slice(0, 10)}}</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 {queryHonorTeacherListData} from '@/reqApi/teacherHonor.js';
	import {
		reactive,
		ref,
		onMounted,
		nextTick
	} from 'vue';
	const state = reactive({
		tabList:[ {
			label:'更新时间',
			value:'0',
		},{
			label:'获奖数量',
			value:'1',
		},{
			label:'姓名',
			value:'2',
		}],
		pageLists:[],//全部数据
		pageList:[],//列表数据
		sortType:'0',
		queryStr: '',
		pageSize: 30,
		pageNum: 1,
		pages:0,
		scrollTop:0,
		oldScrollTop:0,
		isRefreshing:false,//设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
		isLoading:true,//加载中
		noMoreData:false,//没有更多数据了
		loadStatus:'more',//loading状态
	})
	onMounted(() => {
		OnLoadData(true);
	});
	//切换条件选择器
	const CommonFilterData = (filterOptions,type) => {
		state.queryStr = filterOptions.searchWord;
		state.sortType = filterOptions.tabsSelected;
		OnLoadData(true)
	}
	/*
	*获取数据
	* initPage:初始分页 从第一开始
	*/
	const OnLoadData = (initPage) => {
		state.isLoading = true;
		state.loadStatus = 'loading';
		if (initPage) {
			// uni.showLoading({
			// 	title: '加载中'
			// });
			state.pageNum = 1;//如果是下拉刷新、重新查询 默认加载第一页
			queryHonorTeacherListData({
				queryStr:state.queryStr,
				sortType:state.sortType
			}).then(res=>{
				if(res.code == 200){
					const recordsData = res?.data || [];
					state.pageLists = recordsData;//全部数据
					const total = recordsData?.length || 0;//总数
					state.pages = Math.ceil(total / state.pageSize);//总页数
					//默认加载
					state.pageList = state.pageLists.slice(0, state.pageSize);
					//没有更多了
					state.noMoreData = state.pageNum == state.pages;
					//分页+1
					state.pageNum++;
					state.isLoading = false;
					state.isRefreshing = false;//下拉刷新未被触发
					state.loadStatus = state.noMoreData?'no-more':'more';
				}
			}).finally(()=>{
				// uni.hideLoading();
				//返回到页面顶部
				if(initPage){
					GoTop();
				}
			})
		}else{
			const start = (state.pageNum - 1) * state.pageSize;
			const end = start + state.pageSize;
			const list = state.pageLists.slice(start, end);
			state.pageList = [...state.pageList, ...list];
			state.noMoreData = state.pageNum == state.pages;
			state.pageNum++;
			setTimeout(() => {
				state.isLoading = false;
				state.isRefreshing = false; //下拉刷新未被触发
				state.loadStatus = state.noMoreData?'no-more':'more';
			}, 0)
		}
	}
	// 自定义下拉刷新被触发	 下拉刷新
	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 GoHonorTeacherDetail = (teacherId,teacherName) => {
		uni.navigateTo({
			url: `/pages/teacherHonor/honorTeacher/honorTeacherDetail?teacherId=${teacherId}&teacherName=${teacherName}`
		});
	}
</script>

<style lang="scss" scoped>
	.page_content{
		height: calc(100% - 360rpx);
		.scroll_view_y{
			height: 100%;
			.refresh_loading{
				padding: 10rpx 0;
			}
		}
		.page_list{
			display: flex;
			width: 100%;
			flex-wrap: wrap;
			gap:24rpx 22rpx;
			.list_item{
				width: calc((100% - 22rpx) / 2);
				// min-height: 280rpx;
				padding: 16rpx 12rpx 18rpx 16rpx;
				background-color: #F8FAFE;
				border-radius: 16rpx;
				border: 2rpx solid #EBEEF5;
				box-sizing: border-box;
				overflow: hidden;
				display: flex;
				flex-direction: column;
				.teacher_name{
					display: flex;
					width: 100%;
					align-items: center;
					.icon{
						width: 40rpx;
						height: 40rpx;
						margin-right: 16rpx;
						flex-shrink: 0;
					}
					.name{
						font-weight: 500;
						font-size: 36rpx;
						color: #333333;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}
				.honor_count{
					display: flex;
					flex-wrap: wrap;
					align-items: center;
					width: 100%;
					margin-top: 8rpx;
					font-weight: 500;
					font-size: 28rpx;
					color: #666666;
					.count{
						color: #2E64FA;
						margin:0 8rpx;
					}
				}
				.honor_desc{
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					text-overflow: ellipsis;
					flex-wrap: wrap;
					word-break: break-all;
					width: 100%;
					margin-top: 16rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #999999;
				}
				.honor_date{
					width: 100%;
					display: flex;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					margin-top: 18rpx;
				}
			}
		}
		
	}
</style>