<template>
	<view class="video-history-page">
		<!-- 顶部导航栏 -->
		<uni-nav-bar
			left-icon="left"
			title="收藏视频"
			@clickLeft="goBack"
			fixed
			color="#8B2316"
			height="140rpx"
			:border="false"
			backgroundColor="#eeeeee"
		>
		</uni-nav-bar>

		<!-- 观看历史列表 -->
		<scroll-view 
			scroll-y 
			class="history-list"
			refresher-enabled
			@refresherrefresh="onRefresh"
			:refresher-triggered="refreshing"
			@scrolltolower="onLoadMore"
			:lower-threshold="100"
		>
			<view class="history-item" v-for="(item, index) in historyList" :key="item.id" @click="onItemClick(item)">
				<view class="thumbnail">
					<image :src="item.thumbnail" width="200rpx" height="120rpx" mode="aspectFill" class="thumb-img"></image>
				</view>
				<view class="content">
					<view class="title">{{ item.title }}</view>
					<view class="author">{{ item.author }}</view>
				</view>
			</view>
			
			<!-- 加载更多提示 -->
			<view v-if="loading" class="loading-more">
				<text>加载中...</text>
			</view>
			
			<!-- 空数据状态 -->
			<view v-if="!loading && historyList.length === 0" class="empty-state">
				<text>暂无收藏视频</text>
			</view>
			
			<!-- 没有更多数据提示 -->
			<view v-if="noMore && historyList.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';
import docUrl from "@/utils/docUrl.js";
const refreshing = ref(false);
const loading = ref(false);
const noMore = ref(false);
const page = ref(1);
const pageSize = ref(10);
const historyList = ref([]);

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



const onItemClick = (item) => {
	// 跳转到视频详情页面
	uni.navigateTo({
		url: `/pages_app/videoDetail/videoDetail?id=${item.id}&title=${encodeURIComponent(item.title)}&author=${encodeURIComponent(item.author)}&videoPath=${encodeURIComponent(item.videoPath)}&userUuid=${encodeURIComponent(item.userUuid)}`
	});
};



onMounted(() => {
	getVideoHistoryList();
});

const getVideoHistoryList = () => {
	api.getCollectionList({
		page: page.value,
		type: 2,
		title: ""
	}).then(res => {
		console.log(res);
		if (res.code === 200 && res.data) {
			const { list, totalRow, totalPage, pageNumber } = res.data;
			
			// 如果是第一页，清空列表
			if (page.value === 1) {
				historyList.value = [];
			}
			
			// 处理视频数据
			const processedList = list.map(item => ({
				id: item.other_uuid,
				thumbnail: item.imgpath || '/static/bo_bg.png',
				title: item.title || '未知标题',
				author: item.public_name || '未知作者',
				videoPath: item.path,
				userUuid: item.user_uuid,
				videoType: item.video_type,
				agreenum: item.agreenum,
				readnum: item.readnum,
				type: item.type
			}));
			
			// 添加到列表
			historyList.value.push(...processedList);
			
			console.log(historyList.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刷新数据
	getVideoHistoryList();
};

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

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

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

.edit-btn {
	color: #8B2316;
	font-size: 28rpx;
	padding: 10rpx;
}

.history-item {
	display: flex;
	align-items: center;
	padding: 30rpx;
	background: #ffffff;
	margin-bottom: 2rpx;
	
	.thumbnail {
		margin-right: 20rpx;
		border-radius: 8rpx;
		overflow: hidden;
		
		.thumb-img {
			width: 200rpx;
			height: 120rpx;
			border-radius: 8rpx;
		}
	}
	
	.content {
		flex: 1;
		display: flex;
		height: 120rpx;
		flex-direction: column;
		justify-content: space-between;
		margin-right: 20rpx;
		
		.title {
			font-size: 28rpx;
			color: #333333;
			line-height: 1.4;
			margin-bottom: 10rpx;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}
		
		.author {
			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>
