<template>
	<view class="infoBox">
		<!-- :style="{top: `${statusBarHeight}px`}" -->
		<view class="headerBox">
			<view :style="{height: statusBarHeight + 'px'}"></view>
			<!-- 头部 -->
			<Header />
			<!-- 搜索框 -->
			<Search />
			<!-- 头部导航 -- tab标签 -->
			<MenuTab :typeList="typeList" @changeTab="handleSelectTab" />
		</view>
		<!-- 内容列表 -->
		<view class="content" :style="{marginTop:(statusBarHeight*2+100+88+40+80)+'rpx'}">
			<!-- 主资讯 -->
			<view class="firstInfo" @click="goDetail" v-if="mainInfo && mainInfo.id">
				<!-- <image src="@/static/2.jpg" alt=""></image> -->
				<!-- <image :src="getUrl(mainInfo.banner)" alt=""></image> -->
				<view class="left">
					<view class="time">
						{{mainInfo.updateTime ? 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>
			<template v-for="(item, index) in infoList">
				<view class="info" :key="index" @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">
								{{item.updateTime ? formatDate(Date.parse(item.updateTime)): ""}}
							</view>
						</view>
					</view>
					<image :src="getUrl(item.banner)" alt="图片走丢啦" />
				</view>
			</template>

			<!-- 上拉Loading -->
			<u-loadmore :status="loadingStatus" marginTop="50px" loadmoreText="没有更多了" line />
			<!-- 上拉Loading -->

		</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,
		getInfoType
	} from '@/api/info.js'
	import {
		baseUrl
	} from "@/utils/apiconfig.js";
	import {
		parseTime,
		formatDate
	} from '@/utils/util.js'


	export default {
		components: {
			Header,
			Search,
			MenuTab,
			Tabbar
		},
		data() {
			return {
				typeList: [], // 分类
				mainInfo: {}, // 主资讯（资讯列表的第一条数据）
				infoList: [], // 资讯列表（除资讯列表第一条数据的）
				total: 0, // 数据总量
				infoParams: {
					pageNum: 1,
					pageSize: 10,
					checkStatus: '5', // 已发布的数据
					categoryId: '', // 分类
				},
				statusBarHeight: getApp().globalData.statusBarHeight,
				loadingStatus: 'loadmore',
				isRequesting: false, // 是否正在请求，如果是则返回，用于节流
			}
		},
		onLoad() {
			this.getType();
			this.getList();
		},
		// 监控上拉默认距离底部50px触发
		onReachBottom() {
			console.log('触发底部啦')
			// 判断一下是否已经请求完所有数据，是的话return 不继续请求了
			if (this.loadingStatus === 'nomore') return
			if (this.isRequesting) return
			this.infoParams.pageNum += 1
			this.getList()
		},
		onPullDownRefresh(){
			console.log('下拉刷新啦')
			this.infoParams.pageNum = 1
			this.getList()
		},

		// 监控滚动(可用于判断是否展示返回顶部)
		// onPageScroll(e) {

		// },
		methods: {
			parseTime,
			formatDate,
			// 获取图片完整地址
			getUrl(url) {
				return baseUrl + url
			},
			// 获取资讯分类
			getType() {
				let params = {
					status: '0',
					pageNum: 1,
					pageSize: 9999,
				}
				getInfoType(params).then(res => {
					console.log(res)
					let obj = {
						id: -1,
						name: '全部'
					}
					this.typeList = [obj, ...res.rows]
				})
			},
			// 获取资讯列表
			getList() {
				this.isRequesting = true
				getInfoList(this.infoParams).then(res => {
					// console.log("res:", res)
					// this.infoList = [...this.infoList, ...res.rows]
					// 如果是第一页的数据，需要将第一条数据单独拿出来
					if (this.infoParams.pageNum === 1) {
						console.log('触发第一页的数据啦')
						this.mainInfo = res.rows[0]
						this.infoList = res.rows.filter((item, index) => index !== 0)
					} else {
						console.log('触发其余页码的数据啦')
						this.infoList = [...this.infoList, ...res.rows]
					}
					// console.log('infoList', this.infoList)
					this.total = res.total - 1
					this.loadingStatus = this.infoList.length >= this.total - 1 ? 'nomore' : 'loading'
				}).finally(() => this.isRequesting = false)
			},
			// 切换tab
			handleSelectTab(val) {
				// 切换tab时页面要回到顶部
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0, // 直接跳转到顶部，不需要动画
				})
				this.infoParams = {
					...this.infoParams,
					pageNum: 1,
					categoryId: val.id === -1 ? '' : val.id,
				}
				
				this.getList()
			},
			// 点击资讯跳转详情
			goDetail(val) {
				console.log('跳转')
				uni.setStorageSync('infoDetail', val)
				uni.navigateTo({
					url: '/pages/index/detail'
				});
			},
		}
	}
</script>

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

	.infoBox {
		height: 100%;
		padding-top: 1px;
		// position: relative;

		// background-color: red;
		.headerBox {
			width: 100%;
			background-color: #f9f9f9;
			position: fixed;
			top: 0;
			z-index: 20;
		}

		.Search {
			margin: 20rpx 0;
			// position: sticky;
			// top: 50;
		}

		.content {
			width: 100%;

			// height: calc(100% - 100rpx - (88rpx + 40rpx) - 80rpx);
			// background-color: yellow;
			.u-loadmore {
				padding-bottom: 200rpx;
			}
		}

		.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;
			color: #000;
			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>