<template>
	<view class="container">
		<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
		<view class="top">
			<image class="return" @click="goBack" src="/static/image/back_white.png"></image>
			<text>任务详情</text>
		</view>
		<scroll-view class="content" scroll-y>
			<!-- 任务卡片 -->
			<view class="task-card">
				<view class="card-header">
					<view class="card-title">{{ taskData.title }}</view>
				</view>
				<view class="card-meta">
					<view class="meta-tag">{{ taskData.tag }}</view>
					<view class="meta-id">ID：{{ taskData.id }}</view>
				</view>
			</view>

			<!-- 任务信息 -->
			<view class="info-section">
				<view class="section-header">
					<view class="section-indicator"></view>
					<text class="section-title">任务信息</text>
				</view>
				<view class="info-list">
					<view class="info-item">
						<text class="info-label">费用分摊:</text>
						<text class="info-value">{{ taskInfo.costSharing }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">归属项目:</text>
						<text class="info-value">{{ taskInfo.belongProject }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">归属部门:</text>
						<text class="info-value">{{ taskInfo.belongDept }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">归属产品:</text>
						<text class="info-value">{{ taskInfo.belongProduct }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">申请补充直播任务:</text>
						<text class="info-value">{{ taskInfo.applySupplementLive }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">补充任务直播间ID:</text>
						<text class="info-value">{{ taskInfo.supplementRoomId }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">申请补充任务原因:</text>
						<text class="info-value">{{ taskInfo.supplementReason }}</text>
					</view>
				</view>
			</view>

			<!-- 内容信息 -->
			<view class="info-section">
				<view class="section-header">
					<view class="section-indicator"></view>
					<text class="section-title">内容信息</text>
				</view>
				<view class="info-list">
					<view class="info-item">
						<text class="info-label">交付物名称:</text>
						<text class="info-value">{{ contentInfo.deliverableName }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">交付物ID:</text>
						<text class="info-value">{{ contentInfo.deliverableId }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">直播间地址:</text>
						<text class="info-value">{{ contentInfo.liveRoomUrl }}</text>
						<text class="copy-btn" @click="copyToClipboard(contentInfo.liveRoomUrl)">复制</text>
					</view>
					<view class="info-item">
						<text class="info-label">推流地址:</text>
						<text class="info-value">{{ contentInfo.pushUrl }}</text>
						<text class="copy-btn" @click="copyToClipboard(contentInfo.pushUrl)">复制</text>
					</view>
					<view class="info-item">
						<text class="info-label">开播地址:</text>
						<text class="info-value">{{ contentInfo.startUrl }}</text>
						<text class="copy-btn" @click="copyToClipboard(contentInfo.startUrl)">复制</text>
					</view>
					<view class="info-item">
						<text class="info-label">直播回放:</text>
						<text class="info-value">{{ contentInfo.liveReplay }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">播放视频:</text>
						<text class="info-value">{{ contentInfo.playVideo }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">下载视频:</text>
						<text class="info-value">{{ contentInfo.downloadVideo }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">观众人数:</text>
						<text class="info-value">{{ contentInfo.viewerCount }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">有效观看:</text>
						<text class="info-value">{{ contentInfo.validView }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">情况说明:</text>
						<text class="info-value">{{ contentInfo.description }}</text>
					</view>
				</view>
			</view>

			<!-- 客户信息 -->
			<view class="info-section">
				<view class="section-header">
					<view class="section-indicator"></view>
					<text class="section-title">客户信息</text>
				</view>
				<view class="client-list">
					<view class="client-item" v-for="(item, index) in clientList" :key="index">
						<view class="client-info">
							<image class="avatar" src="/static/image/my_heads_icon.png"></image>
							<view class="client-txt">
								<view class="client-name">
									{{ item.name }}
									<text class="client-level">{{ item.level }}</text>
								</view>
								<view class="client-hospital">
									<text>{{ item.hospital }} </text>
									<view class="line"></view>
									<text>{{ item.department }}</text>
								</view>
							</view>
						</view>
						<view class="client-stats">
							<view class="stat-item"><text class="num">{{ item.taskCount }} </text>任务</view>
							<view class="stat-item"><text class="num">{{ item.points }} </text>积分</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 补充材料 -->
			<view class="info-section">
				<view class="section-header">
					<view class="section-indicator"></view>
					<text class="section-title">补充材料</text>
				</view>
				<view class="info-list">
					<view class="info-item">
						<text class="info-label">交付物名称:</text>
						<text class="info-value">{{ supplementMaterial.deliverableName }}</text>
					</view>
					<view class="info-item">
						<text class="info-label">情况说明:</text>
						<text class="info-value">{{ supplementMaterial.description }}</text>
					</view>
					<view class="info-item column">
						<text class="info-label">附件:</text>
						<view class="attachment-item" v-if="supplementMaterial.attachment.name">
							<view class="left">
								<image class="img" src="/static/image/icon_attachment.png"></image>
								<view class="txt-item">
									<view class="attachment-name">{{ supplementMaterial.attachment.name }}</view>
									<view class="attachment-size">{{ supplementMaterial.attachment.size }}</view>
								</view>
							</view>
							<image class="download-btn" @click="downloadAttachment"
								src="/static/image/icon_downlad.png"></image>
						</view>
					</view>
				</view>
			</view>

			<!-- 审批信息 -->
			<view class="info-section">
				<view class="section-header">
					<view class="section-indicator"></view>
					<text class="section-title">审批信息</text>
				</view>
				<view class="approval-list">
					<view class="approval-item" v-for="(item, index) in approvalInfo" :key="index">
						<view class="left">
							<view class="avatar-box">
								<image class="avatar" src="/static/image/my_heads_icon.png"></image>
								<image class="icon" v-if="item.status==2" src="/static/image/icon_wait.png"></image>
								<image class="icon" v-if="item.status==0" src="/static/image/icon_pass.png"></image>
								<image class="icon" v-if="item.status==1" src="/static/image/icon_refuse.png"></image>

							</view>
							<view class="approval-user">
								<view class="user-name">{{ item.name }}</view>
								<view class="user-status" :style="{ color: item.statusColor }">{{ item.status }}</view>
							</view>
						</view>
						<text class="approval-time">{{ item.time }}</text>
						<view class="approval-line" v-if="index < approvalInfo.length - 1"></view>
					</view>

				</view>
				<view class="reason" v-if="item.status==3">
					费用分摊部门填写错误，应该是学术研究事业部，请修改后重新提交。
				</view>

			</view>
		</scroll-view>
	</view>
</template>

<script>
	import { getTaskFinishAuditInfo} from '@/api/task.js';

	export default {
		data() {
			return {
				// 仅获取微信小程序状态栏高度，无多余逻辑
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				taskId: '',
				// 任务基础信息
				taskData: {
				
				},
				// 任务信息
				taskInfo: {
				
				},
				// 内容信息
				contentInfo: {
				
				},
				// 客户信息列表
				clientList: [
				],
				// 补充材料
				supplementMaterial: {
					deliverableName: '-',
					description: '-',
					attachment: {
					
					}
				},
				// 审批信息
				approvalInfo: []
			}
		},
		onLoad(options) {
			if (options.taskId) {
				this.taskId = options.taskId
				this.loadData()
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			async loadData() {
				try {
					uni.showLoading({
						title: '加载中...'
					})
					// 调用接口获取任务详情
					const res = await getTaskFinishAuditInfo({ taskId: this.taskId })
					uni.hideLoading()
					if (res.code === 200) {
						// 处理返回的数据
						const data = res.data
						// 更新任务基础信息
						this.taskData = {
							title: data.auditTaskInfoVO?.taskName || '任务名称',
							tag: data.auditTaskInfoVO?.taskTypeName || '任务类型',
							id: data.auditTaskInfoVO?.taskNo || ''
						}
						// 更新任务信息
						this.taskInfo = {
							costSharing: data.auditTaskInfoVO?.costShareName || '-',
							belongProject: data.auditTaskInfoVO?.projectName || '-',
							belongDept: data.auditTaskInfoVO?.deptName || '-',
							belongProduct: '-', // 接口返回数据中没有产品名称
							applySupplementLive: '否', // 接口返回数据中没有该字段
							supplementRoomId: '-', // 接口返回数据中没有该字段
							supplementReason: '-', // 接口返回数据中没有该字段
						}
						// 更新内容信息
						this.contentInfo = {
							deliverableName: data.auditTaskInfoVO?.taskName || '-',
							deliverableId: data.auditTaskInfoVO?.taskNo || '-',
							liveRoomUrl: '-', // 接口返回数据中没有该字段
							pushUrl: '-', // 接口返回数据中没有该字段
							startUrl: '-', // 接口返回数据中没有该字段
							liveReplay: '-', // 接口返回数据中没有该字段
							playVideo: '-', // 接口返回数据中没有该字段
							downloadVideo: '-', // 接口返回数据中没有该字段
							viewerCount: '-', // 接口返回数据中没有该字段
							validView: '-', // 接口返回数据中没有该字段
							description: '-', // 接口返回数据中没有该字段
						}
						// 更新客户信息列表
						this.clientList = [{
								name: data.docterVO?.doctorName || '医生姓名',
								level: '', // 接口返回数据中没有该字段
								hospital: data.docterVO?.institution || '-',
								department: data.docterVO?.department || '-',
								taskCount: data.auditTaskInfoVO?.taskCount || '0',
								points: data.auditTaskInfoVO?.taskIntegral || '0'
							}]
						// 更新审批信息
						this.approvalInfo = data.auditFlowVO?.map(item => ({
							name: item.auditUserName || '审批人',
							status: item.statusName || '-',
							statusColor: this.getAuditStatusColor(item.status),
							time: item.auditTime || ''
						})) || []
					} else {
						uni.showToast({
							title: '获取任务详情失败',
							icon: 'none'
						})
					}
				} catch (e) {
					uni.hideLoading()
					console.error('加载数据失败', e)
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					})
				}
			},
			getAuditStatusText(status) {
				// 根据审核状态获取状态文本
				const statusMap = {
					0: '待审核',
					1: '已通过',
					2: '已驳回'
				}
				return statusMap[status] || '-'
			},
			getAuditStatusColor(status) {
				// 根据审核状态获取状态颜色
				const colorMap = {
					1: '#FF9800',
					2: '#4CAF50',
					3: '#F44336'
				}
				return colorMap[status] || '#999'
			},
			// 复制到剪贴板
			copyToClipboard(text) {
				uni.setClipboardData({
					data: text,
					success() {
						uni.showToast({
							title: '复制成功',
							icon: 'success'
						})
					}
				})
			},
			// 下载附件
			downloadAttachment() {
				uni.showToast({
					title: '开始下载附件',
					icon: 'none'
				})
				// 实际项目中添加下载逻辑：uni.downloadFile + uni.saveFile
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #f5f5f5;
		display: flex;
		flex-direction: column;
		height: auto;

		&::before {
			content: '';
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			width: 100%;
			height: 532rpx;
			background: linear-gradient(180deg, rgba(56, 139, 255, 0.79) 0%, rgba(56, 139, 255, 0) 100%);
		}

	}

	.top {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 88rpx;
		position: relative;
		font-weight: 600;
		font-size: 36rpx;
		color: #FFFFFF;

		.return {
			position: absolute;
			left: 32rpx;
			width: 40rpx;
			height: 40rpx;
		}
	}

	.content {
		flex: 1;
		padding: 24rpx;
		box-sizing: border-box;
	}

	/* 任务卡片样式 */
	.task-card {
		background: #fff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 24rpx;

		.card-header {
			.card-title {
				font-weight: 500;
				font-size: 36rpx;
				color: #333333;
				margin-bottom: 24rpx;
			}
		}

		.card-meta {
			display: flex;
			align-items: center;
			gap: 16rpx;
			font-size: 24rpx;
			color: #666;

			.meta-tag {
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				border: 2rpx solid rgba(56, 139, 255, 0.4);
				padding: 4rpx 8rpx;
				color: #388BFF;
			}
		}
	}

	/* 通用信息区块样式 */
	.info-section {
		background: #fff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 24rpx;

		.section-header {
			display: flex;
			align-items: center;
			margin-bottom: 24rpx;

			.section-indicator {
				width: 6rpx;
				height: 32rpx;
				background: #388BFF;
				border-radius: 3rpx;
				margin-right: 16rpx;
			}

			.section-title {
				font-weight: 600;
				font-size: 32rpx;
				color: #333333;
			}
		}

		.info-list {
			.info-item {
				display: flex;
				margin-bottom: 24rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.info-label {
					width: 50%;
					font-size: 28rpx;
					color: #666;
					flex-shrink: 0;
				}

				.info-value {
					flex: 1;
					text-align: end;
					font-size: 28rpx;
					color: #333;
				}

				.copy-btn {
					margin-left: 16rpx;
					color: #2196F3;
					font-size: 24rpx;
					cursor: pointer;
				}

				.column {
					flex-direction: column;
				}

				.attachment-item {
					margin-top: 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					gap: 16rpx;
					background: #F7F8FA;
					border-radius: 16rpx 16rpx 16rpx 16rpx;

					.left {
						display: flex;
						align-items: center;

						.img {
							width: 72rpx;
							height: 72rpx;
							margin-right: 24rpx;
						}

						.txt-item {
							.attachment-name {
								font-size: 28rpx;
								color: #333;
							}

							.attachment-size {
								font-size: 24rpx;
								color: #999;
							}
						}

					}



					.download-btn {
						width: 32rpx;
						height: 32rpx;
					}
				}
			}
		}
	}

	/* 客户信息样式 */
	.client-list {
		.client-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 16rpx 0;

			&:last-child {
				border-bottom: none;
			}

			.client-info {
				display: flex;
				align-items: center;

				.avatar {
					width: 88rpx;
					height: 88rpx;
					margin-right: 24rpx;
				}

				.client-txt {
					.client-name {
						font-size: 28rpx;
						font-weight: 500;
						color: #333;

						.client-level {
							margin-left: 8rpx;
							padding: 2rpx 8rpx;
							background: #FFF6E5;
							color: #C89743;
							font-size: 22rpx;
							border-radius: 8rpx;
						}
					}
				}


				.client-hospital {
					font-size: 24rpx;
					color: #666;
					margin-top: 4rpx;
					display: flex;
					align-items: center;

					.line {
						width: 2rpx;
						height: 28rpx;
						background: #EAEBEE;
						margin: 0 24rpx;
					}
				}
			}

			.client-stats {
				display: flex;
				gap: 24rpx;
				font-size: 24rpx;
				color: #999999;

				.stat-item {
					display: flex;
					flex-direction: column;
					align-items: center;

					.num {
						font-weight: 500;
						font-size: 28rpx;
						color: #333333;
					}
				}
			}
		}
	}

	/* 审批信息样式 */
	.approval-list {
		.approval-item {
			position: relative;
			padding-left: 24rpx;
			margin-bottom: 68rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&:last-child {
				margin-bottom: 0;
			}

			.left {
				display: flex;
				align-items: center;

				.avatar-box {
					width: 80rpx;
					height: 80rpx;
					margin-right: 32rpx;
					position: relative;

					.avatar {
						width: 100%;
						height: 100%;
					}

					.icon {
						position: absolute;
						bottom: 0;
						right: 0;
						width: 32rpx;
						height: 32rpx;
					}
				}


				.approval-user {
					gap: 12rpx;

					.user-name {
						font-weight: 500;
						font-size: 28rpx;
						color: #333;
					}

					.user-status {
						font-size: 24rpx;
					}
				}
			}


			.approval-time {
				font-size: 24rpx;
				color: #999;
				margin-top: 4rpx;
			}
		}

		.approval-line {
			position: absolute;
			left: 64rpx;
			bottom: -64rpx;
			width: 1rpx;
			height: 56rpx;
			background: #BFD8FF;
		}
	}

	.reason {
		font-size: 28rpx;
		color: #666666;
		line-height: 40rpx;
		margin-left: 136rpx;
	}
</style>