<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.day}}</view>
                    <text style="margin: 0 10rpx;">|</text>
					<view class="item-name">
                        <text>{{ item.itemsText[0] }}</text>
                        <text v-if="item.itemsText.length >= 2">
                            {{ item.itemsText[0] }}+{{ item.itemsText[item.itemsText.length - 1] }}{{ item.itemsText.length }}项
                        </text>
                    </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.pets && item.pets.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-if="expandedItems.includes(index)">
					<view class="detail-area-item" v-for="(pet, petIndex) in item.pets" :key="petIndex">
                        <!-- 宠物名称和头像 -->
                        <view class="item-pet" v-if="pet">
                            <view class="pet-avatar">
                                <image :src="pet.photo" mode="aspectFill"></image>
                            </view>
                            <text>{{pet.name}}</text>
                        </view>
                        
                        <!-- 定制服务 -->
                        <view class="custom-services-list" v-if="pet.itemList && pet.itemList.length > 0">
                            <view class="custom-service-item" v-for="(t, itemIndex) in pet.itemList" :key="itemIndex">
                                <view class="service-name">{{t.productName}}</view>
                                <view class="service-price">¥{{ t.salePrice && t.salePrice.toFixed(2) }} × {{t.quantity}} 次</view>
                            </view>
                        </view>
                    </view>
				</view>
			</view>
		</view>
		
		<!-- 费用合计 -->
		<view class="cost-summary" v-if="totalAmount">
			<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 {
		padding: 30rpx 10rpx;
		padding-bottom: 0;
	}
	
	.card-title {
		font-size: 30rpx;
		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: 26rpx;
					margin-right: 10rpx;
				}
				
				.item-name {
					font-size: 26rpx;
					color: #333;
					flex: 1;
                    display: flex;
				}
				
				.item-price-action {
					display: flex;
					align-items: center;
					gap: 15rpx;
					
					.item-price {
						font-size: 28rpx;
						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 {
				animation: fadeIn 0.3s ease;
                display: flex;
                flex-direction: column;
                gap: 10rpx;
                .detail-area-item{
                    background-color: #F8F8F8;
                    border-radius: 10rpx;
                    margin-top: 10rpx;
                    padding: 15rpx;
                }
                .item-pet {
                    font-size: 28rpx;
                    color: #333;
                    margin-bottom: 20rpx;
                    display: flex;
                    align-items: center;
                    
                    .pet-avatar {
                        width: 80rpx;
                        height: 80rpx;
                        border-radius: 50%;
                        margin-right: 20rpx;
                        
                        image {
                            width: 100%;
                            height: 100%;
                            border-radius: 50%;
                        }
                    }
                    
                    text {
                        font-weight: 500;
                        font-size: 32rpx;
                    }
                }
                
                .custom-services-list {
                    .custom-service-item {
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        padding: 15rpx 0;
                        border-bottom: 1px solid #F0F0F0;
                        
                        &:last-child {
                            border-bottom: none;
                        }
                        
                        .service-name {
                            font-size: 28rpx;
                            color: #333;
                            font-weight: 400;
                        }
                        
                        .service-price {
                            font-size: 28rpx;
                            color: #999;
                            font-weight: 400;
                        }
                    }
                }
			}
			
			
			.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>