<template>
	<view class="page_body">
		<!-- 头部 -->
		<nav-bar height="200" :title="state.honorTypeName" :defaultTabValue="state.honorStatus" :tabList="state.tabList" :onlyTitle="false" :showHeadSearchInput="false" :showFilterPicker="false" :showTabs="true" :showTabBtns="false" rightWidth="0rpx" @CommonFilterData="CommonFilterData" @GoBack="GoBack"></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="count">
						<template v-if="state.honorStatus === '0'">共 {{state.reViewCount}} 人</template>
						<template v-if="state.honorStatus === '1'">共 {{state.overCount}} 人</template>
						<template v-if="state.honorStatus === '2'">共 {{state.overruleCount}} 人</template>
					</view>
					<view class="list_item" v-for="item in state.pageList" :key="item.id" @click="GoTeacherDetail(item.id,item.teacherName)">
						<view class="name_btn">
							<view class="teacher_name">{{item.teacherName}}</view>
							<view class="btn">{{state.honorStatus === '0'?'审核':'查看'}}</view>
						</view>
						<view class="created_at">上传时间：{{item.createdAt}}</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>
	</view>
</template>

<script setup>
	import navBar from '@/components/navBar.vue';
	import {queryHonorReviewDetailList} from '@/reqApi/teacherHonor.js';
	import {
		reactive,
		onUnmounted,
		nextTick
	} from 'vue';
	import { onLoad,onShow } from '@dcloudio/uni-app';
	const state = reactive({
		tabList:[{
            label:'待审核',
            value:'0'
        },{
            label:'已审核',
            value:'1'
        },{
            label:'已驳回',
            value:'2',
        }],
		pageLists:[],//全部数据
		pageList:[],//列表数据
		reViewCount:0,//待审核数量
		overCount:0,//已审核数量
		overruleCount:0,//驳回数量
		honorTypeId:'',//荣誉类型id
		honorTypeName:'',//荣誉类型
		honorStatus:'',//审核状态
		pageSize: 30,
		pageNum: 1,
		pages:0,
		scrollTop:0,
		oldScrollTop:0,
		isRefreshing:false,//设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
		isLoading:true,//加载中
		noMoreData:false,//没有更多数据了
		loadStatus:'more',//loading状态
	})
	onShow(()=>{
		const isLoadHonorAuditList = uni.getStorageSync('isLoadHonorAuditList');
		if(isLoadHonorAuditList){//审核驳回通过返回刷新列表
			OnLoadData(true);
		}
	})
	onLoad((option) => {
		state.honorTypeId = option.honorTypeId;
		state.honorTypeName = decodeURIComponent(option.honorTypeName);
		state.honorStatus = option.honorStatus;
		OnLoadData(true);
	});
	onUnmounted(()=>{
		uni.setStorageSync('isLoadHonorAuditList', false);
	})
	//切换条件选择器
	const CommonFilterData = (filterOptions,type) => {
		const { tabsSelected } = filterOptions;
		state.honorStatus = tabsSelected;
		OnLoadData(true)
	}
	/*
	*获取数据
	* initPage:初始分页 从第一开始
	*/
	const OnLoadData = (initPage) => {
		state.isLoading = true;
		state.loadStatus = 'loading';
		if (initPage) {
			// uni.showLoading({
			// 	title: '加载中'
			// });
			state.pageNum = 1;//如果是下拉刷新、重新查询 默认加载第一页
			queryHonorReviewDetailList({
				honorTypeId:state.honorTypeId,
				honorStatus:state.honorStatus
			}).then(res=>{
				if(res.code == 200){
					const tableData = res?.data?.detailList || [];
					state.reViewCount = res?.data?.reViewCount || 0;//待审核数量
					state.overCount = res?.data?.overCount || 0;//已审核数量
					state.overruleCount = res?.data?.overruleCount || 0;//驳回数量
					state.pageLists = tableData;//全部数据
					const total = tableData?.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 GoBack = () => {
		uni.navigateBack({
			delta: 1
		});
	}
	const GoTeacherDetail = (id,teacherName) => {
		uni.navigateTo({
			url: `/pages/teacherHonor/honorAudit/teacherDetail?id=${id}&pageTitle=${teacherName}&pageFoot=1`
		});
	}
</script>

<style lang="scss" scoped>
	.page_body{
		height: 100%;
		min-height: auto;
		.page_content{
			height: calc(100% - 200rpx);
			.scroll_view_y{
				height: 100%;
				.refresh_loading{
					padding: 10rpx 0;
				}
			}
			.page_list{
				display: flex;
				width: 100%;
				flex-direction: column;
				.count{
					display: flex;
					align-items: center;
					width: 100%;
					height: 88rpx;
					border-top: 2rpx solid #F3F3F3;
					border-bottom: 2rpx solid #F3F3F3;
					font-weight: 400;
					font-size: 28rpx;
					color: #333333;
				}
				.list_item{
					display: flex;
					width: 100%;
					flex-direction: column;
					padding:24rpx 0;
					border-bottom: 2rpx solid #F3F3F3;
					.name_btn{
						display: flex;
						width: 100%;
						align-items: center;
						.teacher_name{
							flex: 1;
							font-weight: 500;
							font-size: 32rpx;
							color: #333333;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
						.btn{
							display: inline-flex;
							align-items: center;
							justify-content: center;
							width: 104rpx;
							height: 64rpx;
							border-radius: 8rpx;
							border: 2rpx solid #2E64FA;
							font-weight: 400;
							font-size: 28rpx;
							color: #2E64FA;
						}
					}
					.created_at{
						display: flex;
						width: 100%;
						margin-top: 16rpx;
						font-weight: 400;
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}
	}
</style>