<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="搜索您想要的内容"
					@search="onSearchConfirm"
					@custom="onSearchConfirm"
				></u-search>
			</view>
			<view class="tab-box" style="padding: 40rpx 0 35rpx;">
				<u-tabs 
					name="label"
					:list="searchType"
					:is-scroll="true"
					v-model="searchForm.codeCurrent"
					@change="onSearchTypeChange"
					font-size="32"
					inactive-color="#000000"
					active-color="#ffffff"
					:show-bar="false"
					:bar-style="{'background-color': '#2979ff'}"
					:active-item-style="{'background-color': '#2979ff', 'border-radius':'30rpx'}"
					:gutter="40"
					height="55"
					duration="0"
				></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="onGuideClick(item)">
				<view class="main-box">
					<view class="icon-box">
						<view class="iconfont icon-banshizhiyin"></view>
						<!-- <cover-image class="icon"
							:src="`${FILE_URL}/notice/icon-1.jpg`"></cover-image> -->
					</view>
					<view class="content-box">
						<view class="title">
							{{item.title}}
						</view>
						<!-- <view class="text">
							{{item.desc}}
						</view> -->
					</view>
					<!-- <view class="other-box">
						<view class="date">
							{{item.date}}
						</view>
					</view> -->
				</view>
				<view class="line-box"></view>
			</view>
			<uni-load-more v-show="visualLoadMore" :status="loadMoreStatus"></uni-load-more>
			<!-- <view class="buttom-block" v-show="false"></view> -->
		</view>
	</view>
</template>

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

	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const searchType = ref([
		{
			label: '全部',
			value: null
		}
	])
	const pageNum = ref(1)
	const pageSize = ref(15)
	const visualLoadMore = ref(false)
	const loadMoreStatus = ref('more')
	const searchForm = ref({
		keyword: '',
		code: null,
		codeCurrent: 0,
		pageNumber: 1,
		pageSize: 15
	})
	const list = ref([
	])
	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.code = searchType.value[val].value
		onSearchConfirm()
	}
	
	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(()=>{
		getGuideType().then(res => {
			if (res && res.code === 0) {
				let data = res.data.map(item => {
					return {
						label: item.name,
						value: item.code
					}
				})
				searchType.value.push(...data)
			}
		})
		onSearch()
	})
	
	function onGuideClick(guide) {
		uni.navigateTo({
			url: `/pages/guide/guideDetail/guideDetail?id=${guide.id}&title=${guide.title}`
		})
	}
</script>
<style>
	::v-deep(.u-tab-item){
		margin-right: 20rpx;
	}
</style>
<style lang="scss" scoped>
	.container {
		height: 100vh;
		width: 100vw;
		background-color: #FFFFFF;
		padding: 0 20rpx env(safe-area-inset-bottom, 0);
		.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 {
			padding: 0 20rpx calc(env(safe-area-inset-bottom, 0));
			
			.list-item-box {
				&:first-child{
					.main-box{
						padding: 0 0 30rpx;
					}
				}
				.main-box {
					display: flex;
					// justify-content: space-between;
					align-items: center;
					padding: 30rpx 0;
					
					&:active {
						background-color: $uni-bg-color-hover;
					}

					.icon-box {
						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 {
						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;
				}
			}
		}
	}
	.buttom-block {
		height: 100rpx;
		width: 100vw;
		padding-bottom: calc(5rpx + env(safe-area-inset-bottom, 0));
	}
</style>