<template>
	<view class="matchmaker-ranking">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-btn" @click="goBack"></view>
			<text class="header-title">红娘排行榜</text>
			<view class="placeholder"></view>
		</view>

		<!-- 更新提示 -->
		<view class="update-info">
			<text class="update-text">每天17点自动更新数据</text>
		</view>

		<!-- 本周最佳榜标题 -->
		<view class="section-title-wrapper">
			<text class="section-title">
				<text class="title-icon">👩‍❤️‍💋‍👨</text>
				本周最佳榜
				<text class="title-icon">👩‍❤️‍💋‍👨</text>
			</text>
		</view>

		<!-- 榜说明 -->
		<view class="ranking-description">
			<text class="description-text">榜单综合万粉以下红娘的日牵线数,用户好评数 等综合因素,上榜为前20的红娘</text>
		</view>

		<!-- 前三名展示 -->
		<view class="top-three-container" v-if="topThree.length >= 3">
			<view class="top-three-item second" @click="goToDetail(topThree[1])">
				<text class="rank-number">2</text>
				<image class="avatar-large" :src="topThree[1].avatar_url || defaultAvatar" mode="aspectFill"></image>
				<text class="matchmaker-name">{{ topThree[1].real_name }}</text>
				<text class="success-count">成功人数: {{ topThree[1].success_couples || 0 }}</text>
				<view class="like-btn" :class="{ liked: topThree[1].hasLiked }" @click.stop="handleLike(topThree[1])">
					{{ topThree[1].hasLiked ? '已点赞' : '点赞' }}
				</view>
			</view>

			<view class="top-three-item first" @click="goToDetail(topThree[0])">
				<text class="rank-number">1</text>
				<image class="avatar-large" :src="topThree[0].avatar_url || defaultAvatar" mode="aspectFill"></image>
				<text class="matchmaker-name">{{ topThree[0].real_name }}</text>
				<text class="success-count">成功人数: {{ topThree[0].success_couples || 0 }}</text>
				<view class="like-btn" :class="{ liked: topThree[0].hasLiked }" @click.stop="handleLike(topThree[0])">
					{{ topThree[0].hasLiked ? '已点赞' : '点赞' }}
				</view>
			</view>

			<view class="top-three-item third" @click="goToDetail(topThree[2])">
				<text class="rank-number">3</text>
				<image class="avatar-large" :src="topThree[2].avatar_url || defaultAvatar" mode="aspectFill"></image>
				<text class="matchmaker-name">{{ topThree[2].real_name }}</text>
				<text class="success-count">成功人数: {{ topThree[2].success_couples || 0 }}</text>
				<view class="like-btn" :class="{ liked: topThree[2].hasLiked }" @click.stop="handleLike(topThree[2])">
					{{ topThree[2].hasLiked ? '已点赞' : '点赞' }}
				</view>
			</view>
		</view>

		<!-- 排行榜列表 -->
		<scroll-view scroll-y class="ranking-list">
			<view class="ranking-item" v-for="(item, index) in restList" :key="item.matchmaker_id" @click="goToDetail(item)">
				<text class="rank-number-normal">{{ index + 4 }}</text>
				<image class="avatar-small" :src="item.avatar_url || defaultAvatar" mode="aspectFill"></image>
				<view class="matchmaker-info">
					<text class="matchmaker-name-normal">{{ item.real_name }}</text>
					<text class="success-count-normal">成功: {{ item.success_couples || 0 }} | 点赞: {{ item.weeklyLikes || 0 }}</text>
					<text class="user-rating">等级: {{ item.level_name || '青铜红娘' }}</text>
				</view>
				<view class="like-btn-small" :class="{ liked: item.hasLiked }" @click.stop="handleLike(item)">
					{{ item.hasLiked ? '已赞' : '点赞' }}
				</view>
			</view>
			<view v-if="loading" class="loading-tip">加载中...</view>
			<view v-if="!loading && restList.length === 0" class="empty-tip">暂无更多红娘</view>
		</scroll-view>

		<!-- 底部导航 -->
		<view class="tabbar">
			<view class="tabbar-item home" @click="navigateToWorkbench">
				<view class="tabbar-icon"></view>
				<text class="tabbar-text">工作台</text>
			</view>
			<view class="tabbar-item resources" @click="navigateToMyResources">
				<view class="tabbar-icon"></view>
				<text class="tabbar-text">我的资源</text>
			</view>
			<view class="tabbar-item trophy active" @click="navigateToRanking">
				<view class="tabbar-icon"></view>
				<text class="tabbar-text">排行榜</text>
			</view>
			<view class="tabbar-item message" @click="navigateToMessage">
				<view class="tabbar-icon">
					<view v-if="unreadCount > 0" class="badge">{{ unreadCount }}</view>
				</view>
				<text class="tabbar-text">消息</text>
			</view>
			<view class="tabbar-item mine" @click="navigateToMine">
				<view class="tabbar-icon"></view>
				<text class="tabbar-text">我的</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/utils/api.js'
	
export default {
		data() {
			return {
				loading: false,
				rankingList: [],
				defaultAvatar: '/static/default-avatar.svg',
				userId: null
			}
		},
		computed: {
			// 前三名
			topThree() {
				return this.rankingList.slice(0, 3)
			},
			// 第4名及以后
			restList() {
				return this.rankingList.slice(3)
			},
			// 全局未读消息数
			unreadCount() {
				return this.$store.getters.getTotalUnread || 0
			}
		},
		onLoad() {
			// 获取当前用户ID
			const userInfo = uni.getStorageSync('userInfo')
			if (userInfo && userInfo.userId) {
				this.userId = userInfo.userId
			}
			// 加载排行榜数据
			this.loadRankingData()
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack()
			},
			// 加载排行榜数据（使用本周排行榜API）
			async loadRankingData() {
				this.loading = true
				try {
					// 使用本周排行榜API，传入userId以获取点赞状态
					const res = await api.matchmaker.getWeeklyRanking({ 
						limit: 20,
						userId: this.userId
					})
					console.log('API原始返回数据:', JSON.stringify(res))
					
					let dataList = []
					if (res && Array.isArray(res)) {
						dataList = res
					} else if (res && res.data && Array.isArray(res.data)) {
						dataList = res.data
					} else if (res && typeof res === 'object') {
						// 可能直接返回的是对象数组
						dataList = Object.values(res).filter(item => typeof item === 'object' && item.matchmakerId)
					}
					
					this.rankingList = dataList.map(item => this.formatMatchmaker(item))
					console.log('格式化后的排行榜数据:', JSON.stringify(this.rankingList.slice(0, 3)))
				} catch (e) {
					console.error('加载排行榜数据失败:', e)
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			// 格式化红娘数据（统一字段名）
			formatMatchmaker(item) {
				return {
					...item,
					matchmaker_id: item.matchmakerId || item.matchmaker_id,
					real_name: item.realName || item.real_name,
					avatar_url: item.avatarUrl || item.avatar_url,
					success_couples: item.successCouples || item.success_couples,
					level_name: item.levelName || item.level_name,
					weeklyLikes: item.weekly_likes || item.weeklyLikes || 0,
					hasLiked: item.has_liked || item.hasLiked || false
				}
			},
			// 点赞
			async handleLike(matchmaker) {
				// 检查是否已点赞
				if (matchmaker.hasLiked) {
					uni.showToast({
						title: '本周已点赞过该红娘',
						icon: 'none'
					})
					return
				}
				
				// 检查是否登录
				if (!this.userId) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
					return
				}
				
				try {
					const matchmakerId = matchmaker.matchmaker_id || matchmaker.matchmakerId
					const res = await api.matchmaker.likeMatchmaker(this.userId, matchmakerId)
					
					if (res && (res.code === 200 || res.liked)) {
						uni.showToast({
							title: '点赞成功',
							icon: 'success'
						})
						// 更新本地状态 - 直接修改数据，不重新加载
						matchmaker.hasLiked = true
						matchmaker.weeklyLikes = (matchmaker.weeklyLikes || 0) + 1
						// 强制更新视图
						this.$forceUpdate()
					} else {
						uni.showToast({
							title: res.message || '点赞失败',
							icon: 'none'
						})
					}
				} catch (e) {
					console.error('点赞失败:', e)
					uni.showToast({
						title: e.message || '点赞失败',
						icon: 'none'
					})
				}
			},
			// 导航到工作台
			navigateToWorkbench() {
				uni.redirectTo({
					url: '/pages/matchmaker-workbench/index'
				})
			},
			// 导航到我的资源
			navigateToMyResources() {
				uni.redirectTo({
					url: '/pages/matchmaker-workbench/my-resources'
				})
			},
			// 导航到排行榜
			navigateToRanking() {
				// 已在排行榜页面，无需跳转
			},
			// 导航到消息
			navigateToMessage() {
				uni.redirectTo({
					url: '/pages/matchmaker-workbench/message'
				})
			},
			// 导航到我的
			navigateToMine() {
				uni.redirectTo({
					url: '/pages/matchmaker-workbench/mine'
				})
			},
			// 跳转到红娘详情
			goToDetail(matchmaker) {
				const id = matchmaker.matchmaker_id || matchmaker.matchmakerId
				if (id) {
					uni.navigateTo({
						url: `/pages/matchmakers/detail?id=${id}`
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.matchmaker-ranking {
		min-height: 100vh;
		background: linear-gradient(135deg, #FFF3F6 0%, #FFE4E8 100%);
		display: flex;
		flex-direction: column;
	}

	/* 顶部导航栏 */
	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 25rpx 30rpx;
		padding-top: calc(25rpx + env(safe-area-inset-top));
		background: #FFF9F9;
		border-bottom: 1rpx solid #F0F0F0;

		.back-btn {
			width: 70rpx;
			height: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: rgba(240, 240, 240, 0.5);
			border-radius: 50%;
			background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23333"><path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/></svg>');
			background-size: 40rpx 40rpx;
			background-repeat: no-repeat;
			background-position: center;
		}

		.header-title {
			font-size: 38rpx;
			font-weight: bold;
			color: #333;
		}

		.placeholder {
			width: 70rpx;
		}
	}

	/* 更新提示 */
	.update-info {
		background: #FFF3E0;
		padding: 15rpx;
		text-align: center;

		.update-text {
			font-size: 24rpx;
			color: #FF9800;
		}
	}

	/* 本周最佳榜标题 */
	.section-title-wrapper {
		text-align: center;
		margin: 30rpx 0 15rpx;

		.section-title {
			display: inline-block;
			font-size: 36rpx;
			font-weight: bold;
			color: #E91E63;
			line-height: 1.4;

			.title-icon {
				font-size: 40rpx;
				margin: 0 10rpx;
			}
		}
	}

	/* 榜说明 */
	.ranking-description {
		padding: 0 30rpx 25rpx;

		.description-text {
			display: block;
			font-size: 26rpx;
			color: #666;
			line-height: 1.5;
			text-align: center;
		}
	}

	/* 前三名展示 */
	.top-three-container {
		display: flex;
		justify-content: space-around;
		align-items: flex-end;
		padding: 0 20rpx 40rpx;
		background: #FFF9F9;
		border-radius: 20rpx;
		margin: 0 20rpx 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

		.top-three-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 20rpx;
			border-radius: 20rpx;
			position: relative;

			&.first {
				background: linear-gradient(135deg, #FFD700 0%, #FFA000 100%);
				width: 280rpx;
				height: 400rpx;
				order: 2;
				margin-bottom: 30rpx;
			}

			&.second {
				background: linear-gradient(135deg, #C0C0C0 0%, #A8A8A8 100%);
				width: 240rpx;
				height: 350rpx;
				order: 1;
			}

			&.third {
				background: linear-gradient(135deg, #CD7F32 0%, #B87333 100%);
				width: 240rpx;
				height: 350rpx;
				order: 3;
			}

			.rank-number {
				position: absolute;
				top: 20rpx;
				left: 20rpx;
				font-size: 48rpx;
				font-weight: bold;
				color: #FFFFFF;
			}

			.avatar-large {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
				background: rgba(255, 255, 255, 0.3);
				margin: 40rpx 0 20rpx;
			}

			.matchmaker-name {
				font-size: 32rpx;
				font-weight: bold;
				color: #FFFFFF;
				margin-bottom: 15rpx;
			}

			.success-count {
				font-size: 28rpx;
				color: #FFFFFF;
				margin-bottom: 30rpx;
			}

			.like-btn {
				padding: 12rpx 35rpx;
				background: rgba(255, 255, 255, 0.2);
				color: #FFFFFF;
				border-radius: 25rpx;
				font-size: 26rpx;
				font-weight: bold;
				border: 2rpx solid #FFFFFF;

				&.liked {
					background: rgba(255, 255, 255, 0.5);
					color: rgba(255, 255, 255, 0.8);
					border-color: rgba(255, 255, 255, 0.5);
				}
			}
		}
	}

	.ranking-list {
		flex: 1;
		padding: 0 20rpx 120rpx;

		.ranking-item {
			display: flex;
			align-items: center;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 25rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

			.rank-number-normal {
				width: 50rpx;
				height: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				font-weight: bold;
				color: #999;
				border-radius: 50%;
				background: #F0F0F0;
				margin-right: 20rpx;
			}

			.avatar-small {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				background: #F0F0F0;
				margin-right: 20rpx;
			}

			.matchmaker-info {
				flex: 1;

				.matchmaker-name-normal {
					display: block;
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 8rpx;
				}

				.success-count-normal,
				.user-rating {
					display: block;
					font-size: 24rpx;
					color: #666;
					margin-bottom: 4rpx;
				}
			}

			.like-btn-small {
				padding: 10rpx 30rpx;
				background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
				color: #FFFFFF;
				border-radius: 25rpx;
				font-size: 26rpx;
				font-weight: bold;
				
				&.liked {
					background: #CCCCCC;
					color: #999999;
				}
			}
		}

		.loading-tip,
		.empty-tip {
			text-align: center;
			padding: 40rpx;
			font-size: 28rpx;
			color: #999;
		}
	}

	/* 底部导航 */
	.tabbar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		background: #FFFFFF;
		border-top: 1rpx solid #F0F0F0;
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding-bottom: env(safe-area-inset-bottom);

		.tabbar-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 8rpx;
			padding: 10rpx 0;

			.tabbar-icon {
				width: 44rpx;
				height: 44rpx;
				background-size: contain;
				background-repeat: no-repeat;
				background-position: center;
				position: relative;

				.badge {
					position: absolute;
					top: -8rpx;
					right: -8rpx;
					background: #FF4444;
					color: #FFFFFF;
					font-size: 20rpx;
					font-weight: bold;
					width: 32rpx;
					height: 32rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 16rpx;
				}
			}

			.tabbar-text {
				font-size: 20rpx;
				color: #999;
			}

			&.active .tabbar-text {
				color: #9C27B0;
				font-weight: bold;
			}

			&.home .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>');
			}
			&.home.active .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%239C27B0"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>');
			}

			&.resources .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"/></svg>');
			}
			&.resources.active .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%239C27B0"><path d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"/></svg>');
			}

			&.trophy .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M19 5h-2V3H7v2H5c-1.1 0-2 .9-2 2v1c0 2.55 1.92 4.63 4.39 4.94.63 1.5 1.98 2.63 3.61 2.96V19H7v2h10v-2h-4v-3.1c1.63-.33 2.98-1.46 3.61-2.96C19.08 12.63 21 10.55 21 8V7c0-1.1-.9-2-2-2zM5 8V7h2v3.82C5.84 10.4 5 9.3 5 8zm14 0c0 1.3-.84 2.4-2 2.82V7h2v1z"/></svg>');
			}
			&.trophy.active .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%239C27B0"><path d="M19 5h-2V3H7v2H5c-1.1 0-2 .9-2 2v1c0 2.55 1.92 4.63 4.39 4.94.63 1.5 1.98 2.63 3.61 2.96V19H7v2h10v-2h-4v-3.1c1.63-.33 2.98-1.46 3.61-2.96C19.08 12.63 21 10.55 21 8V7c0-1.1-.9-2-2-2zM5 8V7h2v3.82C5.84 10.4 5 9.3 5 8zm14 0c0 1.3-.84 2.4-2 2.82V7h2v1z"/></svg>');
			}

			&.message .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H6l-2 2V4h16v12z"/></svg>');
			}
			&.message.active .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%239C27B0"><path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H6l-2 2V4h16v12z"/></svg>');
			}

			&.mine .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>');
			}
			&.mine.active .tabbar-icon {
				background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%239C27B0"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>');
			}
		}
	}
</style>