<template>
	<view class="client-detail" :class="{ 'dark-mode': isDarkTheme }">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-icon" @click="handleBack"></view>
			<text class="header-title">客户信息</text>
			<view class="header-right"></view>
		</view>

		<!-- 客户基本信息 -->
		<view class="client-basic-info">
			<image :src="clientInfo.avatar" class="client-avatar" mode="aspectFill"></image>
			<view class="basic-info">
				<view class="client-name-wrapper">
					<text class="client-name">{{ clientInfo.name }}</text>
					<text class="gender-tag">{{ clientInfo.gender }}</text>
				</view>
				<view class="status-tag-wrapper">
					<text class="status-tag register-tag registered">已注册</text>
					<text class="status-tag match-tag" :class="{ 'matched': followForm.matchStatus === 1, 'unmatched': followForm.matchStatus === 0 || followForm.matchStatus === null }">
						{{ followForm.matchStatus === 1 ? '已匹配' : '未匹配' }}
					</text>
				</view>
				<view class="client-tags">
					<text class="tag" v-for="(tag, index) in clientInfo.tags" :key="index">{{ tag }}</text>
				</view>
			</view>
		</view>

		<!-- 择偶要求 -->
		<view class="requirement-section">
			<view class="section-title">
				<text class="title-icon">💑</text>
				<text class="title-text">择偶要求</text>
			</view>
			<text class="requirement-content">{{ clientInfo.requirement }}</text>
		</view>

		<!-- 联系方式 -->
		<view class="contact-section">
			<view class="contact-item">
				<text class="contact-label">联系方式</text>
				<view class="contact-value">
					<text class="contact-number">{{ clientInfo.contact }}</text>
					<text class="copy-btn" @click="handleCopy(clientInfo.contact)">复制</text>
				</view>
			</view>
			<view class="contact-item" v-if="clientInfo.backupContact">
				<text class="contact-label">备用联系方式</text>
				<view class="contact-value">
					<text class="contact-number">{{ clientInfo.backupContact }}</text>
					<text class="copy-btn" @click="handleCopy(clientInfo.backupContact)">复制</text>
				</view>
			</view>
		</view>

		<!-- 跟进统计 -->
		<view class="stats-section">
			<text class="stats-title">跟进统计</text>
			<view class="stats-grid">
				<view class="stat-item">
					<text class="stat-value">{{ clientInfo.stats.followTimes }}</text>
					<text class="stat-label">跟进次数</text>
				</view>
				<view class="stat-item">
					<text class="stat-value">{{ clientInfo.stats.matchCount }}</text>
					<text class="stat-label">匹配对象</text>
				</view>
				<view class="stat-item">
					<text class="stat-value">{{ clientInfo.stats.phoneCalls }}</text>
					<text class="stat-label">电话沟通</text>
				</view>
				<view class="stat-item">
					<text class="stat-value">{{ clientInfo.stats.interviews }}</text>
					<text class="stat-label">面谈次数</text>
				</view>
			</view>
		</view>

		<!-- 标签切换 -->
		<view class="tabs-section">
			<text class="tab" :class="{ active: activeTab === 'details' }" @click="switchTab('details')">详细信息</text>
			<text class="tab" :class="{ active: activeTab === 'follow' }" @click="switchTab('follow')">跟进</text>
		</view>

		<!-- 详细信息 -->
		<scroll-view scroll-y class="details-section" v-if="activeTab === 'details'">
			<!-- 基本信息 -->
			<view class="detail-item">
				<text class="detail-label">婚姻状况:</text>
				<text class="detail-value">{{ clientInfo.details.maritalStatus }}</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">学历:</text>
				<text class="detail-value">{{ clientInfo.details.education }}</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">职业:</text>
				<text class="detail-value">{{ clientInfo.details.occupation }}</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">收入:</text>
				<text class="detail-value">
					{{ clientInfo.details.income }}
					<text v-if="clientInfo.details.income && clientInfo.details.income !== '未知'">万元/年</text>
				</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">购房情况:</text>
				<text class="detail-value">{{ clientInfo.details.housing }}</text>
			</view>

			<!-- 择偶要求详情 -->
			<view class="sub-section-title requirement-section-title">择偶详细要求</view>
			<view class="detail-item">
				<text class="detail-label">年龄范围:</text>
				<text class="detail-value">{{ clientInfo.details.requirement.ageRange }}</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">身高要求:</text>
				<text class="detail-value">{{ clientInfo.details.requirement.height }}</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">婚姻状况:</text>
				<text class="detail-value">{{ clientInfo.details.requirement.maritalStatus }}</text>
			</view>
			<view class="detail-item">
				<text class="detail-label">学历要求:</text>
				<text class="detail-value">{{ clientInfo.details.requirement.education }}</text>
			</view>
			<view class="detail-item" v-if="clientInfo.details.requirement.other">
				<text class="detail-label">其他要求:</text>
				<text class="detail-value">{{ clientInfo.details.requirement.other }}</text>
			</view>

			<!-- 客户标签 -->
			<view class="sub-section-title tags-section-title">客户标签</view>
			<view class="client-tags">
				<text class="tag" v-for="(tag, index) in clientInfo.tags" :key="index">{{ tag }}</text>
			</view>
		</scroll-view>

		<!-- 跟进信息 -->
		<scroll-view scroll-y class="follow-section" v-else>
			<!-- 跟进方式 -->
			<view class="follow-group">
				<text class="group-label">跟进方式</text>
				<view class="follow-type-options">
					<view class="follow-type-item" :class="{ active: followForm.type === 'phone' }" @click="followForm.type = 'phone'">
						<text class="radio-circle"></text>
						<text class="option-text">电话</text>
					</view>
					<view class="follow-type-item" :class="{ active: followForm.type === 'meet' }" @click="followForm.type = 'meet'">
						<text class="radio-circle"></text>
						<text class="option-text">面谈</text>
					</view>
					<view class="follow-type-item" :class="{ active: followForm.type === 'other' }" @click="followForm.type = 'other'">
						<text class="radio-circle"></text>
						<text class="option-text">其他</text>
					</view>
				</view>
			</view>

			<!-- 匹配状态 -->
			<view class="follow-group">
				<text class="group-label">匹配状态</text>
				<view class="status-tags-row">
					<view class="status-tag" :class="{ active: followForm.matchStatus === 0 }" @click="handleMatchStatusChange(0)">未匹配</view>
					<view class="status-tag" :class="{ active: followForm.matchStatus === 1 }" @click="handleMatchStatusChange(1)">已匹配</view>
				</view>
			</view>

			<!-- 跟进进度 -->
			<view class="follow-group">
				<text class="group-label">跟进进度</text>
				<view class="status-tags-row">
					<view class="status-tag" :class="{ active: followForm.progress === 'notMet' }" @click="followForm.progress = 'notMet'">未见面</view>
					<view class="status-tag" :class="{ active: followForm.progress === 'communicating' }" @click="followForm.progress = 'communicating'">沟通中</view>
					<view class="status-tag" :class="{ active: followForm.invitationStatus === 0 }" @click="handleInvitationStatusChange(0)">未邀约</view>
					<view class="status-tag" :class="{ active: followForm.invitationStatus === 1 }" @click="handleInvitationStatusChange(1)">已邀约</view>
				</view>
			</view>

			<!-- 邀约时间 -->
			<view class="follow-group">
				<text class="group-label">邀约时间</text>
				<view class="input-like">
					<picker mode="date" :value="followForm.inviteDate" @change="onInviteDateChange">
						<view class="picker-inner">{{ followForm.inviteDate || '请选择日期' }}</view>
					</picker>
				</view>
			</view>

			<!-- 跟进备注 -->
			<view class="follow-group">
				<text class="group-label">跟进备注</text>
				<textarea class="textarea" v-model="followForm.remark" placeholder="请输入跟进备注详细信息..." placeholder-style="color:#C5A4D8" />
			</view>

			<!-- 提交按钮（预留） -->
			<view class="follow-submit-btn" :class="{ 'disabled': !canFollow }" @click="handleSubmitFollow">保存跟进</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	name: 'client-detail',
	data() {
		return {
			activeTab: 'details',
			followForm: {
				type: 'phone',
				progress: 'matched',
				matchStatus: 0, // 0-未匹配，1-已匹配
				invitationStatus: 0, // 0-未邀约，1-已邀约
				inviteDate: '',
				remark: ''
			},
			resourceId: null, // 资源ID
			fromQualityResources: false, // 是否从优质资源列表进入
			currentUserId: null, // 当前登录用户的用户ID
			currentMatchmakerId: null, // 当前红娘的matchmaker_id
			resourceMatchmakerId: null, // 资源的matchmaker_id
			canFollow: true, // 是否可以跟进
			clientInfo: {
				id: 1,
				name: '',
				gender: '男',
				status: '已匹配',
				tags: [],
				avatar: '',
				requirement: '暂无要求',
				contact: '',
				backupContact: '',
				originalPhone: '', // 保存原始手机号用于复制
				originalBackupPhone: '', // 保存原始备用手机号用于复制
				stats: {
					followTimes: 0,
					matchCount: 0,
					phoneCalls: 0,
					interviews: 0
				},
				details: {
					maritalStatus: '',
					education: '',
					occupation: '',
					income: '',
					housing: '',
					requirement: {
						ageRange: '',
						height: '',
						maritalStatus: '',
						education: '',
						other: ''
					}
				}
			},
			isDarkTheme: false
		}
	},
	onShow() {
		const stored = uni.getStorageSync('matchmakerDarkMode')
		if (stored === true || stored === false) {
			this.isDarkTheme = stored
		}
		
		// 如果已经有resourceId，刷新统计数据
		if (this.resourceId) {
			this.loadFollowStatistics(this.resourceId)
		}
	},
	onLoad(options) {
		
		// 从URL参数获取资源ID
		let resourceId = null
		if (options.resourceId) {
			resourceId = options.resourceId
			
		} else if (options.id) {
			// 兼容旧的id参数
			resourceId = options.id
			
		}
		
		// 检查是否从优质资源列表进入
		if (options.fromQualityResources === '1' || options.fromQualityResources === 1) {
			this.fromQualityResources = true
			
		}
		
		// 获取当前登录用户的ID
		const userId = uni.getStorageSync('userId')
		if (userId) {
			const rawUserId = parseInt(userId)
			if (!isNaN(rawUserId) && rawUserId > 0) {
				this.currentUserId = rawUserId
				
				// 获取当前红娘的matchmaker_id
				this.getCurrentMatchmakerId()
			}
		}
		
		if (resourceId) {
			// 确保resourceId是数字类型
			resourceId = parseInt(resourceId)
			if (isNaN(resourceId) || resourceId <= 0) {
				
				uni.showToast({
					title: '资源ID无效',
					icon: 'none'
				})
				return
			}
			
			this.resourceId = resourceId
			this.loadClientInfo(resourceId)
		} else {
			
			uni.showToast({
				title: '未获取到资源ID',
				icon: 'none'
			})
		}
	},
	methods: {
		// 返回上一页
		handleBack() {
			uni.navigateBack()
		},
		// 复制联系方式
		handleCopy(contact) {
			// 如果传入的是脱敏的手机号，使用原始手机号
			let phoneToCopy = contact
			if (contact === this.clientInfo.contact && this.clientInfo.originalPhone) {
				phoneToCopy = this.clientInfo.originalPhone
			} else if (contact === this.clientInfo.backupContact && this.clientInfo.originalBackupPhone) {
				phoneToCopy = this.clientInfo.originalBackupPhone
			} else {
				// 尝试从脱敏号码中提取
				phoneToCopy = contact.replace(/\*+/g, '')
			}
			
			uni.setClipboardData({
				data: phoneToCopy,
				success: () => {
					uni.showToast({
						title: '复制成功',
						icon: 'success'
					})
				},
				fail: () => {
					uni.showToast({
						title: '复制失败',
						icon: 'none'
					})
				}
			})
		},
		// 切换标签
		switchTab(tab) {
			this.activeTab = tab
		},
		// 邀约日期变更
		onInviteDateChange(e) {
			this.followForm.inviteDate = e.detail.value
		},
		// 匹配状态变更
		handleMatchStatusChange(status) {
			this.followForm.matchStatus = status
		},
		// 邀约状态变更
		handleInvitationStatusChange(status) {
			this.followForm.invitationStatus = status
			// 如果选择已邀约且没有设置邀约时间，提示用户选择
			if (status === 1 && !this.followForm.inviteDate) {
				uni.showToast({
					title: '请选择邀约时间',
					icon: 'none'
				})
			}
		},
		// 加载最新的跟进信息，更新表单字段
		async loadLatestFollowInfo(resourceId) {
			try {
				const baseUrl = process.env.NODE_ENV === 'development' 
					? 'https://api.zhongruanke.cn/api'  // 开发环境 - 通过网关
					: 'https://your-domain.com/api'  // 生产环境
				
				// 获取最新的跟进记录
				const [error, res] = await uni.request({
					url: `${baseUrl}/my-resource/latest-follow/${resourceId}`,
					method: 'GET',
					header: {
						'Content-Type': 'application/json'
					}
				})
				
				if (error) {
					
					return
				}
				
				if (res.statusCode === 200 && res.data && res.data.code === 200) {
					const followUp = res.data.data
					
					if (followUp) {
						// 更新跟进方式（1-电话，2-面谈，3-其他）
						if (followUp.followType !== null && followUp.followType !== undefined) {
							const followType = followUp.followType
							if (followType === 1) {
								this.followForm.type = 'phone'
							} else if (followType === 2) {
								this.followForm.type = 'meet'
							} else if (followType === 3) {
								this.followForm.type = 'other'
							}
						}
						
						// 更新跟进进度（1-已匹配，2-未见面，3-沟通中）
						if (followUp.followProgress !== null && followUp.followProgress !== undefined) {
							const progress = followUp.followProgress
							if (progress === 1) {
								this.followForm.progress = 'matched'
							} else if (progress === 2) {
								this.followForm.progress = 'notMet'
							} else if (progress === 3) {
								this.followForm.progress = 'communicating'
							}
						}
						
						// 更新匹配状态
						if (followUp.isMatch !== null && followUp.isMatch !== undefined) {
							this.followForm.matchStatus = parseInt(followUp.isMatch) || 0
						}
						
						// 更新邀约状态
						if (followUp.isInvitation !== null && followUp.isInvitation !== undefined) {
							this.followForm.invitationStatus = parseInt(followUp.isInvitation) || 0
						}
						
						// 更新邀约时间
						if (followUp.invitationTime) {
							const date = new Date(followUp.invitationTime)
							const year = date.getFullYear()
							const month = String(date.getMonth() + 1).padStart(2, '0')
							const day = String(date.getDate()).padStart(2, '0')
							this.followForm.inviteDate = `${year}-${month}-${day}`
						} else {
							this.followForm.inviteDate = ''
						}
						
						// 更新备注
						if (followUp.remark !== null && followUp.remark !== undefined) {
							this.followForm.remark = followUp.remark || ''
						} else {
							this.followForm.remark = ''
						}
						
						
					} 
				}
			} catch (e) {
			
			}
		},
		// 加载跟进统计数据
		async loadFollowStatistics(resourceId) {
			try {
				const baseUrl = process.env.NODE_ENV === 'development' 
					? 'https://api.zhongruanke.cn/api'  // 开发环境 - 通过网关
					: 'https://your-domain.com/api'  // 生产环境
				
				const [error, res] = await uni.request({
					url: `${baseUrl}/my-resource/follow-statistics/${resourceId}`,
					method: 'GET',
					header: {
						'Content-Type': 'application/json'
					}
				})
				
				if (error) {
					
					return
				}
				
				if (res.statusCode === 200 && res.data && res.data.code === 200) {
					const statistics = res.data.data
				
					
					// 更新统计数据（支持驼峰和下划线两种字段名格式）
					if (this.clientInfo && this.clientInfo.stats) {
						const followCount = statistics?.followCount ?? statistics?.follow_count ?? 0
						const matchCount = statistics?.matchCount ?? statistics?.match_count ?? 0
						const phoneCount = statistics?.phoneCount ?? statistics?.phone_count ?? 0
						const interviewCount = statistics?.interviewCount ?? statistics?.interview_count ?? 0
						
						this.clientInfo.stats.followTimes = followCount
						this.clientInfo.stats.matchCount = matchCount
						this.clientInfo.stats.phoneCalls = phoneCount
						this.clientInfo.stats.interviews = interviewCount
						
						
					} 
				} 
			} catch (e) {
				
			}
		},
		// 提交跟进
		async handleSubmitFollow() {
			// 检查是否可以跟进
			if (!this.canFollow) {
				uni.showToast({
					title: '该用户还没有添加到你的资源当中',
					icon: 'none',
					duration: 2000
				})
				return
			}
			
			if (!this.resourceId) {
				uni.showToast({
					title: '资源ID不存在',
					icon: 'none'
				})
				return
			}
			
			try {
				uni.showLoading({
					title: '保存中...'
				})
				
				const baseUrl = process.env.NODE_ENV === 'development' 
					? 'https://api.zhongruanke.cn/api'  // 开发环境 - 通过网关
					: 'https://your-domain.com/api'  // 生产环境
				
				// 准备请求数据
				// 跟进方式转换：phone->1, meet->2, other->3
				let followType = 1 // 默认电话
				if (this.followForm.type === 'meet') {
					followType = 2
				} else if (this.followForm.type === 'other') {
					followType = 3
				}
				
				// 跟进进度转换：matched->1, notMet->2, communicating->3
				let progress = 1 // 默认已匹配
				if (this.followForm.progress === 'notMet') {
					progress = 2
				} else if (this.followForm.progress === 'communicating') {
					progress = 3
				}
				
				const requestData = {
					followType: followType,
					progress: this.followForm.progress, // 保持字符串格式，后端会转换
					matchStatus: this.followForm.matchStatus,
					invitationStatus: this.followForm.invitationStatus,
					invitationTime: this.followForm.inviteDate || null,
					remark: this.followForm.remark || '' // 添加备注字段
				}
				
				
				
				const [error, res] = await uni.request({
					url: `${baseUrl}/my-resource/update-follow/${this.resourceId}`,
					method: 'PUT',
					data: requestData,
					header: {
						'Content-Type': 'application/json'
					}
				})
				
				uni.hideLoading()
				
				if (error) {
				
					uni.showToast({
						title: '保存失败',
						icon: 'none'
					})
					return
				}
				
				if (res.statusCode === 200 && res.data && res.data.code === 200) {
					uni.showToast({
						title: '跟进已保存',
						icon: 'success'
					})
					
					// 刷新统计数据
					if (this.resourceId) {
						this.loadFollowStatistics(this.resourceId)
					}
					
					// 重新加载最新的跟进信息，更新表单字段
					if (this.resourceId) {
						this.loadLatestFollowInfo(this.resourceId)
					}
					
					// 触发资源列表刷新事件
					uni.$emit('refreshResourceList')
				} else {
					const errorMsg = res.data?.message || '保存失败'
					uni.showToast({
						title: errorMsg,
						icon: 'none'
					})
				}
			} catch (e) {
				uni.hideLoading()
				
				uni.showToast({
					title: '保存失败，请稍后重试',
					icon: 'none'
				})
			}
		},
		// 获取当前红娘的matchmaker_id
		async getCurrentMatchmakerId() {
			if (!this.currentUserId) {
				
				return
			}
			
			try {
				const baseUrl = process.env.NODE_ENV === 'development' 
					? 'https://api.zhongruanke.cn/api'  // 开发环境 - 通过网关
					: 'https://your-domain.com/api'  // 生产环境
				
				// 根据user_id查询红娘信息
				const [error, res] = await uni.request({
					url: `${baseUrl}/matchmaker/by-user/${this.currentUserId}`,
					method: 'GET'
				})
				
				if (error) {
					
					return
				}
				
				if (res.statusCode === 200 && res.data && res.data.code === 200) {
					const matchmaker = res.data.data
					this.currentMatchmakerId = matchmaker.matchmakerId || matchmaker.matchmaker_id
				
					// 如果已经加载了资源信息，重新判断是否可以跟进
					if (this.resourceMatchmakerId !== null && this.resourceMatchmakerId !== undefined) {
						if (this.fromQualityResources) {
							if (!this.currentMatchmakerId || this.resourceMatchmakerId !== this.currentMatchmakerId) {
								this.canFollow = false
								
							} else {
								this.canFollow = true
								
							}
						}
					}
				}
			} catch (e) {
			
			}
		},
		// 从API加载客户信息
		async loadClientInfo(resourceId) {
			try {
				
				
				uni.showLoading({
					title: '加载中...'
				})
				
				const baseUrl = process.env.NODE_ENV === 'development' 
					? 'https://api.zhongruanke.cn/api'  // 开发环境 - 通过网关
					: 'https://your-domain.com/api'  // 生产环境
				
				const requestUrl = `${baseUrl}/my-resource/client-detail/${resourceId}`
			
				const [error, res] = await uni.request({
					url: requestUrl,
					method: 'GET',
					timeout: 30000, // 设置30秒超时
					header: {
						'Content-Type': 'application/json'
					}
				})
				
				
				
				uni.hideLoading()
				
				if (error) {
					
					let errorMsg = '加载失败'
					if (error.errMsg) {
						if (error.errMsg.includes('timeout') || error.errMsg.includes('超时')) {
							errorMsg = '请求超时，请检查网络连接'
						} else if (error.errMsg.includes('fail')) {
							errorMsg = '网络连接失败，请检查服务器是否启动'
						}
					}
					uni.showToast({
						title: errorMsg,
						icon: 'none',
						duration: 3000
					})
					return
				}
				
				if (res.statusCode === 200 && res.data && res.data.code === 200) {
					const data = res.data.data
					
					
					
					// 支持驼峰和下划线两种字段名格式
					const resourceId = data.resourceId || data.resource_id
					const avatarUrl = data.avatarUrl || data.avatar_url
					const backupPhone = data.backupPhone || data.backup_phone
					const marrStatus = data.marrStatus !== undefined ? data.marrStatus : data.marr_status
					// 择偶要求从my_resource表的mate_selection_criteria字段获取
					const mateSelectionCriteria = data.mateSelectionCriteria || data.mate_selection_criteria
					// 获取资源的matchmaker_id
					const resourceMatchmakerId = data.matchmakerId || data.matchmaker_id
					this.resourceMatchmakerId = resourceMatchmakerId
					
					// 判断是否可以跟进：如果从优质资源列表进入，且资源不属于当前红娘，则不能跟进
					if (this.fromQualityResources) {
						// 如果还没有获取到当前红娘的ID，先获取
						if (!this.currentMatchmakerId) {
							await this.getCurrentMatchmakerId()
						}
						if (!this.currentMatchmakerId || this.resourceMatchmakerId !== this.currentMatchmakerId) {
							this.canFollow = false
							
						} else {
							this.canFollow = true
						
						}
					} else {
						// 从我的资源列表进入，默认可以跟进
						this.canFollow = true
					}
					
					// 择偶要求详情字段（从partner_requirement表，用于详情页）
					const minAge = data.minAge !== undefined ? data.minAge : data.min_age
					const maxAge = data.maxAge !== undefined ? data.maxAge : data.max_age
					const minHeight = data.minHeight !== undefined ? data.minHeight : data.min_height
					const maxHeight = data.maxHeight !== undefined ? data.maxHeight : data.max_height
					const educationLevel = data.educationLevel !== undefined ? data.educationLevel : data.education_level
					const salaryRange = data.salaryRange !== undefined ? data.salaryRange : data.salary_range
					const houseRequirement = data.houseRequirement !== undefined ? data.houseRequirement : data.house_requirement
					const carRequirement = data.carRequirement !== undefined ? data.carRequirement : data.car_requirement
					const maritalStatusRequirement = data.maritalStatusRequirement !== undefined ? data.maritalStatusRequirement : data.marital_status_requirement
					const preferredCity = data.preferredCity || data.preferred_city
					const otherRequirements = data.otherRequirements || data.other_requirements
					
					
					
					// 处理标签列表（从后端返回的tags字段，如果没有则使用星座和职业）
					let clientTags = []
					if (data.tags && Array.isArray(data.tags) && data.tags.length > 0) {
						// 使用后端返回的标签
						clientTags = data.tags
					} else {
						// 如果没有tags，使用星座和职业作为标签
						if (data.constellation) {
							clientTags.push(data.constellation)
						}
						if (data.occupation) {
							clientTags.push(data.occupation)
						}
					}
					
					// 加载匹配状态和邀约状态
					const isMatch = data.isMatch !== null && data.isMatch !== undefined ? data.isMatch : 
					               (data.is_match !== null && data.is_match !== undefined ? data.is_match : 0)
					const isInvitation = data.isInvitation !== null && data.isInvitation !== undefined ? data.isInvitation : 
					                    (data.is_Invitation !== null && data.is_Invitation !== undefined ? data.is_Invitation : 0)
					const invitationTime = data.invitationTime || data.Invitation_time
					
					this.followForm.matchStatus = parseInt(isMatch) || 0
					this.followForm.invitationStatus = parseInt(isInvitation) || 0
					if (invitationTime) {
						// 将日期转换为YYYY-MM-DD格式
						const date = new Date(invitationTime)
						const year = date.getFullYear()
						const month = String(date.getMonth() + 1).padStart(2, '0')
						const day = String(date.getDate()).padStart(2, '0')
						this.followForm.inviteDate = `${year}-${month}-${day}`
					}
					
					// 映射数据到clientInfo
					this.clientInfo = {
						id: resourceId,
						name: data.name || '',
						gender: data.gender === 1 ? '男' : data.gender === 2 ? '女' : '未知',
						status: '已匹配', // 可以根据实际字段判断
						tags: clientTags, // 使用处理后的标签列表
						avatar: avatarUrl || '',
						requirement: mateSelectionCriteria || '暂无要求', // 择偶要求显示my_resource表的mate_selection_criteria字段
						contact: data.phone ? data.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '',
						backupContact: backupPhone ? backupPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '',
						originalPhone: data.phone || '', // 保存原始手机号
						originalBackupPhone: backupPhone || '', // 保存原始备用手机号
						stats: {
							followTimes: 0, // 将从统计数据接口获取
							matchCount: 0,
							phoneCalls: 0,
							interviews: 0
						},
						details: {
							maritalStatus: this.formatMaritalStatus(marrStatus),
							education: data.diploma || '未知',
							occupation: data.occupation || '未知',
							income: data.income || '未知',
							housing: data.house === 1 ? '已购房' : data.house === 0 ? '未购房' : '未知',
							requirement: {
								ageRange: minAge && maxAge ? `${minAge}-${maxAge}岁` : '不限',
								height: minHeight ? `${minHeight}cm以上` : '不限',
								maritalStatus: this.formatMaritalStatusRequirement(maritalStatusRequirement),
								education: this.formatEducationLevel(educationLevel),
								other: otherRequirements || ''
							}
						}
					}
					
					// 标签已经在上面处理过了，这里不需要再添加
					
					
					// 加载跟进统计数据
					this.loadFollowStatistics(resourceId)
				} else {
					uni.showToast({
						title: res.data.message || '加载失败',
						icon: 'none'
					})
				}
			} catch (e) {
				uni.hideLoading()
				
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				})
			}
		},
		// 格式化婚姻状况
		formatMaritalStatus(status) {
			if (status === null || status === undefined) return '未知'
			switch (status) {
				case 0: return '未婚'
				case 1: return '离异'
				case 2: return '丧偶'
				default: return '未知'
			}
		},
		// 格式化婚姻状况要求
		formatMaritalStatusRequirement(status) {
			if (status === null || status === undefined) return '不限'
			switch (status) {
				case 0: return '不限'
				case 1: return '未婚'
				case 2: return '离异'
				case 3: return '丧偶'
				default: return '不限'
			}
		},
		// 格式化学历要求
		formatEducationLevel(level) {
			if (level === null || level === undefined) return '不限'
			switch (level) {
				case 0: return '不限'
				case 1: return '高中及以上'
				case 2: return '专科及以上'
				case 3: return '本科及以上'
				case 4: return '硕士及以上'
				case 5: return '博士及以上'
				default: return '不限'
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.client-detail {
	min-height: 100vh;
	background: linear-gradient(180deg, #FFF5F8 0%, #F8E8F0 50%, #FFF9F9 100%);
	padding-top: 90rpx;
}

/* 顶部导航栏 */
.header {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	height: 90rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 20rpx;
	background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
	z-index: 999;

	.back-icon {
		width: 44rpx;
		height: 44rpx;
		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: contain;
		background-repeat: no-repeat;
		background-position: center;
	}

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

	.header-right {
		width: 44rpx;
	}
}

/* 客户基本信息 */
.client-basic-info {
	display: flex;
	align-items: flex-start;
	padding: 35rpx;
	background: linear-gradient(135deg, #FFFFFF 0%, #FFFBFD 100%);
	margin: 20rpx;
	border-radius: 24rpx;
	box-shadow: 0 8rpx 24rpx rgba(156, 39, 176, 0.12), 0 2rpx 8rpx rgba(156, 39, 176, 0.08);
	border: 1rpx solid rgba(243, 229, 245, 0.8);

	.client-avatar {
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
		margin-right: 30rpx;
		background-color: #F5F5F5;
	}

	.basic-info {
		flex: 1;

		.client-name-wrapper {
			display: flex;
			align-items: center;
			gap: 12rpx;
			margin-bottom: 12rpx;
		}

		.client-name {
			font-size: 38rpx;
			font-weight: 600;
			color: #2C2C2C;
			letter-spacing: 0.5rpx;
		}

		.gender-tag {
			font-size: 24rpx;
			font-weight: 500;
			padding: 6rpx 14rpx;
			border-radius: 12rpx;
			background: #E3F2FD;
			color: #2196F3;
			border: 1rpx solid #BBDEFB;
		}

		.status-tag-wrapper {
			display: flex;
			align-items: center;
			gap: 10rpx;
			margin-bottom: 18rpx;
		}

		.status-tag {
			display: inline-block;
			padding: 4rpx 12rpx;
			border-radius: 12rpx;
			font-size: 22rpx;
			font-weight: 500;

			&.register-tag {
				&.registered {
					background: #E8F5E9;
					color: #4CAF50;
				}
			}

			&.match-tag {
				&.matched {
					background: #E3F2FD;
					color: #2196F3;
				}

				&.unmatched {
					background: #FFF3E0;
					color: #FF9800;
				}
			}
		}

		.client-status {
			font-size: 26rpx;
			color: #FF6B8A;
			margin-bottom: 18rpx;
			display: inline-block;
			padding: 4rpx 16rpx;
			background: linear-gradient(135deg, #FFE5EB 0%, #FFF0F5 100%);
			border-radius: 12rpx;
			font-weight: 500;
		}

		.client-tags {
			display: flex;
			flex-wrap: wrap;
			gap: 10rpx;

			.tag {
				padding: 10rpx 22rpx;
				background: linear-gradient(135deg, #F3E5F5 0%, #E1BEE7 100%);
				color: #7B1FA2;
				border-radius: 24rpx;
				font-size: 24rpx;
				font-weight: 600;
				box-shadow: 0 2rpx 8rpx rgba(156, 39, 176, 0.15);
				border: 1rpx solid rgba(156, 39, 176, 0.2);
			}
		}
	}
}

/* 择偶要求 */
.requirement-section {
	padding: 35rpx;
	background: linear-gradient(135deg, #FFFFFF 0%, #FFFBFD 100%);
	margin: 0 20rpx 20rpx;
	border-radius: 24rpx;
	box-shadow: 0 8rpx 24rpx rgba(156, 39, 176, 0.12), 0 2rpx 8rpx rgba(156, 39, 176, 0.08);
	border: 1rpx solid rgba(243, 229, 245, 0.8);

	.section-title {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;

		.title-icon {
			font-size: 28rpx;
			margin-right: 10rpx;
		}

		.title-text {
			font-size: 30rpx;
			font-weight: 600;
			color: #2C2C2C;
			letter-spacing: 0.5rpx;
		}
	}

	.requirement-content {
		font-size: 28rpx;
		color: #555;
		line-height: 1.8;
		font-weight: 400;
	}
}

/* 联系方式 */
.contact-section {
	padding: 35rpx;
	background: linear-gradient(135deg, #FFFFFF 0%, #FFFBFD 100%);
	margin: 0 20rpx 20rpx;
	border-radius: 24rpx;
	box-shadow: 0 8rpx 24rpx rgba(156, 39, 176, 0.12), 0 2rpx 8rpx rgba(156, 39, 176, 0.08);
	border: 1rpx solid rgba(243, 229, 245, 0.8);

	.contact-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 25rpx;

		&:last-child {
			margin-bottom: 0;
		}

		.contact-label {
			font-size: 28rpx;
			color: #2C2C2C;
			font-weight: 500;
		}

		.contact-value {
			display: flex;
			align-items: center;
			gap: 20rpx;

			.contact-number {
				font-size: 28rpx;
				color: #555;
				font-weight: 400;
			}

			.copy-btn {
				padding: 10rpx 24rpx;
				background: linear-gradient(135deg, #F3E5F5 0%, #E1BEE7 100%);
				color: #7B1FA2;
				border-radius: 24rpx;
				font-size: 24rpx;
				font-weight: 600;
				box-shadow: 0 2rpx 8rpx rgba(156, 39, 176, 0.2);
				border: 1rpx solid rgba(156, 39, 176, 0.2);
				transition: all 0.3s;
				
				&:active {
					transform: scale(0.95);
					box-shadow: 0 1rpx 4rpx rgba(156, 39, 176, 0.3);
				}
			}
		}
	}
}

/* 跟进统计 */
.stats-section {
	padding: 30rpx;
	background-color: #FFFFFF;
	margin: 0 20rpx 20rpx;
	border-radius: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

	.stats-title {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 25rpx;
	}

	.stats-grid {
		display: grid;
		grid-template-columns: repeat(4, 1fr);
		gap: 20rpx;

		.stat-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 20rpx;
			background-color: #FFF3F5;
			border-radius: 15rpx;

			.stat-value {
				font-size: 48rpx;
				font-weight: bold;
				color: #FF6B8A;
				margin-bottom: 10rpx;
			}

			.stat-label {
				font-size: 22rpx;
				color: #666;
			}
		}
	}
}

/* 标签切换 */
.tabs-section {
	display: flex;
	padding: 0 20rpx;
	background: linear-gradient(135deg, #FFFFFF 0%, #FFFBFD 100%);
	margin: 0 20rpx 20rpx;
	border-radius: 24rpx;
	box-shadow: 0 8rpx 24rpx rgba(156, 39, 176, 0.12), 0 2rpx 8rpx rgba(156, 39, 176, 0.08);
	border: 1rpx solid rgba(243, 229, 245, 0.8);

	.tab {
		flex: 1;
		text-align: center;
		padding: 24rpx 0;
		font-size: 30rpx;
		color: #888;
		font-weight: 500;
		transition: all 0.3s;
		position: relative;

		&.active {
			color: #7B1FA2;
			font-weight: 600;
			
			&::after {
				content: '';
				position: absolute;
				bottom: 0;
				left: 50%;
				transform: translateX(-50%);
				width: 60rpx;
				height: 6rpx;
				background: linear-gradient(90deg, #9C27B0 0%, #E1BEE7 100%);
				border-radius: 3rpx;
			}
		}
	}
}

/* 详细信息滚动区域 */
.details-section {
	padding: 35rpx 25rpx 100rpx;
	background: linear-gradient(135deg, #FFFFFF 0%, #FFFBFD 100%);
	margin: 0 20rpx 20rpx;
	border-radius: 24rpx;
	box-shadow: 0 8rpx 24rpx rgba(156, 39, 176, 0.12), 0 2rpx 8rpx rgba(156, 39, 176, 0.08);
	border: 1rpx solid rgba(243, 229, 245, 0.8);
	box-sizing: border-box;
	position: relative;

	.detail-item {
		display: flex;
		margin-bottom: 28rpx;
		padding: 12rpx 0;
		border-bottom: 1rpx solid rgba(240, 240, 240, 0.6);
		
		&:last-of-type {
			border-bottom: none;
		}

		.detail-label {
			width: 200rpx;
			font-size: 28rpx;
			color: #2C2C2C;
			font-weight: 500;
			letter-spacing: 0.3rpx;
		}

		.detail-value {
			flex: 1;
			font-size: 28rpx;
			color: #555;
			font-weight: 400;
			text-align: right;
		}
	}

	.sub-section-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #2C2C2C;
		margin: 30rpx 0 20rpx;
		padding-bottom: 12rpx;
		border-bottom: 2rpx solid rgba(240, 240, 240, 0.8);
		letter-spacing: 0.5rpx;
	}
	
	/* 择偶详细要求部分 - 添加明显的上分隔 */
	.requirement-section-title {
		margin-top: 60rpx;
		padding: 32rpx 24rpx 20rpx;
		border-top: 3rpx solid rgba(156, 39, 176, 0.15);
		border-bottom: 2rpx solid rgba(156, 39, 176, 0.1);
		position: relative;
		background: linear-gradient(135deg, #F8F4F9 0%, #FFFBFD 100%);
		border-radius: 16rpx 16rpx 0 0;
	}
	
	.requirement-section-title::before {
		content: '';
		position: absolute;
		left: 0;
		top: 0;
		width: 8rpx;
		height: 100%;
		background: linear-gradient(180deg, #9C27B0 0%, #BA68C8 50%, #E1BEE7 100%);
		border-radius: 0 4rpx 4rpx 0;
		box-shadow: 2rpx 0 8rpx rgba(156, 39, 176, 0.2);
	}
	
	/* 客户标签部分 - 添加明显的上分隔 */
	.tags-section-title {
		margin-top: 60rpx;
		padding: 32rpx 24rpx 20rpx;
		border-top: 3rpx solid rgba(156, 39, 176, 0.15);
		border-bottom: 2rpx solid rgba(156, 39, 176, 0.1);
		position: relative;
		background: linear-gradient(135deg, #F8F4F9 0%, #FFFBFD 100%);
		border-radius: 16rpx 16rpx 0 0;
	}
	
	.tags-section-title::before {
		content: '';
		position: absolute;
		left: 0;
		top: 0;
		width: 8rpx;
		height: 100%;
		background: linear-gradient(180deg, #9C27B0 0%, #BA68C8 50%, #E1BEE7 100%);
		border-radius: 0 4rpx 4rpx 0;
		box-shadow: 2rpx 0 8rpx rgba(156, 39, 176, 0.2);
	}

	.client-tags {
		display: flex;
		flex-wrap: wrap;
		gap: 12rpx;
		padding: 20rpx 0;

		.tag {
			padding: 12rpx 24rpx;
			background: linear-gradient(135deg, #F3E5F5 0%, #E1BEE7 100%);
			color: #7B1FA2;
			border-radius: 24rpx;
			font-size: 26rpx;
			font-weight: 600;
			box-shadow: 0 2rpx 8rpx rgba(156, 39, 176, 0.15);
			border: 1rpx solid rgba(156, 39, 176, 0.2);
			transition: all 0.3s;
			
			&:active {
				transform: scale(0.95);
			}
		}
	}
}

/* 跟进信息 */
.follow-section {
	background-color: #FFFFFF;
	margin: 0 20rpx 20rpx;
	border-radius: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
	padding: 30rpx 20rpx 40rpx;
	max-height: 700rpx;
	box-sizing: border-box;
}

.follow-group {
	margin-bottom: 30rpx;

	.group-label {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 16rpx;
		display: block;
	}
}

/* 跟进方式单选 */
.follow-type-options {
	display: flex;
	align-items: center;
	gap: 30rpx;

	.follow-type-item {
		display: flex;
		align-items: center;
		gap: 10rpx;
		font-size: 26rpx;
		color: #666;

		.radio-circle {
			width: 26rpx;
			height: 26rpx;
			border-radius: 50%;
			border: 2rpx solid #C5A4D8;
			box-sizing: border-box;
		}

		&.active {
			color: #D81B60;

			.radio-circle {
				background-color: #D81B60;
				border-color: #D81B60;
			}
		}
	}
}

/* 跟进进度标签 */
.status-tags-row {
	display: flex;
	flex-wrap: wrap;
	gap: 16rpx;

	.status-tag {
		min-width: 140rpx;
		text-align: center;
		padding: 14rpx 20rpx;
		border-radius: 20rpx;
		border: 2rpx solid #E1BEE7;
		font-size: 26rpx;
		color: #9C27B0;
		background-color: #FAF5FF;
		box-sizing: border-box;

		&.active {
			background-color: #F8BBD0;
			border-color: #F06292;
			color: #FFFFFF;
		}
	}
}

/* 邀约状态/时间 等输入类展示 */
.input-like {
	width: 100%;
	padding: 18rpx 22rpx;
	border-radius: 16rpx;
	background-color: #FAF5FF;
	font-size: 26rpx;
	color: #666;
	box-sizing: border-box;
}

.picker-inner {
	font-size: 26rpx;
	color: #666;
}

/* 备注输入框 */
.textarea {
	width: 100%;
	min-height: 160rpx;
	padding: 18rpx 22rpx;
	border-radius: 16rpx;
	background-color: #FAF5FF;
	font-size: 26rpx;
	color: #333;
	box-sizing: border-box;
}

/* 保存按钮 */
.follow-submit-btn {
	&.disabled {
		background: #CCCCCC !important;
		color: #999999 !important;
		opacity: 0.6;
		cursor: not-allowed;
	}
	margin-top: 10rpx;
	width: 100%;
	padding: 22rpx 0;
	text-align: center;
	border-radius: 26rpx;
	background: linear-gradient(135deg, #F48FB1 0%, #EC407A 100%);
	color: #FFFFFF;
	font-size: 30rpx;
	font-weight: bold;
}
</style>