<template>
	<scroll-view 
		class="container" 
		scroll-y 
		show-scrollbar="false"
		@scrolltolower="loadMore"
	>
		<view class="header">
			<view class="welcome">
				<text class="welcome-title">学识分记录</text>
				<text class="welcome-subtitle">查看你的学识分获取历史</text>
			</view>
		</view>
		
		<view class="record-section">
			<view v-if="isNotLoggedIn" class="empty-record">
				<text class="empty-icon">🔐</text>
				<text class="empty-text">需要登录</text>
				<text class="empty-hint">登录后才会记录学识分获取记录</text>
			</view>
			<view v-else-if="recordList.length > 0" class="record-list">
				<view v-for="(record, index) in recordList" :key="index" class="record-item">
					<view class="record-header">
						<view class="record-source" :class="getSourceClass(record.source)">
							<text class="source-text">{{ getSourceText(record.source) }}</text>
						</view>
						<view class="record-point" :class="record.point > 0 ? 'point-positive' : 'point-negative'">
							<text class="point-text">{{ record.point > 0 ? '+' : '' }}{{ record.point }}</text>
						</view>
					</view>
					<view class="record-footer">
						<text class="record-time">{{ formatTime(record.create_time) }}</text>
					</view>
				</view>
			</view>
			<view v-else class="empty-record">
				<text class="empty-icon">📊</text>
				<text class="empty-text">暂无学识分记录</text>
				<text class="empty-hint">参与答题获取学识分</text>
			</view>
			
			<view v-if="isLoading" class="loading-more">
				<text class="loading-text">加载中...</text>
			</view>
			
			<view v-if="!hasMore && recordList.length > 0" class="no-more">
				<text class="no-more-text">没有更多了</text>
			</view>
		</view>
	</scroll-view>
</template>

<script>
import api from '../../utils/api.js'

export default {
	data() {
		return {
			recordList: [],
			currentPage: 1,
			pageSize: 10,
			totalCount: 0,
			hasMore: true,
			isLoading: false,
			isFirstLoad: true,
			isNotLoggedIn: false
		}
	},
	
	onLoad() {
		this.loadRecord()
	},
	
	onShow() {
		if (!this.isFirstLoad) {
			this.loadRecord()
		}
		this.isFirstLoad = false
	},
	
	methods: {
		async loadRecord(isLoadMore = false) {
			if (this.isLoading) return
			if (!isLoadMore) {
				this.currentPage = 1
				this.hasMore = true
				this.isNotLoggedIn = false
			}
			
			if (!this.hasMore) return
			
			this.isLoading = true
			
			try {
				const res = await api.getPointRecord(this.currentPage, this.pageSize)
				if (res.data) {
					const newRecords = res.data.records || []
					
					if (isLoadMore) {
						this.recordList = [...this.recordList, ...newRecords]
					} else {
						this.recordList = newRecords
					}
					
					this.totalCount = res.data.count || 0
					
					if (this.recordList.length >= this.totalCount) {
						this.hasMore = false
					} else {
						this.currentPage++
					}
				}
			} catch (error) {
				console.error('获取学识分记录失败:', error)
				// 检查是否是401错误
				if (error.message && error.message.includes('401')) {
					this.isNotLoggedIn = true
				}
			} finally {
				this.isLoading = false
			}
		},
		
		loadMore() {
			this.loadRecord(true)
		},
		
		getSourceText(source) {
			// 根据source值返回对应的文本
			const sourceMap = {
				0: '未知',
				1: '随机答题',
				2: '类目答题',
				3: '限时答题'
			}
			return sourceMap[source] || '其他'
		},
		
		getSourceClass(source) {
			// 根据source值返回对应的样式类
			const classMap = {
				0: 'source-unknown',
				1: 'source-random',
				2: 'source-category',
				3: 'source-quick'
			}
			return classMap[source] || 'source-other'
		},
		
		formatTime(timestamp) {
			if (!timestamp) return ''
			const date = new Date(timestamp * 1000)
			const now = new Date()
			const diff = now - date
			
			const minute = 60 * 1000
			const hour = 60 * minute
			const day = 24 * hour
			
			if (diff < minute) {
				return '刚刚'
			} else if (diff < hour) {
				return Math.floor(diff / minute) + '分钟前'
			} else if (diff < day) {
				return Math.floor(diff / hour) + '小时前'
			} else if (diff < 7 * day) {
				return Math.floor(diff / day) + '天前'
			} else {
				const month = date.getMonth() + 1
				const dayOfMonth = date.getDate()
				return `${month}月${dayOfMonth}日`
			}
		}
	}
}
</script>

<style scoped>
.container {
	height: 100vh;
	width: 100%;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	padding: 40rpx;
	box-sizing: border-box;
}

.container::-webkit-scrollbar {
	display: none;
	width: 0;
	height: 0;
	background: transparent;
}

/deep/ ::-webkit-scrollbar {
	display: none;
	width: 0;
	height: 0;
	background: transparent;
}

.header {
	margin-bottom: 60rpx;
}

.welcome {
	text-align: center;
}

.welcome-title {
	display: block;
	font-size: 48rpx;
	font-weight: bold;
	color: #ffffff;
	margin-bottom: 20rpx;
}

.welcome-subtitle {
	display: block;
	font-size: 28rpx;
	color: rgba(255, 255, 255, 0.9);
}

.record-section {
	margin-bottom: 60rpx;
}

.record-list {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.record-item {
	background: #ffffff;
	border-radius: 20rpx;
	padding: 30rpx;
	box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	width: 100%;
	box-sizing: border-box;
}

.record-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.record-source {
	padding: 8rpx 20rpx;
	border-radius: 16rpx;
	font-size: 24rpx;
	font-weight: bold;
}

.source-unknown {
	background: #f5f5f5;
	color: #666666;
}

.source-random {
	background: #e3f2fd;
	color: #0d47a1;
}

.source-category {
	background: #d4edda;
	color: #155724;
}

.source-quick {
	background: #fff3cd;
	color: #856404;
}

.source-other {
	background: #f5f5f5;
	color: #666666;
}

.source-text {
	font-weight: bold;
}

.record-point {
	font-size: 36rpx;
	font-weight: bold;
}

.point-positive {
	color: #28a745;
}

.point-negative {
	color: #dc3545;
}

.point-text {
	font-weight: bold;
}

.record-footer {
	padding-top: 20rpx;
	border-top: 2rpx solid #f0f0f0;
}

.record-time {
	font-size: 24rpx;
	color: #999;
}

.empty-record {
	background: rgba(255, 255, 255, 0.15);
	border-radius: 20rpx;
	padding: 100rpx 40rpx;
	text-align: center;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 20rpx;
}

.empty-icon {
	font-size: 80rpx;
}

.empty-text {
	font-size: 32rpx;
	color: rgba(255, 255, 255, 0.9);
	font-weight: bold;
}

.empty-hint {
	font-size: 26rpx;
	color: rgba(255, 255, 255, 0.7);
}

.loading-more {
	text-align: center;
	padding: 40rpx 0;
}

.loading-text {
	font-size: 28rpx;
	color: rgba(255, 255, 255, 0.9);
}

.no-more {
	text-align: center;
	padding: 40rpx 0;
}

.no-more-text {
	font-size: 26rpx;
	color: rgba(255, 255, 255, 0.6);
}
</style>