<template>
	<view class="blacklist-page">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-btn" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<text class="header-title">黑名单</text>
			<view class="placeholder"></view>
		</view>

		<scroll-view scroll-y class="content">
		<!-- 说明提示 -->
		<view class="tips-section">
			<view class="tips-icon">ℹ️</view>
			<view class="tips-content">
				<text class="tips-text">拉黑后，对方将无法查看您的资料，也无法向您发送消息</text>
			</view>
		</view>

			<!-- 黑名单列表 -->
			<view class="list-section" v-if="blacklist.length > 0">
				<view class="blacklist-item" v-for="(item, index) in blacklist" :key="item.id">
					<view class="user-info" @click="viewProfile(item)">
						<image class="avatar" :src="item.avatar || defaultAvatar" mode="aspectFill"></image>
						<view class="info">
							<view class="name-row">
								<text class="nickname">{{ item.nickname }}</text>
								<text class="gender-icon" v-if="item.gender === 1">♂</text>
								<text class="gender-icon female" v-if="item.gender === 2">♀</text>
								<view class="vip-badge" v-if="item.isVip">
									<text class="vip-icon">👑</text>
								</view>
							</view>
							<view class="detail-row">
								<text class="age">{{ item.age > 0 ? item.age + '岁' : '年龄未知' }}</text>
								<text class="divider">|</text>
								<text class="location">{{ item.city }}</text>
							</view>
							<text class="block-time">拉黑时间：{{ formatDate(item.blockTime) }}</text>
						</view>
					</view>
					<view class="action-btn" @click="removeFromBlacklist(item, index)">
						<text class="btn-text">移除</text>
					</view>
				</view>
			</view>

			<!-- 空状态 -->
			<view class="empty-state" v-else>
				<text class="empty-icon">📋</text>
				<text class="empty-text">暂无黑名单用户</text>
				<text class="empty-tip">拉黑的用户将显示在这里</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				gatewayURL: 'http://localhost:8083',
				currentUserId: null,
				defaultAvatar: 'https://via.placeholder.com/100',
				blacklist: []
			}
		},
		onLoad() {
			// 获取当前用户ID
			this.currentUserId = uni.getStorageSync('userId') || 1
			this.loadBlacklist()
		},
		methods: {
			// 返回
			goBack() {
				uni.navigateBack()
			},
			
			// 加载黑名单
			loadBlacklist() {
				uni.showLoading({
					title: '加载中...'
				})
				
				// 调用后端接口获取黑名单
				uni.request({
					url: `${this.gatewayURL}/api/chatfriend/list`,
					method: 'GET',
					data: {
						userId: this.currentUserId
					},
					success: (res) => {
						console.log('黑名单列表响应:', res)
						if (res.data && res.data.code === 200) {
							// 转换后端数据格式到前端需要的格式
							this.blacklist = res.data.data.map(item => ({
							  id: item.id,
							  userId: item.friendId, // 后端用friendId表示被拉黑用户ID
							  nickname: item.friendName || '未知用户', // 对应后端friendName
							  avatar: item.friendAvatar || this.defaultAvatar, // 对应后端friendAvatar
							  gender: item.gender || 0, // 若后端有gender字段，若无则默认0
							  age: item.age || 0, // 若后端有age字段
							  city: item.city || '未知', // 若后端有city字段
							  isVip: false, // 后端无此数据，保持默认
							  blockTime: item.createTime, // 后端用createTime表示拉黑时间
							  reason: item.reason || '' // 若后端有reason字段
							}))
						} else {
							uni.showToast({
								title: res.data ? res.data.message : '加载失败',
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						console.error('加载黑名单失败:', err)
						uni.showToast({
							title: '网络请求失败',
							icon: 'none'
						})
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			},
			
			// 格式化日期
			formatDate(dateStr) {
				if (!dateStr) return ''
				// 简单格式化，只显示日期
				return dateStr.split(' ')[0]
			},
			
			// 查看用户资料
			viewProfile(item) {
				uni.showModal({
					title: '提示',
					content: '该用户已在黑名单中，是否先移除黑名单？',
					success: (res) => {
						if (res.confirm) {
							// 移除后跳转
							const index = this.blacklist.findIndex(u => u.id === item.id)
							this.removeFromBlacklist(item, index, true)
						}
					}
				})
			},
			
			// 移除黑名单
			removeFromBlacklist(item, index, needRedirect = false) {
				uni.showModal({
					title: '移除黑名单',
					content: `确定要将"${item.nickname}"移出黑名单吗？`,
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '处理中...'
							})
							
						// 调用后端接口移除黑名单
						uni.request({
							url: `${this.gatewayURL}/api/chatfriend/remove?userId=${this.currentUserId}&blockedUserId=${item.userId}`,
							method: 'DELETE',
								success: (response) => {
									console.log('移除黑名单响应:', response)
									if (response.data && response.data.code === 200) {
										// 从列表中移除
										this.blacklist.splice(index, 1)
										
										uni.showToast({
											title: '已移除黑名单',
											icon: 'success'
										})
										
										// 如果需要跳转到用户资料
										if (needRedirect) {
											setTimeout(() => {
												uni.navigateTo({
													url: '/pages/user-detail/index?userId=' + item.userId
												})
											}, 1000)
										}
									} else {
										uni.showToast({
											title: response.data ? response.data.message : '移除失败',
											icon: 'none'
										})
									}
								},
								fail: (err) => {
									console.error('移除黑名单失败:', err)
									uni.showToast({
										title: '网络请求失败',
										icon: 'none'
									})
								},
								complete: () => {
									uni.hideLoading()
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.blacklist-page {
		min-height: 100vh;
		background: #F5F5F5;
		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: linear-gradient(135deg, #FF8A9B 0%, #FFB4C0 100%);
		
		.back-btn {
			width: 70rpx;
			height: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: rgba(255, 255, 255, 0.2);
			border-radius: 50%;
			
			.back-icon {
				font-size: 44rpx;
				color: #FFFFFF;
				font-weight: bold;
			}
		}
		
		.header-title {
			font-size: 38rpx;
			font-weight: bold;
			color: #FFFFFF;
		}
		
		.placeholder {
			width: 70rpx;
		}
	}

	.content {
		flex: 1;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	}

	/* 提示区域 */
	.tips-section {
		margin: 30rpx;
		padding: 25rpx;
		background: linear-gradient(135deg, #FFF4E6 0%, #FFE8CC 100%);
		border-radius: 15rpx;
		border-left: 6rpx solid #FFA726;
		display: flex;
		align-items: flex-start;
		
		.tips-icon {
			font-size: 36rpx;
			margin-right: 15rpx;
			flex-shrink: 0;
		}
		
		.tips-content {
			flex: 1;
		}
		
		.tips-text {
			font-size: 26rpx;
			color: #E65100;
			line-height: 1.6;
		}
	}

	/* 列表区域 */
	.list-section {
		padding: 0 30rpx;
		
		.blacklist-item {
			background: #FFFFFF;
			border-radius: 15rpx;
			padding: 25rpx;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-shadow: 0 2rpx 15rpx rgba(0, 0, 0, 0.05);
			
			.user-info {
				flex: 1;
				display: flex;
				align-items: center;
				margin-right: 20rpx;
				
				.avatar {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					margin-right: 20rpx;
					border: 3rpx solid #F0F0F0;
					flex-shrink: 0;
				}
				
				.info {
					flex: 1;
					
					.name-row {
						display: flex;
						align-items: center;
						margin-bottom: 10rpx;
						
						.nickname {
							font-size: 32rpx;
							font-weight: bold;
							color: #333333;
							margin-right: 10rpx;
						}
						
						.gender-icon {
							font-size: 28rpx;
							color: #4A90E2;
							margin-right: 10rpx;
							font-weight: bold;
							
							&.female {
								color: #FF6B9D;
							}
						}
						
						.vip-badge {
							background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
							padding: 4rpx 12rpx;
							border-radius: 20rpx;
							display: flex;
							align-items: center;
							
							.vip-icon {
								font-size: 20rpx;
							}
						}
					}
					
					.detail-row {
						display: flex;
						align-items: center;
						margin-bottom: 8rpx;
						
						.age,
						.location {
							font-size: 26rpx;
							color: #666666;
						}
						
						.divider {
							margin: 0 15rpx;
							color: #CCCCCC;
						}
					}
					
					.block-time {
						font-size: 22rpx;
						color: #999999;
					}
				}
			}
			
			.action-btn {
				padding: 12rpx 30rpx;
				background: linear-gradient(135deg, #FF8A9B 0%, #FF6B8A 100%);
				border-radius: 30rpx;
				flex-shrink: 0;
				
				.btn-text {
					font-size: 26rpx;
					color: #FFFFFF;
					font-weight: 500;
				}
			}
		}
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 150rpx 50rpx;
		
		.empty-icon {
			font-size: 120rpx;
			margin-bottom: 30rpx;
			opacity: 0.3;
		}
		
		.empty-text {
			font-size: 32rpx;
			color: #999999;
			margin-bottom: 15rpx;
		}
		
		.empty-tip {
			font-size: 26rpx;
			color: #CCCCCC;
		}
	}
</style>

