<template>
	<view class="news-page">
		<!-- 顶部导航栏 -->
		<uni-nav-bar
			left-icon="left"
			title="肝胆新闻"
			@clickLeft="goBack"
			fixed
			color="#8B2316"
			height="180rpx"
			:border="false"
			backgroundColor="#eeeeee"
		>
		</uni-nav-bar>

		<!-- 新闻列表 -->
		<scroll-view 
			scroll-y 
			class="news-list"
			refresher-enabled
			@refresherrefresh="onRefresh"
			:refresher-triggered="refreshing"
			@scrolltolower="onLoadMore"
			:lower-threshold="100"
		>
			<view class="news-item" v-for="(item, index) in newsList" :key="item.id" @click="onItemClick(item)">
				<view class="thumbnail">
					<image :src="item.thumbnail" width="200rpx" height="120rpx" mode="aspectFill" class="thumb-img"></image>
					<!-- 重要通知标识 -->
					<view v-if="item.isImportant" class="important-badge">
						<text class="important-text">重要通知</text>
						<text class="click-text">点击查看</text>
						<view class="red-dot"></view>
					</view>
				</view>
				<view class="content">
					<view class="title">{{ item.title }}</view>
					<view class="subtitle">{{ item.subtitle }}</view>
				</view>
			</view>
			
			<!-- 加载更多提示 -->
			<view v-if="loading" class="loading-more">
				<text>加载中...</text>
			</view>
			
			<!-- 空数据状态 -->
			<view v-if="!loading && newsList.length === 0" class="empty-state">
				<text>暂无新闻</text>
			</view>
			
			<!-- 没有更多数据提示 -->
			<view v-if="noMore && newsList.length > 0" class="no-more">
				<text>没有更多数据了</text>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import api from '@/api/api.js';

const refreshing = ref(false);
const loading = ref(false);
const noMore = ref(false);
const page = ref(1);
const pageSize = ref(10);

// 新闻数据列表
const newsList = ref([]);

const goBack = () => {
	uni.navigateBack();
};

const onItemClick = (item) => {
	// 跳转到新闻详情页面
	uni.navigateTo({
		url: `/pages_app/news?id=${item.id}&title=${encodeURIComponent(item.title)}&subtitle=${encodeURIComponent(item.subtitle)}&path=${encodeURIComponent(item.path)}&userUuid=${encodeURIComponent(item.userUuid)}`
	});
};

// 获取新闻列表
const getNewsList = () => {
	api.getCollectionList({
		page: page.value,
		type: 1,
		title: ""
	}).then(res => {
		console.log(res);
		if (res.code === 200 && res.data) {
			const { list, totalRow, totalPage, pageNumber } = res.data;
			
			// 如果是第一页，清空列表
			if (page.value === 1) {
				newsList.value = [];
			}
			
			// 处理新闻数据
			const processedList = list.map(item => ({
				id: item.other_uuid,
				title: item.title || '未知标题',
				subtitle: item.public_name || '未知来源',
				thumbnail: item.imgpath || '/static/news1.png',
				userUuid: item.user_uuid,
				path: item.path,
				type: item.type,
				agreenum: item.agreenum,
				readnum: item.readnum,
				videoType: item.video_type,
				// 判断是否为重要通知（根据标题关键词）
				isImportant: item.title && (item.title.includes('卫健委') || item.title.includes('重要') || item.title.includes('通知'))
			}));
			
			// 添加到列表
			newsList.value.push(...processedList);
			
			console.log(newsList.value);
			// 更新分页状态
			noMore.value = pageNumber >= totalPage;
			
			// 如果是第一页且有数据，显示总数
			if (page.value === 1 && totalRow > 0) {
				uni.showToast({ 
					title: `共找到 ${totalRow} 条记录`, 
					icon: 'none',
					duration: 2000
				});
			}
		} else {
			uni.showToast({ 
				title: res.message || '获取数据失败', 
				icon: 'none' 
			});
		}
	}).catch(err => {
		console.error('获取新闻列表失败:', err);
		uni.showToast({ 
			title: '网络错误，请重试', 
			icon: 'none' 
		});
	}).finally(() => {
		loading.value = false;
		refreshing.value = false;
	});
};

const onRefresh = () => {
	refreshing.value = true;
	page.value = 1;
	noMore.value = false;
	
	// 调用API刷新数据
	getNewsList();
};

const onLoadMore = () => {
	if (loading.value || noMore.value) return;
	
	loading.value = true;
	page.value++;
	
	// 调用API加载更多数据
	getNewsList();
};

onMounted(() => {
	getNewsList();
});
</script>

<style lang="scss" scoped>
.news-page {
	min-height: 100vh;
	background: #ffffff;
}

.news-list {
	height: calc(100vh - 180rpx);
	position: fixed;
	top: 180rpx;
	bottom: 0;
	width: 100%;
}

.news-item {
	display: flex;
	align-items: center;
	padding: 30rpx;
	background: #ffffff;
	border-bottom: 1rpx solid #f0f0f0;
	
	.thumbnail {
		position: relative;
		margin-right: 20rpx;
		border-radius: 8rpx;
		overflow: hidden;
		
		.thumb-img {
			width: 200rpx;
			height: 120rpx;
			border-radius: 8rpx;
		}
		
		.important-badge {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			background: linear-gradient(135deg, #ff4757, #ff3742);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			border-radius: 8rpx;
			
			.important-text {
				color: #ffffff;
				font-size: 24rpx;
				font-weight: bold;
				margin-bottom: 8rpx;
			}
			
			.click-text {
				color: #ffffff;
				font-size: 20rpx;
				margin-bottom: 8rpx;
			}
			
			.red-dot {
				width: 8rpx;
				height: 8rpx;
				background: #ffffff;
				border-radius: 50%;
			}
		}
	}
	
	.content {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		
		.title {
			font-size: 28rpx;
			color: #333333;
			line-height: 1.4;
			margin-bottom: 8rpx;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}
		
		.subtitle {
			font-size: 24rpx;
			color: #666666;
		}
	}
}

.loading-more, .no-more, .empty-state {
	text-align: center;
	padding: 30rpx;
	color: #999999;
	font-size: 26rpx;
}

.empty-state {
	padding: 100rpx 30rpx;
	color: #999999;
}
</style>
