<template>
	<view class="matchmaker-detail-page">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<view class="nav-left" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<view class="nav-title">红娘详情</view>
			<view class="nav-right"></view>
		</view>

		<scroll-view class="content-scroll" scroll-y v-if="matchmaker">
			<!-- 头部卡片 -->
			<view class="header-card">
				<view class="avatar-wrapper">
					<image :src="getAvatarUrl(matchmaker.avatar_url)" 
						class="avatar" 
						mode="aspectFill"
						@error="handleImageError(item)"
						@load="handleImageLoad(item)">
					</image>
					<view class="avatar-placeholder" v-if="shouldShowPlaceholder()">
						<text class="placeholder-icon">👤</text>
					</view>
					<view class="level-badge" :class="'level-' + (matchmaker.level || 1)">
						{{ matchmaker.level_name || '青铜红娘' }}
					</view>
				</view>

				<view class="basic-info">
					<view class="name-row">
						<text class="name">{{ matchmaker.real_name }}</text>
						<view class="gender-icon">
							{{ matchmaker.gender === 1 ? '♂' : '♀' }}
						</view>
					</view>

					<view class="type-tag" :class="matchmaker.matchmaker_type === 2 ? 'formal' : 'parttime'">
						{{ matchmaker.type_name || '兼职红娘' }}
					</view>

					<view class="stats-grid">
						<view class="stats-item">
							<text class="stats-value">{{ matchmaker.success_couples || 0 }}</text>
							<text class="stats-label">成功撮合</text>
						</view>
						<view class="stats-divider"></view>
						<view class="stats-item">
							<text class="stats-value">{{ matchmaker.age || '--' }}</text>
							<text class="stats-label">年龄</text>
						</view>
						<view class="stats-divider"></view>
						<view class="stats-item">
							<text class="stats-value">{{ getLevelText(matchmaker.level) }}</text>
							<text class="stats-label">等级</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 联系方式卡片 -->
			<view class="section-card">
				<view class="section-title">
					<text class="title-icon">📞</text>
					<text class="title-text">联系方式</text>
				</view>
				<view class="contact-list">
					<view class="contact-item" @click="callPhone(matchmaker.phone)">
						<view class="contact-left">
							<text class="contact-icon">📱</text>
							<text class="contact-label">手机号码</text>
						</view>
						<view class="contact-right">
							<text class="contact-value">{{ matchmaker.phone }}</text>
							<text class="contact-arrow">→</text>
						</view>
					</view>
					<view class="contact-item" v-if="matchmaker.email">
						<view class="contact-left">
							<text class="contact-icon">📧</text>
							<text class="contact-label">电子邮箱</text>
						</view>
						<view class="contact-right">
							<text class="contact-value">{{ matchmaker.email }}</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 地址信息卡片 -->
			<view class="section-card" v-if="matchmaker.full_address">
				<view class="section-title">
					<text class="title-icon">📍</text>
					<text class="title-text">服务地区</text>
				</view>
				<view class="address-content">
					<text class="address-text">{{ matchmaker.full_address }}</text>
				</view>
			</view>

			<!-- 个人简介卡片 -->
			<view class="section-card" v-if="matchmaker.profile">
				<view class="section-title">
					<text class="title-icon">✏️</text>
					<text class="title-text">个人简介</text>
				</view>
				<view class="profile-content">
					<text class="profile-text">{{ matchmaker.profile }}</text>
				</view>
			</view>

			<!-- 服务优势卡片 -->
			<view class="section-card">
				<view class="section-title">
					<text class="title-icon">⭐</text>
					<text class="title-text">服务优势</text>
				</view>
				<view class="advantage-list">
					<view class="advantage-item">
						<text class="advantage-icon">✓</text>
						<text class="advantage-text">专业婚恋指导</text>
					</view>
					<view class="advantage-item">
						<text class="advantage-icon">✓</text>
						<text class="advantage-text">海量优质资源</text>
					</view>
					<view class="advantage-item">
						<text class="advantage-icon">✓</text>
						<text class="advantage-text">一对一贴心服务</text>
					</view>
					<view class="advantage-item">
						<text class="advantage-icon">✓</text>
						<text class="advantage-text">高效精准匹配</text>
					</view>
				</view>
			</view>

			<!-- 成功案例展示 -->
			<view class="section-card">
				<view class="section-title">
					<text class="title-icon">💑</text>
					<text class="title-text">成功案例</text>
				</view>
				<view class="success-tip">
					<text class="tip-text">已成功撮合 {{ matchmaker.success_couples || 0 }} 对佳偶</text>
				</view>
			</view>

			<!-- 底部占位 -->
			<view class="bottom-placeholder"></view>
		</scroll-view>

		<!-- 加载中 -->
		<view class="loading-container" v-if="loading">
			<text class="loading-text">加载中...</text>
		</view>

		<!-- 底部操作栏 -->
		<view class="bottom-bar" v-if="matchmaker">
			<view class="action-btn call-btn" @click="callPhone(matchmaker.phone)">
				<text class="btn-icon">📞</text>
				<text class="btn-text">拨打电话</text>
			</view>
			<view class="action-btn consult-btn" @click="consultMatchmaker">
				<text class="btn-icon">💬</text>
				<text class="btn-text">在线咨询</text>
			</view>
		</view>
	</view>
</template>

<script>
import api from '@/utils/api.js'
import { DEFAULT_IMAGES } from '@/config/index.js'

export default {
	data() {
		return {
			// 红娘ID
			matchmakerId: null,
			
			// 红娘详情
			matchmaker: null,
			
			// 加载状态
			loading: false,
			
			// 图片加载错误状态
			imageLoadError: false,
			
			// 默认头像
			defaultAvatar: DEFAULT_IMAGES.avatar
		}
	},

	onLoad(options) {
		console.log('红娘详情页接收到的参数:', options)
		if (options.id) {
			const parsedId = parseInt(options.id)
			console.log('解析后的红娘ID:', parsedId)
			
			// 检查解析后的ID是否有效
			if (isNaN(parsedId)) {
				console.error('红娘ID解析失败，原始值:', options.id)
				uni.showToast({
					title: 'ID参数无效',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
				return
			}
			
			this.matchmakerId = parsedId
			this.loadMatchmakerDetail()
		} else {
			console.error('未接收到红娘ID参数')
			uni.showToast({
				title: '参数错误',
				icon: 'none'
			})
			setTimeout(() => {
				uni.navigateBack()
			}, 1500)
		}
	},

	methods: {
		/**
		 * 加载红娘详情
		 */
		async loadMatchmakerDetail() {
			this.loading = true
			
			try {
				const result = await api.matchmaker.getDetail(this.matchmakerId)
				console.log(result)
				this.matchmaker = result
			} catch (error) {
				console.error('加载红娘详情失败:', error)
				uni.showToast({
					title: '加载失败，请重试',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			} finally {
				this.loading = false
			}
		},
		/**
		 * 图片加载成功处理
		 */
		handleImageLoad(item) {
			console.log(item)
			// 防御性检查：确保item存在且是对象
			if (!item || typeof item !== 'object') {
				console.warn('handleImageLoad: item参数无效', item)
				return
			}
			this.$set(item, 'imageLoadError', false)
			this.$set(item, 'imageLoaded', true)
		},
		

		/**
		 * 获取等级文本
		 */
		getLevelText(level) {
			const levelMap = {
				1: '青铜',
				2: '白银',
				3: '黄金',
				4: '铂金',
				5: '钻石'
			}
			return levelMap[level] || '青铜'
		},

		/**
		 * 拨打电话
		 */
		callPhone(phone) {
			uni.showModal({
				title: '拨打电话',
				content: `是否拨打 ${phone}？`,
				success: (res) => {
					if (res.confirm) {
						uni.makePhoneCall({
							phoneNumber: phone,
							fail: () => {
								uni.showToast({
									title: '拨号失败',
									icon: 'none'
								})
							}
						})
					}
				}
			})
		},

		/**
		 * 在线咨询
		 */
		consultMatchmaker() {
			uni.showToast({
				title: '功能开发中',
				icon: 'none'
			})
		},

		/**
		 * 获取头像URL
		 */
		getAvatarUrl(avatarUrl) {
			if (avatarUrl) {
				// 如果是完整URL（MinIO生成的预签名URL或其他有效URL）
				if (avatarUrl.startsWith('http')) {
					// 检查是否是无效的placeholder URL
					if (avatarUrl.includes('placeholder')) {
						console.log('检测到placeholder URL，使用默认头像')
						return this.defaultAvatar
					}
					return avatarUrl
				}
				// 如果是相对路径，添加基础路径
				if (!avatarUrl.startsWith('/')) {
					return `/static/${avatarUrl}`
				}
				return avatarUrl
			}
			// 无头像URL时使用默认头像
			return this.defaultAvatar
		},

		/**
		 * 判断是否显示占位符
		 */
		shouldShowPlaceholder() {
			if (!this.matchmaker) return false
			// 如果没有头像URL，或头像加载失败，显示占位符
			return !this.matchmaker.avatar_url || this.imageLoadError || 
				   (this.matchmaker.avatar_url && this.matchmaker.avatar_url.includes('placeholder'))
		},


		/**
		 * 图片加载错误处理
		 */
		handleImageError(item) {
			console.log(item)
			// 防御性检查：确保item存在且是对象
			if (!item || typeof item !== 'object') {
				console.warn('handleImageError: item参数无效', item)
				return
			}
			
			console.log('头像加载失败:', item.real_name, item.avatar_url)
			this.$set(item, 'imageLoadError', true)
			this.$set(item, 'imageLoaded', false)
		},

		/**
		 * 返回
		 */
		goBack() {
			uni.navigateBack()
		}
	}
}
</script>

<style lang="scss" scoped>
.matchmaker-detail-page {
	min-height: 100vh;
	background-color: #F5F5F5;
	padding-bottom: 120rpx;
}

/* 导航栏 */
.nav-bar {
	display: flex;
	justify-content: space-between;
	align-items: center;
	height: 88rpx;
	padding: 0 30rpx;
	background-color: #FFFFFF;
	border-bottom: 1rpx solid #F0F0F0;
	position: sticky;
	top: 0;
	z-index: 999;

	.nav-left {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		.back-icon {
			font-size: 48rpx;
			color: #333333;
		}
	}

	.nav-title {
		flex: 1;
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.nav-right {
		width: 60rpx;
	}
}

/* 内容滚动区 */
.content-scroll {
	height: calc(100vh - 88rpx - 120rpx);
}

/* 头部卡片 */
.header-card {
	margin: 20rpx 30rpx;
	padding: 40rpx;
	background: linear-gradient(135deg, #FFE5EE 0%, #FFF9F9 100%);
	border-radius: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(233, 30, 99, 0.1);

	.avatar-wrapper {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		margin: 0 auto 30rpx;

		.avatar {
			width: 100%;
			height: 100%;
			border-radius: 50%;
			border: 6rpx solid #FFFFFF;
			background: linear-gradient(135deg, #F5F5F5 0%, #E0E0E0 100%);
		}

		.avatar-placeholder {
			position: absolute;
			top: 6rpx;
			left: 6rpx;
			width: calc(100% - 12rpx);
			height: calc(100% - 12rpx);
			border-radius: 50%;
			background: linear-gradient(135deg, #FFE5F1 0%, #FFECF3 100%);
			display: flex;
			align-items: center;
			justify-content: center;
			border: 2rpx solid rgba(233, 30, 99, 0.1);

			.placeholder-icon {
				font-size: 100rpx;
				color: rgba(233, 30, 99, 0.4);
			}
		}

		.level-badge {
			position: absolute;
			bottom: 0;
			left: 50%;
			transform: translateX(-50%);
			padding: 8rpx 20rpx;
			border-radius: 20rpx;
			font-size: 24rpx;
			color: #FFFFFF;
			white-space: nowrap;

			&.level-1 {
				background: linear-gradient(135deg, #CD7F32 0%, #8B4513 100%);
			}

			&.level-2 {
				background: linear-gradient(135deg, #C0C0C0 0%, #808080 100%);
			}

			&.level-3 {
				background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
			}

			&.level-4 {
				background: linear-gradient(135deg, #00CED1 0%, #4682B4 100%);
			}

			&.level-5 {
				background: linear-gradient(135deg, #9370DB 0%, #8A2BE2 100%);
			}
		}
	}

	.basic-info {
		text-align: center;

		.name-row {
			display: flex;
			align-items: center;
			justify-content: center;
			gap: 15rpx;
			margin-bottom: 15rpx;

			.name {
				font-size: 40rpx;
				font-weight: bold;
				color: #333333;
			}

			.gender-icon {
				font-size: 36rpx;
				color: #E91E63;
			}
		}

		.type-tag {
			display: inline-block;
			padding: 8rpx 20rpx;
			border-radius: 20rpx;
			font-size: 24rpx;
			color: #FFFFFF;
			margin-bottom: 30rpx;

			&.formal {
				background-color: #E91E63;
			}

			&.parttime {
				background-color: #FF9800;
			}
		}

		.stats-grid {
			display: flex;
			justify-content: space-around;
			align-items: center;

			.stats-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				gap: 10rpx;

				.stats-value {
					font-size: 36rpx;
					font-weight: bold;
					color: #E91E63;
				}

				.stats-label {
					font-size: 24rpx;
					color: #666666;
				}
			}

			.stats-divider {
				width: 2rpx;
				height: 60rpx;
				background-color: #E0E0E0;
			}
		}
	}
}

/* 区块卡片 */
.section-card {
	margin: 20rpx 30rpx;
	padding: 30rpx;
	background-color: #FFFFFF;
	border-radius: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

	.section-title {
		display: flex;
		align-items: center;
		gap: 15rpx;
		margin-bottom: 30rpx;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #F0F0F0;

		.title-icon {
			font-size: 36rpx;
		}

		.title-text {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
		}
	}
}

/* 联系方式列表 */
.contact-list {
	.contact-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 25rpx 0;
		border-bottom: 1rpx solid #F5F5F5;

		&:last-child {
			border-bottom: none;
		}

		.contact-left {
			display: flex;
			align-items: center;
			gap: 15rpx;

			.contact-icon {
				font-size: 32rpx;
			}

			.contact-label {
				font-size: 28rpx;
				color: #666666;
			}
		}

		.contact-right {
			display: flex;
			align-items: center;
			gap: 10rpx;

			.contact-value {
				font-size: 28rpx;
				color: #333333;
			}

			.contact-arrow {
				font-size: 28rpx;
				color: #999999;
			}
		}
	}
}

/* 地址内容 */
.address-content {
	.address-text {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.6;
	}
}

/* 个人简介 */
.profile-content {
	.profile-text {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.8;
		white-space: pre-wrap;
	}
}

/* 服务优势列表 */
.advantage-list {
	display: grid;
	grid-template-columns: repeat(2, 1fr);
	gap: 20rpx;

	.advantage-item {
		display: flex;
		align-items: center;
		gap: 10rpx;
		padding: 20rpx;
		background-color: #F9F9F9;
		border-radius: 10rpx;

		.advantage-icon {
			font-size: 28rpx;
			color: #4CAF50;
		}

		.advantage-text {
			font-size: 26rpx;
			color: #666666;
		}
	}
}

/* 成功案例提示 */
.success-tip {
	padding: 20rpx;
	background: linear-gradient(135deg, #FFE5EE 0%, #FFF9F9 100%);
	border-radius: 10rpx;
	text-align: center;

	.tip-text {
		font-size: 28rpx;
		color: #E91E63;
	}
}

/* 加载中 */
.loading-container {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 80vh;

	.loading-text {
		font-size: 28rpx;
		color: #999999;
	}
}

/* 底部操作栏 */
.bottom-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	display: flex;
	gap: 20rpx;
	padding: 20rpx 30rpx;
	background-color: #FFFFFF;
	border-top: 1rpx solid #F0F0F0;
	box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);
	padding-bottom: constant(safe-area-inset-bottom);
	padding-bottom: env(safe-area-inset-bottom);

	.action-btn {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 10rpx;
		padding: 25rpx 0;
		border-radius: 50rpx;
		font-size: 28rpx;
		font-weight: bold;
		color: #FFFFFF;
		transition: transform 0.2s;

		&:active {
			transform: scale(0.95);
		}

		.btn-icon {
			font-size: 32rpx;
		}

		&.call-btn {
			background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
		}

		&.consult-btn {
			background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
		}
	}
}

/* 底部占位 */
.bottom-placeholder {
	height: 40rpx;
}
</style>

