<template>
	<view class="container">
		<view class="header-box">
			<view class="search-box">
				<u-search
					v-model="searchForm.keyword"
					:clearabled="true"
					bg-color="#FFFFFF"
					:input-style="searchInputStyle"
					placeholder="搜索您想要的内容"
					@search="onSearchConfirm"
					@custom="onSearchConfirm"
				></u-search>
			</view>
			<!-- <view class="tab-box">
				<u-tabs 
					name="label"
					:list="searchType"
					:is-scroll="false"
					v-model="searchForm.typeCurrent"
					@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: '#FFFFFF'
	}
	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,
		typeCurrent: 0,
		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.code === 0) {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
			}
		})
	}
	
	function onSearchTypeChange(val) {
		searchForm.value.type = searchType[val].value
		onSearchConfirm()
	}
	
	function onClickReport(report) {
		if (report.objType === '1') {
			uni.navigateTo({
				url: `/pages/reportDetail/reportDetail?id=${report.objId}&title=${report.title}`
			})
		}
		if (report.objType === '2') {
			uni.navigateTo({
				url: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${report.objId}&title=${report.title}`
			})
		}
		
	}
	onPullDownRefresh((e) => {
		searchForm.value.pageNumber = 1
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				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.pageNumber++
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				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>
	$certificate-width: 220rpx;
	$certificate-height: 300rpx;
	.container {
		height: 100vh;
		width: 100vw;
		background: rgb(141, 204, 255);
		background: -moz-linear-gradient(   90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -webkit-linear-gradient(90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -o-linear-gradient(     90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: -ms-linear-gradient(    90deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		background: linear-gradient(       180deg, rgb(141, 204, 255) 10%, rgb(247, 247, 247) 30%);
		padding: 0 20rpx;
		
		.header-box {
			// background-color: $uni-text-color-inverse;
			padding-top: 40rpx;
			@include topMagnet();
		}
		
		.search-box {
			margin-bottom: 20rpx;
			::v-deep(.u-search) {
				background-color: #FFFFFF;
				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;
				    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
				    background-color: #fff;
				    border-radius: 20rpx;
				    padding: 40rpx 20rpx;
				    margin: 35rpx 0;
				&: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: 28rpx;
						font-weight: bold;
						margin-bottom: 5rpx;
						@include text-overflow()
					}
					.text {
						line-height: 40rpx;
						font-size: 24rpx;
						font-weight: bold;
					}
				}
				.suffix-box {
					// width: 15%;
					flex: 0 0 auto;
					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: 24rpx;
						margin: 0 10rpx;
						letter-spacing: 3rpx;
					}
				}
			}
		}
	}
</style>