<template>
	<view class="success-case-page">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<view class="navbar-left" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<view class="navbar-title">成功案例</view>
			<view class="navbar-right"></view>
		</view>

		<!-- 成功案例列表 -->
		<view class="case-list">
			<view class="case-card" v-for="(item, index) in caseList" :key="index"
				@click="goToDetail(item.case_no)">
				<image :src="item.image_url || DEFAULT_IMAGES.couple" class="case-image" mode="aspectFill"></image>
				<view class="case-info">
					<view class="case-names">{{ item.male_user_nickname }} & {{ item.female_user_nickname }}</view>
					<view class="case-quote">{{ item.quote }}</view>
					<view class="case-meta">
						<text class="case-date">💑 {{ formatDate(item.marriage_date) }}</text>
					</view>
					<view class="case-btn">查看他们的故事</view>
				</view>
				<view class="success-badge">
					<text class="badge-icon">✓</text>
				</view>
			</view>
		</view>

		<!-- 加载更多 -->
		<view class="load-more" v-if="hasMore">
			<text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
		</view>
		<view class="no-more" v-else>
			<text class="no-more-text">没有更多了</text>
		</view>
	</view>
</template>

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

	export default {
		data() {
			return {
				caseList: [
					// 默认数据，确保页面有内容
					{
						case_no: 'CASE001',
						male_user_nickname: '张先生',
						female_user_nickname: '李女士',
						image_url: DEFAULT_IMAGES.couple,
						quote: '从第一次咖啡约会到领证，只用了90天',
						marriage_date: '2024-08-20',
						story: '我们通过平台相识，第一次见面是在咖啡厅...'
					},
					{
						case_no: 'CASE002', 
						male_user_nickname: '王先生',
						female_user_nickname: '赵女士',
						image_url: DEFAULT_IMAGES.couple,
						quote: '感谢红娘的专业服务，让我们相遇相知',
						marriage_date: '2024-09-15',
						story: '感谢红娘阿姨的介绍，让我们找到了彼此...'
					},
					{
						case_no: 'CASE003',
						male_user_nickname: '刘先生',
						female_user_nickname: '陈女士', 
						image_url: DEFAULT_IMAGES.couple,
						quote: '缘分天注定，感恩平台让我们走到一起',
						marriage_date: '2024-10-01',
						story: '国庆节的婚礼特别有意义...'
					}
				],
				pageNum: 1,
				pageSize: 10,
				hasMore: true,
				loading: false,
				DEFAULT_IMAGES
			}
		},

		onLoad() {
			console.log('成功案例列表页面加载')
			console.log('默认案例数据:', this.caseList.length)
			
			// 数据健康检查
			this.checkDataHealth()
			
			this.loadCaseList()
		},

		onReachBottom() {
			if (this.hasMore && !this.loading) {
				this.pageNum++
				this.loadCaseList()
			}
		},

		methods: {
			// 加载成功案例列表
			async loadCaseList() {
				if (this.loading) return
				this.loading = true

				try {
					console.log('尝试加载成功案例API数据...')
					const data = await api.successCase.getList({
						pageNum: this.pageNum,
						pageSize: this.pageSize
					})

					if (data && data.length > 0) {
						if (this.pageNum === 1) {
							// 第一页，替换数据
							this.caseList = data
						} else {
							// 后续页，追加数据
							this.caseList = [...this.caseList, ...data]
						}
						this.hasMore = data.length >= this.pageSize
						console.log('API数据加载成功:', data.length, '条')
					} else {
						this.hasMore = false
						console.log('API返回数据为空，使用默认数据')
					}
				} catch (error) {
					console.error('加载成功案例失败:', error)
					
					// API失败时，如果是第一页且当前列表为空，保留默认数据
					if (this.pageNum === 1 && this.caseList.length === 0) {
						console.log('API失败，保留默认数据')
						uni.showToast({
							title: '使用示例数据',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '加载失败',
							icon: 'none'
						})
					}
					this.hasMore = false
				} finally {
					this.loading = false
				}
			},

			// 格式化日期
            formatDate(dateStr) {
                if (!dateStr) return '未知日期'
                const date = this.$util && this.$util.parseDate ? this.$util.parseDate(dateStr) : new Date(String(dateStr).replace(/-/g,'/'))
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				return `${year}年${month}月${day}日`
			},

			// 跳转到详情
			goToDetail(caseNo) {
				console.log('跳转到案例详情, caseNo:', caseNo)
				uni.navigateTo({
					url: `/pages/success-case/detail?caseNo=${caseNo}`,
					success: () => {
						console.log('跳转成功')
					},
					fail: (err) => {
						console.error('跳转失败:', err)
						uni.showToast({
							title: '跳转失败',
							icon: 'none'
						})
					}
				})
			},

			// 返回
			goBack() {
				console.log('返回上一页')
				uni.navigateBack({
					fail: () => {
						// 返回失败时跳转首页
						uni.navigateTo({
							url: '/pages/index/index'
						})
					}
				})
			},
			
			// 数据健康检查
			checkDataHealth() {
				console.log('=== 成功案例列表页面数据健康检查 ===')
				console.log('caseList长度:', this.caseList.length)
				console.log('DEFAULT_IMAGES:', this.DEFAULT_IMAGES)
				console.log('第一个案例:', this.caseList[0])
				
				// 确保每个案例都有必要的字段
				this.caseList.forEach((item, index) => {
					if (!item.case_no) {
						item.case_no = `CASE00${index + 1}`
					}
					if (!item.image_url) {
						item.image_url = this.DEFAULT_IMAGES.couple
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.success-case-page {
		min-height: 100vh;
		background-color: #FFF9F9;
		padding-top: 90rpx;
	}

	/* 自定义导航栏 */
	.custom-navbar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		background-color: #E91E63;
		z-index: 999;

		.navbar-left,
		.navbar-right {
			width: 80rpx;
		}

		.back-icon {
			font-size: 40rpx;
			color: #FFFFFF;
			font-weight: bold;
		}

		.navbar-title {
			flex: 1;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
		}
	}

	/* 成功案例列表 */
	.case-list {
		padding: 20rpx 30rpx;

		.case-card {
			position: relative;
			margin-bottom: 20rpx;
			background-color: #FFFFFF;
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

			.case-image {
				width: 100%;
				height: 400rpx;
				background-color: #F5F5F5;
			}

			.case-info {
				padding: 30rpx;

				.case-names {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 15rpx;
				}

				.case-quote {
					font-size: 28rpx;
					color: #666666;
					line-height: 1.6;
					margin-bottom: 20rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}

				.case-meta {
					font-size: 24rpx;
					color: #999999;
					margin-bottom: 20rpx;
				}

				.case-btn {
					display: inline-block;
					padding: 10rpx 30rpx;
					background-color: #E91E63;
					color: #FFFFFF;
					border-radius: 30rpx;
					font-size: 24rpx;
				}
			}

			.success-badge {
				position: absolute;
				top: 20rpx;
				right: 20rpx;
				width: 60rpx;
				height: 60rpx;
				background-color: #4CAF50;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				box-shadow: 0 4rpx 12rpx rgba(76, 175, 80, 0.3);

				.badge-icon {
					color: #FFFFFF;
					font-size: 36rpx;
					font-weight: bold;
				}
			}
		}
	}

	/* 加载更多 */
	.load-more,
	.no-more {
		padding: 30rpx 0;
		text-align: center;

		.load-text,
		.no-more-text {
			font-size: 24rpx;
			color: #999999;
		}
	}
</style>


