<!--家长端合同管理页面-->
<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="contracts_list">
			<view class="section_title">合同列表</view>
			<view class="contracts_items">
				<view 
					v-for="contract in contractsList" 
					:key="contract.id"
					class="contract_item"
					@click="viewContractDetail(contract)"
				>
					<view class="contract_main">
						<view class="contract_header">
							<view class="contract_title">{{ contract.title }}</view>
							<view class="contract_status" :class="contract.status">{{ contract.status_text }}</view>
						</view>
						<view class="contract_details">
							<view class="detail_row">
								<text class="detail_label">合同金额：</text>
								<text class="detail_value amount">¥{{ contract.amount }}</text>
							</view>
							<view class="detail_row">
								<text class="detail_label">签订日期：</text>
								<text class="detail_value">{{ contract.sign_date }}</text>
							</view>
							<view class="detail_row">
								<text class="detail_label">有效期：</text>
								<text class="detail_value">{{ contract.valid_date }}</text>
							</view>
						</view>
					</view>
					<view class="contract_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 && contractsList.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 {
			contractsList: [],
			loading: false,
			childId: null
		}
	},
	computed: {
		...mapState(['selectedChild'])
	},
	onLoad(options) {
		this.childId = options.childId
		this.loadContractsList()
	},
	methods: {
		async loadContractsList() {
			if (!this.childId) {
				uni.showToast({
					title: '缺少孩子ID参数',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			try {
				const response = await apiRoute.parent_getChildContracts({
					child_id: this.childId
				})
				
				if (response.code === 1) {
					this.contractsList = 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
			}
		},
		
		viewContractDetail(contract) {
			this.$navigateTo({
				url: `/pages/parent/contracts/contract-detail?contractId=${contract.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;
		}
	}
}

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

.contract_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);
	
	.contract_main {
		flex: 1;
		
		.contract_header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 16rpx;
			
			.contract_title {
				font-size: 30rpx;
				font-weight: 600;
				color: #333;
			}
			
			.contract_status {
				font-size: 22rpx;
				padding: 4rpx 12rpx;
				border-radius: 12rpx;
				
				&.active {
					background: rgba(40, 167, 69, 0.1);
					color: #28a745;
				}
				
				&.expired {
					background: rgba(220, 53, 69, 0.1);
					color: #dc3545;
				}
			}
		}
		
		.contract_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;
					}
				}
			}
		}
	}
	
	.contract_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>