<template>
	<view class="content">
		<!-- 头部 -->
		<Header />
		<!-- 搜索框 -->
		<Search />
		<!-- 头部导航 -- tab标签 -->
		<MenuTab />
		<!-- 内容列表 -->
		<scroll-view style="height:calc(100% - 100rpx - (88rpx + 40rpx) - 80rpx)" :scroll-y="true"
			:refresher-enabled="true" :refresher-triggered="triggered" @scrolltolower="scrolltolower"
			@refresherrefresh="onRefresh" refresher-default-style="black">
			<!-- 需要滚动的区域 -->
			<u-list @scrolltolower="scrolltolower">
				<!-- 主资讯 -->
				<u-list-item v-if="mainInfo">
					<view class="firstInfo" @click="goDetail">
						<!-- <image src="@/static/2.jpg" alt=""></image> -->
						<!-- <image :src="getUrl(mainInfo.banner)" alt=""></image> -->
						<view class="left">
							<!-- <view class="time">2024/11/07</view> -->
							<view class="time">{{parseTime(mainInfo.updateTime, '{y}/{m}/{d}')}}</view>
							<view class="title">{{mainInfo.title}}</view>
							<!-- <view class="subTitle">副标题副标题副标题</view> -->
						</view>
						<view>
							<i class="custom-icon custom-icon-collect"></i>
						</view>
					</view>
				</u-list-item>
				<!-- 其余资讯 -->
				<u-list-item v-for="(item, index) in infoList" :key="index">
					<view class="info" @click="goDetail(item)">
						<view class="leftBox">
							<view class="title">
								{{item.title}}
							</view>
							<view class="attentionSituation">
								<view class="collectCount">
									<i class="custom-icon custom-icon-collected"></i>
									2145
								</view>
								<!-- <view class="commentCount">
									<i class="custom-icon custom-icon-comment"></i>
									543
								</view> -->
								<view class="time">
									3分钟前
								</view>
							</view>
						</view>
						<image :src="getUrl(item.banner)" alt="图片走丢啦" />
					</view>
				</u-list-item>
			</u-list>
			<view class="finished" v-if="finished">到底啦~~</view>
		</scroll-view>

		<!-- 底部导航 -->
		<Tabbar :current-page="0" />
	</view>
</template>

<script>
	import Header from "@/components/Header/index.vue";
	import Search from "@/components/Search/index.vue";
	import MenuTab from "@/components/MenuTab/index.vue";
	import Tabbar from "@/components/Tabbar/index.vue";

	import {
		getInfoList
	} from '@/api/info.js'
	import {
		baseUrl
	} from "@/utils/apiconfig.js";
	import {
		parseTime
	} from '@/utils/util.js'


	export default {
		components: {
			Header,
			Search,
			MenuTab,
			Tabbar
		},
		data() {
			return {
				triggered: false, // 下拉是否激活
				tabDetailList: [],
				total: 0,
				pageNum: 1,
				pageSize: 10,
				isRequesting: false, // 是否正在请求，如果是则返回，用于节流
				infoList: [], // 资讯列表（除资讯列表第一条数据的）
				mainInfo: {}, // 主资讯（资讯列表的第一条数据）
				finished: false, // 是否请求完所有的数据
			}
		},
		onLoad() {
			this.getInfoType();
			this.loadmore();
		},
		methods: {
			parseTime,
			// 获取图片完整地址
			getUrl(url) {
				return baseUrl + url
			},
			// 获取资讯分类
			getInfoType() {
				// this.request('/info/category/list', 'GET').then(res => {
				// 	console.log("res:", res)
				// 	if (res) {}
				// })
			},

			// 上拉加载
			scrolltolower() {
				if (this.infoList.length < this.total) {
					this.pageNum += 1;
					// 继续请求下一页，记得保留原先数组
					this.loadmore();
				} else {
					this.finished = true
					uni.showToast({
						title: "没有更多了",
						icon: "none",
						duration: 2000,
					});
					return;
				}
			},
			// 自定义下拉刷新被触发
			onRefresh() {
				// 开启下拉自定义样式
				this.triggered = true;
				//  重新调用
				this.loadmore();
				// 1秒之后复位
				setTimeout(() => {
					this.triggered = false;
				}, 1000);
			},
			loadmore() {
				// console.log('parseTime', parseTime('2025-01-01 10:22:22', '{y}/{m}/{d}'))
				// this.request('/info/infoContent/list', 'GET').then(res => {
				// 	console.log("res:", res)
				// 	if (res) {}
				// })
				let params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
				}
				getInfoList(params).then(res => {
					console.log("res:", res)
					this.infoList = [...this.infoList, ...res.rows]
					console.log("res:", this.infoList)
					// 如果是第一页的数据，需要将第一条数据单独拿出来
					if (this.pageNum === 1) {
						this.mainInfo = res.rows[0]
						this.infoList = res.rows.filter((item, index) => index !== 0)
					} else {
						this.infoList = [...this.infoList, ...res.rows]
					}
					this.total = res.total - 1
				})
			},
			// 点击资讯跳转详情
			goDetail(val) {
				console.log('跳转')
				uni.navigateTo({
					url: '/pages/index/detail'
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f9f9f9;
		font-size: 28rpx;
		height: 100%;
		// background-color: yellow;
	}

	.content {
		height: 100%;
		padding-top: 20rpx;
		// display: flex;
		// flex-direction: column;
		// background-color: red;

		.Search {
			margin: 20rpx 0;
		}

		.u-list {
			display: inline-block;
			// margin-bottom: 200rpx;
			// height: calc(100% - 100rpx - 88rpx - 80rpx - 120rpx) !important;
			// background-color: red;
			height: 100% !important;
			// margin-bottom: 100px;
		}


		.firstInfo {
			margin: 20rpx auto;
			width: 90%;
			height: 320rpx;
			// background-image: url(../../static/2.jpg);
			// background-repeat: no-repeat;
			// background-size: 100% 100%;
			// border-radius: 30rpx;
			// background-color: red;
			display: flex;
			justify-content: space-between;
			padding: 40rpx;
			color: #fff;
			font-size: 32rpx;
			position: relative;
			z-index: 0;

			image {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 320rpx;
				border-radius: 30rpx;
				z-index: -1;
			}

			.time {
				opacity: 0.8;
				font-size: 24rpx;
			}

			.title {
				margin: 10rpx 0;
			}

			.custom-icon-collect {
				font-size: 40rpx;
			}
		}

		.info {
			width: 90%;
			background-color: #fff;
			display: flex;
			padding: 20rpx;
			height: 260rpx;
			margin: 10rpx auto;
			border-radius: 24rpx;
			box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);

			.leftBox {
				width: 66%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}

			.title {
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 3;
				-webkit-box-orient: vertical;
			}

			.attentionSituation {
				font-size: 24rpx;
				display: flex;
				align-items: center;

				.collectCount,
				.commentCount {
					display: flex;
					align-items: center;
				}

				.custom-icon {
					margin-right: 10rpx;

					&::before {
						background: -webkit-linear-gradient(left, #f84a1a, #ffa53e);
						-webkit-background-clip: text;
						-webkit-text-fill-color: transparent;
						font-weight: bold;
					}
				}

				.collectCount {
					margin-right: 30rpx;
				}

				.time {
					color: #aaa;
				}

			}

			image {
				flex: 1;
				width: 100%;
				height: 100%;
				border-radius: 20rpx;
			}

			.rightBox {
				width: 100%;
				height: 100%;
				// background-color: yellow;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

	}
</style>