<template>
	<view class="report-card" style="background-image: url(/static/chanjianTool/bg1.png);">
		<view class="page-top">
			<image @tap="backHandler" class="btnback" src="/static/chanjianTool/back.png"></image>
			<view class="page_title">我的报告单</view>
		</view>
		<view class="report-card-con">
			<view class="con-box">
				<view class="list-item" v-for="(item, index) in listData" :key="index">
					<view class="item-time">
						<view class="item-time-l">
							<image src="/static/chanjianTool/icon19.png"></image>
							{{item.checkupDate}}
						</view>

						<view class="item-time-r" @click="onDelete(item.id)">
							<image src="/static/chanjianTool/delete.png"></image>
						</view>
					</view>
					<view class="item-line">

					</view>
					<view class="item-content">
						<view class="content-1">
							第{{item.checkupTimes}}次产检:孕{{item.pregnancyCycle}}周
						</view>
						<view class="content-2">
							重点: {{getProject(item.items)}}
						</view>
						<view class="content-3">
							<template v-for="(e, i) in item.reportImages.slice(0, 3)" :key="i">
								<view class="content-3-v" :class="{ 'has-more': i === 2 && item.reportImages.length > 3 }">
									<image @click="onPreviewImage(e)" :src="e" mode="widthFix"></image>
									<view @click="onMoreImage(item.reportImages)" class="more-count"
										v-if="i === 2 && item.reportImages.length > 3">
										+{{ item.reportImages.length - 3 }}
									</view>
								</view>
							</template>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 查看更多图片 -->
		<view class="more-popup" v-if="showMore">
			<view class="more-popup-list">
				<view class="title">
					我的报告单
				</view>
				<view class="more-image">
					<view v-for="(item, index) in imageListData" :key="index" class="more-image-1"
						@click="onPreviewImage(item)">
						<image :src="item"></image>
					</view>
				</view>
			</view>
			<view class="close-btn-bottom" @tap="onClose">
				<image src="/static/chanjianTool/icon21.png"></image>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		throttleTap
	} from '@/utils/index.js';
	import {
		getReportList,
		getDelete
	} from '../../api/obstetric.js';
	import md from '../../md';

	// 控制查看更多图片
	const showMore = ref(false)

	// 存储查看的图片数组
	const imageListData = ref([])
	
	// 报告单数据
	const listData = ref([])
	
	// 查看更多图片
	const onMoreImage = (item) => {
		console.log(item)
		showMore.value = true
		imageListData.value = item
	}
	// 关闭
	const onClose = () => {
		showMore.value = false;
	}
	// 图片预览
	const onPreviewImage = (url) => {
		console.log(url)
		uni.previewImage({
			current: '1',
			urls: [url]
		})
	}
	// 拼接检查项目名称
	const getProject = (projects) => {
		return projects.map(project => project.itemName).join('、');
	}
	// 删除事件
	const onDelete = throttleTap((id) => {
		console.log(id)
		uni.showModal({
			// title: '提示',
			content: '确认删除吗？',
			success: function (res) {
				if (res.confirm) {
					getDeleteFn(id)
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		// getDeleteFn(id)
	})
	// 返回上一页面
	const backHandler = () => {
		uni.navigateBack();
	}
  // 删除报告单
	const getDeleteFn = async (id) => {
		// const { code, message, data, success } = await getDelete({id})
		const { code, message, data, success } = {
			code: 200,
			message: '成功',
			success: true,
			data: {}
		}
		if (success) {
			uni.showToast({
				title: '删除成功',
				icon: 'none'
			})
			// 我的报告单
			getReportListFn()
		} else {
			uni.showToast({
				title: message,
				icon: 'none'
			})
		}
	}
	// 获取报告单
	const getReportListFn = async () => {
		console.log('获取报告单')
		// const { code, message, data, success } = await getReportList()
		const {
			code,
			message,
			data,
			success
		} = {
			code: 200,
			message: '成功',
			success: true,
			data: [{
					id: 1,
					checkupDate: '2025-07-09',
					checkupTimes: '一', // 产检次数
					pregnancyCycle: '5-6', // 周数
					// 产检项目
					items: [{
							itemName: '测量胎儿颈部透明层厚度（NT)',
							id: 1
						},
						{
							itemName: '无创产前基因检测(NIPT)(非必查)',
							id: 2
						}
					],
					reportImages: [
						'https://momclub-picture-1253290912.cos.ap-beijing.myqcloud.com/xmh-mini-program/user/image/2025/07/17/xmh-mini-program_1752739702325_167279.jpeg',	
						'https://momclub-picture-1253290912.cos.ap-beijing.myqcloud.com/xmh-mini-program/user/image/2025/07/17/xmh-mini-program_1752739702325_167279.jpeg',
						'https://momclub-picture-1253290912.cos.ap-beijing.myqcloud.com/xmh-mini-program/user/image/2025/07/17/xmh-mini-program_1752739702325_167279.jpeg',
						'https://momclub-picture-1253290912.cos.ap-beijing.myqcloud.com/xmh-mini-program/user/image/2025/07/17/xmh-mini-program_1752739702325_167279.jpeg'
					]
				}
			]
		}
		if (success) {
			listData.value = data
		} else {
			uni.showToast({
				title: message,
				icon: 'none'
			})
		}
	}

	onLoad(() => {
		// 我的报告单
		getReportListFn()
	})
</script>

<style lang="less" scoped>
	.report-card {
		// position: absolute;
		width: 100%;
		height: 100vh;
		background-size: 100% 100%;

		&-con {
			position: absolute;
			// width: 750rpx;
			top: 190rpx;
			left: 31rpx;

			.con-box {
				width: 682rpx;
				height: calc(100vh - 220rpx);
				overflow-y: auto;
			}

			.list-item {
				width: 682rpx;
				border-radius: 24rpx;
				background: #ffffff;
				padding: 35rpx 34rpx 45rpx 34rpx;
				box-sizing: border-box;
				margin-bottom: 30rpx;

				.item-time {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.item-time-l {
						display: flex;
						align-items: center;
						font-weight: 600;
						font-size: 32rpx;

						image {
							width: 32rpx;
							height: 32rpx;
							margin-right: 15rpx;
						}
					}

					.item-time-r {
						image {
							width: 31rpx;
							height: 31rpx;
						}
					}
				}

				.item-line {
					width: 100%;
					height: 1rpx;
					margin: 30rpx 0;
					background-image: url(/static/chanjianTool/icon20.png);
				}

				.item-content {

					.content-1,
					.content-2 {
						font-size: 26rpx;
						font-weight: 600;
					}

					.content-2 {
						margin-top: 15rpx;
					}

					.content-3 {
						display: flex;
						flex-wrap: wrap;
						margin-top: 35rpx;

						.content-3-v {
							width: 192rpx;
							height: 192rpx;
							border-radius: 10rpx;
							overflow: hidden;
							margin-right: 13rpx;
							position: relative;
						}

						.content-3-v.has-more image {
							opacity: 0.7;
						}

						.more-count {
							position: absolute;
							top: 0;
							left: 0;
							background-color: rgba(0, 0, 0, 0.5);
							color: white;
							width: 192rpx;
							height: 192rpx;
							border-radius: 10rpx;
							font-size: 38rpx;
							display: flex;
							justify-content: center;
							align-items: center;
						}

						image {
							width: 192rpx;
							height: 192rpx;
						}
					}
				}
			}
		}

		// 自定义头部
		.page-top {
			width: 100%;
		}

		.btnback {
			width: 16rpx;
			height: 29rpx;
			margin-top: 110rpx;
			margin-left: 35rpx;
		}

		.page_title {
			width: 100%;
			font-size: 34rpx;
			font-weight: 500;
			text-align: center;
			color: #1d1e25;
			margin-top: -46rpx;
			margin-left: 0rpx;
		}

		.more-popup {
			position: fixed;
			left: 0;
			top: 0;
			right: 0;
			bottom: 0;
			background: rgba(0, 0, 0, 0.7);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			z-index: 9999;

			.more-popup-list {
				width: 661rpx;
				height: 881rpx;
				background: #ffffff;
				border-radius: 52rpx;
				padding: 50rpx 58rpx;
				box-sizing: border-box;


				.title {
					text-align: center;
					font-size: 36rpx;
					color: #b27c1e;
					margin-bottom: 45rpx;
				}

				.more-image {
					width: 100%;
					height: 695rpx;
					overflow: auto;
					display: flex;
					flex-wrap: wrap;
					align-content: flex-start;
				}

				.more-image-1 {
					width: 169rpx;
					height: 169rpx;
					border-radius: 10rpx;
					overflow: hidden;
					margin-right: 12rpx;
					margin-bottom: 30rpx;
					flex-shrink: 0;

					image {
						width: 100%;
						height: 100%;
						/* 新增：确保图片填充模式与父容器一致 */
						object-fit: cover;
						/* 可选：加载时避免闪烁 */
						display: block;
					}
				}
			}
		}

		.close-btn-bottom {
			margin: 40rpx auto 0 auto;
			width: 70rpx;
			height: 70rpx;
			text-align: center;
			z-index: 10001;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 70rpx;
				height: 70rpx;
			}
		}
	}
</style>