<template>
	<view class="all-trips">
		<!-- 顶部导航栏 -->
		<view class="navbar">
			<view class="back-btn" @tap="goBack">
				<text class="back-icon">←</text>
				<text class="back-text">返回</text>
			</view>
			<view class="title">全部行程</view>
		</view>
		
		<view class="container">
			<!-- 行程卡片列表 -->
			<view class="trip-list">
				<!-- 显示所有已保存的行程 -->
				<view 
					v-for="(trip, index) in savedTrips" 
					:key="trip.id" 
					class="trip-card" 
					hover-class="card-hover" 
					@tap="viewTripDetail(trip.id)"
				>
					<!-- 添加景区图片背景 -->
					<image 
						class="trip-background-image" 
						:src="getTripCoverImage(trip)" 
						mode="aspectFill"
					></image>
					<view class="trip-overlay"></view>
					
					<view class="trip-info">
						<view class="trip-location">
							<image class="location-icon" src="/static/location_icon.png"></image>
							<view class="trip-main-info">
								<text class="trip-name">{{trip.name}}</text>
								<text v-if="trip.startDate" class="trip-date">{{formatDate(trip.startDate)}}</text>
								<view v-else class="trip-date-wrap">
									<text class="trip-date-label">出发时间</text>
									<text class="trip-date-value">待定</text>
								</view>
							</view>
						</view>

						<view class="trip-members">
							<image class="member-icon" src="/static/member_icon.png"></image>
							<text class="member-count">共{{trip.peopleCount || 1}}名成员</text>
						</view>
					</view>

					<view class="trip-stats">
						<view class="trip-tag">
							<text class="tag-title">THE TRIP</text>
							<text class="tag-subtitle">MUST GO ON</text>
							<image class="globe-icon" src="/static/globe_icon.png"></image>
						</view>
						<view class="trip-divider"></view>
						<view class="trip-duration">
							<text class="stat-label">时长：</text>
							<text class="stat-value">{{trip.days}}天{{trip.days - 1}}晚</text>
						</view>
						<view v-if="trip.budget" class="trip-distance">
							<text class="stat-label">预算：</text>
							<text class="stat-value">¥{{trip.budget}}</text>
						</view>
					</view>

					<view class="card-shadow"></view>
				</view>
				
				<!-- 没有行程时显示的提示 -->
				<view class="empty-trip" v-if="savedTrips.length === 0">
					<view class="empty-icon">🗺️</view>
					<view class="empty-text">您还没有创建任何行程</view>
					<view class="empty-subtext">点击上方选项开始规划您的旅行</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			savedTrips: [],
			defaultImages: [
				'/static/baoding.jpg',
				'/static/custom_plan_icon.png',
				'/static/beijing.jpg',
				'/static/chengdu.jpg'
			]
		}
	},
	
	onLoad() {
		// 加载保存的行程数据
		this.loadSavedTrips();
	},
	
	methods: {
		// 加载保存的行程数据
		loadSavedTrips() {
			try {
				const trips = uni.getStorageSync('savedTrips') || [];
				console.log('已加载保存的行程:', trips.length);
				this.savedTrips = trips;
			} catch (e) {
				console.error('加载保存的行程失败:', e);
				this.savedTrips = [];
			}
		},
		
		// 获取行程封面图片
		getTripCoverImage(trip) {
			// 如果行程有spots并且第一个spot有图片，则使用该图片
			if (trip.spots && trip.spots.length > 0 && trip.spots[0].coverImage) {
				return trip.spots[0].coverImage;
			}
			
			// 根据行程名称选择默认图片
			if (trip.name.includes('保定')) {
				return '/static/baoding.jpg';
			} else if (trip.name.includes('西安')) {
				return '/static/xian.jpg';
			} else if (trip.name.includes('北京')) {
				return '/static/beijing.jpg';
			} else if (trip.name.includes('上海')) {
				return '/static/shanghai.jpg';
			}
			
			// 默认使用景点图片数组中的一张
			const hash = trip.id.split('_')[1] || Date.now();
			const index = hash % this.defaultImages.length;
			
			return this.defaultImages[index];
		},
		
		// 格式化日期显示
		formatDate(dateString) {
			if (!dateString) return '';
			const date = new Date(dateString);
			return `${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()}`;
		},
		
		// 查看行程详情
		viewTripDetail(id) {
			uni.navigateTo({
				url: `/pages/travel-detail/index?planId=${id}`,
				fail: (err) => {
					console.error('跳转到行程详情页失败:', err);
				}
			});
		},
		
		// 返回上一页
		goBack() {
			uni.navigateBack();
		}
	}
}
</script>

<style>
.all-trips {
	min-height: 100vh;
	background: linear-gradient(to bottom, #dbf0ff 0%, #e3f4ff 40%, #f0f9ff 100%);
	padding-bottom: 30rpx;
}

.navbar {
	display: flex;
	align-items: center;
	height: 90rpx;
	padding: 0 30rpx;
	background-color: rgba(255, 255, 255, 0.8);
	border-bottom: 1rpx solid #eaeaea;
	position: relative;
}

.back-btn {
	display: flex;
	align-items: center;
	position: absolute;
	left: 30rpx;
}

.back-icon {
	font-size: 40rpx;
}

.back-text {
	font-size: 28rpx;
	margin-left: 6rpx;
}

.navbar .title {
	flex: 1;
	text-align: center;
	font-size: 32rpx;
	font-weight: 500;
}

.container {
	padding: 20rpx;
}

/* 行程卡片样式 */
.trip-list {
	display: flex;
	flex-direction: column;
	gap: 30rpx;
	margin-top: 15rpx;
}

.trip-card {
	background-color: #fff;
	border-radius: 20rpx;
	display: flex;
	overflow: hidden;
	position: relative;
	box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
	transition: all 0.3s ease;
}

/* 行程卡片背景图片 */
.trip-background-image {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	object-fit: cover;
	z-index: 1;
}

.trip-overlay {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: linear-gradient(to right, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0.3) 100%);
	z-index: 2;
}

.card-shadow {
	position: absolute;
	bottom: 0;
	left: 0;
	right: 0;
	height: 8rpx;
	background: linear-gradient(to right, #4facfe, #00f2fe);
	border-radius: 0 0 20rpx 20rpx;
	z-index: 5;
}

.trip-info {
	flex: 1;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	position: relative;
	z-index: 3;
}

.trip-location {
	display: flex;
	align-items: flex-start;
	margin-bottom: 30rpx;
}

.location-icon {
	width: 40rpx;
	height: 40rpx;
	margin-right: 15rpx;
	margin-top: 6rpx;
}

.trip-main-info {
	flex: 1;
}

/* 修改文本颜色以适应图片背景 */
.trip-name {
	font-size: 32rpx;
	font-weight: bold;
	color: #ffffff;
	margin-bottom: 15rpx;
	line-height: 1.4;
	text-shadow: 1rpx 1rpx 3rpx rgba(0, 0, 0, 0.5);
}

.trip-date {
	font-size: 28rpx;
	color: #f0f0f0;
	text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.5);
}

.trip-date-wrap {
	display: flex;
	align-items: center;
}

.trip-date-label {
	font-size: 26rpx;
	color: #e0e0e0;
	margin-right: 10rpx;
	text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.5);
}

.trip-date-value {
	font-size: 26rpx;
	color: #ffffff;
	background-color: rgba(0, 0, 0, 0.3);
	padding: 4rpx 16rpx;
	border-radius: 100rpx;
}

.trip-members {
	display: flex;
	align-items: center;
}

.member-icon {
	width: 40rpx;
	height: 40rpx;
	margin-right: 15rpx;
}

.member-count {
	font-size: 26rpx;
	color: #f0f0f0;
	text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.5);
}

.trip-stats {
	width: 200rpx;
	background: linear-gradient(to bottom, rgba(182, 230, 255, 0.9), rgba(132, 212, 255, 0.9));
	padding: 30rpx 20rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	position: relative;
	z-index: 3;
}

.trip-tag {
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-bottom: 20rpx;
}

.tag-title {
	font-size: 24rpx;
	font-weight: bold;
	color: #333;
}

.tag-subtitle {
	font-size: 24rpx;
	font-weight: bold;
	color: #333;
}

.globe-icon {
	width: 40rpx;
	height: 40rpx;
	margin-top: 10rpx;
}

.trip-divider {
	width: 80%;
	height: 2rpx;
	background-color: rgba(51, 51, 51, 0.2);
	margin: 15rpx 0;
}

.trip-duration,
.trip-distance {
	width: 100%;
	display: flex;
	flex-direction: column;
	align-items: flex-end;
	margin-top: 10rpx;
}

.stat-label {
	font-size: 24rpx;
	color: #333;
	text-align: right;
}

.stat-value {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	text-align: right;
}

/* 空行程状态样式 */
.empty-trip {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	background-color: #f9f9f9;
	border-radius: 20rpx;
	padding: 60rpx 0;
	box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.05);
}

.empty-icon {
	font-size: 80rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 32rpx;
	color: #666;
	margin-bottom: 10rpx;
}

.empty-subtext {
	font-size: 26rpx;
	color: #999;
}
</style> 