<!--家长端订单管理页面-->
<template>
	<view class="main_box">
		<!-- 选中孩子信息 -->
		<view class="child_info_bar" v-if="selectedChild">
			<view class="child_avatar">
				<image :src="selectedChild.avatar" mode="aspectFill"></image>
			</view>
			<view class="child_details">
				<view class="child_name">{{ selectedChild.name }}</view>
				<view class="child_class">{{ selectedChild.class_name || '未分配班级' }}</view>
			</view>
		</view>
		
		<!-- 订单列表 -->
		<view class="order_list">
			<view class="section_title">订单列表</view>
			<view class="order_items">
				<view 
					v-for="order in orderList" 
					:key="order.id"
					class="order_item"
					@click="viewOrderDetail(order)"
				>
					<view class="order_main">
						<view class="order_header">
							<view class="order_no">订单号：{{ order.order_no }}</view>
							<view class="order_status" :class="order.status">{{ order.status_text }}</view>
						</view>
						<view class="order_details">
							<view class="detail_row">
								<text class="detail_label">课程名称：</text>
								<text class="detail_value">{{ order.course_name }}</text>
							</view>
							<view class="detail_row">
								<text class="detail_label">订单金额：</text>
								<text class="detail_value amount">¥{{ order.amount }}</text>
							</view>
							<view class="detail_row">
								<text class="detail_label">下单时间：</text>
								<text class="detail_value">{{ order.created_at }}</text>
							</view>
							<view class="detail_row" v-if="order.pay_time">
								<text class="detail_label">支付时间：</text>
								<text class="detail_value">{{ order.pay_time }}</text>
							</view>
						</view>
					</view>
					<view class="order_arrow">
						<image :src="$util.img('/uniapp_src/static/images/index/right_arrow.png')" class="arrow-icon"></image>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 空状态 -->
		<view class="empty_state" v-if="!loading && orderList.length === 0">
			<image :src="$util.img('/uniapp_src/static/images/common/empty.png')" class="empty_icon"></image>
			<view class="empty_text">暂无订单信息</view>
		</view>
		
		<!-- 加载状态 -->
		<view class="loading_state" v-if="loading">
			<view class="loading_text">加载中...</view>
		</view>
	</view>
</template>

<script>
import { mapState } from 'vuex'
import apiRoute from '@/api/apiRoute.js'

export default {
	data() {
		return {
			orderList: [],
			loading: false,
			childId: null
		}
	},
	computed: {
		...mapState(['selectedChild'])
	},
	onLoad(options) {
		this.childId = options.childId
		this.loadOrderList()
	},
	methods: {
		async loadOrderList() {
			if (!this.childId) {
				uni.showToast({
					title: '缺少孩子ID参数',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			try {
				const response = await apiRoute.parent_getChildOrders({
					child_id: this.childId
				})
				
				if (response.code === 1) {
					this.orderList = response.data.data || []
				} else {
					uni.showToast({
						title: response.msg || '获取订单列表失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('获取订单列表失败:', error)
				uni.showToast({
					title: '获取订单列表失败',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},
		
		viewOrderDetail(order) {
			this.$navigateTo({
				url: `/pages/parent/orders/order-detail?orderId=${order.id}&childId=${this.childId}`
			})
		}
	}
}
</script>

<style lang="less" scoped>
.main_box {
	background: #f8f9fa;
	min-height: 100vh;
	padding: 20rpx;
}

.child_info_bar {
	background: #fff;
	border-radius: 16rpx;
	padding: 24rpx;
	margin-bottom: 20rpx;
	display: flex;
	align-items: center;
	gap: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	
	.child_avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		overflow: hidden;
		
		image {
			width: 100%;
			height: 100%;
		}
	}
	
	.child_details {
		flex: 1;
		
		.child_name {
			font-size: 28rpx;
			font-weight: 600;
			color: #333;
			margin-bottom: 8rpx;
		}
		
		.child_class {
			font-size: 24rpx;
			color: #666;
		}
	}
}

.order_list {
	.section_title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 24rpx;
		padding-left: 8rpx;
	}
	
	.order_items {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
	}
}

.order_item {
	background: #fff;
	border-radius: 16rpx;
	padding: 28rpx;
	display: flex;
	align-items: center;
	gap: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	
	.order_main {
		flex: 1;
		
		.order_header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 16rpx;
			
			.order_no {
				font-size: 26rpx;
				font-weight: 600;
				color: #333;
			}
			
			.order_status {
				font-size: 22rpx;
				padding: 4rpx 12rpx;
				border-radius: 12rpx;
				
				&.paid {
					background: rgba(40, 167, 69, 0.1);
					color: #28a745;
				}
				
				&.unpaid {
					background: rgba(220, 53, 69, 0.1);
					color: #dc3545;
				}
				
				&.refund {
					background: rgba(108, 117, 125, 0.1);
					color: #6c757d;
				}
			}
		}
		
		.order_details {
			.detail_row {
				display: flex;
				margin-bottom: 8rpx;
				
				.detail_label {
					font-size: 24rpx;
					color: #666;
					min-width: 160rpx;
				}
				
				.detail_value {
					font-size: 24rpx;
					color: #333;
					flex: 1;
					
					&.amount {
						color: #e67e22;
						font-weight: 600;
					}
				}
			}
		}
	}
	
	.order_arrow {
		.arrow-icon {
			width: 24rpx;
			height: 24rpx;
			opacity: 0.4;
		}
	}
}

.empty_state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx 0;
	
	.empty_icon {
		width: 160rpx;
		height: 160rpx;
		margin-bottom: 32rpx;
		opacity: 0.3;
	}
	
	.empty_text {
		font-size: 28rpx;
		color: #999;
	}
}

.loading_state {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 60rpx 0;
	
	.loading_text {
		font-size: 28rpx;
		color: #666;
	}
}
</style>