<template>
	<view class="zhinan-list-page">
		<!-- 头部导航栏 -->
		<uni-nav-bar
			left-icon="left" 
			title="诊疗指南" 
			@clickLeft="goBack" 
			fixed 
			color="#8B2316" 
			height="140rpx" 
			:border="false" 
			backgroundColor="#eeeeee"
		></uni-nav-bar>
		
		<!-- 指南列表 -->
		<view class="guidelines-list">
			<view 
				class="guideline-item" 
				v-for="(item, index) in guidelinesList" 
				:key="item.uuid || index"
			>
				<!-- 指南信息 -->
				<view class="item-content">
					<view class="item-title">{{ item.title }}</view>
					<view class="item-bottom">
						<view class="item-date">{{ formatDate(item.releaseTime) }}</view>
						<!-- 操作按钮 -->
						<view class="item-action">
							<view 
								v-if="item.can_download" 
								class="download-btn"
								@click="downloadGuideline(item)"
							>
								<up-icon name="download" color="#8D2316" size="28"></up-icon>
							</view>
							<view 
								v-else 
								class="view-btn"
								@click="viewGuideline(item)"
							>
								查看
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 加载状态 -->
		<view class="loading-status">
			<view v-if="isLoading && currentPage === 1" class="loading">
				<uni-load-more status="loading" :content-text="loadingText"></uni-load-more>
			</view>
			<view v-else-if="hasMoreData && !isLoading" class="load-more" @click="loadMoreData">
				<uni-load-more status="more" :content-text="loadingText"></uni-load-more>
			</view>
			<view v-else-if="!hasMoreData && guidelinesList.length > 0" class="no-more">
				<uni-load-more status="noMore" :content-text="loadingText"></uni-load-more>
			</view>
		</view>
		
		<!-- 无数据提示 -->
		<view class="no-data" v-if="guidelinesList.length === 0 && !isLoading">
			<uni-icons type="info" size="60" color="#999"></uni-icons>
			<text>暂无诊疗指南数据</text>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted} from 'vue'
import api from '@/api/api.js'
import { onShow, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";

// 响应式数据
const guidelinesList = ref([])
const isLoading = ref(false)
const hasMoreData = ref(true)
const currentPage = ref(1)
const pageSize = ref(20)
const isRefreshing = ref(false)

// 加载文本配置
const loadingText = {
	contentdown: '上拉显示更多',
	contentrefresh: '正在加载...',
	contentnomore: '没有更多数据了'
}

// 页面加载
onMounted(() => {
	loadGuidelinesList()
})

onShow(() => {
	// 页面显示时可以刷新数据
	loadGuidelinesList()
})

// 下拉刷新
onPullDownRefresh(() => {
	refreshData()
})

// 上拉加载更多
onReachBottom(() => {
	if (hasMoreData.value && !isLoading.value) {
		loadMoreData()
	}
})

// 刷新数据
const refreshData = async () => {
	if (isRefreshing.value) return
	
	try {
		isRefreshing.value = true
		currentPage.value = 1
		hasMoreData.value = true
		
		await loadGuidelinesList()
		
		uni.showToast({
			title: '刷新成功',
			icon: 'success'
		})
	} catch (error) {
		console.error('刷新失败:', error)
		uni.showToast({
			title: '刷新失败',
			icon: 'none'
		})
	} finally {
		isRefreshing.value = false
		uni.stopPullDownRefresh()
	}
}

// 加载指南列表
const loadGuidelinesList = async (isLoadMore = false) => {
	if (isLoading.value) return
	
	try {
		isLoading.value = true
		
		const params = {
			page: currentPage.value,
			pageSize: pageSize.value,
			type: 1, // 诊疗指南类型
			keywords: ''
		}
		
		const res = await api.searchLibraryU(params)
		console.log('指南列表响应:', res)
		
		if (res.code === 200 && res.data) {
			const newData = res.data
			
			if (isLoadMore) {
				guidelinesList.value = [...guidelinesList.value, ...newData]
			} else {
				guidelinesList.value = newData
			}
			
			// 判断是否还有更多数据
			hasMoreData.value = newData.length === pageSize.value
		} else {
			console.error('加载指南列表失败:', res.message)
			uni.showToast({
				title: res.message || '加载失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('加载指南列表异常:', error)
		uni.showToast({
			title: '网络异常，请重试',
			icon: 'none'
		})
	} finally {
		isLoading.value = false
	}
}

// 加载更多数据
const loadMoreData = () => {
	if (hasMoreData.value && !isLoading.value) {
		currentPage.value++
		loadGuidelinesList(true)
	}
}

// 下载指南
const downloadGuideline = (item) => {
	console.log('下载指南:', item)
	uni.showToast({
		title: '开始下载...',
		icon: 'none'
	})
	
	// 这里可以调用下载API
	// 示例：下载PDF文件
	if (item.file_path) {
		uni.downloadFile({
			url: item.file_path,
			success: (res) => {
				if (res.statusCode === 200) {
					uni.showToast({
						title: '下载成功',
						icon: 'success'
					})
				}
			},
			fail: (err) => {
				console.error('下载失败:', err)
				uni.showToast({
					title: '下载失败',
					icon: 'none'
				})
			}
		})
	}
}

// 查看指南
const viewGuideline = (item) => {
	console.log('查看指南:', item)
	// 跳转到指南详情页或预览页
	uni.navigateTo({
		url: `/pages_app/zhinanDetail/zhinanDetail?uuid=${item.uuid}&title=${encodeURIComponent(item.title)}`
	})
}

// 返回上一页
const goBack = () => {
	uni.navigateBack()
}

// 格式化日期
const formatDate = (dateString) => {
	if (!dateString) return ''
	
	try {
		const date = new Date(dateString)
		const year = date.getFullYear()
		const month = String(date.getMonth() + 1).padStart(2, '0')
		const day = String(date.getDate()).padStart(2, '0')
		return `${year}-${month}-${day}`
	} catch (error) {
		return dateString
	}
}
</script>

<style lang="scss" scoped>
// 颜色变量
$primary-color: #ff4757;
$text-primary: #333;
$text-secondary: #666;
$text-light: #999;
$border-color: #e0e0e0;
$bg-color: #f6f6f6;
$white: #ffffff;

.zhinan-list-page {
	background-color: $bg-color;
	min-height: 100vh;
}

// 指南列表
.guidelines-list {
	padding: 20rpx 30rpx;
	
	.guideline-item {
		background-color: $white;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
		
		.item-content {
			flex: 1;
			margin-right: 30rpx;
			
			.item-title {
				font-size: 32rpx;
				color: $text-primary;
				line-height: 1.5;
				margin-bottom: 16rpx;
				font-weight: 500;
			}
			
			.item-bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 10rpx;
				
				.item-date {
					font-size: 26rpx;
					color: $text-secondary;
				}
				
				.item-action {
					display: flex;
					align-items: center;
					
					.download-btn {
						width: 60rpx;
						height: 60rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 50%;
						background-color: rgba(255, 71, 87, 0.1);
						transition: all 0.3s ease;
						
						&:active {
							transform: scale(0.95);
							background-color: rgba(255, 71, 87, 0.2);
						}
					}
					
					.view-btn {
						color:#8B2316;
						font-size: 28rpx;
						font-weight: 500;
						transition: all 0.3s ease;
						
						&:active {
							transform: scale(0.95);
							background-color: darken($primary-color, 10%);
						}
					}
				}
			}
		}
	}
}

// 加载状态
.loading-status {
	padding: 20rpx 0;
	text-align: center;
	
	.loading, .load-more, .no-more {
		padding: 20rpx 0;
	}
}



// 无数据提示
.no-data {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx 0;
	
	text {
		margin-top: 30rpx;
		font-size: 28rpx;
		color: $text-light;
	}
}
</style>
