<template>
	<view class="container">
		<view class="header-box">
			<view class="search-box">
				<u-search
					v-model="searchForm.keyword"
					:clearabled="true"
					bg-color="#E5E5E5"
					:input-style="searchInputStyle"
					placeholder="搜索您想要的内容"
				></u-search>
			</view>
			<view class="tab-box">
				<u-tabs 
					name="label"
					:list="searchType"
					:is-scroll="false"
					v-model="searchForm.type"
					@change="onSearchTypeChange"
					font-size="24"
					:bold="false"
					inactive-color="#000000"
					active-color="#000000"
					:bar-style="{'background-color': '#2979ff'}"
					height="50"
				></u-tabs>
			</view>
		</view>
		<u-empty
			mode="data"
			v-if="list.length === 0"
			iconSize="120"
			textSize="58"
			text="暂无数据"
		>
		</u-empty>
		<view class="list-box">
			<view class="list-item-box" v-for="item in list" :key="item.id" @click="onClickReport(item)">
				<view class="icon-box">
					<view class="icon" v-if="item.isNew"></view>
				</view>
				<view class="info-box">
					<view class="title">
						{{item.title}}
					</view>
					<view class="text">
						【{{item.typeName}}】
					</view>
				</view>
				<view class="suffix-box">
					<view class="button">
						查看
					</view>
				</view>
			</view>
		</view>
		<uni-load-more v-show="visualLoadMore" :status="loadMoreStatus"></uni-load-more>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad, onReachBottom, onPullDownRefresh  } from '@dcloudio/uni-app'
	import { query } from '@/api/collect.js'
	
	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const searchType = [
		{
			label: '全部',
			value: null
		},
		{
			label: '研究报告',
			value: 1
		},
		{
			label: '培训课程',
			value: 2
		},
	]
	const pageNum = ref(1)
	const pageSize = ref(10)
	const visualLoadMore = ref(false)
	const loadMoreStatus = ref('more')
	const searchForm = ref({
		keyword: '',
		type: null,
		pageNumber: 1,
		pageSize: 10
	})
	
	const list = ref([
		{
			id: '01',
			title: '2024年11月关注是中介促成二手住宅市场交易简报',
			type: 1,
			typeName: '月度成交简报',
			isNew: true
		},
	])
	
	function onSearchConfirm() {
		searchForm.value.pageNumber = 1
		pageNum.value = 1
		onSearch()
	}
	
	function onSearchClear() {
		searchForm.value.keyword = null
		searchForm.value.pageNumber = 1
		pageNum.value = 1
		onSearch()
	}
	
	function onSearch() {
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.message === 'success') {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
			}
		})
	}
	
	function onSearchTypeChange(val) {
		searchForm.value.type = searchType[val].value
		onSearchConfirm()
	}
	
	function onClickReport(report) {
		uni.navigateTo({
			url: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${report.objId}&name=${report.title}`
		})
	}
	onPullDownRefresh((e) => {
		searchForm.value.pageNumber = 1
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.message === 'success') {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
				uni.showToast({
					title: '刷新成功',
					icon: 'success',
					duration: 2000
				})
			}
			uni.stopPullDownRefresh()
		})
	})
	
	onReachBottom(async () => {
		if (loadMoreStatus.value === 'noMore') {
			uni.showToast({
				title: '没有更多啦＞﹏＜',
				icon: 'none'
			})
			return
		}
		visualLoadMore.value = true
		loadMoreStatus.value = 'loading'
		searchForm.value.pageNum++
		query(searchForm.value).then(res => {
			if (res && res.message === 'success') {
				if (res.data) {
					if (res.count >= list.value.length) {
						if (list.value.length === res.count) {
							loadMoreStatus.value = 'noMore'
							visualLoadMore.value = true
						} else {
							list.value.push(...res.data)
							loadMoreStatus.value = 'more'
							visualLoadMore.value = false
						}							
					} else {
						loadMoreStatus.value = 'noMore'
						visualLoadMore.value = true
					}
				} else {
					loadMoreStatus.value = 'noMore'
					visualLoadMore.value = true
				}
			}
		})
	})
	
	onLoad(() => {
		onSearch()
	})
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		width: 100vw;
		background-color: $uni-text-color-inverse;
		padding: 0 20rpx;
		
		.header-box {
			background-color: $uni-text-color-inverse;
			@include topMagnet();
		}
		
		.search-box {
			margin-bottom: 20rpx;
			::v-deep(.u-search) {
				background-color: #e5e5e5;
				border-radius: 50rpx;
				
				.u-action {
					width: 18%;
					background-color: $uni-color-primary;
					border-radius: 50rpx;
					color: $uni-text-color-inverse;
					margin-right: 8rpx;
					font-size: 28rpx;
					line-height: 50rpx;
					letter-spacing: 3rpx;
					text-align: center;
				}
			}
		}
		
		.list-box {
			.list-item-box {
				padding: 20rpx 10rpx;
				border-bottom: 5rpx solid #E6E6E6;
				display: flex;
				&:active {
					background-color: $uni-bg-color-hover;
				}
				.icon-box {
					width: 3%;
					padding-top: 10rpx;
					.icon {
						width: 10rpx;
						height: 10rpx;
						background-color: $uni-color-primary;
						border-radius: 50%;
					}
				}
				.info-box {
					width: 82%;
					.title {
						font-size: $uni-title-font-size-2;
						font-weight: bold;
						margin-bottom: 5rpx;
						@include text-overflow()
					}
					.text {
						line-height: 40rpx;
						font-size: $uni-font-size-2;
						font-weight: bold;
					}
				}
				.suffix-box {
					width: 15%;
					display: flex;
					align-items: center;
					justify-content: center;
					
					.button {
						color: $uni-text-color-inverse;
						padding: 5rpx 18rpx;
						border-radius: $uni-card-border-radius;
						background-color: $uni-color-primary;
						font-size: $uni-font-size-3;
						letter-spacing: 3rpx;
					}
				}
			}
		}
	}
</style>