<template>
	<view class="service-items-card">
		<view class="card-title">
			<text>服务项目及费用</text>
		</view>
		
		<!-- 服务项目列表 -->
		<view class="service-items-list">
			<view class="service-item" v-for="(item, index) in items" :key="index">
				<view class="item-header">
					<view class="item-id">{{item.id}}</view>
					<view class="item-name">{{item.name}}</view>
					<view class="item-price-action">
						<view class="item-price">¥{{item.price.toFixed(2)}}</view>
						<!-- 展开按钮 -->
						<view class="expand-btn" @click="toggleExpand(index)" v-if="item.pet || (item.customServices && item.customServices.length > 0)">
							<!-- <text>{{ expandedItems.includes(index) ? '收起' : '展开' }}</text> -->
							<view class="expand-icon" :class="{'expanded': expandedItems.includes(index)}">
								<text class="icon-arrow">{{ expandedItems.includes(index) ? '∧' : '∨' }}</text>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 详细信息区域 -->
				<view class="detail-area" v-show="expandedItems.includes(index)">
					<!-- 宠物名称和头像 -->
					<view class="item-pet" v-if="item.pet">
						<view class="pet-avatar">
							<image :src="item.petAvatar || '/static/images/default-pet.png'" mode="aspectFill"></image>
						</view>
						<text>{{item.pet}}</text>
					</view>
					
					<!-- 定制服务 -->
					<view class="custom-services" v-if="item.customServices && item.customServices.length > 0">
						<view class="custom-service-item" v-for="(service, serviceIndex) in item.customServices" :key="serviceIndex">
							<view class="service-name">{{service.name}}</view>
							<view class="service-price">¥{{service.price.toFixed(2)}} × {{service.quantity}} 次</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 费用合计 -->
		<view class="cost-summary">
			<view class="cost-item">
				<text class="cost-label">费用合计</text>
				<text class="cost-value">¥{{totalAmount.toFixed(2)}}</text>
			</view>
			<view class="cost-item discount" v-if="discount > 0">
				<text class="cost-label">平台优惠</text>
				<text class="cost-value">- ¥{{discount.toFixed(2)}}</text>
			</view>
			<view class="cost-item discount" v-if="memberDiscount > 0">
				<text class="cost-label">会员优惠</text>
				<text class="cost-value">- ¥{{memberDiscount.toFixed(2)}}</text>
			</view>
			<view class="cost-item total">
				<text class="cost-label">应付金额</text>
				<text class="cost-value">¥{{finalAmount.toFixed(2)}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			items: {
				type: Array,
				default: () => []
			},
			totalAmount: {
				type: Number,
				default: 0
			},
			discount: {
				type: Number,
				default: 0
			},
			memberDiscount: {
				type: Number,
				default: 0
			},
			finalAmount: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				expandedItems: [] // 存储已展开的项目索引
			}
		},
		methods: {
			toggleExpand(index) {
				const position = this.expandedItems.indexOf(index);
				if (position === -1) {
					// 如果不在数组中，则添加（展开）
					this.expandedItems.push(index);
				} else {
					// 如果在数组中，则移除（折叠）
					this.expandedItems.splice(position, 1);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.service-items-card {
		background-color: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.card-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		
		&::before {
			content: '';
			display: inline-block;
			width: 8rpx;
			height: 32rpx;
			background-color: #FFAA48;
			margin-right: 16rpx;
			border-radius: 4rpx;
		}
	}
	
	.service-items-list {
		.service-item {
			padding: 20rpx 0;
			border-bottom: 1px solid #EEEEEE;
			
			&:last-child {
				border-bottom: none;
			}
			
			.item-header {
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;
				
				.item-id {
					font-size: 24rpx;
					color: #999;
					margin-right: 10rpx;
				}
				
				.item-name {
					font-size: 28rpx;
					color: #333;
					flex: 1;
				}
				
				.item-price-action {
					display: flex;
					align-items: center;
					gap: 15rpx;
					
					.item-price {
						font-size: 28rpx;
						color: #FF5252;
						font-weight: bold;
					}
				}
			}
			
			// 展开按钮样式
			.expand-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				// padding: 6rpx 12rpx;
                width: 40rpx;
				height: 40rpx;
				font-size: 24rpx;
				color: #666;
				background-color: #FFF5E6;
				border-radius: 20rpx;
				border: 1px solid #FFAA48;
				
				.expand-icon {
					// margin-left: 6rpx;
					transition: transform 0.3s ease;
					
					&.expanded {
						transform: rotate(180deg);
					}
					
					.icon-arrow {
						font-size: 24rpx;
						color: #FFAA48;
					}
				}
			}
			
			// 详细信息区域
			.detail-area {
				padding: 15rpx;
				animation: fadeIn 0.3s ease;
				background-color: #F8F8F8;
				border-radius: 10rpx;
				margin-top: 10rpx;
			}
			
			.item-pet {
				font-size: 24rpx;
				color: #666;
				margin-bottom: 10rpx;
				display: flex;
				align-items: center;
				
				.pet-avatar {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					overflow: hidden;
					margin-right: 10rpx;
					border: 1px solid #EEEEEE;
					
					image {
						width: 100%;
						height: 100%;
					}
				}
			}
			
			.custom-services {
				padding: 10rpx 0 10rpx 20rpx;
				
				.custom-service-item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 6rpx;
					
					.service-name {
						font-size: 24rpx;
						color: #666;
					}
					
					.service-price {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}
	
	@keyframes fadeIn {
		from { opacity: 0; }
		to { opacity: 1; }
	}
	
	.cost-summary {
		margin-top: 30rpx;
		padding-top: 20rpx;
		border-top: 1px dashed #EEEEEE;
		
		.cost-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 10rpx;
			
			.cost-label {
				font-size: 26rpx;
				color: #666;
			}
			
			.cost-value {
				font-size: 26rpx;
				color: #333;
			}
			
			&.discount {
				.cost-value {
					color: #FF5252;
				}
			}
			
			&.total {
				margin-top: 20rpx;
				padding-top: 20rpx;
				border-top: 1px dashed #EEEEEE;
				
				.cost-label {
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
				}
				
				.cost-value {
					font-size: 32rpx;
					font-weight: bold;
					color: #FF5252;
				}
			}
		}
	}
</style>