<template>
	<view class="container">
		<u-sticky>
			<view class="search-box">
				<u-search
					v-model="searchForm.keyword"
					:clearabled="true"
					bg-color="#E5E5E5"
					:input-style="searchInputStyle"
					placeholder="请输入搜索内容"
					@search="onSearchConfirm"
					@custom="onSearchConfirm"
				></u-search>
			</view>
		</u-sticky>
		<view class="list-box">
			<view class="list-item-box" v-for="item in list" :key="item.id" @click="onNoticeClick(item)">
				<view class="main-box">
					<view class="icon-box">
						<view class="iconfont icon-a-tongzhigonggao"></view>
						<!-- <cover-image class="icon" :src="`${FILE_URL}/notice/icon-2.jpg`"></cover-image> -->
					</view>
					<view style="width: 100%;display: flex;
					justify-content: space-between;
					align-items: center;gap: 10rpx;">
						<view class="content-box">
							<view class="title">
								{{item.title}}
							</view>
						</view>
						<view class="other-box">
							<view class="date">
								{{item.date}}
							</view>
						</view>
					</view>
				</view>
				<view class="line-box"></view>
			</view>
		</view>
		<u-loadmore :status="status" :load-text="loadText" style="margin: 20rpx 0;"/>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad, onReachBottom } from '@dcloudio/uni-app'
	import { getNoticeList } from '@/api/notice.js'
	import configService from '@/utils/baseurl.js'
	const FILE_URL = configService.FILE_URL;
	const uToast = ref()

	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const searchForm = ref({
		keyword: '',
		pageNumber: 1,
		pageSize: 20
	})
	const total = ref(0);
	const status = ref('loadmore');
	const loadText = ref({
		loadmore: '点击或上拉加载更多',
		loading: '努力加载中',
		nomore: '已加载全部数据'
	})
	const list = ref([])
	function init() {
		getNoticeList().then(res=>{
			if(res?.data){
				list.value = res.data
			}
		})
	}
	function onNoticeClick(notice) {
		uni.navigateTo({
			url: `/pages/notice/noticeDetail/noticeDetail?id=${notice.id}&title=${notice.title}`
		})
	}
	function onSearchConfirm() {
		// console.log(searchForm.value.keyword, 1211)
		searchForm.value.pageNumber = 1
		search()
	}
	function search() {
		status.value = 'loading'
		list.value = searchForm.value.pageNumber === 1 ? [] : list.value
		getNoticeList(searchForm.value).then(res => {
			total.value = res.count
			list.value = [...list.value, ...res.data]
			searchForm.value.pageNumber = searchForm.value.pageNumber + 1
			if(total.value === list.value.length){
				status.value = 'nomore' 
			}else{
				status.value = 'loadmore' 
			}
		}).catch(err=>{
			status.value = 'nomore'
		})
	}
	onLoad(() => {
		searchForm.value.pageNumber = 1
		search()
	})
	onReachBottom(()=>{
		if(status.value === 'nomore'){
			return
		}
		search()
	})
</script>

<style lang="scss" scoped>
	.container {
		width: 100vw;
		background-color: #FFFFFF;
		padding: 0 20rpx env(safe-area-inset-bottom, 0);
	
		.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 {
			padding: 0 20rpx;
	
			.list-item-box {
				.main-box {
					display: flex;
					// justify-content: space-between;
					align-items: center;
					padding: 30rpx 0;
	
					&:active {
						background-color: $uni-bg-color-hover;
					}
	
					.icon-box {
						flex: 0 0 auto;
						width: 15%;
						display: flex;
						justify-content: center;
						align-items: center;
	
						.icon {
							width: 70rpx;
							height: 70rpx;
						}
	
						.iconfont {
							font-size: 60rpx;
							color: #0069f6;
						}
					}
	
					.content-box {
						// width: 67%;
						display: flex;
						flex-direction: column;
						justify-content: space-around;
						gap: 10rpx;
	
						.title {
							height: calc(70% - 5rpx);
							font-size: $uni-title-font-size-2;
							font-weight: bold;
						}
	
						.text {
							height: calc(30% - 5rpx);
							font-size: $uni-font-size-3;
							color: $uni-text-color-grey;
							@include text-overflow();
						}
					}
	
					.other-box {
						flex: 0 0 auto;
						width: 18%;
						display: flex;
						flex-direction: column;
						justify-content: space-around;
						align-items: center;
						gap: 10rpx;
	
						.date {
							font-size: $uni-font-size-3;
							color: $uni-text-color-grey;
						}
	
						.tag {
							font-size: $uni-font-size-3;
							width: 42rpx;
							background-color: $uni-color-error;
							color: $uni-text-color-inverse;
							border-radius: 20rpx;
							text-align: center;
						}
					}
				}
	
				.line-box {
					height: 1rpx;
					width: 80%;
					background-color: #E5E5E5;
					margin: 0 auto;
				}
			}
		}
	}


</style>
